- 新增 /api/auth/bind-openid-by-code 接口,用 wx.login code 绑定 open_id - 密码登录成功后自动调用绑定接口,无需用户手动操作 - 修复 auth.py 文档注释中的参数描述 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
58 lines
1.7 KiB
JavaScript
58 lines
1.7 KiB
JavaScript
/**
|
|
* 统一登录页面(微信手机号一键登录)
|
|
* 职责:微信手机号授权登录,自动匹配系统用户。保留账号密码登录作为备用。
|
|
*/
|
|
var auth = require("../../utils/auth");
|
|
|
|
Page({
|
|
data: {
|
|
loading: false,
|
|
message: "",
|
|
},
|
|
|
|
handleGetPhoneNumber: async function (e) {
|
|
console.log("[DEBUG] getPhoneNumber event:", JSON.stringify(e.detail));
|
|
if (e.detail.errMsg !== "getPhoneNumber:ok") {
|
|
this.setData({ message: "授权失败:" + (e.detail.errMsg || JSON.stringify(e.detail)) });
|
|
return;
|
|
}
|
|
this.setData({ loading: true, message: "" });
|
|
try {
|
|
var app = getApp();
|
|
// wx.login 拿 code
|
|
var loginRes = await new Promise(function (resolve, reject) {
|
|
wx.login({ success: resolve, fail: reject });
|
|
});
|
|
// 调用后端微信登录接口
|
|
var data = await app.request({
|
|
url: "/api/auth/wechat-login",
|
|
method: "POST",
|
|
data: {
|
|
code: loginRes.code,
|
|
phone_code: e.detail.code,
|
|
},
|
|
});
|
|
// 保存登录态
|
|
var role = data.role_code || "driver";
|
|
app.globalData.authToken = data.token;
|
|
app.globalData.userRole = role;
|
|
auth.setToken(data.token);
|
|
auth.setUser(data);
|
|
|
|
wx.showToast({ title: "欢迎," + data.real_name, icon: "success" });
|
|
var entry = role === "driver"
|
|
? "/pages/driver/task-list/task-list"
|
|
: "/pages/manager/dashboard/dashboard";
|
|
wx.switchTab({ url: entry });
|
|
} catch (error) {
|
|
this.setData({ message: error.message || "登录失败" });
|
|
} finally {
|
|
this.setData({ loading: false });
|
|
}
|
|
},
|
|
|
|
goPasswordLogin: function () {
|
|
wx.navigateTo({ url: "/pages/login/login-password/login-password" });
|
|
},
|
|
});
|