// app.js App({ globalData: { isLoggedIn: false, // 登录状态 isLoggedIns: false, // 登录状态 token: null }, // 检查登录状态并弹出提示框 checkLoginStatus() { const isLoggedIn = wx.getStorageSync('userInfo') || false; if (!isLoggedIn) { wx.showModal({ title: '登录身份选择', content: '请选择您的身份以完成登录', confirmText: '我是老师', cancelText: '我是家长', success: (res) => { if (res.confirm) { // 家长:跳转本小程序登录页 wx.navigateTo({ url: '/pages/logins/logins', }); } else if (res.cancel) { // 老师:跳转到老师端小程序 wx.navigateToMiniProgram({ appId: 'wx06f2b1b09ac5684f', // ⚠️ 替换成实际 appId path: 'pages/logins/logins', }); } } }); return false; } return true; }, async login() { try { const res = await wx.cloud.callFunction({ name: 'login', data: { username: 'admin', password: 'Admin1234_' } }) if (res.result.code === 0) { const token = res.result.token this.globalData.isLoggedIns = true this.globalData.token = token wx.setStorageSync('token', token) return token } else { wx.showToast({ title: res.result.msg, icon: 'none' }) } } catch (err) { console.error(err) wx.showToast({ title: '登录失败', icon: 'none' }) } }, onLaunch() { // 可以尝试读取本地缓存 const token = wx.getStorageSync('token') if (token) { this.globalData.isLoggedIns = true this.globalData.token = token } // wx.cloud.init({ // env: 'cloud1-6g98iw7i28b01747', // traceUser: true // }) } })