App.vue 4.0 KB

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