app.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import {
  2. HTTP_REQUEST_URL,
  3. CACHE_USERINFO,
  4. CACHE_TOKEN,
  5. CACHE_EXPIRES_TIME
  6. } from './config.js';
  7. import Server from './utils/Server.js';
  8. import util from './utils/util.js';
  9. App({
  10. onLaunch: function (option) {
  11. if (HTTP_REQUEST_URL == '') {
  12. console.error("请配置根目录下的config.js文件中的 'HTTP_REQUEST_URL'\n\n请修改开发者工具中【详情】->【AppID】改为自己的Appid\n\n请前往后台【小程序】->【小程序配置】填写自己的 appId and AppSecret");
  13. return false;
  14. }
  15. let that = this;
  16. let token = wx.getStorageSync(CACHE_TOKEN);
  17. let expiresTime = wx.getStorageSync(CACHE_EXPIRES_TIME);
  18. let userInfo = wx.getStorageSync(CACHE_USERINFO);
  19. this.globalData.isLog = !!userInfo && util.isLoginValid(token, expiresTime, true);
  20. if (this.globalData.isLog) {
  21. this.globalData.token = token;
  22. this.globalData.expiresTime = expiresTime;
  23. this.globalData.userInfo = userInfo ? JSON.parse(userInfo) : {};
  24. }
  25. if (option.query.hasOwnProperty('scene')) {
  26. switch (option.scene) {
  27. //扫描小程序码
  28. case 1047:
  29. that.globalData.code = option.query.scene;
  30. break;
  31. //长按图片识别小程序码
  32. case 1048:
  33. that.globalData.code = option.query.scene;
  34. break;
  35. //手机相册选取小程序码
  36. case 1049:
  37. that.globalData.code = option.query.scene;
  38. break;
  39. //直接进入小程序
  40. case 1001:
  41. that.globalData.spid = option.query.scene;
  42. break;
  43. } // switch
  44. } // if
  45. // 获取导航高度;
  46. wx.getSystemInfo({
  47. success: res => {
  48. //导航高度
  49. this.globalData.navHeight = res.statusBarHeight * (750 / res.windowWidth) + 97;
  50. var model = res.model;
  51. if (/iphone\sx/i.test(model) ||
  52. (/iphone/i.test(model) && /unknown/.test(model)) ||
  53. /iphone\s11/i.test(model)) {
  54. that.globalData.isFixed = true;
  55. } else {
  56. that.globalData.isFixed = false;
  57. }
  58. },
  59. fail(err) {}
  60. });
  61. const updateManager = wx.getUpdateManager();
  62. updateManager.onCheckForUpdate(function (res) {
  63. // 请求完新版本信息的回调
  64. })
  65. updateManager.onUpdateReady(function () {
  66. wx.showModal({
  67. title: '更新提示',
  68. content: '新版本已经准备好,是否重启应用?',
  69. success: function (res) {
  70. if (res.confirm) {
  71. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  72. updateManager.applyUpdate()
  73. }
  74. }
  75. })
  76. });
  77. updateManager.onUpdateFailed(function () {
  78. return that.Tips({
  79. title: '新版本下载失败'
  80. });
  81. })
  82. //实例化聊天服务
  83. this.$chat = new Server(this);
  84. }, // onLaunch
  85. $chat: null,
  86. globalData: {
  87. navHeight: 0,
  88. routineStyle: '#ffffff',
  89. openPages: '',
  90. spid: 0,
  91. code: 0,
  92. urlImages: '',
  93. url: HTTP_REQUEST_URL,
  94. token: '',
  95. isLog: false, // 是否已登录
  96. expiresTime: 0,
  97. MyMenus: [],
  98. userInfo: {},
  99. loginType: 'routine',
  100. isFixed: false,
  101. unread: 0, // 未读消息条数
  102. show_benefit: false, // 是否显示赔付比例
  103. channel: 0,
  104. },
  105. /**
  106. * 聊天事件快捷注册
  107. */
  108. $on: function (name, action) {
  109. this.$chat.$on(name, action);
  110. },
  111. /**
  112. * 信息提示 + 跳转
  113. * @param object opt {title:'提示语',icon:''} | url
  114. * @param object to_url 跳转url 有5种跳转方式 {tab:1-5,url:跳转地址}
  115. */
  116. Tips: function (opt, to_url) {
  117. return util.Tips(opt, to_url);
  118. },
  119. /**
  120. * 快捷调取助手函数
  121. */
  122. help: function () {
  123. return util.$h;
  124. },
  125. setUnread: function (newVal) {
  126. this.globalData.unread = newVal
  127. this.updateUnread();
  128. },
  129. /**
  130. * update unread message tip
  131. * @param {int} newval
  132. */
  133. updateUnread: function () {
  134. if (this.globalData.unread != undefined && this.globalData.unread > 0) {
  135. wx.setTabBarBadge({
  136. index: 2,
  137. text: this.globalData.unread.toString(),
  138. });
  139. } else {
  140. wx.removeTabBarBadge({
  141. index: 2,
  142. });
  143. }
  144. }, // updateUnread
  145. /**
  146. * 合并数组
  147. * @param array list 请求返回数据
  148. * @param array sp 原始数组
  149. * @return array
  150. */
  151. SplitArray: function (list, sp) {
  152. return util.SplitArray(list, sp)
  153. },
  154. }) // App