236 lines
8.2 KiB
JavaScript
236 lines
8.2 KiB
JavaScript
const Controller = require('egg').Controller;
|
|
|
|
class AuthController extends Controller {
|
|
async bind() {
|
|
const { ctx, app } = this;
|
|
const { roomId, code, printer, warehouse, computerId } = ctx.request.body;
|
|
console.log('Received bind request with parameters:', { roomId, code, printer, warehouse, computerId });
|
|
if (!roomId || !code || !printer || !warehouse || !computerId) {
|
|
ctx.body = {
|
|
status: 'fail',
|
|
message: '缺少必要参数: roomId, code, printer, warehouse, computerId',
|
|
};
|
|
return;
|
|
}
|
|
|
|
try {
|
|
// 检查房间是否在线
|
|
const roomData = await app.redis.get(roomId);
|
|
if (!roomData) {
|
|
ctx.body = {
|
|
status: 'fail',
|
|
message: '房间不在线',
|
|
};
|
|
return;
|
|
}
|
|
|
|
// 生成 JWT 和刷新令牌
|
|
const jwtToken = app.jwt.sign({ code, printer, warehouse, computerId }, app.config.jwt.secret, {
|
|
expiresIn: '1h',
|
|
});
|
|
const refreshToken = app.jwt.sign({ code, printer, warehouse, computerId }, app.config.jwt.secret, {
|
|
expiresIn: '100years',
|
|
});
|
|
console.log('发送消息到room:', roomId, { jwtToken, refreshToken });
|
|
if (app.ws.getRooms) {
|
|
const rooms = await app.ws.getRooms();
|
|
console.log('所有活跃房间:', rooms);
|
|
}
|
|
|
|
const tokenExpireTime = Date.now() + 3600 * 1000; // 1 hour
|
|
const res = await app.ws.sendTo(roomId, JSON.stringify({ data: { jwtToken, refreshToken, tokenExpireTime }, method: 'bind.tokenInfo' }));
|
|
console.log('消息发送结果:', res);
|
|
|
|
// 判断消息是否发送成功
|
|
if (res !== 1) {
|
|
ctx.body = {
|
|
status: 'fail',
|
|
message: '房间不在线',
|
|
};
|
|
return;
|
|
}
|
|
|
|
// 下发令牌
|
|
ctx.body = {
|
|
message: '令牌生成成功',
|
|
jwtToken,
|
|
refreshToken,
|
|
status: 'success',
|
|
};
|
|
} catch (error) {
|
|
ctx.body = {
|
|
status: 'fail',
|
|
message: `绑定失败: ${error.message}`,
|
|
};
|
|
}
|
|
}
|
|
|
|
// 外部通知注销电脑(强制下线)
|
|
async logout() {
|
|
const { ctx, app } = this;
|
|
const { warehouse, computerId } = ctx.request.body;
|
|
|
|
if (!warehouse || !computerId) {
|
|
ctx.body = {
|
|
status: 'fail',
|
|
message: '缺少必要参数: warehouse, computerId',
|
|
};
|
|
return;
|
|
}
|
|
|
|
try {
|
|
// 1. 核实数据库:确认电脑存在
|
|
const computerInfo = await app.mysql.get('wms').get('wms_print_computer', {
|
|
warehouse_id: warehouse,
|
|
id: computerId,
|
|
});
|
|
|
|
if (!computerInfo) {
|
|
ctx.body = {
|
|
status: 'fail',
|
|
message: '未在数据库中找到该电脑信息',
|
|
};
|
|
return;
|
|
}
|
|
|
|
console.log(`注销电脑: warehouse=${warehouse}, computerId=${computerId}, deleted_at=${computerInfo.deleted_at}`);
|
|
|
|
// 2. 从 Redis 查找电脑是否在线
|
|
const computerInfoKey = `warehouse:${warehouse}:printer:${computerId}`;
|
|
const cachedInfo = await app.redis.get(computerInfoKey);
|
|
|
|
if (!cachedInfo) {
|
|
// 电脑不在线,但数据库验证已通过,返回成功(已离线无需操作)
|
|
ctx.body = {
|
|
status: 'success',
|
|
message: '电脑当前离线,无需注销',
|
|
};
|
|
return;
|
|
}
|
|
|
|
const { uniqueId } = JSON.parse(cachedInfo);
|
|
|
|
// 3. 向客户端发送解绑注销命令
|
|
const res = await app.ws.sendTo(uniqueId, JSON.stringify({
|
|
method: 'setting.unbind',
|
|
data: { reason: '管理员强制注销' },
|
|
}));
|
|
|
|
// 4. 清理 Redis 中的在线状态
|
|
await app.redis.del(computerInfoKey);
|
|
await app.redis.del(uniqueId);
|
|
console.log(`已清理 Redis: ${computerInfoKey}, ${uniqueId}`);
|
|
|
|
if (res !== 1) {
|
|
ctx.body = {
|
|
status: 'fail',
|
|
message: '发送注销命令失败,电脑可能已断开连接',
|
|
};
|
|
return;
|
|
}
|
|
|
|
ctx.body = {
|
|
status: 'success',
|
|
message: '注销命令已发送',
|
|
};
|
|
} catch (error) {
|
|
console.error('注销电脑时发生错误:', error);
|
|
ctx.body = {
|
|
status: 'fail',
|
|
message: `注销失败: ${error.message}`,
|
|
};
|
|
}
|
|
}
|
|
|
|
// 外部通知电脑信息变更(拉取最新数据并推送给客户端)
|
|
async refreshInfo() {
|
|
const { ctx, app } = this;
|
|
const { warehouse, computerId } = ctx.request.body;
|
|
|
|
if (!warehouse || !computerId) {
|
|
ctx.body = {
|
|
status: 'fail',
|
|
message: '缺少必要参数: warehouse, computerId',
|
|
};
|
|
return;
|
|
}
|
|
|
|
try {
|
|
// 1. 从数据库拉取最新电脑信息
|
|
const computerInfo = await app.mysql.get('wms').get('wms_print_computer', {
|
|
warehouse_id: warehouse,
|
|
id: computerId,
|
|
});
|
|
|
|
if (!computerInfo) {
|
|
ctx.body = {
|
|
status: 'fail',
|
|
message: '未在数据库中找到该电脑信息',
|
|
};
|
|
return;
|
|
}
|
|
|
|
console.log(`刷新电脑信息: warehouse=${warehouse}, computerId=${computerId}`);
|
|
|
|
// 2. 组装下发的 info 数据
|
|
const infoData = {
|
|
name: computerInfo.name || '',
|
|
status: computerInfo.status,
|
|
is_recording: computerInfo.is_recording || 0,
|
|
recording_size_value: computerInfo.recording_size_value || 0,
|
|
recording_size_unit: computerInfo.recording_size_unit || '',
|
|
remote_play_url: computerInfo.remote_play_url || '',
|
|
recording_directory: computerInfo.recording_directory || '',
|
|
};
|
|
|
|
// 3. 查找电脑是否在线
|
|
const computerInfoKey = `warehouse:${warehouse}:printer:${computerId}`;
|
|
const cachedInfo = await app.redis.get(computerInfoKey);
|
|
|
|
if (!cachedInfo) {
|
|
// 电脑不在线,无需推送
|
|
ctx.body = {
|
|
status: 'success',
|
|
message: '电脑当前离线,信息已保存,上线后将自动获取最新信息',
|
|
};
|
|
return;
|
|
}
|
|
|
|
const cached = JSON.parse(cachedInfo);
|
|
const { uniqueId } = cached;
|
|
|
|
// 4. 合并完整数据并更新 Redis 缓存
|
|
const updatedData = { ...cached, ...infoData };
|
|
await app.redis.set(computerInfoKey, JSON.stringify(updatedData), 'EX', 300);
|
|
|
|
// 5. 向客户端推送最新信息
|
|
const res = await app.ws.sendTo(uniqueId, JSON.stringify({
|
|
method: 'setting.setInfo',
|
|
data: { ...cached, ...infoData },
|
|
message: '电脑信息已更新',
|
|
}));
|
|
|
|
if (res !== 1) {
|
|
ctx.body = {
|
|
status: 'fail',
|
|
message: '推送信息失败,电脑可能已断开连接',
|
|
};
|
|
return;
|
|
}
|
|
|
|
ctx.body = {
|
|
status: 'success',
|
|
message: '信息已推送',
|
|
data: infoData,
|
|
};
|
|
} catch (error) {
|
|
console.error('刷新电脑信息时发生错误:', error);
|
|
ctx.body = {
|
|
status: 'fail',
|
|
message: `刷新失败: ${error.message}`,
|
|
};
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = AuthController; |