app.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // app.js
  2. App({
  3. globalData: {
  4. isLoggedIn: false, // 登录状态
  5. isLoggedIns: false, // 登录状态
  6. token: null,
  7. },
  8. // 检查登录状态并弹出提示框
  9. checkLoginStatus() {
  10. const isLoggedIn = wx.getStorageSync('userInfo') || false;
  11. if (!isLoggedIn) {
  12. wx.showModal({
  13. title: '登录身份选择',
  14. content: '请选择您的身份以完成登录',
  15. confirmText: '我是家长',
  16. cancelText: '我是老师',
  17. success: (res) => {
  18. if (res.confirm) {
  19. // 家长:跳转本小程序登录页
  20. wx.navigateTo({
  21. url: '/pages/logins/logins',
  22. });
  23. } else if (res.cancel) {
  24. // 老师:跳转到老师端小程序
  25. wx.navigateToMiniProgram({
  26. appId: 'wx06f2b1b09ac5684f', // ⚠️ 替换成实际 appId
  27. path: 'pages/logins/logins',
  28. });
  29. }
  30. }
  31. });
  32. return false;
  33. }
  34. return true;
  35. },
  36. async login() {
  37. try {
  38. const res = await wx.cloud.callFunction({
  39. name: 'login',
  40. data: {
  41. username: 'admin',
  42. password: 'Admin1234_'
  43. }
  44. })
  45. if (res.result.code === 0) {
  46. const token = res.result.token
  47. this.globalData.isLoggedIns = true
  48. this.globalData.token = token
  49. wx.setStorageSync('token', token)
  50. return token
  51. } else {
  52. wx.showToast({
  53. title: res.result.msg,
  54. icon: 'none'
  55. })
  56. }
  57. } catch (err) {
  58. console.error(err)
  59. wx.showToast({
  60. title: '登录失败',
  61. icon: 'none'
  62. })
  63. }
  64. },
  65. onLaunch() {
  66. // 可以尝试读取本地缓存 云函数登录
  67. const token = wx.getStorageSync('token')
  68. if (token) {
  69. this.globalData.isLoggedIns = true
  70. this.globalData.token = token
  71. }
  72. // wx.cloud.init({
  73. // env: 'cloud1-6g98iw7i28b01747',
  74. // traceUser: true
  75. // })
  76. // 微信支付 初始化
  77. // wx.cloud.init({
  78. // // env 参数决定接下来小程序发起的云开发调用(wx.cloud.xxx)会默认请求到哪个云环境的资源
  79. // env: 'honghgaier-5guiffgcf17a2eea',
  80. // // 是否在将用户访问记录到用户管理中,在控制台中可见,默认为false
  81. // traceUser: false,
  82. // });
  83. }
  84. })