avatar

uniapp管理APP更新

uniapp管理APP更新

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
// 获取
import { upgrade } from '@/api/user'

export default function upgradeApp(latestTip = '') {
plus.runtime.getProperty(plus.runtime.appid, (widgetInfo) => {
let packageName = ''
if (plus.os.name === 'Android') {
packageName = plus.android.runtimeMainActivity().getPackageName()
} else {
packageName = plus.ios.importClass('NSBundle').mainBundle().bundleIdentifier()
}
getApp().globalData.wgtVersion = widgetInfo.version
getApp().globalData.appVersion = plus.runtime.version
upgrade({
id: packageName,
version: plus.runtime.version,
platform: plus.os.name,
isWeb: 0,
}).then((res) => {
if (res.data.pkgUrl) {
uni.showModal({
title: '更新提示',
content: res.data.content,
showCancel: !res.data.force,
success: (res1) => {
if (res1.confirm) {
// if (plus.os.name == 'Android') {
// pkgUrl = market://details?id=io.dcloud.hellouniapp
// } else {
// pkgUrl = 'itms-apps://itunes.apple.com/cn/app/hello-uni-app/id1417078253'
// }
plus.runtime.openURL(res.data.pkgUrl)
}
},
})
}
if (!res.data.force || !res.data.pkgUrl) {
getApp().globalData.isIosApprove = res.data.isApprove
if (res.data.isApprove) {
return
}
upgrade({
id: packageName,
version: widgetInfo.version,
platform: plus.os.name,
isWeb: 1,
}).then((res2) => {
getApp().globalData.wgtUrl = res2.data.wgtUrl
const token = uni.getStorageSync('token')
if (res2.data.wgtUrl && token) {
uni.showModal({
title: '资源包更新提示',
content: res2.data.content,
showCancel: !res2.data.force,
success: (res3) => {
if (res3.confirm) {
uni.showLoading({
title: '新版本下载中......',
})
uni.downloadFile({
url: res2.data.wgtUrl,
header: {
Authorization: `Bearer ${token}`,
Accept: 'application/vnd.api+json',
'Content-Type': 'application/json',
},
success: (res4) => {
if (res4.statusCode === 200) {
uni.showToast({
title: `下载成功${res4.tempFilePath}`,
icon: 'none',
})
plus.runtime.install(
res4.tempFilePath,
{
force: true,
},
(widgetInfo) => {
uni.showToast({
title: '安装成功',
icon: 'success',
})
getApp().globalData.wgtVersion = widgetInfo.version
plus.runtime.restart()
},
(e) => {
uni.showToast({
title: `安装失败${e.message}`,
icon: 'none',
})
},
)
}
},
complete: () => {
uni.hideLoading()
},
})
}
},
})
}
if (!res.data.wgtUrl && !res.data.pkgUrl && latestTip) {
uni.showToast({
title: latestTip,
icon: 'none',
})
}
})
}
})
})
}
文章作者: pengweifu
文章链接: https://www.pengwf.com/2023/01/02/web/JS-Uniapp-Upgrade/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 麦子的博客
打赏
  • 微信
    微信
  • 支付宝
    支付宝

评论