This commit is contained in:
aihe 2024-06-16 01:56:52 +08:00
parent a14d6135f4
commit 9257d4018d
3 changed files with 131 additions and 6 deletions

11
App.vue
View File

@ -5,8 +5,9 @@
import {
version
} from './package.json'
import checkUpdate from '@/uni_modules/uni-upgrade-center-app/utils/check-update';
// import checkUpdate from '@/uni_modules/uni-upgrade-center-app/utils/check-update';
import { USERKEY, COMMONKEY } from "@/store/actionKey"
import { checkUpdate } from "@/common/appUpdate"
export default {
globalData: {
@ -27,9 +28,11 @@
console.log('App Launch');
// #ifdef APP-PLUS
// App平台检测升级,服务端代码是通过uniCloud的云函数实现的,详情可参考:https://ext.dcloud.net.cn/plugin?id=4542
if (plus.runtime.appid !== 'HBuilder') { // 真机运行不需要检查更新,真机运行时appid固定为'HBuilder',这是调试基座的appid
checkUpdate()
}
// if (plus.runtime.appid !== 'HBuilder') { // 真机运行不需要检查更新,真机运行时appid固定为'HBuilder',这是调试基座的appid
// checkUpdate()
// }
checkUpdate()
// 一键登录预登陆,可以显著提高登录速度
uni.preLogin({

106
common/appUpdate/index.js Normal file
View File

@ -0,0 +1,106 @@
import { confirmModal, showLoading, hideLoading, showToast } from "@/common/untool"
export const getSystemInfo = ()=> {
let info = uni.getSystemInfoSync()
console.log('getSystemInfo=====', info)
return info
}
export const getCurrentAppInfo = ()=> {
return new Promise((resolve, reject)=> {
// 保存 app 版本信息
// #ifdef APP-PLUS
plus.runtime.getProperty(plus.runtime.appid, (widgetInfo)=> {
console.log('plus.runtime.appid=========', plus.runtime.appid);
console.log('widgetInfo=========', widgetInfo);
widgetInfo._appVersion = `${widgetInfo.version}.${widgetInfo.versionCode}`
resolve(widgetInfo)
});
// #endif
})
}
const installNewApk = (url)=> {
return new Promise((resolve, reject)=> {
let downInstance = uni.downloadFile({
url: url,
success: (downloadResult) => {
hideLoading()
if (downloadResult.statusCode === 200) {
showLoading('正在安装...')
plus.runtime.install(downloadResult.tempFilePath, {
force: true
}, function() {
hideLoading()
console.log('install success...');
plus.runtime.restart();
resolve()
}, function(e) {
hideLoading()
console.error('install fail...');
reject()
});
} else {
reject()
}
},
fail: (err)=> {
console.log('download file err...', err);
hideLoading()
}
});
downInstance.onProgressUpdate((res)=> {
let { totalBytesExpectedToWrite, totalBytesWritten } = res
let percent = (totalBytesWritten / totalBytesExpectedToWrite) * 100
showLoading(`下载进度:${parseInt(percent)}%`)
})
})
}
const getLastAppInfo = async (curAppInfo)=> {
return {
version: '1.0.1',
versionCode: 101,
apkUrl: '',
wgtUrl: '',
// whiteDeviceIds: ['F9E778C90D0E9A8DA9D95AA97B00A3FB'],
}
}
const compareInfo = (curAppInfo, lastAppInfo)=> {
let hasNewVersion = false
let url = ''
// 强制更新app
if(lastAppInfo.version && curAppInfo.version != lastAppInfo.version) {
hasNewVersion = true
url = lastAppInfo.apkUrl
}
// 热更新
if(!url && lastAppInfo.versionCode && curAppInfo.versionCode != lastAppInfo.versionCode) {
hasNewVersion = true
url = lastAppInfo.wgtUrl
}
// 判断是否走白名单更新
if(lastAppInfo.whiteDeviceIds?.length > 0) {
let sysInfo = getSystemInfo()
hasNewVersion = lastAppInfo.whiteDeviceIds.indexOf(sysInfo?.deviceId) > -1
}
return {
hasNewVersion,
url,
lastAppInfo
}
}
export const checkUpdate = async ()=> {
let curAppInfo = await getCurrentAppInfo()
let lastAppInfo = await getLastAppInfo(curAppInfo)
let cmpRes = await compareInfo(curAppInfo, lastAppInfo)
if(cmpRes.hasNewVersion) {
let fmBl = confirmModal(`app最新版本:${cmpRes.lastAppInfo.version}.${cmpRes.lastAppInfo.versionCode}, 是否更新?`)
if(fmBl) {
await installNewApk(cmpRes.url)
}
}
}

View File

@ -64,7 +64,18 @@
<u-text margin="0 0 0 30rpx" text="版本更新" size="28" color="#333"></u-text>
</u-row>
<u-row>
<u-text margin="0 10rpx 0 0" text="v1.0.0" size="28" color="#666"></u-text>
<u-text margin="0 10rpx 0 0" :text="curAppVersion" size="28" color="#666"></u-text>
<u-icon name="arrow-right" size="32" color="#333"></u-icon>
</u-row>
</u-row>
<u-line></u-line>
<u-row @click="setClipboardData(curDeviceId)" custom-style="padding: 30rpx;" justify="between">
<u-row custom-style="margin-right: 30rpx;">
<u-image :src="getCdnUrl(`mine/banbenup.png`)" height="34" width="34"></u-image>
<u-text margin="0 0 0 30rpx" text="设备Id" size="28" color="#333"></u-text>
</u-row>
<u-row custom-style="flex: 1;">
<view class="font-28 mr-10">{{curDeviceId}}</view>
<u-icon name="arrow-right" size="32" color="#333"></u-icon>
</u-row>
</u-row>
@ -117,6 +128,7 @@
import { dealBtnMap } from "@/common/enum"
import * as apiService from "@/common/api"
import { getAuthList, authCodeEnum } from "@/common/authCode"
import { getCurrentAppInfo, getSystemInfo } from "@/common/appUpdate"
export default {
computed: {
@ -152,7 +164,8 @@
orderStat: {},
},
saomiao: true,
curAppVersion: '',
curDeviceId: ''
}
},
@ -170,6 +183,9 @@
methods: {
async initData(){
this.getAppHomeStatData()
let appRes = await getCurrentAppInfo()
this.curAppVersion = appRes._appVersion
this.curDeviceId = getSystemInfo()?.deviceId
},
async getAppHomeStatData(){
let res = await apiService.getAppHomeStatData()