| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- <script>
- import { showModal, showLoading } from '@/utils/myfun.js';
- export default {
- globalData: {
- shopInfo: {},
- version: ""
- },
- onLaunch() {
- let logs = uni.getStorageSync("logs") || [];
- logs.unshift(Date.now());
- uni.setStorageSync("logs", logs);
- uni.setStorageSync("mallName", "茶付宝");
- // #ifdef MP-WEIXIN
- let accountInfo = __wxConfig.accountInfo
- this.globalData.shopInfo = {
- shop_name: accountInfo.nickname,
- shop_image: accountInfo.icon
- }
- // #endif
- },
- onShow() {
- this.autoUpdate();
- },
- onHide() {},
- methods: {
- autoUpdate() {
- // 更新的功能基础库要1.9.90以上版本才支持,要做低版本的兼容处理
- if (uni.canIUse('getUpdateManager')) {
- // uni.getUpdateManager接口,可以获知是否有新版本的小程序、新版本是否下载好以及应用新版本的能力,会返回一个UpdateManager实例
- const updateManager = uni.getUpdateManager();
- // 检查小程序是否有新版本发布,onCheckForUpdate:当小程序向后台请求完新版本信息,会通知这个版本告知检查结果
- updateManager.onCheckForUpdate(res => {
- // 请求完新版本信息的回调
- if (res.hasUpdate) {
- // 检测到新版本,需要更新,给出提示
- showModal({ content: '检测到新版本,是否下载新版本并重启小程序' }).then(res => {
- if (res.confirm) {
- this.downLoadAndUpdate(updateManager);
- } else if (res.cancel) {
- showModal({ content: '本次版本更新涉及到新功能的添加,旧版本将无法正常使用', showCancel: false, confirmText: '确认更新' }).then(res => {
- if (res.confirm) this.downLoadAndUpdate(updateManager);
- });
- }
- });
- }
- });
- } else {
- // 在最新版本客户端上体验小程序
- showModal({ content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试' });
- }
- },
- // 下载小程序最新版本并重启
- downLoadAndUpdate(updateManager) {
- showLoading({ title: '正在下载' });
- // 等待下载更新小程序新版本,onUpdateReady:当新版本下载完成回调
- updateManager.onUpdateReady(() => {
- uni.hideLoading();
- // applyUpdate:强制当前小程序应用上新版本并重启
- updateManager.applyUpdate();
- // 可在此处添加清除缓存跳转首页操作,由于本人在通过其他判断方式跳转首页,故不再此处跳转
- // ....
- });
- // 当新版本下载失败回调
- updateManager.onUpdateFailed(() => {
- uni.hideLoading();
- // 下载新版本失败
- showModal({ content: '新版本已经上线了,请删除当前小程序,重新搜索打开' })
- .then(() => {
- // 点击确定,清空小程序缓存,是小程序情况而定
- clearPartStorageSync();
- setTimeout(() => {
- // 跳转到首页
- uni.reLaunch({ url: '/pages/index/index' });
- }, 100);
- })
- .catch(() => {
- // 点击取消,同理
- clearPartStorageSync();
- setTimeout(() => {
- uni.reLaunch({ url: '/pages/index/index' });
- }, 100);
- });
- });
- }
- },
- };
- </script>
- <style>
- /*每个页面公共css */
- @import "./app.scss";
- </style>
|