App.vue 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <script>
  2. import { showModal, showLoading } from '@/utils/myfun.js';
  3. export default {
  4. globalData: {
  5. shopInfo: {},
  6. version: ""
  7. },
  8. onLaunch() {
  9. let logs = uni.getStorageSync("logs") || [];
  10. logs.unshift(Date.now());
  11. uni.setStorageSync("logs", logs);
  12. uni.setStorageSync("mallName", "茶付宝");
  13. // #ifdef MP-WEIXIN
  14. let accountInfo = __wxConfig.accountInfo
  15. this.globalData.shopInfo = {
  16. shop_name: accountInfo.nickname,
  17. shop_image: accountInfo.icon
  18. }
  19. // #endif
  20. },
  21. onShow() {
  22. this.autoUpdate();
  23. },
  24. onHide() {},
  25. methods: {
  26. autoUpdate() {
  27. // 更新的功能基础库要1.9.90以上版本才支持,要做低版本的兼容处理
  28. if (uni.canIUse('getUpdateManager')) {
  29. // uni.getUpdateManager接口,可以获知是否有新版本的小程序、新版本是否下载好以及应用新版本的能力,会返回一个UpdateManager实例
  30. const updateManager = uni.getUpdateManager();
  31. // 检查小程序是否有新版本发布,onCheckForUpdate:当小程序向后台请求完新版本信息,会通知这个版本告知检查结果
  32. updateManager.onCheckForUpdate(res => {
  33. // 请求完新版本信息的回调
  34. if (res.hasUpdate) {
  35. // 检测到新版本,需要更新,给出提示
  36. showModal({ content: '检测到新版本,是否下载新版本并重启小程序' }).then(res => {
  37. if (res.confirm) {
  38. this.downLoadAndUpdate(updateManager);
  39. } else if (res.cancel) {
  40. showModal({ content: '本次版本更新涉及到新功能的添加,旧版本将无法正常使用', showCancel: false, confirmText: '确认更新' }).then(res => {
  41. if (res.confirm) this.downLoadAndUpdate(updateManager);
  42. });
  43. }
  44. });
  45. }
  46. });
  47. } else {
  48. // 在最新版本客户端上体验小程序
  49. showModal({ content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试' });
  50. }
  51. },
  52. // 下载小程序最新版本并重启
  53. downLoadAndUpdate(updateManager) {
  54. showLoading({ title: '正在下载' });
  55. // 等待下载更新小程序新版本,onUpdateReady:当新版本下载完成回调
  56. updateManager.onUpdateReady(() => {
  57. uni.hideLoading();
  58. // applyUpdate:强制当前小程序应用上新版本并重启
  59. updateManager.applyUpdate();
  60. // 可在此处添加清除缓存跳转首页操作,由于本人在通过其他判断方式跳转首页,故不再此处跳转
  61. // ....
  62. });
  63. // 当新版本下载失败回调
  64. updateManager.onUpdateFailed(() => {
  65. uni.hideLoading();
  66. // 下载新版本失败
  67. showModal({ content: '新版本已经上线了,请删除当前小程序,重新搜索打开' })
  68. .then(() => {
  69. // 点击确定,清空小程序缓存,是小程序情况而定
  70. clearPartStorageSync();
  71. setTimeout(() => {
  72. // 跳转到首页
  73. uni.reLaunch({ url: '/pages/index/index' });
  74. }, 100);
  75. })
  76. .catch(() => {
  77. // 点击取消,同理
  78. clearPartStorageSync();
  79. setTimeout(() => {
  80. uni.reLaunch({ url: '/pages/index/index' });
  81. }, 100);
  82. });
  83. });
  84. }
  85. },
  86. };
  87. </script>
  88. <style>
  89. /*每个页面公共css */
  90. @import "./app.scss";
  91. </style>