app.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. const { net } = require('./utils/net.js');
  2. App({
  3. onLaunch: function () {
  4. this.autoUpdate();
  5. },
  6. autoUpdate: function () {
  7. var self = this
  8. // 获取小程序更新机制兼容
  9. if (wx.canIUse('getUpdateManager')) {
  10. const updateManager = wx.getUpdateManager()
  11. //1. 检查小程序是否有新版本发布
  12. updateManager.onCheckForUpdate(function (res) {
  13. // 请求完新版本信息的回调
  14. if (res.hasUpdate) {
  15. //检测到新版本,需要更新,给出提示
  16. wx.showModal({
  17. title: '更新提示',
  18. content: '检测到新版本,是否下载新版本并重启小程序?',
  19. success: function (res) {
  20. if (res.confirm) {
  21. //2. 用户确定下载更新小程序,小程序下载及更新静默进行
  22. self.downLoadAndUpdate(updateManager)
  23. } else if (res.cancel) {
  24. //用户点击取消按钮的处理,如果需要强制更新,则给出二次弹窗,如果不需要,则这里的代码都可以删掉了
  25. wx.showModal({
  26. title: '温馨提示~',
  27. content: '本次版本更新涉及到新的功能添加,旧版本无法正常访问的哦~',
  28. showCancel: false,//隐藏取消按钮
  29. confirmText: "确定更新",//只保留确定更新按钮
  30. success: function (res) {
  31. if (res.confirm) {
  32. //下载新版本,并重新应用
  33. self.downLoadAndUpdate(updateManager)
  34. }
  35. }
  36. })
  37. }
  38. }
  39. })
  40. }
  41. })
  42. } else {
  43. // 如果希望用户在最新版本的客户端上体验您的小程序,可以这样子提示
  44. wx.showModal({
  45. title: '提示',
  46. content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'
  47. })
  48. }
  49. },
  50. /**
  51. * 下载小程序新版本并重启应用
  52. */
  53. downLoadAndUpdate: function (updateManager) {
  54. var self = this
  55. wx.showLoading();
  56. //静默下载更新小程序新版本
  57. updateManager.onUpdateReady(function () {
  58. wx.hideLoading()
  59. //新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  60. updateManager.applyUpdate()
  61. })
  62. updateManager.onUpdateFailed(function () {
  63. // 新的版本下载失败
  64. wx.showModal({
  65. title: '已经有新版本了哟~',
  66. content: '新版本已经上线啦~,请您删除当前小程序,重新搜索打开哟~',
  67. })
  68. })
  69. }
  70. })