- 新增 /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>
72 lines
2.0 KiB
JavaScript
72 lines
2.0 KiB
JavaScript
/**
|
||
* 账号密码登录页面(备用)
|
||
* 职责:提供账号密码登录,后端自动识别角色,根据 role_code 跳转对应首页。
|
||
* 作为微信手机号登录的备用入口。
|
||
*/
|
||
var auth = require("../../../utils/auth");
|
||
|
||
Page({
|
||
data: {
|
||
username: "",
|
||
password: "",
|
||
loading: false,
|
||
message: "",
|
||
},
|
||
|
||
handleInput: function (event) {
|
||
var field = event.currentTarget.dataset.field;
|
||
this.setData({ [field]: event.detail.value });
|
||
},
|
||
|
||
handleLogin: async function () {
|
||
var app = getApp();
|
||
this.setData({ loading: true, message: "" });
|
||
try {
|
||
var data = await app.request({
|
||
url: "/api/auth/login",
|
||
method: "POST",
|
||
data: {
|
||
username: this.data.username,
|
||
password: this.data.password,
|
||
},
|
||
});
|
||
var role = data.role_code || "driver";
|
||
app.globalData.authToken = data.token;
|
||
app.globalData.userRole = role;
|
||
auth.setToken(data.token);
|
||
auth.setUser(data);
|
||
|
||
// 密码登录成功后自动绑定 open_id(为微信消息通知做准备)
|
||
try {
|
||
var loginCode = await new Promise(function (resolve, reject) {
|
||
wx.login({ success: resolve, fail: reject });
|
||
});
|
||
await app.request({
|
||
url: "/api/auth/bind-openid-by-code",
|
||
method: "POST",
|
||
data: { code: loginCode.code },
|
||
});
|
||
} catch (bindErr) {
|
||
// 绑定失败不影响登录
|
||
console.log("open_id 绑定跳过:", bindErr.message);
|
||
}
|
||
|
||
wx.showToast({
|
||
title: "欢迎," + (data.real_name || data.username),
|
||
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 });
|
||
}
|
||
},
|
||
|
||
goBack: function () {
|
||
wx.navigateBack();
|
||
},
|
||
});
|