2026-07-25 01:24:08 +08:00

54 lines
2.4 KiB
JavaScript

const BaseService = require('./base');
class AuthService extends BaseService {
async getInfo(token) {
console.log('AuthService.getInfo called with token:', token);
const { app } = this;
try {
// 验证 JWT Token
const decoded = app.jwt.verify(token, app.config.jwt.secret);
const { computerId, warehouse } = decoded;
const uniqueId = this.ctx.uniqueId;
const data = decoded;
data.uniqueId = uniqueId;
this.ctx.print_computer_info = data;
const key = `warehouse:${warehouse}:printer:${computerId}`;
const computerInfo = await this.app.mysql.get("wms").get('wms_print_computer', {
warehouse_id: warehouse,
id: computerId
});
if(computerInfo.status==0) {
//更新数据库为激活状态
await this.app.mysql.get("wms").update('wms_print_computer', { status: 1 }, { where: { warehouse_id: warehouse, id: computerId } });
}
console.log('查询打印机信息:', computerInfo);
this.app.redis.del(key); // 删除旧的打印机信息
this.app.redis.set(key, JSON.stringify(data), 'EX', 300); // 设置过期时间为 5分钟
return this.successResponse('setting.setInfo', data, 'Token 验证成功');
} catch (error) {
console.log(error);
return this.errorResponse('setting.refreshToken', error.message || 'Token 验证失败');
}
}
async refreshToken(data) {
const { token } = data;
console.log('AuthService.refreshToken called with oldToken:', token);
const { app } = this;
try {
// 验证旧 Token
const decoded = app.jwt.verify(token, app.config.jwt.secret);
const { code, printer, warehouse, computerId } = decoded;
const jwtToken = app.jwt.sign({ code, printer, warehouse, computerId }, app.config.jwt.secret, {
expiresIn: '1h',
});
return this.successResponse('setting.setToken', { token: jwtToken, tokenExpireTime: Date.now() + 3600 * 1000 }, 'Token 刷新成功');
} catch (error) {
console.log('Token 刷新失败:', error);
return this.errorResponse('setting.refreshToken', error.message || 'Token 刷新失败');
}
}
}
module.exports = AuthService;