App.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <script>
  2. let updateManager = null
  3. let canIUse = true
  4. class WxUpdate {
  5. updateManager = wx.getUpdateManager();
  6. canIUse = wx.canIUse('getUpdateManager');
  7. static getInstance(){
  8. if (!this.instance) {
  9. this.instance = new WxUpdate();
  10. }
  11. return this.instance
  12. }
  13. check() {
  14. const that = this
  15. if (that.canIUse) {
  16. that.updateManager.onCheckForUpdate(function(res) {
  17. if (res.hasUpdate) {
  18. wx.showModal({
  19. title: '更新提示',
  20. content: '检测到新版本,是否下载新版本并重启小程序?',
  21. success: function(res) {
  22. if (res.confirm) {
  23. that.downLoadAndUpdateApplet()
  24. } else if (res.cancel) {
  25. wx.showModal({
  26. title: '温馨提示~',
  27. content: '本次版本更新比较重要,旧版本可能无法正常访问的哦~',
  28. showCancel: false,
  29. confirmText: "确定更新",
  30. success: function(res) {
  31. if (res.confirm) {
  32. that.downLoadAndUpdateApplet()
  33. }
  34. }
  35. })
  36. }
  37. }
  38. })
  39. }
  40. // 不走if就是最新版
  41. })
  42. } else {
  43. that.downLoadAndUpdateWeChat()
  44. }
  45. }
  46. downLoadAndUpdateApplet() {
  47. const that = this;
  48. wx.showLoading();
  49. that.updateManager.onUpdateReady(function() {
  50. wx.hideLoading();
  51. that.updateManager.applyUpdate()
  52. })
  53. that.updateManager.onUpdateFailed(function() {
  54. wx.hideLoading();
  55. wx.showModal({
  56. title: '已经有新版本了哟~',
  57. content: '新版本已经上线啦~,请您删除当前小程序,重新从公众号或搜索打开小程序哟~',
  58. })
  59. })
  60. }
  61. downLoadAndUpdateWeChat() {
  62. wx.showModal({
  63. title: '重要提示',
  64. content: '您当前微信版本过低,使用过程可能会不稳定。点击确认立刻升级微信。',
  65. success: function(res) {
  66. if (res.confirm) {
  67. wx.updateWeChatApp({
  68. success: function(res) {
  69. console.log('res', res)
  70. },
  71. fail: function(err) {
  72. console.log('err', err)
  73. },
  74. })
  75. }
  76. }
  77. })
  78. }
  79. }
  80. export default {
  81. onLaunch: function() {
  82. //#ifdef MP-WEIXIN
  83. WxUpdate.getInstance()
  84. //#endif
  85. },
  86. onShow: function() {
  87. //#ifdef MP-WEIXIN
  88. const wxUpdate = WxUpdate.getInstance()
  89. wxUpdate.check();
  90. //#endif
  91. },
  92. onHide: function() {
  93. console.log('App Hide')
  94. }
  95. }
  96. </script>
  97. <style>
  98. /*每个页面公共css */
  99. </style>