app.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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 originalShowModal = wx.showModal;
  11. wx.showModal = function(options) {
  12. // 如果是"登录信息已过期"的提示,改为友好的重新登录提示
  13. if (options.content && (options.content.includes('登录信息过期') || options.content.includes('登录已过期'))) {
  14. console.log('⚠️ [全局拦截] 拦截到"登录信息已过期"提示');
  15. return originalShowModal.call(this, {
  16. title: '提示',
  17. content: '登录状态异常,是否重新登录?',
  18. showCancel: true,
  19. cancelText: '取消',
  20. confirmText: '重新登录',
  21. success: (result) => {
  22. if (result.confirm) {
  23. // 清除登录状态
  24. wx.removeStorageSync("sessionToken");
  25. wx.removeStorageSync("userLogin");
  26. // 重新加载首页
  27. const rootPage = getApp().globalData.rootPage || getApp().globalData.defaultTabBar?.list?.[0]?.pagePath || '/pages/index/index';
  28. wx.reLaunch({
  29. url: rootPage,
  30. success: () => {
  31. console.log('✅ 重新加载首页成功');
  32. },
  33. fail: (err) => {
  34. console.error('❌ 重新加载失败:', err);
  35. }
  36. });
  37. }
  38. // 调用原始的 success 回调
  39. if (options.success) {
  40. options.success(result);
  41. }
  42. }
  43. });
  44. }
  45. // 其他提示正常显示
  46. return originalShowModal.call(this, options);
  47. };
  48. // 展示本地存储能力
  49. const logs = wx.getStorageSync('logs') || []
  50. logs.unshift(Date.now())
  51. wx.setStorageSync('logs', logs)
  52. let extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {}
  53. if (extConfig && extConfig.company) {
  54. this.globalData.company = extConfig.company
  55. this.globalData.appid = extConfig.wxappid
  56. }
  57. let {
  58. model,
  59. platform,
  60. statusBarHeight,
  61. safeArea,
  62. screenHeight
  63. } = wx.getSystemInfoSync();
  64. this.globalData.platform = platform;
  65. this.globalData.statusBarHeight = statusBarHeight;
  66. this.globalData.safeArea = safeArea;
  67. this.globalData.screenHeight = screenHeight;
  68. this.globalData.isIpx = model.includes("iPhone X");
  69. let {
  70. system
  71. } = wx.getSystemInfoSync();
  72. let headHeight;
  73. if (/iphone\s{0,}x/i.test(model)) {
  74. headHeight = 88;
  75. } else if (system.indexOf("Android") !== -1) {
  76. headHeight = 68;
  77. } else {
  78. headHeight = 64;
  79. }
  80. this.globalData.headerHeight = headHeight;
  81. },
  82. Nova: Nova,
  83. Parse: Parse,
  84. JIMData: {
  85. convers: [],
  86. messages: {},
  87. },
  88. globalData: {
  89. userInfo: null,
  90. theme: CONFIG.default.theme,
  91. //腾讯地图的key
  92. tencentKey: CONFIG.default.tencentKey,
  93. // NovaCloud
  94. ncloud: request,
  95. // JMessage
  96. jmessage: CONFIG.default.jmessage,
  97. // We7
  98. api: CONFIG.default.api,
  99. approot: CONFIG.default.approot,
  100. // Tabbar
  101. defaultTabBar: CONFIG.default.defaultTabBar,
  102. // checkAuth
  103. appid: CONFIG.default.appid,
  104. company: CONFIG.default.company,
  105. rootPage: CONFIG.default.rootPage,
  106. appType: CONFIG.default.appType,
  107. //获取相关tabs的name
  108. moduleTab:CONFIG.default.moduleTab,
  109. },
  110. checkAuth: plugin.checkAuth,
  111. parseLogin(token){
  112. return Parse.User.become(token).then(async currentUser => {
  113. console.log('✅ parseLogin 成功:', currentUser.id);
  114. return currentUser;
  115. }).catch(async err => {
  116. console.log('❌ parseLogin 失败:', err);
  117. // 清除过期的 token
  118. wx.removeStorageSync("sessionToken");
  119. wx.removeStorageSync("userLogin");
  120. // 不要直接退出,而是尝试重新登录
  121. wx.showModal({
  122. title: '提示',
  123. content: '登录状态异常,是否重新登录?',
  124. showCancel: true,
  125. cancelText: '取消',
  126. confirmText: '重新登录',
  127. success: async (result) => {
  128. if (result.confirm) {
  129. try {
  130. // 尝试重新登录
  131. await Parse.User.logOut();
  132. await this.checkAuth(true);
  133. // 重新获取用户信息
  134. const currentUser = Parse.User.current();
  135. if (currentUser && currentUser.get('mobile')) {
  136. wx.setStorageSync("userLogin", currentUser.id);
  137. console.log('✅ 重新登录成功');
  138. // 刷新当前页面
  139. const pages = getCurrentPages();
  140. const currentPage = pages[pages.length - 1];
  141. if (currentPage && currentPage.onLoad) {
  142. currentPage.onLoad(currentPage.options || {});
  143. }
  144. }
  145. } catch (reloginErr) {
  146. console.error('❌ 重新登录失败:', reloginErr);
  147. wx.showToast({
  148. title: '登录失败,请稍后重试',
  149. icon: 'none'
  150. });
  151. }
  152. }
  153. },
  154. });
  155. throw err;
  156. })
  157. },
  158. // checkAuth: async function (force = true) {
  159. // return new Promise(async (resolve,rej)=>{
  160. // let that = this;
  161. // // 0.检测是否开启各页面强制登陆
  162. // let enabledOptions = wx.getStorageSync("enabledOptions");
  163. // if (force || (enabledOptions && enabledOptions["check-auth"])) {
  164. // let invite = wx.getStorageSync('invite')
  165. // let token = wx.getStorageSync('sessionToken')
  166. // if(!token){
  167. // token = await this.getParseToken()
  168. // let userInfo = wx.getStorageSync("userInfo");
  169. // that.globalData.userInfo = userInfo
  170. // if(!token){
  171. // wx.showModal({
  172. // title: '提示',
  173. // content: '获取登录信息失败,请删除小程序重新进入',
  174. // showCancel: true,
  175. // cancelText: '取消',
  176. // cancelColor: '#000000',
  177. // confirmText: '确定',
  178. // confirmColor: '#3CC51F',
  179. // success: (result) => {
  180. // if(result.confirm){
  181. // wx.exitMiniProgram()
  182. // }
  183. // },
  184. // });
  185. // resolve(false)
  186. // return
  187. // }
  188. // }
  189. // Parse.User.become(token).then(async currentUser => {
  190. // console.log(currentUser)
  191. // if (invite && !currentUser.get('invite') && currentUser.id != invite && !currentUser.get('agentLevel')) {
  192. // //查询邀请人user,邀请人不能是自己的下级
  193. // let query = new Parse.Query("_User")
  194. // query.equalTo('objectId',invite)
  195. // query.select('invite')
  196. // let result = await query.first()
  197. // if (!(result && result.id && result.get("invite")?.id == currentUser.id)) {
  198. // console.log('上下级绑定成功');
  199. // currentUser.set('invite', {
  200. // __type: "Pointer",
  201. // className: "_User",
  202. // objectId: invite
  203. // })
  204. // currentUser.set('agent', {
  205. // __type: "Pointer",
  206. // className: "_User",
  207. // objectId: invite
  208. // })
  209. // await currentUser.save()
  210. // }
  211. // }
  212. // currentUser.get('mobile') ? wx.setStorageSync("userLogin", currentUser.id) : wx.removeStorageSync("userLogin");
  213. // resolve(currentUser)
  214. // return true
  215. // }).catch(err => {
  216. // wx.setStorageSync("sessionToken", null);
  217. // wx.showModal({
  218. // title: '提示',
  219. // content: '登录信息过期,请退出重新进入小程序',
  220. // showCancel: false,
  221. // cancelText: '取消',
  222. // cancelColor: '#000000',
  223. // confirmText: '确定',
  224. // confirmColor: '#3CC51F',
  225. // success: (result) => {
  226. // if(result.confirm){
  227. // wx.exitMiniProgram()
  228. // }
  229. // },
  230. // });
  231. // rej(err)
  232. // })
  233. // } else {
  234. // resolve(false)
  235. // }
  236. // })
  237. // },
  238. // async getParseToken(){
  239. // return new Promise((resolve, reject) => {
  240. // wx.login({
  241. // success: function (res) {
  242. // if (res.code) {
  243. // let url = 'https://server.fmode.cn/api/wxapp/login'
  244. // wx.request({
  245. // url: url,
  246. // data: {
  247. // companyId: getApp().globalData.company,
  248. // code: res.code,
  249. // appType: getApp().globalData.appType ? getApp().globalData.appType : ''
  250. // },
  251. // async success(res) {
  252. // console.log(res);
  253. // let data = res.data
  254. // if(data.code == 200){
  255. // wx.setStorageSync("sessionToken", data.data.token);
  256. // wx.setStorageSync("userInfo", data.data?.userInfo);
  257. // resolve(data.data.token)
  258. // }else{
  259. // resolve()
  260. // }
  261. // },
  262. // });
  263. // }
  264. // },
  265. // fail: function (err) {
  266. // console.warn('小程序wx.login失败');
  267. // resolve()
  268. // }
  269. // });
  270. // })
  271. // },
  272. //获取全局定位信息
  273. // async getLocationInfo(refrensh = false) {
  274. // let that = this;
  275. // let locationInfo = that.globalData.locationInfo;
  276. // if (locationInfo && !refrensh) {
  277. // return locationInfo;
  278. // } else {
  279. // let locationInfo = await util.getLocation();
  280. // that.globalData.locationInfo = locationInfo;
  281. // return locationInfo;
  282. // }
  283. // },
  284. onShow: function (options) {
  285. this.autoUpdate()
  286. let { referrerInfo } = options
  287. if(referrerInfo?.extraData?.status && referrerInfo.extraData.status == 'success'){
  288. getApp().globalData.merchant_trade_no = referrerInfo.extraData.req_extradata.merchant_trade_no
  289. }
  290. },
  291. autoUpdate() {
  292. let self = this
  293. if (wx.canIUse('getUpdateManager')) {
  294. const updateManager = wx.getUpdateManager()
  295. //1. 检查小程序是否有新版本发布
  296. updateManager.onCheckForUpdate(function (res) {
  297. // 请求完新版本信息的回调
  298. if (res.hasUpdate) {
  299. wx.showModal({
  300. title: '更新提示',
  301. content: '检测到新版本,是否下载新版本并重启小程序?',
  302. success: function (res) {
  303. if (res.confirm) {
  304. //2. 用户确定下载更新小程序,小程序下载及更新静默进行
  305. self.downLoadAndUpdate(updateManager)
  306. } else if (res.cancel) {
  307. //用户点击取消按钮的处理,强制更新
  308. wx.showModal({
  309. title: '温馨提示~',
  310. content: '本次版本更新涉及到新的功能添加,旧版本无法正常访问的哦~',
  311. showCancel: false,
  312. confirmText: "确定更新",
  313. success: function (res) {
  314. if (res.confirm) {
  315. //下载新版本,并重新应用
  316. self.downLoadAndUpdate(updateManager)
  317. }
  318. }
  319. })
  320. }
  321. }
  322. })
  323. }
  324. })
  325. } else {
  326. wx.showModal({
  327. title: '提示',
  328. content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'
  329. })
  330. }
  331. },
  332. /**
  333. * 下载小程序新版本并重启应用
  334. */
  335. downLoadAndUpdate(updateManager) {
  336. wx.showLoading();
  337. //静默下载更新小程序新版本
  338. updateManager.onUpdateReady(function () {
  339. wx.hideLoading()
  340. //新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  341. updateManager.applyUpdate()
  342. })
  343. updateManager.onUpdateFailed(function () {
  344. // 新的版本下载失败
  345. wx.showModal({
  346. title: '已经有新版本了哟~',
  347. content: '新版本已经上线啦~,请您删除当前小程序,重新搜索打开哟~',
  348. })
  349. })
  350. },
  351. onHide: function () {
  352. let pages = getCurrentPages()
  353. if (pages.length > 0) {
  354. let prevPage = pages[pages.length - 1]
  355. if (prevPage.route === 'nova-exam/pages/live-pusher/index') {
  356. prevPage.data.client.leave()
  357. prevPage.stop()
  358. prevPage.data.timer && clearInterval(prevPage.data.timer)
  359. wx.showModal({
  360. title: '提示',
  361. content: '考生将应用切换至后台,请重新进入',
  362. showCancel: false,
  363. success: (res) => {
  364. wx.navigateBack({
  365. delta: 1,
  366. })
  367. }
  368. });
  369. }
  370. }
  371. }
  372. })