118 lines
4.8 KiB
JavaScript
118 lines
4.8 KiB
JavaScript
const Controller = require('egg').Controller;
|
||
|
||
'use strict';
|
||
|
||
|
||
class AuthController extends Controller {
|
||
async printerSetting() {
|
||
const { ctx } = this;
|
||
const redis = this.app.redis;
|
||
if (ctx.method === 'GET') {
|
||
const code = ctx.params.code || '';
|
||
if (!code) {
|
||
ctx.body = { error: '缺少code参数' };
|
||
return;
|
||
}
|
||
const bindValue = await redis.get('printer').get(`bind:${code}`);
|
||
if (!bindValue) {
|
||
ctx.body = { error: '无效code或未绑定' };
|
||
return;
|
||
}
|
||
await redis.get('printer').setex(`binding:${code}`, 300, bindValue);
|
||
await ctx.render('printer_setting.html', {
|
||
title: '打印台设置',
|
||
code,
|
||
bindValue,
|
||
});
|
||
} else if (ctx.method === 'POST') {
|
||
const { code, printer, warehouse } = ctx.request.body;
|
||
if (!code || !printer || !warehouse) {
|
||
ctx.body = { success: false, error: '参数缺失' };
|
||
return;
|
||
}
|
||
// 获取临时binding:code
|
||
const bindingValue = await redis.get('printer').get(`binding:${code}`);
|
||
if (!bindingValue) {
|
||
ctx.body = { success: false, error: '超时或未绑定' };
|
||
return;
|
||
}
|
||
// 反序列化bindingValue
|
||
let bindingValueObj = bindingValue;
|
||
try {
|
||
bindingValueObj = JSON.parse(bindingValue);
|
||
} catch (e) {
|
||
bindingValueObj = bindingValue;
|
||
}
|
||
// 使用 Store model 校验仓库id
|
||
const Store = ctx.model.Store;
|
||
const warehouseInfo = await Store.findOne({ where: { id: warehouse } });
|
||
console.log('warehouseInfo:', warehouseInfo);
|
||
if (!warehouseInfo || warehouseInfo.project !== 'warehouse') {
|
||
ctx.body = { success: false, error: '仓库id校验失败' };
|
||
return;
|
||
}
|
||
// 查找仓库id下是否有重名的电脑
|
||
const PrintComputer = ctx.model.PrintComputer;
|
||
let computerId = null;
|
||
const existComputer = await PrintComputer.findOne({ where: { warehouse_id: warehouse, name: printer } });
|
||
if (existComputer) {
|
||
if (existComputer.status === 1) {
|
||
ctx.body = { success: false, error: '该仓库下打印台名称已存在且可用' };
|
||
return;
|
||
} else {
|
||
computerId = existComputer.id;
|
||
}
|
||
} else {
|
||
// 创建新打印电脑
|
||
console.log('创建新打印电脑:', { warehouse_id: warehouse, name: printer, status: 0 });
|
||
const newComputer = await PrintComputer.create({ warehouse_id: warehouse, name: printer, status: 0 });
|
||
console.log('newComputer:', newComputer);
|
||
computerId = newComputer.id;
|
||
}
|
||
// 调用打印服务 /auth/bind,使用egg内置curl
|
||
const printServiceUrl = ctx.app.config.printService.url;
|
||
let printServiceResult = null;
|
||
try {
|
||
const res = await ctx.curl(`${printServiceUrl}/auth/bind`, {
|
||
method: 'POST',
|
||
contentType: 'json',
|
||
data: { code, printer, warehouse, roomId: bindingValueObj.uniqueId, computerId },
|
||
dataType: 'json',
|
||
});
|
||
printServiceResult = res.data;
|
||
} catch (err) {
|
||
console.log('调用打印服务失败:', err);
|
||
printServiceResult = { success: false, error: '打印服务通讯失败' };
|
||
}
|
||
console.log('printServiceResult:', printServiceResult);
|
||
// 判断打印服务返回结果
|
||
if (printServiceResult && printServiceResult.status === 'fail') {
|
||
ctx.body = {
|
||
success: false,
|
||
msg: printServiceResult.message || '打印服务失败',
|
||
code,
|
||
printer,
|
||
warehouse,
|
||
bindingValue: bindingValueObj,
|
||
printServiceResult
|
||
};
|
||
return;
|
||
}
|
||
console.log('printServiceResult:', printServiceResult);
|
||
ctx.body = {
|
||
success: true,
|
||
msg: '参数已接收',
|
||
code,
|
||
printer,
|
||
warehouse,
|
||
bindingValue: bindingValueObj,
|
||
computerId,
|
||
printServiceResult
|
||
};
|
||
} else {
|
||
ctx.body = { msg: '不支持的请求方法' };
|
||
}
|
||
}
|
||
}
|
||
|
||
module.exports = AuthController; |