index.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. const app = getApp()
  2. Page({
  3. data: {
  4. frameStyle: { useTop: true, name: '系统', leftArrow: false, useBar: false },
  5. },
  6. toCommon: function (e) {
  7. const { route } = e.currentTarget.dataset;
  8. if (route) wx.navigateTo({ url: `/pages/${route}` });
  9. },
  10. /**
  11. * 生命周期函数--监听页面加载
  12. */
  13. onLoad: function (options) { },
  14. // 监听用户是否登录
  15. watchLogin: async function () {
  16. // 监听用户是否登录,
  17. wx.getStorage({
  18. key: 'user',
  19. success: async res => {
  20. if (res.data) wx.redirectTo({ url: '/pages/school/index' })
  21. },
  22. fail: async res => {
  23. wx.login({
  24. success: async (arr) => {
  25. if (app.globalData?.wxInfo?.openid) { await this.toLogin(app.globalData?.wxInfo?.openid); return; }
  26. const { code: js_code } = arr;
  27. const aee = await app.$get('/wechat/api/login/app', { js_code: js_code, config: 'newCourtApp' });
  28. if (aee.errcode === 0 && aee.data?.openid) {
  29. const openid = aee.data.openid;
  30. app.globalData.wxInfo = { openid };
  31. await this.toLogin(openid);
  32. } else {
  33. wx.showToast({ title: `${aee.errmsg}`, icon: 'fail', duration: 2000 });
  34. wx.redirectTo({ url: '/pages/register/index' });
  35. }
  36. },
  37. });
  38. }
  39. })
  40. },
  41. async toLogin(openid) {
  42. const res = await app.$post('/newCourt/api/user/wxAppLogin', { openid });
  43. if (app.$checkRes(res)) {
  44. const { data } = res;
  45. // 没有用户,去注册
  46. if (!data) wx.redirectTo({ url: '/pages/register/index' });
  47. // 有用户,存起来,跳转
  48. else { wx.setStorageSync('user', data); wx.redirectTo({ url: '/pages/school/index' }); }
  49. }
  50. },
  51. /**
  52. * 生命周期函数--监听页面初次渲染完成
  53. */
  54. onReady: function () {
  55. },
  56. /**
  57. * 生命周期函数--监听页面显示
  58. */
  59. onShow: function () {
  60. const that = this;
  61. // 监听用户是否登录
  62. that.watchLogin();
  63. },
  64. /**
  65. * 生命周期函数--监听页面隐藏
  66. */
  67. onHide: function () {
  68. },
  69. /**
  70. * 生命周期函数--监听页面卸载
  71. */
  72. onUnload: function () {
  73. },
  74. /**
  75. * 页面相关事件处理函数--监听用户下拉动作
  76. */
  77. onPullDownRefresh: function () {
  78. },
  79. /**
  80. * 页面上拉触底事件的处理函数
  81. */
  82. onReachBottom: function () {
  83. },
  84. /**
  85. * 用户点击右上角分享
  86. */
  87. onShareAppMessage: function () {
  88. }
  89. })