index.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. console.log(url);
  104. wx.redirectTo({
  105. url: url,
  106. });
  107. }
  108. catch (err) {
  109. console.log(err);
  110. /* 登录身份信息到期,重新登陆 */
  111. if((err?.message.indexOf('Session token is expired') != -1 || err?.message.indexOf('Invalid session token') != -1) && !force){
  112. let invite = wx.getStorageSync('invite')
  113. wx.clearStorageSync()
  114. invite && wx.setStorageSync('invite', invite)
  115. /* 强制重新登录 */
  116. this.review(true)
  117. return
  118. }
  119. this.setData({
  120. loading:false
  121. })
  122. wx.showModal({
  123. title: '温馨提示',
  124. content: '服务器正在升级,请稍后重试。',
  125. showCancel: false,
  126. cancelText: '取消',
  127. cancelColor: '#000000',
  128. confirmText: '确定',
  129. confirmColor: '#3CC51F',
  130. success: (result) => {
  131. if (result.confirm) {
  132. wx.exitMiniProgram()
  133. }
  134. },
  135. });
  136. }
  137. },
  138. async updateUser(id) {
  139. let User = new Parse.Query('_User')
  140. let user = await User.get(id)
  141. let invite = wx.getStorageSync('invite')
  142. //查询邀请人user
  143. let query = new Parse.Query("_User")
  144. query.equalTo('objectId', invite)
  145. let result = await query.first()
  146. if (result && result.id && result.get("invite")?.id == user.id) {
  147. console.error('邀请人不能是自己的下级')
  148. return
  149. }
  150. if (invite && !user.get('invite') && user.id != invite && !user.get('agentLevel')) {
  151. console.log('上下级绑定成功');
  152. user.set('invite', {
  153. __type: "Pointer",
  154. className: "_User",
  155. objectId: invite
  156. })
  157. user.set('agent', {
  158. __type: "Pointer",
  159. className: "_User",
  160. objectId: invite
  161. })
  162. await Parse.Cloud.run('user_save', {
  163. userJson: user.toJSON()
  164. })
  165. }
  166. },
  167. async getCompanyServerExpire(url) {
  168. let query = new Parse.Query('Company')
  169. query.equalTo('objectId', getApp().globalData.company)
  170. query.select('expireDate', 'expireMap')
  171. let com = await query.first()
  172. if (com?.id && com?.get('expireDate')) {
  173. let now = + new Date()
  174. let expireTime = + new Date(com?.get('expireDate'))
  175. if (com?.get('expireMap') && com.get('expireMap')[getApp().globalData.appid]) {
  176. expireTime = + new Date(com.get('expireMap')[getApp().globalData.appid])
  177. }
  178. if (now >= expireTime) {
  179. console.log('服务器到期');
  180. wx.reLaunch({
  181. url: `common-page/pages/loading/index?url=${url}`,
  182. });
  183. return
  184. }
  185. }
  186. return true
  187. },
  188. onUnload: function () {
  189. wx.setStorageSync("active", 0);
  190. },
  191. getParaName(url) {
  192. if (!url || url.indexOf('?') == -1) {
  193. return
  194. }
  195. let paraname = url.split('?')[1]
  196. return this.setObject(paraname) //封装成对象
  197. },
  198. setObject(paraArr) {
  199. let obj = {}
  200. let arr1 = paraArr.split('&')
  201. arr1.forEach(item => {
  202. let str = item.split('=')
  203. let key = str[0]
  204. let val = str[1]
  205. obj[key] = val
  206. })
  207. return obj
  208. }
  209. });