index.js 6.5 KB

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