1
This commit is contained in:
@@ -26,6 +26,10 @@ module.exports = {
|
||||
* type: string
|
||||
* description: 密码
|
||||
* example: 'password123'
|
||||
* device_id:
|
||||
* type: string
|
||||
* description: 设备ID(客户端生成,可选,如果提供则使用提供的,否则使用数据库中的)
|
||||
* example: 'device_1234567890abcdef'
|
||||
* responses:
|
||||
* 200:
|
||||
* description: 登录成功
|
||||
@@ -71,9 +75,8 @@ module.exports = {
|
||||
* example: '用户不存在或密码错误'
|
||||
*/
|
||||
"POST /user/login": async (ctx) => {
|
||||
const { phone, password } = ctx.getBody();
|
||||
const { phone, password, device_id: client_device_id } = ctx.getBody();
|
||||
const dayjs = require('dayjs');
|
||||
const crypto = require('crypto');
|
||||
|
||||
// 验证参数
|
||||
if (!phone || !password) {
|
||||
@@ -116,18 +119,21 @@ module.exports = {
|
||||
}
|
||||
}
|
||||
|
||||
// 生成设备ID(如果不存在,基于手机号和机器特征生成)
|
||||
let device_id = user.device_id;
|
||||
if (!device_id) {
|
||||
// 生成唯一设备ID
|
||||
const machineInfo = `${phone}_${Date.now()}_${Math.random()}`;
|
||||
device_id = crypto.createHash('sha256').update(machineInfo).digest('hex').substring(0, 32);
|
||||
|
||||
// 保存设备ID到账号表
|
||||
// 处理设备ID:优先使用客户端传递的 device_id,如果没有则使用数据库中的
|
||||
let device_id = client_device_id || user.device_id;
|
||||
|
||||
// 如果客户端提供了 device_id 且与数据库中的不同,则更新数据库
|
||||
if (client_device_id && client_device_id !== user.device_id) {
|
||||
await pla_account.update(
|
||||
{ device_id: device_id },
|
||||
{ device_id: client_device_id },
|
||||
{ where: { id: user.id } }
|
||||
);
|
||||
device_id = client_device_id;
|
||||
}
|
||||
|
||||
// 如果既没有客户端传递的,数据库中也为空,则返回错误(不应该发生,因为客户端会生成)
|
||||
if (!device_id) {
|
||||
return ctx.fail('设备ID不能为空,请重新登录');
|
||||
}
|
||||
|
||||
// 创建token
|
||||
|
||||
Reference in New Issue
Block a user