App.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. showCancel:false,
  22. success: function(res) {
  23. if (res.confirm) {
  24. that.downLoadAndUpdateApplet()
  25. } else if (res.cancel) {
  26. wx.showModal({
  27. title: '温馨提示~',
  28. content: '本次版本更新比较重要,旧版本可能无法正常访问的哦~',
  29. showCancel: false,
  30. confirmText: "确定更新",
  31. success: function(res) {
  32. if (res.confirm) {
  33. that.downLoadAndUpdateApplet()
  34. }
  35. }
  36. })
  37. }
  38. }
  39. })
  40. }
  41. // 不走if就是最新版
  42. })
  43. } else {
  44. that.downLoadAndUpdateWeChat()
  45. }
  46. }
  47. downLoadAndUpdateApplet() {
  48. const that = this;
  49. wx.showModal({
  50. title: '升级提示',
  51. showCancel:false,
  52. content: '如果小程序自动升级失败~请您手动删除当前小程序后,重新打开小程序~',
  53. success:function(res){
  54. wx.showLoading();
  55. that.updateManager.onUpdateReady(function() {
  56. wx.hideLoading();
  57. that.updateManager.applyUpdate()
  58. })
  59. }
  60. })
  61. that.updateManager.onUpdateFailed(function() {
  62. wx.hideLoading();
  63. wx.showModal({
  64. title: '已经有新版本了哟~',
  65. content: '新版本已经上线啦~,请您删除当前小程序,重新从公众号或搜索打开小程序哟~',
  66. })
  67. })
  68. }
  69. downLoadAndUpdateWeChat() {
  70. wx.showModal({
  71. title: '重要提示',
  72. content: '您当前微信版本过低,使用过程可能会不稳定。点击确认立刻升级微信。',
  73. success: function(res) {
  74. if (res.confirm) {
  75. wx.updateWeChatApp({
  76. success: function(res) {
  77. console.log('res', res)
  78. },
  79. fail: function(err) {
  80. console.log('err', err)
  81. },
  82. })
  83. }
  84. }
  85. })
  86. }
  87. }
  88. export default {
  89. onLaunch: function() {
  90. //#ifdef MP-WEIXIN
  91. WxUpdate.getInstance()
  92. //#endif
  93. },
  94. onShow: function() {
  95. //#ifdef MP-WEIXIN
  96. const wxUpdate = WxUpdate.getInstance()
  97. wxUpdate.check();
  98. //#endif
  99. },
  100. onHide: function() {
  101. console.log('App Hide')
  102. }
  103. }
  104. </script>
  105. <style>
  106. /*每个页面公共css */
  107. </style>