app.js 3.0 KB

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