index.js 3.0 KB

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