335 lines
8.0 KiB
JavaScript
335 lines
8.0 KiB
JavaScript
'use strict';
|
|
|
|
const Service = require('egg').Service;
|
|
const crypto = require('node:crypto');
|
|
|
|
class PrintApiService extends Service {
|
|
getPrinterRedis() {
|
|
return this.app.redis.get('printer');
|
|
}
|
|
|
|
normalizeBindValue(bindValue) {
|
|
let bindValueObj = bindValue;
|
|
try {
|
|
bindValueObj = JSON.parse(bindValue);
|
|
} catch (_) {
|
|
bindValueObj = bindValue;
|
|
}
|
|
return bindValueObj;
|
|
}
|
|
|
|
async getEmployeePermissions(employeeId) {
|
|
const { ctx } = this;
|
|
const roleRows = await ctx.model.EmployeeRole.findAll({
|
|
attributes: [ 'role_id' ],
|
|
where: {
|
|
employee_id: employeeId,
|
|
},
|
|
include: [
|
|
{
|
|
model: ctx.model.Role,
|
|
as: 'role',
|
|
attributes: [],
|
|
where: {
|
|
status: 1,
|
|
},
|
|
required: true,
|
|
},
|
|
],
|
|
raw: true,
|
|
});
|
|
|
|
if (!roleRows || roleRows.length === 0) {
|
|
return [];
|
|
}
|
|
|
|
const roleIds = [ ...new Set(roleRows.map(item => item.role_id).filter(Boolean)) ];
|
|
if (roleIds.length === 0) {
|
|
return [];
|
|
}
|
|
|
|
const permissionIds = await ctx.service.systems.auth.rolePermission.getPermissionIdsForRoles(roleIds, {
|
|
includeDisabled: false,
|
|
});
|
|
if (permissionIds.length === 0) {
|
|
return [];
|
|
}
|
|
|
|
const permissions = await ctx.model.Permission.findAll({
|
|
attributes: [ 'path' ],
|
|
where: {
|
|
id: {
|
|
[ctx.app.Sequelize.Op.in]: permissionIds,
|
|
},
|
|
status: 1,
|
|
type: 'api',
|
|
},
|
|
raw: true,
|
|
});
|
|
|
|
return permissions.map(item => item.path).filter(Boolean);
|
|
}
|
|
|
|
async getBindInfo(data = {}) {
|
|
const { ctx } = this;
|
|
const accountId = Number(data.account_id || data.accountId || data.wms_account_id || data.wmsAccountId);
|
|
const code = String(data.code || '').trim();
|
|
|
|
if (!Number.isFinite(accountId) || accountId <= 0 || !code) {
|
|
return {
|
|
success: false,
|
|
code: 0,
|
|
data: null,
|
|
message: '缺少有效参数 account_id 或 code',
|
|
};
|
|
}
|
|
|
|
const account = await ctx.model.Account.findOne({
|
|
where: { id: accountId },
|
|
attributes: [ 'id', 'status' ],
|
|
raw: true,
|
|
});
|
|
if (!account || Number(account.status) !== 1) {
|
|
return {
|
|
success: false,
|
|
code: 0,
|
|
data: null,
|
|
message: '账号不存在或已禁用',
|
|
};
|
|
}
|
|
|
|
const redis = this.getPrinterRedis();
|
|
const bindValue = await redis.get(`bind:${code}`);
|
|
if (!bindValue) {
|
|
return {
|
|
success: false,
|
|
code: 0,
|
|
data: null,
|
|
message: '无效code或未绑定',
|
|
};
|
|
}
|
|
|
|
const employeeRows = await ctx.model.AccountStoreEmployee.findAll({
|
|
where: {
|
|
account_id: accountId,
|
|
status: 1,
|
|
},
|
|
include: [
|
|
{
|
|
model: ctx.model.Store,
|
|
as: 'store',
|
|
attributes: [ 'id', 'company_name', 'project', 'status', 'auth_end' ],
|
|
where: {
|
|
status: 1,
|
|
project: 'warehouse',
|
|
},
|
|
required: true,
|
|
},
|
|
],
|
|
order: [[ 'created_at', 'DESC' ]],
|
|
});
|
|
|
|
const now = new Date();
|
|
const warehouseMap = new Map();
|
|
for (const employee of employeeRows) {
|
|
const permissions = await this.getEmployeePermissions(employee.id);
|
|
if (!permissions.includes('printer.bind')) {
|
|
continue;
|
|
}
|
|
|
|
const store = employee.store;
|
|
if (!store) {
|
|
continue;
|
|
}
|
|
|
|
if (store.auth_end && new Date(store.auth_end) < now) {
|
|
continue;
|
|
}
|
|
|
|
warehouseMap.set(String(store.id), {
|
|
id: Number(store.id),
|
|
name: store.company_name,
|
|
});
|
|
}
|
|
|
|
const warehouses = Array.from(warehouseMap.values());
|
|
if (warehouses.length === 0) {
|
|
return {
|
|
success: true,
|
|
code: 200,
|
|
data: {
|
|
warehouses: [],
|
|
},
|
|
message: '无可绑定仓库',
|
|
};
|
|
}
|
|
|
|
const tempCode = crypto.randomBytes(16).toString('hex').toUpperCase();
|
|
const tokenPayload = {
|
|
accountId,
|
|
code,
|
|
bindValue,
|
|
createdAt: new Date().toISOString(),
|
|
warehouses: warehouses.map(item => item.id),
|
|
};
|
|
await redis.setex(`binding_token:${tempCode}`, 300, JSON.stringify(tokenPayload));
|
|
|
|
return {
|
|
success: true,
|
|
code: 200,
|
|
data: {
|
|
warehouses,
|
|
tempCode,
|
|
},
|
|
message: '获取绑定信息成功',
|
|
};
|
|
}
|
|
|
|
async bindByTempCode(data = {}) {
|
|
const { ctx } = this;
|
|
const tempCode = String(data.temp_code || data.tempCode || '').trim();
|
|
const printer = String(data.printer || data.workbench_name || data.workbenchName || '').trim();
|
|
const warehouse = Number(data.warehouse || data.warehouse_id || data.warehouseId);
|
|
|
|
if (!tempCode || !printer || !Number.isFinite(warehouse) || warehouse <= 0) {
|
|
return {
|
|
success: false,
|
|
code: 0,
|
|
data: null,
|
|
message: '参数缺失',
|
|
};
|
|
}
|
|
|
|
const redis = this.getPrinterRedis();
|
|
const tokenText = await redis.get(`binding_token:${tempCode}`);
|
|
if (!tokenText) {
|
|
return {
|
|
success: false,
|
|
code: 0,
|
|
data: null,
|
|
message: '临时code无效或已过期',
|
|
};
|
|
}
|
|
|
|
let tokenData = null;
|
|
try {
|
|
tokenData = JSON.parse(tokenText);
|
|
} catch (_) {
|
|
tokenData = null;
|
|
}
|
|
|
|
if (!tokenData || !tokenData.code || !Array.isArray(tokenData.warehouses)) {
|
|
return {
|
|
success: false,
|
|
code: 0,
|
|
data: null,
|
|
message: '临时code数据异常',
|
|
};
|
|
}
|
|
|
|
if (!tokenData.warehouses.includes(warehouse)) {
|
|
return {
|
|
success: false,
|
|
code: 0,
|
|
data: null,
|
|
message: '仓库无绑定权限',
|
|
};
|
|
}
|
|
|
|
const Store = ctx.model.Store;
|
|
const warehouseInfo = await Store.findOne({
|
|
where: {
|
|
id: warehouse,
|
|
status: 1,
|
|
},
|
|
raw: true,
|
|
});
|
|
if (!warehouseInfo || warehouseInfo.project !== 'warehouse') {
|
|
return {
|
|
success: false,
|
|
code: 0,
|
|
data: null,
|
|
message: '仓库id校验失败',
|
|
};
|
|
}
|
|
|
|
const PrintComputer = ctx.model.PrintComputer;
|
|
let computerId = null;
|
|
const existComputer = await PrintComputer.findOne({
|
|
where: {
|
|
warehouse_id: warehouse,
|
|
name: printer,
|
|
},
|
|
});
|
|
if (existComputer) {
|
|
if (Number(existComputer.status) === 1) {
|
|
return {
|
|
success: false,
|
|
code: 0,
|
|
data: null,
|
|
message: '该仓库下打印台名称已存在且可用',
|
|
};
|
|
}
|
|
computerId = existComputer.id;
|
|
} else {
|
|
const newComputer = await PrintComputer.create({
|
|
warehouse_id: warehouse,
|
|
name: printer,
|
|
status: 0,
|
|
});
|
|
computerId = newComputer.id;
|
|
}
|
|
|
|
const bindValueObj = this.normalizeBindValue(tokenData.bindValue);
|
|
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: tokenData.code,
|
|
printer,
|
|
warehouse,
|
|
roomId: bindValueObj && bindValueObj.uniqueId ? bindValueObj.uniqueId : '',
|
|
computerId,
|
|
},
|
|
dataType: 'json',
|
|
});
|
|
printServiceResult = res.data;
|
|
} catch (err) {
|
|
printServiceResult = { success: false, error: '打印服务通讯失败' };
|
|
}
|
|
|
|
if (printServiceResult && printServiceResult.status === 'fail') {
|
|
return {
|
|
success: false,
|
|
code: 0,
|
|
data: {
|
|
printer,
|
|
warehouse,
|
|
printServiceResult,
|
|
},
|
|
message: printServiceResult.message || '打印服务失败',
|
|
};
|
|
}
|
|
|
|
await redis.del(`binding_token:${tempCode}`);
|
|
|
|
return {
|
|
success: true,
|
|
code: 200,
|
|
data: {
|
|
code: tokenData.code,
|
|
printer,
|
|
warehouse,
|
|
computerId,
|
|
printServiceResult,
|
|
},
|
|
message: '绑定成功',
|
|
};
|
|
}
|
|
}
|
|
|
|
module.exports = PrintApiService;
|