王飞
2025-03-07 c5e49c10499435802a6551b98772f95660c6f85a
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import callCheckVersion, { UniUpgradeCenterResult } from "./call-check-version"
import { platform_iOS } from './utils'
// #ifdef UNI-APP-X
import { openSchema } from '@/uni_modules/uts-openSchema'
// #endif
 
// 推荐再App.vue中使用
const PACKAGE_INFO_KEY = '__package_info__'
// #ifdef APP-HARMONY
export default function (component?: any) : Promise<UniUpgradeCenterResult> {
// #endif
// #ifndef APP-HARMONY
export default function () : Promise<UniUpgradeCenterResult> {
// #endif
    return new Promise<UniUpgradeCenterResult>((resolve, reject) => {
        callCheckVersion().then(async (uniUpgradeCenterResult) => {
            // NOTE uni-app x 3.96 解构有问题
            const code = uniUpgradeCenterResult.code
            const message = uniUpgradeCenterResult.message
            const url = uniUpgradeCenterResult.url // 安装包下载地址
        // 此处逻辑仅为示例,可自行编写
        if (code > 0) {
          // 腾讯云和阿里云下载链接不同,需要处理一下,阿里云会原样返回
          const tcbRes = await uniCloud.getTempFileURL({ fileList: [url] });
          if (typeof tcbRes.fileList[0].tempFileURL !== 'undefined') uniUpgradeCenterResult.url = tcbRes.fileList[0].tempFileURL;
 
          /**
           * 提示升级一
           * 使用 uni.showModal
           */
          // return updateUseModal(uniUpgradeCenterResult)
 
          // #ifndef UNI-APP-X
          // 静默更新,只有wgt有
          if (uniUpgradeCenterResult.is_silently) {
            uni.downloadFile({
              url,
              success: res => {
                if (res.statusCode == 200) {
                  // 下载好直接安装,下次启动生效
                  plus.runtime.install(res.tempFilePath, {
                    force: false
                  });
                }
              }
            });
            return;
          }
          // #endif
 
          /**
           * 提示升级二
           * 官方适配的升级弹窗,可自行替换资源适配UI风格
           */
          // #ifndef UNI-APP-X 
          // #ifdef APP-PLUS
          uni.setStorageSync(PACKAGE_INFO_KEY, uniUpgradeCenterResult)
          uni.navigateTo({
            url: `/uni_modules/uni-upgrade-center-app/pages/upgrade-popup?local_storage_key=${PACKAGE_INFO_KEY}`,
            fail: (err) => {
              console.error('更新弹框跳转失败', err)
              uni.removeStorageSync(PACKAGE_INFO_KEY)
            }
          })
          // #endif
          // #ifdef APP-HARMONY
          if (component) {
            component.show(true, uniUpgradeCenterResult)
          } else {
            reject({
              code: -1,
              message: '在 HarmonyOS Next 平台请传递组件使用'
            })
          }
          // #endif
          // #endif
          // #ifdef UNI-APP-X
          uni.setStorageSync(PACKAGE_INFO_KEY, uniUpgradeCenterResult)
          uni.openDialogPage({
            url: `/uni_modules/uni-upgrade-center-app/pages/uni-app-x/upgrade-popup?local_storage_key=${PACKAGE_INFO_KEY}`,
            disableEscBack: true,
            fail: (err) => {
              console.error('更新弹框跳转失败', err)
              uni.removeStorageSync(PACKAGE_INFO_KEY)
            }
          })
          // #endif
 
          return resolve(uniUpgradeCenterResult)
        } else if (code < 0) {
          console.error(message)
          return reject(uniUpgradeCenterResult)
        }
        return resolve(uniUpgradeCenterResult)
      }).catch((err) => {
        reject(err)
      })
    });
  }
 
/**
 * 使用 uni.showModal 升级
 */
function updateUseModal(packageInfo : UniUpgradeCenterResult) : void {
    // #ifdef APP
    const {
        title, // 标题
        contents, // 升级内容
        is_mandatory, // 是否强制更新
        url, // 安装包下载地址
        type,
        platform
    } = packageInfo;
 
    let isWGT = type === 'wgt'
    let isiOS = !isWGT ? platform.includes(platform_iOS) : false;
 
    // #ifndef UNI-APP-X
    let confirmText = isiOS ? '立即跳转更新' : '立即下载更新'
    // #endif
    // #ifdef UNI-APP-X
    let confirmText = '立即下载更新'
    // #endif
 
    return uni.showModal({
      title,
      content: contents,
      showCancel: !is_mandatory,
      confirmText,
      success: res => {
        if (res.cancel) return;
 
            if (isiOS) {
                // iOS 平台跳转 AppStore
                // #ifndef UNI-APP-X
                plus.runtime.openURL(url);
                // #endif
                // #ifdef UNI-APP-X
                openSchema(url)
                // #endif
                return;
            }
 
        uni.showToast({
          title: '后台下载中……',
          duration: 1000
        });
 
            // wgt 和 安卓下载更新
            uni.downloadFile({
                url,
                success: res => {
                    if (res.statusCode !== 200) {
                        console.error('下载安装包失败');
                        return;
                    }
                    // 下载好直接安装,下次启动生效
          // uni-app x 项目没有 plus5+ 故使用条件编译
                    // #ifndef UNI-APP-X
                    plus.runtime.install(res.tempFilePath, {
                        force: false
                    }, () => {
                        if (is_mandatory) {
                            //更新完重启app
                            // #ifdef APP-PLUS
                            plus.runtime.restart();
                            // #endif
                            // #ifdef APP-HARMONY
                            uni.showModal({
                                title: '安装成功',
                                content: '请手动重启应用',
                                showCancel: false,
                                success: res => {
                                    plus.runtime.quit();
                                }
                            });
                            // #endif
                            return;
                        }
                        uni.showModal({
                            title: '安装成功是否重启?',
                            success: res => {
                                if (res.confirm) {
                                    //更新完重启app
                                    // #ifdef APP-PLUS
                                    plus.runtime.restart();
                                    // #endif
                                    // #ifdef APP-HARMONY
                                    plus.runtime.quit();
                                    // #endif
                                }
                            }
                        });
                    }, err => {
                        uni.showModal({
                            title: '更新失败',
                            content: err
                                .message,
                            showCancel: false
                        });
                    });
                    // #endif
 
          // #ifdef UNI-APP-X
          uni.installApk({
              filePath: res.tempFilePath,
              success: () => {
                  uni.showModal({
                      title: '安装成功请手动重启'
                  });
              },
              fail: err => {
                  uni.showModal({
                      title: '更新失败',
                      content: err.errMsg,
                      showCancel: false
                  });
              }
          });
          // #endif
                }
            });
        }
    });
    // #endif
}