app.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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('✅ parseLogin 成功:', currentUser.id);
  75. return currentUser;
  76. }).catch(async err => {
  77. console.log('❌ parseLogin 失败:', err);
  78. // 清除过期的 token
  79. wx.removeStorageSync("sessionToken");
  80. wx.removeStorageSync("userLogin");
  81. // 不要直接退出,而是尝试重新登录
  82. wx.showModal({
  83. title: '提示',
  84. content: '登录状态异常,是否重新登录?',
  85. showCancel: true,
  86. cancelText: '取消',
  87. confirmText: '重新登录',
  88. success: async (result) => {
  89. if (result.confirm) {
  90. try {
  91. // 尝试重新登录
  92. await Parse.User.logOut();
  93. await this.checkAuth(true);
  94. // 重新获取用户信息
  95. const currentUser = Parse.User.current();
  96. if (currentUser && currentUser.get('mobile')) {
  97. wx.setStorageSync("userLogin", currentUser.id);
  98. console.log('✅ 重新登录成功');
  99. // 刷新当前页面
  100. const pages = getCurrentPages();
  101. const currentPage = pages[pages.length - 1];
  102. if (currentPage && currentPage.onLoad) {
  103. currentPage.onLoad(currentPage.options || {});
  104. }
  105. }
  106. } catch (reloginErr) {
  107. console.error('❌ 重新登录失败:', reloginErr);
  108. wx.showToast({
  109. title: '登录失败,请稍后重试',
  110. icon: 'none'
  111. });
  112. }
  113. }
  114. },
  115. });
  116. throw err;
  117. })
  118. },
  119. // checkAuth: async function (force = true) {
  120. // return new Promise(async (resolve,rej)=>{
  121. // let that = this;
  122. // // 0.检测是否开启各页面强制登陆
  123. // let enabledOptions = wx.getStorageSync("enabledOptions");
  124. // if (force || (enabledOptions && enabledOptions["check-auth"])) {
  125. // let invite = wx.getStorageSync('invite')
  126. // let token = wx.getStorageSync('sessionToken')
  127. // if(!token){
  128. // token = await this.getParseToken()
  129. // let userInfo = wx.getStorageSync("userInfo");
  130. // that.globalData.userInfo = userInfo
  131. // if(!token){
  132. // wx.showModal({
  133. // title: '提示',
  134. // content: '获取登录信息失败,请删除小程序重新进入',
  135. // showCancel: true,
  136. // cancelText: '取消',
  137. // cancelColor: '#000000',
  138. // confirmText: '确定',
  139. // confirmColor: '#3CC51F',
  140. // success: (result) => {
  141. // if(result.confirm){
  142. // wx.exitMiniProgram()
  143. // }
  144. // },
  145. // });
  146. // resolve(false)
  147. // return
  148. // }
  149. // }
  150. // Parse.User.become(token).then(async currentUser => {
  151. // console.log(currentUser)
  152. // if (invite && !currentUser.get('invite') && currentUser.id != invite && !currentUser.get('agentLevel')) {
  153. // //查询邀请人user,邀请人不能是自己的下级
  154. // let query = new Parse.Query("_User")
  155. // query.equalTo('objectId',invite)
  156. // query.select('invite')
  157. // let result = await query.first()
  158. // if (!(result && result.id && result.get("invite")?.id == currentUser.id)) {
  159. // console.log('上下级绑定成功');
  160. // currentUser.set('invite', {
  161. // __type: "Pointer",
  162. // className: "_User",
  163. // objectId: invite
  164. // })
  165. // currentUser.set('agent', {
  166. // __type: "Pointer",
  167. // className: "_User",
  168. // objectId: invite
  169. // })
  170. // await currentUser.save()
  171. // }
  172. // }
  173. // currentUser.get('mobile') ? wx.setStorageSync("userLogin", currentUser.id) : wx.removeStorageSync("userLogin");
  174. // resolve(currentUser)
  175. // return true
  176. // }).catch(err => {
  177. // wx.setStorageSync("sessionToken", null);
  178. // wx.showModal({
  179. // title: '提示',
  180. // content: '登录信息过期,请退出重新进入小程序',
  181. // showCancel: false,
  182. // cancelText: '取消',
  183. // cancelColor: '#000000',
  184. // confirmText: '确定',
  185. // confirmColor: '#3CC51F',
  186. // success: (result) => {
  187. // if(result.confirm){
  188. // wx.exitMiniProgram()
  189. // }
  190. // },
  191. // });
  192. // rej(err)
  193. // })
  194. // } else {
  195. // resolve(false)
  196. // }
  197. // })
  198. // },
  199. // async getParseToken(){
  200. // return new Promise((resolve, reject) => {
  201. // wx.login({
  202. // success: function (res) {
  203. // if (res.code) {
  204. // let url = 'https://server.fmode.cn/api/wxapp/login'
  205. // wx.request({
  206. // url: url,
  207. // data: {
  208. // companyId: getApp().globalData.company,
  209. // code: res.code,
  210. // appType: getApp().globalData.appType ? getApp().globalData.appType : ''
  211. // },
  212. // async success(res) {
  213. // console.log(res);
  214. // let data = res.data
  215. // if(data.code == 200){
  216. // wx.setStorageSync("sessionToken", data.data.token);
  217. // wx.setStorageSync("userInfo", data.data?.userInfo);
  218. // resolve(data.data.token)
  219. // }else{
  220. // resolve()
  221. // }
  222. // },
  223. // });
  224. // }
  225. // },
  226. // fail: function (err) {
  227. // console.warn('小程序wx.login失败');
  228. // resolve()
  229. // }
  230. // });
  231. // })
  232. // },
  233. //获取全局定位信息
  234. // async getLocationInfo(refrensh = false) {
  235. // let that = this;
  236. // let locationInfo = that.globalData.locationInfo;
  237. // if (locationInfo && !refrensh) {
  238. // return locationInfo;
  239. // } else {
  240. // let locationInfo = await util.getLocation();
  241. // that.globalData.locationInfo = locationInfo;
  242. // return locationInfo;
  243. // }
  244. // },
  245. onShow: function (options) {
  246. this.autoUpdate()
  247. let { referrerInfo } = options
  248. if(referrerInfo?.extraData?.status && referrerInfo.extraData.status == 'success'){
  249. getApp().globalData.merchant_trade_no = referrerInfo.extraData.req_extradata.merchant_trade_no
  250. }
  251. },
  252. autoUpdate() {
  253. let self = this
  254. if (wx.canIUse('getUpdateManager')) {
  255. const updateManager = wx.getUpdateManager()
  256. //1. 检查小程序是否有新版本发布
  257. updateManager.onCheckForUpdate(function (res) {
  258. // 请求完新版本信息的回调
  259. if (res.hasUpdate) {
  260. wx.showModal({
  261. title: '更新提示',
  262. content: '检测到新版本,是否下载新版本并重启小程序?',
  263. success: function (res) {
  264. if (res.confirm) {
  265. //2. 用户确定下载更新小程序,小程序下载及更新静默进行
  266. self.downLoadAndUpdate(updateManager)
  267. } else if (res.cancel) {
  268. //用户点击取消按钮的处理,强制更新
  269. wx.showModal({
  270. title: '温馨提示~',
  271. content: '本次版本更新涉及到新的功能添加,旧版本无法正常访问的哦~',
  272. showCancel: false,
  273. confirmText: "确定更新",
  274. success: function (res) {
  275. if (res.confirm) {
  276. //下载新版本,并重新应用
  277. self.downLoadAndUpdate(updateManager)
  278. }
  279. }
  280. })
  281. }
  282. }
  283. })
  284. }
  285. })
  286. } else {
  287. wx.showModal({
  288. title: '提示',
  289. content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'
  290. })
  291. }
  292. },
  293. /**
  294. * 下载小程序新版本并重启应用
  295. */
  296. downLoadAndUpdate(updateManager) {
  297. wx.showLoading();
  298. //静默下载更新小程序新版本
  299. updateManager.onUpdateReady(function () {
  300. wx.hideLoading()
  301. //新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  302. updateManager.applyUpdate()
  303. })
  304. updateManager.onUpdateFailed(function () {
  305. // 新的版本下载失败
  306. wx.showModal({
  307. title: '已经有新版本了哟~',
  308. content: '新版本已经上线啦~,请您删除当前小程序,重新搜索打开哟~',
  309. })
  310. })
  311. },
  312. onHide: function () {
  313. let pages = getCurrentPages()
  314. if (pages.length > 0) {
  315. let prevPage = pages[pages.length - 1]
  316. if (prevPage.route === 'nova-exam/pages/live-pusher/index') {
  317. prevPage.data.client.leave()
  318. prevPage.stop()
  319. prevPage.data.timer && clearInterval(prevPage.data.timer)
  320. wx.showModal({
  321. title: '提示',
  322. content: '考生将应用切换至后台,请重新进入',
  323. showCancel: false,
  324. success: (res) => {
  325. wx.navigateBack({
  326. delta: 1,
  327. })
  328. }
  329. });
  330. }
  331. }
  332. }
  333. })