app.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. },
  104. /**
  105. * 聊天事件快捷注册
  106. */
  107. $on: function (name, action) {
  108. this.$chat.$on(name, action);
  109. },
  110. /**
  111. * 信息提示 + 跳转
  112. * @param object opt {title:'提示语',icon:''} | url
  113. * @param object to_url 跳转url 有5种跳转方式 {tab:1-5,url:跳转地址}
  114. */
  115. Tips: function (opt, to_url) {
  116. return util.Tips(opt, to_url);
  117. },
  118. /**
  119. * 快捷调取助手函数
  120. */
  121. help: function () {
  122. return util.$h;
  123. },
  124. setUnread: function (newVal) {
  125. this.globalData.unread = newVal
  126. this.updateUnread();
  127. },
  128. /**
  129. * update unread message tip
  130. * @param {int} newval
  131. */
  132. updateUnread: function () {
  133. if (this.globalData.unread != undefined && this.globalData.unread > 0) {
  134. wx.setTabBarBadge({
  135. index: 2,
  136. text: this.globalData.unread.toString(),
  137. });
  138. } else {
  139. wx.removeTabBarBadge({
  140. index: 2,
  141. });
  142. }
  143. }, // updateUnread
  144. /**
  145. * 合并数组
  146. * @param array list 请求返回数据
  147. * @param array sp 原始数组
  148. * @return array
  149. */
  150. SplitArray: function (list, sp) {
  151. return util.SplitArray(list, sp)
  152. },
  153. }) // App