const { post } = require("../../utils/api"); const { loginWithWeChat, saveSession } = require("../../utils/auth"); Page({ data: { loading: false, mode: "login", isLoginMode: true, isActivateMode: false, isBindCurrentMode: false, loginMethod: "wechat", isWechatLogin: true, isEmailLogin: false, bindingRequired: false, currentBindingRequired: false, bindToken: "", inviteCode: "", studentId: "", email: "", password: "" }, switchLoginMethod(event) { const method = event.currentTarget.dataset.method; this.setData({ loginMethod: method, isWechatLogin: method === "wechat", isEmailLogin: method === "email" }); }, async loginWithWechatTap() { this.setData({ loading: true }); try { const res = await loginWithWeChat(); if (!res.binding_required && res.token && res.user) { saveSession(res.token, res.user); wx.switchTab({ url: "/pages/home/index" }); return; } this.setData({ mode: "activate", isLoginMode: false, isActivateMode: true, isBindCurrentMode: false, bindingRequired: true, currentBindingRequired: false, bindToken: res.bind_token || "" }); } catch (error) { wx.showToast({ title: error.message || "微信登录失败", icon: "none" }); } finally { this.setData({ loading: false }); } }, onEmailInput(event) { this.setData({ email: event.detail.value.trim() }); }, onPasswordInput(event) { this.setData({ password: event.detail.value }); }, async loginWithEmail() { if (!this.data.email || !this.data.password) { wx.showToast({ title: "请输入邮箱和密码", icon: "none" }); return; } this.setData({ loading: true }); try { const res = await post("/api/auth/login", { email: this.data.email, password: this.data.password }); saveSession(res.token, res.user); if (!res.user.wechat_bound || !res.user.phone) { this.setData({ mode: "bind-current", isLoginMode: false, isActivateMode: false, isBindCurrentMode: true, currentBindingRequired: true, bindingRequired: false }); return; } wx.switchTab({ url: "/pages/home/index" }); } catch (error) { wx.showToast({ title: error.message || "邮箱登录失败", icon: "none" }); } finally { this.setData({ loading: false }); } }, onInviteCodeInput(event) { this.setData({ inviteCode: event.detail.value.trim().toUpperCase() }); }, onStudentIdInput(event) { this.setData({ studentId: event.detail.value.trim() }); }, async bindWithPhone(event) { const phoneCode = event.detail?.code; if (!phoneCode) { wx.showToast({ title: "需要授权手机号完成绑定", icon: "none" }); return; } if (!this.data.inviteCode || !this.data.studentId) { wx.showToast({ title: "请填写邀请码和学号", icon: "none" }); return; } this.setData({ loading: true }); try { const res = await post("/api/wechat/bind", { bind_token: this.data.bindToken, invite_code: this.data.inviteCode, student_id: this.data.studentId, phone_code: phoneCode }); saveSession(res.token, res.user); wx.switchTab({ url: "/pages/home/index" }); } catch (error) { wx.showToast({ title: error.message || "绑定失败", icon: "none" }); } finally { this.setData({ loading: false }); } }, bindCurrentWithPhone(event) { const phoneCode = event.detail?.code; if (!phoneCode) { wx.showToast({ title: "需要授权手机号完成绑定", icon: "none" }); return; } this.setData({ loading: true }); wx.login({ success: async ({ code }) => { try { const res = await post("/api/wechat/bind-current", { code, phone_code: phoneCode }); saveSession(res.token, res.user); wx.switchTab({ url: "/pages/home/index" }); } catch (error) { wx.showToast({ title: error.message || "绑定失败", icon: "none" }); } finally { this.setData({ loading: false }); } }, fail: () => { this.setData({ loading: false }); wx.showToast({ title: "微信登录失败", icon: "none" }); } }); }, skipCurrentBinding() { wx.switchTab({ url: "/pages/home/index" }); }, backToLogin() { this.setData({ mode: "login", isLoginMode: true, isActivateMode: false, isBindCurrentMode: false, loginMethod: "wechat", isWechatLogin: true, isEmailLogin: false, bindingRequired: false, currentBindingRequired: false, bindToken: "" }); } });