【新增】(app.vue):小程序/APP检测更新

This commit is contained in:
15093570141
2024-10-13 13:27:15 +08:00
parent 55a4f3d87f
commit 6feca9691d
3 changed files with 127 additions and 19 deletions

View File

@@ -1,31 +1,134 @@
<script setup lang="ts">
import { onLaunch, onShow, onHide } from '@dcloudio/uni-app';
import { queryShopConfigV2 } from '@/core/api';
import { queryShopConfigV2, queryAppVersions } from '@/core/api';
import { Response, ShopConfigType } from '@/core/models';
import { useShopConfigStore } from '@/core/store';
// 获取项目配置
onShow(() => {
console.log('App Show')
})
onHide(() => {
console.log('App Hide')
})
onLaunch(() => {
getShopConfigV2();
/** 版本设置 */
const version = '0.7.4'
/** 开发环境才提示,生产环境不会提示 */
if (process.env.NODE_ENV === 'development') {
console.log(`\n %c \u6838\u5fc3\u5546\u57ce\u7cfb\u7edf\u0020\u0043\u006f\u0072\u0065\u0053\u0068\u006f\u0070 V${version} %c \u0068\u0074\u0074\u0070\u0073\u003a\u002f\u002f\u0077\u0077\u0077\u002e\u0063\u006f\u0072\u0065\u0073\u0068\u006f\u0070\u002e\u0063\u006e\u002f \n\n`, 'color: #ffffff; background: #3c9cff; padding:5px 0;', 'color: #3c9cff;background: #f1f1f1; padding:5px 0;');
}
/** 微信小程序检测更新措施方式更新功能后要等待24小时内才刷新的问题。 */
// #ifdef MP-WEIXIN
autoUpdate();
// #endif
/** app更新检测 */
// #ifdef APP-PLUS || APP-PLUS-NVUE
checkVersion()
// #endif
})
/** 获取项目配置 */
const shopConfigStore = useShopConfigStore();
const getShopConfigV2 = async () => {
const shopConfig : Response<ShopConfigType> = await queryShopConfigV2();
shopConfigStore.setConfig(shopConfig.data);
}
onLaunch(() => {
getShopConfigV2();
//版本设置
const version = '0.7.4'
// 开发环境才提示,生产环境不会提示
if (process.env.NODE_ENV === 'development') {
console.log(`\n %c \u6838\u5fc3\u5546\u57ce\u7cfb\u7edf\u0020\u0043\u006f\u0072\u0065\u0053\u0068\u006f\u0070 V${version} %c \u0068\u0074\u0074\u0070\u0073\u003a\u002f\u002f\u0077\u0077\u0077\u002e\u0063\u006f\u0072\u0065\u0073\u0068\u006f\u0070\u002e\u0063\u006e\u002f \n\n`, 'color: #ffffff; background: #3c9cff; padding:5px 0;', 'color: #3c9cff;background: #f1f1f1; padding:5px 0;');
const autoUpdate = () => {
/** 获取小程序更新机制兼容 */
if (wx.canIUse('getUpdateManager')) {
const updateManager = wx.getUpdateManager()
/** 1. 检查小程序是否有新版本发布 */
updateManager.onCheckForUpdate(function (res) {
/** 请求完新版本信息的回调 */
if (res.hasUpdate) {
/** 检测到新版本,需要更新,给出提示 */
uni.showModal({
title: '更新提示',
content: '检测到新版本,是否下载新版本并重启小程序?',
success: function (res) {
if (res.confirm) {
/** 2. 用户确定下载更新小程序,小程序下载及更新静默进行 */
downLoadAndUpdate(updateManager)
} else if (res.cancel) {
/** 用户点击取消按钮,需要强制更新,二次弹窗 */
uni.showModal({
title: '温馨提示~',
content: '本次版本更新涉及到新的功能添加,旧版本无法正常访问的哦~',
showCancel: false,
confirmText: "确定更新",
success: function (res) {
if (res.confirm) {
/** 下载新版本,并重新应用 */
downLoadAndUpdate(updateManager)
}
}
})
}
}
})
}
})
} else {
/** 如果希望用户在最新版本的客户端上体验您的小程序,可以这样子提示 */
uni.showModal({
title: '提示',
content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'
})
}
})
onShow(() => {
console.log('App Show')
})
onHide(() => {
console.log('App Hide')
})
}
const downLoadAndUpdate = (updateManager : any) => {
uni.showLoading();
/** 静默下载更新小程序新版本 */
updateManager.onUpdateReady(function () {
uni.hideLoading()
/** 新的版本已经下载好,调用 applyUpdate 应用新版本并重启 */
updateManager.applyUpdate()
})
updateManager.onUpdateFailed(function () {
// 新的版本下载失败
uni.showModal({
title: '已经有新版本了哟~',
content: '新版本已经上线啦~,请您删除当前小程序,重新搜索打开哟~',
})
})
},
const checkVersion = () => {
let version = plus.runtime.version;
console.log('版本号:' + version);
/** 检测当前平台,如果是安卓则启动安卓更新 */
uni.getSystemInfo({
success: res => {
console.log('当前平台:' + res.platform);
if (res.platform == 'android') {
updateHandler(version);
}
}
})
}
const updateHandler = async (version : any) => {
const appVersions : Response<any> = await queryAppVersions();
if (appVersions.status && appVersions.data?.version && appVersions.data?.version > version) {
uni.showModal({
title: '更新提示',
content: appVersions.data.note,
success(resOpen) {
if (resOpen.confirm) {
plus.runtime.openURL(appVersions.data.android);
}
}
})
}
}
</script>
<style lang="scss">

View File

@@ -1,6 +1,11 @@
import { post } from '@/core/utils/http';
import { Response, ShopConfigType, PageConfigType, RecordType } from '@/core/models';
/** 获取APP版本 */
export const queryAppVersions = () : Promise<Response<ShopConfigType>> => {
return post('Api/Common/GetAppVersions');
}
/** 获取项目配置 */
export const queryShopConfigV2 = () : Promise<Response<ShopConfigType>> => {
return post('Api/Common/GetConfigV2');

View File

@@ -1,9 +1,9 @@
// 项目静态资源请求地址, 通常使用cnd存放静态资源 参考 cdnFilesUrl
// 项目静态资源请求地址, 通常使用cnd存放静态资源 目前前端项目集成的小图片等均在项目内,可将项目内图片放置第三方存储后使用此地址拼接。
// 例如 填写https://files.cdn.coreshop.cn 页面上图片路径则为 https://files.cdn.coreshop.cn/static/images/back.png
export const FileHost : string = "";
/** 分享初始化地址 */
export const shareUrl = 'pages/share/jump/jump';
/** cdn路径 */
/** cdn路径 目前前端项目未集成的一些过大图片影响程序分包的使用我们默认提供的cdn访问 */
export const cdnFilesUrl = 'https://files.cdn.coreshop.cn';