app.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //app.js
  2. const utils = require('/utils/util.js').Utils; //载入模块
  3. import { Watch } from './utils/watch'
  4. App({
  5. /**
  6. * 当小程序初始化完成时,会触发 onLaunch(全局只触发一次)
  7. */
  8. onLaunch: function () {
  9. new Watch(wx, this.watchData);
  10. if (!wx.getStorageSync('hasClear')) {
  11. wx.clearStorage({
  12. success: (res) => {
  13. wx.setStorageSync('hasClear', true)
  14. },
  15. })
  16. }
  17. },
  18. /**
  19. * 当小程序启动,或从后台进入前台显示,会触发 onShow
  20. */
  21. onShow: function (options) {
  22. if (!this.captchaTicketExpire) this.captchaTicketExpire = {};
  23. if (options.referrerInfo.appId === 'wx5a3a7366fd07e119') {
  24. const result = options.referrerInfo.extraData;
  25. if (result.ret === 0) {
  26. const ticket = result.ticket;
  27. if (!this.captchaTicketExpire[ticket]) {
  28. this.captchaResult = result;
  29. this.captchaTicketExpire[ticket] = true;
  30. }
  31. }
  32. }
  33. if (wx.getUpdateManager) {
  34. const updateManager = wx.getUpdateManager();
  35. updateManager.onCheckForUpdate(res => {
  36. if (res.hasUpdate) {
  37. updateManager.onUpdateReady(res => {
  38. wx.showModal({
  39. title: '更新提示',
  40. content: '新版本上线了,请点击确定按钮更新!',
  41. showCancel: false,
  42. success: res => {
  43. if (res.confirm) {
  44. updateManager.applyUpdate()
  45. }
  46. }
  47. })
  48. })
  49. updateManager.onUpdateFailed(res => {
  50. wx.showModal({
  51. title: '更新提示',
  52. content: '新版本已上线,请删除当前小程序,重新搜索进入',
  53. showCancel: false,
  54. })
  55. })
  56. }
  57. })
  58. }
  59. },
  60. /**
  61. * 当小程序从前台进入后台,会触发 onHide
  62. */
  63. onHide: function () {
  64. // if (wx.getStorageSync('userinfo').registerFrom === 2) {
  65. // } else {
  66. // wx.clearStorage();
  67. // }
  68. },
  69. /**
  70. * 当小程序发生脚本错误,或者 api 调用失败时,会触发 onError 并带上错误信息
  71. */
  72. onError: function (msg) {
  73. },
  74. /**
  75. * 工具类
  76. */
  77. utils: new utils(wx),
  78. /**
  79. * 全局变量集合
  80. */
  81. globalData: {
  82. userInfo: null,
  83. phone_zz: /1[0-9]{10}/,
  84. webview: null,
  85. version: '1.1.6',
  86. platformId: 1
  87. },
  88. /**
  89. * 需要监测的全局变量
  90. */
  91. watchData: {
  92. offline: false,
  93. is_login: Boolean(wx.getStorageSync('userid'))
  94. },
  95. })