// 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 // }) // 微信支付 初始化 // wx.cloud.init({ // // env 参数决定接下来小程序发起的云开发调用(wx.cloud.xxx)会默认请求到哪个云环境的资源 // env: 'honghgaier-5guiffgcf17a2eea', // // 是否在将用户访问记录到用户管理中,在控制台中可见,默认为false // traceUser: false, // }); } })