app.js 14 KB

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