index.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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.setStorageSync("invite", null);
  25. let str = decodeURIComponent(options.q); //扫描二维码获得的跳转链接
  26. let obj = this.getParaName(str)
  27. if (obj && obj.invite) {
  28. wx.setStorageSync("invite", obj.invite);
  29. }
  30. obj && Object.keys(obj).forEach(key=> options[key] = obj[key])
  31. this.setData({
  32. options: options
  33. })
  34. let {
  35. time,
  36. dramaId,
  37. roomId,
  38. orderId,
  39. shopId,
  40. invite,
  41. activityId,
  42. company,
  43. inviteHost
  44. } = options
  45. time && wx.setStorageSync("time", time);
  46. dramaId && wx.setStorageSync("dramaId", dramaId);
  47. roomId && wx.setStorageSync("roomId", roomId);
  48. orderId && wx.setStorageSync("orderId", orderId);
  49. shopId && wx.setStorageSync("shopId", shopId);
  50. invite && wx.setStorageSync("invite", invite);
  51. activityId && wx.setStorageSync("activityId", activityId);
  52. inviteHost && wx.setStorageSync("inviteHost", true);
  53. if (company) getApp().globalData.toCompany = true;
  54. plugin.init(config, wx.getStorageSync('invite'))
  55. },
  56. /**
  57. * 生命周期函数--监听页面初次渲染完成
  58. */
  59. onReady: async function () { },
  60. /**
  61. * 生命周期函数--监听页面显示
  62. */
  63. onShow: async function () {
  64. await this.review()
  65. },
  66. async review(force){
  67. try {
  68. let options = this.data.options
  69. let url = getApp().globalData.rootPage || getApp().globalData.defaultTabBar.list[0].pagePath
  70. if (options) {
  71. let objArr = Object.keys(options)
  72. if (objArr && objArr.length > 0) {
  73. let parms = '?'
  74. objArr.forEach((o, index) => {
  75. if (index > 0) {
  76. parms += '&' + o + '=' + options[o]
  77. } else {
  78. parms += o + '=' + options[o]
  79. }
  80. })
  81. url += parms
  82. }
  83. }
  84. let currentUser = Parse.User.current()
  85. console.log(Parse.User.current())
  86. if (!currentUser || force) {
  87. // let data = await wxLogin()
  88. // if (data.statusCode == 200) {
  89. let r = await checkAuth(true)
  90. console.log(r);
  91. // getApp().Parse = Parse
  92. // getApp().checkAuth = checkAuth
  93. if(!r) return
  94. // }
  95. } else {
  96. this.updateUser(currentUser.id)
  97. }
  98. getApp().Parse = Parse
  99. getApp().checkAuth = checkAuth
  100. if (!await this.getCompanyServerExpire(url)) {
  101. return
  102. }
  103. wx.redirectTo({
  104. url: url,
  105. });
  106. }
  107. catch (err) {
  108. console.log(err);
  109. /* 登录身份信息到期,重新登陆 */
  110. if((err?.message.indexOf('Session token is expired') != -1 || err?.message.indexOf('Invalid session token') != -1) && !force){
  111. let invite = wx.getStorageSync('invite')
  112. wx.clearStorageSync()
  113. invite && wx.setStorageSync('invite', invite)
  114. /* 强制重新登录 */
  115. this.review(true)
  116. return
  117. }
  118. this.setData({
  119. loading:false
  120. })
  121. wx.showModal({
  122. title: '温馨提示',
  123. content: '服务器正在升级,请稍后重试。',
  124. showCancel: false,
  125. cancelText: '取消',
  126. cancelColor: '#000000',
  127. confirmText: '确定',
  128. confirmColor: '#3CC51F',
  129. success: (result) => {
  130. if (result.confirm) {
  131. wx.exitMiniProgram()
  132. }
  133. },
  134. });
  135. }
  136. },
  137. async updateUser(id) {
  138. let User = new Parse.Query('_User')
  139. let user = await User.get(id)
  140. let invite = wx.getStorageSync('invite')
  141. //查询邀请人user
  142. let query = new Parse.Query("_User")
  143. query.equalTo('objectId', invite)
  144. let result = await query.first()
  145. if (result && result.id && result.get("invite")?.id == user.id) {
  146. console.error('邀请人不能是自己的下级')
  147. return
  148. }
  149. if (invite && !user.get('invite') && user.id != invite && !user.get('agentLevel')) {
  150. console.log('上下级绑定成功');
  151. user.set('invite', {
  152. __type: "Pointer",
  153. className: "_User",
  154. objectId: invite
  155. })
  156. user.set('agent', {
  157. __type: "Pointer",
  158. className: "_User",
  159. objectId: invite
  160. })
  161. await Parse.Cloud.run('user_save', {
  162. userJson: user.toJSON()
  163. })
  164. }
  165. },
  166. async getCompanyServerExpire(url) {
  167. let query = new Parse.Query('Company')
  168. query.equalTo('objectId', getApp().globalData.company)
  169. query.select('expireDate', 'expireMap')
  170. let com = await query.first()
  171. if (com?.id && com?.get('expireDate')) {
  172. let now = + new Date()
  173. let expireTime = + new Date(com?.get('expireDate'))
  174. if (com?.get('expireMap') && com.get('expireMap')[getApp().globalData.appid]) {
  175. expireTime = + new Date(com.get('expireMap')[getApp().globalData.appid])
  176. }
  177. // if (now >= expireTime) {
  178. // console.log('服务器到期');
  179. // wx.reLaunch({
  180. // url: `common-page/pages/loading/index?url=${url}`,
  181. // });
  182. // return
  183. // }
  184. }
  185. return true
  186. },
  187. onUnload: function () {
  188. wx.setStorageSync("active", 0);
  189. },
  190. getParaName(url) {
  191. if (!url || url.indexOf('?') == -1) {
  192. return
  193. }
  194. let paraname = url.split('?')[1]
  195. return this.setObject(paraname) //封装成对象
  196. },
  197. setObject(paraArr) {
  198. let obj = {}
  199. let arr1 = paraArr.split('&')
  200. arr1.forEach(item => {
  201. let str = item.split('=')
  202. let key = str[0]
  203. let val = str[1]
  204. obj[key] = val
  205. })
  206. return obj
  207. }
  208. });