From 3a21b53c492334d1303939e92ef1ae79b1d27b80 Mon Sep 17 00:00:00 2001 From: DCloud_LXH <283700113@qq.com> Date: Mon, 27 Nov 2023 15:40:43 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=8D=87=E7=BA=A7=E4=B8=AD=E5=BF=83?= =?UTF-8?q?=E5=8D=87=E7=BA=A7=E8=87=B3=200.7.3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../uni-upgrade-center-app/changelog.md | 4 + .../uni-upgrade-center-app/package.json | 2 +- .../utils/call-check-version.ts | 18 +- .../utils/check-update-nvue.js | 184 ++++++++++++++++++ 4 files changed, 202 insertions(+), 6 deletions(-) create mode 100644 uni_modules/uni-upgrade-center-app/utils/check-update-nvue.js diff --git a/uni_modules/uni-upgrade-center-app/changelog.md b/uni_modules/uni-upgrade-center-app/changelog.md index ab09aa82..ae375a82 100644 --- a/uni_modules/uni-upgrade-center-app/changelog.md +++ b/uni_modules/uni-upgrade-center-app/changelog.md @@ -1,3 +1,7 @@ +## 0.7.3(2023-11-27) +- 修复 在 uni-app x 中无更新时报错的Bug +## 0.7.2(2023-11-20) +- 新增 插件根目录 utils 文件夹中新增 check-update-nvue.js 文件(vue2 的 nvue 页面请引用该文件) ## 0.7.1(2023-11-17) - 修复 运行至浏览器 ts 语法报错 ## 0.7.0(2023-11-10) diff --git a/uni_modules/uni-upgrade-center-app/package.json b/uni_modules/uni-upgrade-center-app/package.json index a1b9e469..b24b8159 100644 --- a/uni_modules/uni-upgrade-center-app/package.json +++ b/uni_modules/uni-upgrade-center-app/package.json @@ -1,7 +1,7 @@ { "id": "uni-upgrade-center-app", "displayName": "升级中心 uni-upgrade-center - App", - "version": "0.7.1", + "version": "0.7.3", "description": "uni升级中心 - 客户端检查更新", "keywords": [ "uniCloud", diff --git a/uni_modules/uni-upgrade-center-app/utils/call-check-version.ts b/uni_modules/uni-upgrade-center-app/utils/call-check-version.ts index c14ce3d0..998c20f7 100644 --- a/uni_modules/uni-upgrade-center-app/utils/call-check-version.ts +++ b/uni_modules/uni-upgrade-center-app/utils/call-check-version.ts @@ -78,11 +78,19 @@ export default function () : Promise { data: data }).then(res => { const code = res.result['code'] - if (['Int', 'Long', 'number'].includes(typeof code) && (code as number) < 0) { - reject({ - code: res.result['code'], - message: res.result['message'] - }) + const codeIsNumber = ['Int', 'Long', 'number'].includes(typeof code) + if (codeIsNumber) { + if ((code as number) == 0) { + reject({ + code: res.result['code'], + message: res.result['message'] + }) + } else if (code < 0) { + reject({ + code: res.result['code'], + message: res.result['message'] + }) + } } else { const result = JSON.parse(JSON.stringify(res.result)) as UniUpgradeCenterResult resolve(result) diff --git a/uni_modules/uni-upgrade-center-app/utils/check-update-nvue.js b/uni_modules/uni-upgrade-center-app/utils/check-update-nvue.js new file mode 100644 index 00000000..6c0d4bca --- /dev/null +++ b/uni_modules/uni-upgrade-center-app/utils/check-update-nvue.js @@ -0,0 +1,184 @@ +function callCheckVersion() { + // #ifdef APP-PLUS + return new Promise((resolve, reject) => { + plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) { + let data = { + action: 'checkVersion', + appid: plus.runtime.appid, + appVersion: plus.runtime.version, + wgtVersion: widgetInfo.version + } + uniCloud.callFunction({ + name: 'uni-upgrade-center', + data, + success: (e) => { + resolve(e) + }, + fail: (error) => { + reject(error) + } + }) + }) + }) + // #endif + // #ifndef APP-PLUS + return new Promise((resolve, reject) => {}) + // #endif +} + +// 推荐再App.vue中使用 +const PACKAGE_INFO_KEY = '__package_info__' + +export default function() { + // #ifdef APP-PLUS + return new Promise((resolve, reject) => { + callCheckVersion().then(async (e) => { + if (!e.result) return; + const { + code, + message, + is_silently, // 是否静默更新 + url, // 安装包下载地址 + platform, // 安装包平台 + type // 安装包类型 + } = e.result; + + // 此处逻辑仅为实例,可自行编写 + if (code > 0) { + // 腾讯云和阿里云下载链接不同,需要处理一下,阿里云会原样返回 + const { + fileList + } = await uniCloud.getTempFileURL({ + fileList: [url] + }); + if (fileList[0].tempFileURL) + e.result.url = fileList[0].tempFileURL; + + resolve(e) + + // 静默更新,只有wgt有 + if (is_silently) { + uni.downloadFile({ + url: e.result.url, + success: res => { + if (res.statusCode == 200) { + // 下载好直接安装,下次启动生效 + plus.runtime.install(res.tempFilePath, { + force: false + }); + } + } + }); + return; + } + + /** + * 提示升级一 + * 使用 uni.showModal + */ + // return updateUseModal(e.result) + + /** + * 提示升级二 + * 官方适配的升级弹窗,可自行替换资源适配UI风格 + */ + uni.setStorageSync(PACKAGE_INFO_KEY, e.result) + 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) + } + }) + + return + } else if (code < 0) { + // TODO 云函数报错处理 + console.error(message) + return reject(e) + } + return resolve(e) + }).catch(err => { + // TODO 云函数报错处理 + console.error(err.message) + reject(err) + }) + }); + // #endif +} + +/** + * 使用 uni.showModal 升级 + */ +function updateUseModal(packageInfo) { + const { + title, // 标题 + contents, // 升级内容 + is_mandatory, // 是否强制更新 + url, // 安装包下载地址 + platform, // 安装包平台 + type // 安装包类型 + } = packageInfo; + + let isWGT = type === 'wgt' + let isiOS = !isWGT ? platform.includes('iOS') : false; + let confirmText = isiOS ? '立即跳转更新' : '立即下载更新' + + return uni.showModal({ + title, + content: contents, + showCancel: !is_mandatory, + confirmText, + success: res => { + if (res.cancel) return; + + // 安装包下载 + if (isiOS) { + plus.runtime.openURL(url); + return; + } + + uni.showToast({ + title: '后台下载中……', + duration: 1000 + }); + + // wgt 和 安卓下载更新 + downloadTask = uni.downloadFile({ + url, + success: res => { + if (res.statusCode !== 200) { + console.error('下载安装包失败', err); + return; + } + // 下载好直接安装,下次启动生效 + plus.runtime.install(res.tempFilePath, { + force: false + }, () => { + if (is_mandatory) { + //更新完重启app + plus.runtime.restart(); + return; + } + uni.showModal({ + title: '安装成功是否重启?', + success: res => { + if (res.confirm) { + //更新完重启app + plus.runtime.restart(); + } + } + }); + }, err => { + uni.showModal({ + title: '更新失败', + content: err + .message, + showCancel: false + }); + }); + } + }); + } + }); +} -- GitLab