index.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. // var Parse = getApp().Parse;
  2. // var app = getApp()
  3. // const { wxLogin } = require('./utils/login')
  4. const CONFIG = require("config.js");
  5. let config = {
  6. appid: CONFIG.default.appid,
  7. company: CONFIG.default.company,
  8. rootPage: CONFIG.default.rootPage,
  9. }
  10. const plugin = requirePlugin('fm-plugin')
  11. const { Parse, checkAuth } = plugin
  12. Page({
  13. /**
  14. * 页面的初始数据
  15. */
  16. data: {
  17. splashUrl: wx.getStorageSync("enabledOptions")[0],
  18. loading:true
  19. },
  20. /**
  21. * 生命周期函数--监听页面加载
  22. */
  23. onLoad: async function (options) {
  24. wx.login({
  25. success: function (res) {
  26. if (res.code) {
  27. console.log(res);
  28. // wx.request({
  29. // url: "https://server.fmode.cn/api/wxapp/auth_wxapp",
  30. // data: {
  31. // c: getApp().globalData.company,
  32. // code: res.code,
  33. // },
  34. // async success(res) {
  35. // wx.setStorageSync("userInfo", res.data);
  36. // resolve(res)
  37. // },
  38. // });
  39. }
  40. },
  41. fail: function (err) {
  42. wx.showToast({
  43. title: '服务器繁忙,请稍后重试',
  44. })
  45. }
  46. });
  47. wx.setStorageSync("invite", null);
  48. let str = decodeURIComponent(options.q); //扫描二维码获得的跳转链接
  49. let obj = this.getParaName(str)
  50. if (obj && obj.invite) {
  51. wx.setStorageSync("invite", obj.invite);
  52. }
  53. obj && Object.keys(obj).forEach(key=> options[key] = obj[key])
  54. // 兼容从编译参数或页面直达传入的 storeId
  55. if (options && options.scene && !options.storeId) {
  56. try {
  57. const sceneStr = decodeURIComponent(options.scene)
  58. if (sceneStr) {
  59. const pairs = sceneStr.split('&')
  60. for (const p of pairs) {
  61. const [k, v] = p.split('=')
  62. if (k === 'storeId' && v) {
  63. options.storeId = v
  64. break
  65. }
  66. }
  67. }
  68. } catch (e) {
  69. console.warn('解析 scene 失败:', e)
  70. }
  71. }
  72. this.setData({
  73. options: options
  74. })
  75. let {
  76. time,
  77. dramaId,
  78. roomId,
  79. orderId,
  80. shopId,
  81. invite,
  82. activityId,
  83. company,
  84. inviteHost,
  85. storeId
  86. } = options
  87. time && wx.setStorageSync("time", time);
  88. dramaId && wx.setStorageSync("dramaId", dramaId);
  89. roomId && wx.setStorageSync("roomId", roomId);
  90. orderId && wx.setStorageSync("orderId", orderId);
  91. shopId && wx.setStorageSync("shopId", shopId);
  92. invite && wx.setStorageSync("invite", invite);
  93. activityId && wx.setStorageSync("activityId", activityId);
  94. inviteHost && wx.setStorageSync("inviteHost", true);
  95. if (storeId) {
  96. wx.setStorageSync('storeId', storeId)
  97. getApp().globalData.storeId = storeId
  98. console.log('✅ 入口页已设置店铺 ID:', storeId)
  99. }
  100. if (company) getApp().globalData.toCompany = true;
  101. plugin.init(config, wx.getStorageSync('invite'))
  102. },
  103. /**
  104. * 生命周期函数--监听页面初次渲染完成
  105. */
  106. onReady: async function () { },
  107. /**
  108. * 生命周期函数--监听页面显示
  109. */
  110. onShow: async function () {
  111. await this.review()
  112. },
  113. async review(force){
  114. try {
  115. let options = this.data.options
  116. let url = getApp().globalData.rootPage || getApp().globalData.defaultTabBar.list[0].pagePath
  117. if (options) {
  118. let objArr = Object.keys(options)
  119. if (objArr && objArr.length > 0) {
  120. let parms = '?'
  121. objArr.forEach((o, index) => {
  122. if (index > 0) {
  123. parms += '&' + o + '=' + options[o]
  124. } else {
  125. parms += o + '=' + options[o]
  126. }
  127. })
  128. url += parms
  129. }
  130. }
  131. let currentUser = Parse.User.current()
  132. console.log(Parse.User.current())
  133. if (!currentUser || force) {
  134. // let data = await wxLogin()
  135. // if (data.statusCode == 200) {
  136. let r = await checkAuth(true)
  137. console.log(r);
  138. // getApp().Parse = Parse
  139. // getApp().checkAuth = checkAuth
  140. if(!r) return
  141. // }
  142. } else {
  143. this.updateUser(currentUser.id)
  144. }
  145. getApp().Parse = Parse
  146. getApp().checkAuth = checkAuth
  147. if (!await this.getCompanyServerExpire(url)) {
  148. return
  149. }
  150. wx.redirectTo({
  151. url: url,
  152. });
  153. }
  154. catch (err) {
  155. console.log(err);
  156. /* 登录身份信息到期,重新登陆 */
  157. if((err?.message.indexOf('Session token is expired') != -1 || err?.message.indexOf('Invalid session token') != -1) && !force){
  158. let invite = wx.getStorageSync('invite')
  159. wx.clearStorageSync()
  160. invite && wx.setStorageSync('invite', invite)
  161. /* 强制重新登录 */
  162. this.review(true)
  163. return
  164. }
  165. this.setData({
  166. loading:false
  167. })
  168. wx.showModal({
  169. title: '温馨提示',
  170. content: '服务器正在升级,请稍后重试。',
  171. showCancel: false,
  172. cancelText: '取消',
  173. cancelColor: '#000000',
  174. confirmText: '确定',
  175. confirmColor: '#3CC51F',
  176. success: (result) => {
  177. if (result.confirm) {
  178. wx.exitMiniProgram()
  179. }
  180. },
  181. });
  182. }
  183. },
  184. async updateUser(id) {
  185. let User = new Parse.Query('_User')
  186. let user = await User.get(id)
  187. let invite = wx.getStorageSync('invite')
  188. //查询邀请人user
  189. let query = new Parse.Query("_User")
  190. query.equalTo('objectId', invite)
  191. let result = await query.first()
  192. if (result && result.id && result.get("invite")?.id == user.id) {
  193. console.error('邀请人不能是自己的下级')
  194. return
  195. }
  196. if (invite && !user.get('invite') && user.id != invite && !user.get('agentLevel')) {
  197. console.log('上下级绑定成功');
  198. user.set('invite', {
  199. __type: "Pointer",
  200. className: "_User",
  201. objectId: invite
  202. })
  203. user.set('agent', {
  204. __type: "Pointer",
  205. className: "_User",
  206. objectId: invite
  207. })
  208. await Parse.Cloud.run('user_save', {
  209. userJson: user.toJSON()
  210. })
  211. }
  212. },
  213. async getCompanyServerExpire(url) {
  214. let query = new Parse.Query('Company')
  215. query.equalTo('objectId', getApp().globalData.company)
  216. query.select('expireDate', 'expireMap')
  217. let com = await query.first()
  218. if (com?.id && com?.get('expireDate')) {
  219. let now = + new Date()
  220. let expireTime = + new Date(com?.get('expireDate'))
  221. if (com?.get('expireMap') && com.get('expireMap')[getApp().globalData.appid]) {
  222. expireTime = + new Date(com.get('expireMap')[getApp().globalData.appid])
  223. }
  224. if (now >= expireTime) {
  225. console.log('服务器到期');
  226. wx.reLaunch({
  227. url: `common-page/pages/loading/index?url=${url}`,
  228. });
  229. return
  230. }
  231. }
  232. return true
  233. },
  234. onUnload: function () {
  235. wx.setStorageSync("active", 0);
  236. },
  237. getParaName(url) {
  238. if (!url || url.indexOf('?') == -1) {
  239. return
  240. }
  241. let paraname = url.split('?')[1]
  242. return this.setObject(paraname) //封装成对象
  243. },
  244. setObject(paraArr) {
  245. let obj = {}
  246. let arr1 = paraArr.split('&')
  247. arr1.forEach(item => {
  248. let str = item.split('=')
  249. let key = str[0]
  250. let val = str[1]
  251. obj[key] = val
  252. })
  253. return obj
  254. }
  255. });