app.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // app.js
  2. let Nova = require("./utils/nova.js");
  3. const CONFIG = require("config.js");
  4. const request = require("./utils/request");
  5. const plugin = requirePlugin('fm-plugin')
  6. const { Parse } = plugin
  7. App({
  8. onLaunch() {
  9. // 展示本地存储能力
  10. const logs = wx.getStorageSync('logs') || []
  11. logs.unshift(Date.now())
  12. wx.setStorageSync('logs', logs)
  13. let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {}
  14. if (extConfig && extConfig.company) {
  15. this.globalData.company = extConfig.company
  16. this.globalData.appid = extConfig.wxappid
  17. }
  18. let {
  19. model,
  20. platform,
  21. statusBarHeight,
  22. safeArea,
  23. screenHeight
  24. } = wx.getSystemInfoSync();
  25. this.globalData.platform = platform;
  26. this.globalData.statusBarHeight = statusBarHeight;
  27. this.globalData.safeArea = safeArea;
  28. this.globalData.screenHeight = screenHeight;
  29. this.globalData.isIpx = model.includes("iPhone X");
  30. let {
  31. system
  32. } = wx.getSystemInfoSync();
  33. let headHeight;
  34. if (/iphone\s{0,}x/i.test(model)) {
  35. headHeight = 88;
  36. } else if (system.indexOf("Android") !== -1) {
  37. headHeight = 68;
  38. } else {
  39. headHeight = 64;
  40. }
  41. this.globalData.headerHeight = headHeight;
  42. },
  43. Nova: Nova,
  44. Parse: Parse,
  45. JIMData: {
  46. convers: [],
  47. messages: {},
  48. },
  49. globalData: {
  50. userInfo: null,
  51. theme: CONFIG.default.theme,
  52. //腾讯地图的key
  53. tencentKey: CONFIG.default.tencentKey,
  54. // NovaCloud
  55. ncloud: request,
  56. // JMessage
  57. jmessage: CONFIG.default.jmessage,
  58. // We7
  59. api: CONFIG.default.api,
  60. approot: CONFIG.default.approot,
  61. // Tabbar
  62. defaultTabBar: CONFIG.default.defaultTabBar,
  63. // checkAuth
  64. appid: CONFIG.default.appid,
  65. company: CONFIG.default.company,
  66. rootPage: CONFIG.default.rootPage,
  67. appType: CONFIG.default.appType,
  68. //获取相关tabs的name
  69. moduleTab:CONFIG.default.moduleTab,
  70. },
  71. checkAuth: plugin.checkAuth,
  72. parseLogin(token){
  73. return Parse.User.become(token).then(async currentUser => {
  74. console.log(currentUser)
  75. resolve(currentUser)
  76. }).catch(err => {
  77. wx.setStorageSync("sessionToken", null);
  78. wx.showModal({
  79. title: '提示',
  80. content: '登录信息过期,请退出重新进入小程序',
  81. showCancel: false,
  82. cancelText: '取消',
  83. cancelColor: '#000000',
  84. confirmText: '确定',
  85. confirmColor: '#3CC51F',
  86. success: (result) => {
  87. if (result.confirm) {
  88. wx.exitMiniProgram()
  89. }
  90. },
  91. });
  92. rej(err)
  93. })
  94. },
  95. onShow: function (options) {
  96. this.autoUpdate()
  97. let { referrerInfo } = options
  98. if(referrerInfo?.extraData?.status && referrerInfo.extraData.status == 'success'){
  99. getApp().globalData.merchant_trade_no = referrerInfo.extraData.req_extradata.merchant_trade_no
  100. }
  101. },
  102. autoUpdate() {
  103. let self = this
  104. if (wx.canIUse('getUpdateManager')) {
  105. const updateManager = wx.getUpdateManager()
  106. //1. 检查小程序是否有新版本发布
  107. updateManager.onCheckForUpdate(function (res) {
  108. // 请求完新版本信息的回调
  109. if (res.hasUpdate) {
  110. wx.showModal({
  111. title: '更新提示',
  112. content: '检测到新版本,是否下载新版本并重启小程序?',
  113. success: function (res) {
  114. if (res.confirm) {
  115. //2. 用户确定下载更新小程序,小程序下载及更新静默进行
  116. self.downLoadAndUpdate(updateManager)
  117. } else if (res.cancel) {
  118. //用户点击取消按钮的处理,强制更新
  119. wx.showModal({
  120. title: '温馨提示~',
  121. content: '本次版本更新涉及到新的功能添加,旧版本无法正常访问的哦~',
  122. showCancel: false,
  123. confirmText: "确定更新",
  124. success: function (res) {
  125. if (res.confirm) {
  126. //下载新版本,并重新应用
  127. self.downLoadAndUpdate(updateManager)
  128. }
  129. }
  130. })
  131. }
  132. }
  133. })
  134. }
  135. })
  136. } else {
  137. wx.showModal({
  138. title: '提示',
  139. content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'
  140. })
  141. }
  142. },
  143. /**
  144. * 下载小程序新版本并重启应用
  145. */
  146. downLoadAndUpdate(updateManager) {
  147. wx.showLoading();
  148. //静默下载更新小程序新版本
  149. updateManager.onUpdateReady(function () {
  150. wx.hideLoading()
  151. //新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  152. updateManager.applyUpdate()
  153. })
  154. updateManager.onUpdateFailed(function () {
  155. // 新的版本下载失败
  156. wx.showModal({
  157. title: '已经有新版本了哟~',
  158. content: '新版本已经上线啦~,请您删除当前小程序,重新搜索打开哟~',
  159. })
  160. })
  161. },
  162. onHide: function () {
  163. let pages = getCurrentPages()
  164. if (pages.length > 0) {
  165. let prevPage = pages[pages.length - 1]
  166. if (prevPage.route === 'nova-exam/pages/live-pusher/index') {
  167. prevPage.data.client.leave()
  168. prevPage.stop()
  169. prevPage.data.timer && clearInterval(prevPage.data.timer)
  170. wx.showModal({
  171. title: '提示',
  172. content: '考生将应用切换至后台,请重新进入',
  173. showCancel: false,
  174. success: (res) => {
  175. wx.navigateBack({
  176. delta: 1,
  177. })
  178. }
  179. });
  180. }
  181. }
  182. }
  183. })