update.vue
4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<template>
<u-popup :show="show" bgColor="transparent" mode="center" :safeAreaInsetBottom="false" borderRadius="20">
<view class="new-update">
<view class="new-update-1">
<image class="new-update-1-i" src="@/static/imagesV2/icon70.png" mode="widthFix"></image>
<view class="new-update-1-c">
<view class="new-update-1-c-1">
发现新版本
</view>
</view>
</view>
<view class="new-update-2">
<view class="new-update-2-1">新版本更新内容</view>
<scroll-view :scroll-y="true" style="height: 300rpx;">
<view style="font-size: 28rpx !important;">
<view v-html="content"></view>
</view>
</scroll-view>
<view class="immediate" @click="update" v-if="!is_load">立即升级</view>
<view class="progress" v-else>
<u-line-progress :percentage="progressOf" activeColor="#1b60b2"></u-line-progress>
<view class="progress-t">安装包下载中,请稍后({{ size.schedule }}M/{{ size.total }}M)</view>
</view>
</view>
</view>
</u-popup>
</template>
<script>
export default {
data() {
return {
show: true,
content: '更新内容',
is_load: false,
progressOf: 0,
urlObj: {
ios: '',
apk: ''
},
size: {
total: 0,
schedule: 0
}
}
},
onBackPress(e) {
// 这里可以自定义返回逻辑
return true // return true 表示禁止默认返回
},
onLoad() {
this.getData()
},
methods: {
getData() {
let version = plus.runtime.version
this.$service.P_post('/version/index', {
client: uni.getSystemInfoSync().platform,
version: version
}).then(res => {
this.content = res.msg.content
if(plus.os.name.toLowerCase() == 'ios') {
this.urlObj.ios = res.msg.url
} else {
this.urlObj.apk = res.msg.url
}
})
},
update() {
let that = this
if (plus.os.name.toLowerCase() == 'ios') {
plus.runtime.openURL(this.urlObj.ios)
} else {
this.is_load = true
var dtask = this.getCreateDownload()
try {
dtask.start();
var prg = 0;
dtask.addEventListener('statechanged', (task, status) => {
switch (task.state) {
case 3:
that.progressOf = parseInt(
(parseFloat(task.downloadedSize) /
parseFloat(task.totalSize)) *
100
);
that.size.total = (task.totalSize / (1024 * 1024)).toFixed(2)
that.size.schedule = (task.downloadedSize / (1024 * 1024)).toFixed(2)
break;
case 4:
that.is_load = false
//下载完成
break;
}
})
} catch (err) {
console.log('err', err)
// plus.nativeUI.closeWaiting();
that.is_load = false
uni.showToast({
title: '更新失败-03',
mask: false,
duration: 1500
});
}
}
},
// 获取下载实例
getCreateDownload() {
return plus.downloader.createDownload(this.urlObj.apk, {},
function(d, status) {
// 下载完成
if (status == 200) {
plus.runtime.install(plus.io.convertLocalFileSystemURL(d.filename), {}, e => e,
function(error) {
uni.showToast({
title: '安装失败-01',
mask: false,
duration: 1500
});
})
} else {
uni.showToast({
title: '更新失败-02',
mask: false,
duration: 1500
});
}
});
}
}
}
</script>
<style lang="scss" scoped>
.new-update{
width: 611rpx;
.new-update-1{
position: relative;
height: 260rpx;
.new-update-1-i{
width: 100%;
position: absolute;
}
.new-update-1-c{
padding-top: 50rpx;
position: relative;
display: flex;
flex-direction: column;
justify-content: center;
height: 100%;
box-sizing: border-box;
padding-left: 54rpx;
.new-update-1-c-1{
font-size: 36rpx;
font-weight: 800;
color: #FFFFFF;
margin-bottom: 10rpx;
}
.new-update-1-c-2{
font-size: 26rpx;
color: #fff;
}
}
}
.new-update-2{
background: #fff;
border-radius:0 0 40rpx 40rpx;
padding: 50rpx;
padding-top: 20rpx;
box-sizing: border-box;
.new-update-2-1{
margin-bottom: 20rpx;
color: #1b60b2;
font-weight: bold;
}
.immediate{
line-height: 90rpx;
background: linear-gradient(0deg, #098ad5 0%, #1b60b2 100%);
color: #fff;
margin: 60rpx 0 0 0;
border-radius: 500rpx;
text-align: center;
}
.progress{
height: 80rpx;
margin: 60rpx 0 0 0;
.progress-t{
font-size: 26rpx;
color: #323232;
margin-top: 15rpx;
text-align: center;
}
}
}
}
</style>