| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- <script>
- import { showModal, showLoading } from '@/utils/myfun.js';
- import { post } from "@/request/api.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();
- let token = uni.getStorageSync("token");
- if (!token) this.login()
- },
- 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);
- });
- });
- },
- login() {
- // #ifdef MP-WEIXIN
- wx.login({
- success(res) {
- if (res.code) {
- post("v1/appletLogin", {
- code: res.code,
- invite: uni.getStorageSync("inviteCode") || "",
- }).then((res) => {
- if (res.code === 0) {
- if (res.data.token) {
- uni.setStorageSync("token", res.data.token);
- }
- if(res.data.unid){
- uni.setStorageSync("unid", res.data.unid);
- }
- }
- });
- }
- },
- });
- // #endif
- },
- },
- };
- </script>
- <style>
- /*每个页面公共css */
- @import "./app.scss";
- </style>
|