index.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. // /pages/school/index
  22. // /pagesSchool/common/lessoninfo
  23. // /pagesSchool/schAdmin/course/coachlist
  24. // /pagesSchool/stuAdmin/money/list
  25. // /pagesSchool/stuAdmin/course/list
  26. },
  27. fail: async res => {
  28. wx.login({
  29. success: async (arr) => {
  30. if (app.globalData?.wxInfo?.openid) { await this.toLogin(app.globalData?.wxInfo?.openid); return; }
  31. const { code: js_code } = arr;
  32. const aee = await app.$get('/wechat/api/login/app', { js_code: js_code, config: 'newCourtApp' });
  33. if (aee.errcode === 0 && aee.data?.openid) {
  34. const openid = aee.data.openid;
  35. app.globalData.wxInfo = { openid };
  36. await this.toLogin(openid);
  37. } else {
  38. wx.showToast({ title: `${aee.errmsg}`, icon: 'fail', duration: 2000 });
  39. wx.redirectTo({ url: '/pages/register/index' });
  40. }
  41. },
  42. });
  43. }
  44. })
  45. },
  46. async toLogin(openid) {
  47. const res = await app.$post('/user/wxAppLogin', { openid });
  48. if (app.$checkRes(res)) {
  49. const { data } = res;
  50. // 没有用户,去注册
  51. if (!data) wx.redirectTo({ url: '/pages/register/index' });
  52. // 有用户,存起来,跳转
  53. else {
  54. wx.setStorageSync('user', data);
  55. wx.redirectTo({ url: '/pages/school/index' });
  56. }
  57. }
  58. },
  59. /**
  60. * 生命周期函数--监听页面初次渲染完成
  61. */
  62. onReady: function () {
  63. },
  64. /**
  65. * 生命周期函数--监听页面显示
  66. */
  67. onShow: function () {
  68. const that = this;
  69. // 监听用户是否登录
  70. that.watchLogin();
  71. },
  72. /**
  73. * 生命周期函数--监听页面隐藏
  74. */
  75. onHide: function () {
  76. },
  77. /**
  78. * 生命周期函数--监听页面卸载
  79. */
  80. onUnload: function () {
  81. },
  82. /**
  83. * 页面相关事件处理函数--监听用户下拉动作
  84. */
  85. onPullDownRefresh: function () {
  86. },
  87. /**
  88. * 页面上拉触底事件的处理函数
  89. */
  90. onReachBottom: function () {
  91. },
  92. /**
  93. * 用户点击右上角分享
  94. */
  95. onShareAppMessage: function () {
  96. }
  97. })