App.vue 5.1 KB

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