热更新

This commit is contained in:
yangzhq68909 2025-04-30 09:16:10 +08:00
parent cfe8db7730
commit f0060f0a02
25 changed files with 341 additions and 153 deletions

BIN
certificate/book.keystore Normal file

Binary file not shown.

View File

@ -0,0 +1,9 @@
证书指纹:
MD5: 9A:AD:4D:78:17:EE:17:D3:62:B2:6E:B2:56:0B:18:C3
SHA1: B1:1E:CE:4B:70:C7:88:B3:6E:8B:B9:05:98:4B:FD:67:81:C1:3A:8F
SHA256: A9:78:BD:BF:BB:DE:35:14:AA:5D:45:E9:6A:D0:E5:17:6D:BE:58:86:7A:1D:66:A2:F3:62:2F:E7:9C:9E:59:F6
别名: __uni__9f097f0
证书密码为uZ6Stufj
包名com.tianranqi.app

View File

@ -2,13 +2,15 @@
import { onLaunch, onShow, onHide, onLoad, onReady } from '@dcloudio/uni-app' import { onLaunch, onShow, onHide, onLoad, onReady } from '@dcloudio/uni-app'
import 'abortcontroller-polyfill/dist/abortcontroller-polyfill-only' import 'abortcontroller-polyfill/dist/abortcontroller-polyfill-only'
import { beforEach } from '@/router/index' import { beforEach } from '@/router/index'
import { jurisdictionApi } from '@/api/system/login'; import { jurisdictionApi } from '@/api/system';
import { useAppStore } from '@/store' import { useAppStore, useUpdateApp } from '@/store'
export default { export default {
onLaunch: function (options) { onLaunch: function (options) {
console.log('App Launch') console.log('App Launch')
console.log('应用启动路径:', options.path) console.log('应用启动路径:', options.path)
//
useUpdateApp().checkAppUpdate()
}, },
onShow: function (options) { onShow: function (options) {
console.log('App Show') console.log('App Show')

View File

@ -21,6 +21,18 @@ export function loginApi(config : LoginParams) {
}); });
} }
/**
* 热更新
* @param
* @returns
*/
export function upDateAppApi() {
return http({
url: '/sys/common/upDateApp',
method: 'GET'
})
}
/** /**
* 获取是否灰化 * 获取是否灰化
* @param id 登录参数 * @param id 登录参数
@ -33,5 +45,6 @@ export function jurisdictionApi(id : string) { // 是否灰化
data: { data: {
id id
} }
}) });
} }

0
src/api/user/index.ts Normal file
View File

View File

@ -69,7 +69,7 @@
import { useRouter } from '@/plugin/uni-mini-router' import { useRouter } from '@/plugin/uni-mini-router'
// import { useParamsStore } from '@/store/page-params' // import { useParamsStore } from '@/store/page-params'
import Base64 from 'base-64'; import Base64 from 'base-64';
import { loginApi } from '@/api/system/login'; import { loginApi } from '@/api/system';
defineOptions({ defineOptions({
name: 'login', name: 'login',

View File

@ -1,6 +1,7 @@
import { createPinia, defineStore } from 'pinia' import { createPinia, defineStore } from 'pinia'
import { createPersistedState } from 'pinia-plugin-persistedstate' // import { createPersistedState } from 'pinia-plugin-persistedstate' //
import { upDateAppApi } from '@/api/system';
import { hasNewVersion, onClickUpdate } from '@/utils/index';
const store = createPinia() const store = createPinia()
store.use( store.use(
createPersistedState({ createPersistedState({
@ -22,6 +23,66 @@ export const useAppStore = defineStore('app', {
persist: true, // persist: true, //
}) })
export const useUpdateApp = defineStore('updateApp', () => {
const updateOptions = reactive({
force: false,
hasNew: false,
content: '',
url: '',
apkUrl: '',
wgtUrl: ''
})
const systemInfo = uni.getSystemInfoSync()
/**
* 当前处于APP_PLUS时 检查是否有新版本
*
* @param to 是否跳转新页面显示更新 默认值false
*/
function checkAppUpdate(to = false) {
try {
upDateAppApi().then(async (res : any) => {
let {
result
} = res
result.apkUrl = import.meta.env.VITE_SERVER_BASEURL + '/sys/common/static/' + result.apkUrl;
result.wgtUrl = import.meta.env.VITE_SERVER_BASEURL + '/sys/common/static/' + result.wgtUrl
updateOptions.wgtUrl = result.wgtUrl
if (systemInfo.osName === 'android') {
// Android
updateOptions.apkUrl = result.apkUrl
// #ifdef APP_PLUS
updateOptions.hasNew = await hasNewVersion(result.versionCode, result.update == 'wgt') as boolean
// #endif
} else {
// IOS
updateOptions.url = `itms-apps://itunes.apple.com/cn/app/id${123456}?mt=8`
}
updateOptions.hasNew &&
uni.showModal({
title: '更新',
content: '发现新版本,请更新',
success(res) {
if (res.confirm) {
onClickUpdate(result.update, result)
} else {
plus.runtime.quit()
}
}
})
})
} catch (error) {
updateOptions.hasNew = false
}
}
return {
checkAppUpdate,
...toRefs(updateOptions),
systemInfo
}
})
export default store export default store
// //

View File

@ -215,3 +215,106 @@ export function formatDate(value, fmt) {
return value return value
} }
} }
/**
* 判断是否有新版本
* @param version 接口返回的版本号
* @param isWgt 是否是热资源更新 默认不是
*/
export const hasNewVersion = (version, isWgt = false) => {
// #ifdef APP_PLUS
return new Promise((resolve) => {
const transfer = (str) => str.replace(/\./g, '')
if (isWgt) {
plus.runtime.getProperty(plus.runtime.appid, (widgetInfo) => {
const currentVersion = widgetInfo.version
resolve(+transfer(version) > +transfer(currentVersion))
})
} else {
const currentVersion = plus.runtime.version
resolve(+transfer(version) > +transfer(currentVersion))
}
})
// #endif
}
export function onClickUpdate(updateType : string, url) {
// #ifdef APP-PLUS
if (updateType != 'wgt') plus.runtime.openURL(url.apkUrl)
else downloadApp(url.wgtUrl)
// #endif
}
function downloadApp(url : string) {
console.log('url', url);
var dtask = plus.downloader.createDownload(url, {
filename: `_downloads/wgt-${Date.now()}.wgt` //
}, function (d, status) {
//d
if (status == 200) {
//,d.filename使API
var fileSaveUrl = plus.io.convertLocalFileSystemURL(d.filename);
console.log('fileSaveUrl', fileSaveUrl);
installApp(fileSaveUrl)
} else {
//
plus.downloader.clear(); //
uni.showToast({
title: 'App下载失败',
icon: 'error'
})
}
})
let prg = 0
let showLoading = plus.nativeUI.showWaiting('正在下載')
dtask.start()
dtask.addEventListener('statechanged', (task) => {
//
switch (task.state) {
case 1:
showLoading.setTitle("正在下载")
break
case 2:
showLoading.setTitle("已连接到服务器")
break
case 3: {
const downloaded = +task.downloadedSize; //
const total = +task.totalSize; //
prg = total > 0 ? Math.floor((downloaded / total) * 100) : 0;
showLoading.setTitle(`正在下载 ${prg}%`);
break;
}
case 4:
plus.nativeUI.closeWaiting()
//
break
}
})
}
function installApp(tempFilePath : string) {
// #ifdef APP-PLUS
plus.runtime.install(
tempFilePath, {
force: true
},
() => {
uni.showModal({
title: '更新',
content: '更新成功,请点击确认后重启',
showCancel: false,
success(res) {
if (res.confirm) {
plus.runtime.restart()
}
}
})
},
() =>
uni.showToast({
title: '安装失败!',
icon: 'error'
})
)
// #endif
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 34 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.8 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.5 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.1 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.4 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 527 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 689 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 971 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.2 KiB