uni-app升级功能实践
# 简介
# 支持增量升级wgt和全量升级apk
# 实践
# 效果
# 代码
# 预发布功能;
if (vInfo.status === 1) { if (this.userInfo.type === 1) {}}
PopDownload/index.vue
<template>
<view class="pop-content" v-if="updating">
<view class="progress-box content-area pr-2">
<progress style="padding: 10rpx; color: #1388f0;" :percent="downloadPrecent" activeColor="#30CA81"
font-size="14" show-info stroke-width="12" />
<text class="tip p-1">正在下载,请稍后({{ done }}/{{ total }} M)</text>
</view>
</view>
</template>
<script >
import { isApp } from "@/config";
import { getAppVersion, } from '@/api/home'
import { getStorageSync,} from '@/core/utils';
import { LOCAL_STORAGE, } from '@/constants';
export default {
props: {},
data() {
return {
downloadPrecent: 0,
done: 0,
total: 0,
updating: false,
downloadFile: '',
userInfo: getStorageSync(LOCAL_STORAGE.USER_INFO)
}
},
computed: {},
watch: {},
mounted() {
this.getData()
},
methods: {
getData() {
if (isApp) {
this.checkAppInfo()
}
},
checkAppInfo() {
getAppVersion({}).then(res => {
if (res.code == 0 ) {
let vInfo = res.data
console.log("-----getAppVersion---vInfo---", JSON.stringify(vInfo));
// 1.预发布 2.生产发布
if (vInfo.status === 1) {
if (this.userInfo.type === 1) {
this.checkVersion(vInfo)
}
}else if(vInfo.status === 2){
this.checkVersion(vInfo)
}
}
})
},
checkVersion(vInfo){
plus.runtime.getProperty(plus.runtime.appid, (wgtinfo) => {
console.log("-----getAppVersion---wgtinfo---", JSON.stringify(wgtinfo));
var nowVersion = wgtinfo.version;
if (vInfo.appVersion != nowVersion && vInfo.appVersion > nowVersion) {
let titleMsg = '发现新版本,请更新体验'
let contentMsg = vInfo.appDesc || '将为你更新到最新版本'
uni.showModal({
title: titleMsg,
content: contentMsg,
showCancel: false,
buttonText: '确定',
success: (res) => {
if (res.confirm) {
this.downloadWgt(vInfo.appPath)
} else if (res.cancel) {
console.log('用户点击取消');
}
}
})
}
})
},
downloadWgt(url) {
this.updating = true;
let that = this;
let downloadTask = uni.downloadFile({//执行下载
url: url,
success: downloadResult => {//下载成功
if (downloadResult.statusCode === 200) {
that.downloadFile = downloadResult.tempFilePath;
uni.hideLoading();
that.updating = false
setTimeout(() => {
that.installApp();
}, 100)
}
},
complete: () => {
uni.hideLoading();
}
});
downloadTask.onProgressUpdate((res) => {
if (res.totalBytesExpectedToWrite) {
that.total = res.totalBytesExpectedToWrite / 1024 / 1014
that.total = that.total.toFixed(1)
}
if (res.totalBytesWritten) {
that.done = res.totalBytesWritten / 1024 / 1014
that.done = that.done.toFixed(1)
}
that.downloadPrecent = res.progress;
});
},
installApp() {
var that = this;
plus.runtime.install(//安装
that.downloadFile, {
force: true
},
function () {
plus.runtime.restart();
},
function (e) {
uni.showToast({ title: "更新失败,请联系技术人员!", icon: "none", duration: 2000 });
}
);
},
},
}
</script>
<style lang="scss" scoped>
@import './index.scss';
</style>
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
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
# 打包脚本
打包apk时,要预拷贝icons和
rm -rf $appBuildDir/app-plus
pkgName=$pkgNameApp
echo $pkgName
if [ $isHBuildApk == true ]; then
yarn proApp
printVersion $appBuildDir/app-plus
mkdir -p $appBuildDir/app-plus/unpackage/res/
cp -rf $curDir/src/static/icons $appBuildDir/app-plus/unpackage/res/
cd $appBuildDir/app-plus
cli pack --config D:/samy/company/sk/data/pkg-build-info/pkg-apk-config.json
else
if [ $isUseHBuildX == true ]; then
cli publish --platform APP --project $proName --name $pkgName.wgt --type wgt
mv $appBuildDir/$pkgName.wgt $distRelease/
else
yarn proApp
printVersion $appBuildDir/app-plus
if [[ "$OSTYPE" == "darwin"* ]]; then
mv $appBuildDir/app-plus $appBuildDir/$pkgName #mv在win上拷贝失效
else
cp -rf $appBuildDir/app-plus $appBuildDir/$pkgName
fi
cd $appBuildDir/$pkgName
zip -rqm $pkgName.zip *
mv $appBuildDir/$pkgName/$pkgName.zip $distRelease/$pkgName.wgt
cd ..
rm -rf $appBuildDir/$pkgName
fi
fi
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
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
上次更新: 2023/11/17, 05:08:20