Files
account-manager/app/controller/SNAdmin.js

244 lines
9.7 KiB
JavaScript
Raw Normal View History

'use strict';
const Controller = require('../core/base_controller');
2026-06-05 17:38:10 +08:00
const fs = require('fs');
const fileName = "SNAdmin";
class SNAdminController extends Controller {
// 搜索可分配的SN号
async searchAllocationSN() {
const { ctx } = this;
try {
let rdmcode = await this.checkUid(ctx);
if (!rdmcode) {
this.setResponse(ctx, 401, 'token过期或错误', "none");
return;
}
const result = await ctx.service.sNAdmin.searchAllocationSN();
if (result == "error") {
this.setResponse(ctx, 500, '服务器错误', "none");
return;
}
if (result) {
const data = {
snArr: result
};
this.setResponse(ctx, 200, '搜索可分配的SN号成功', "deconstruction", data);
} else {
this.setResponse(ctx, 404, '搜索可分配的SN号失败', "none");
}
} catch (error) {
const blockName = fileName + "--" + "searchAllocationSN";
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
this.setResponse(ctx, 500, '服务器错误', "none");
}
}
// 搜索可分配的单位用户
async searchAllocationUser() {
const { ctx } = this;
try {
let rdmcode = await this.checkUid(ctx);
if (!rdmcode) {
this.setResponse(ctx, 401, 'token过期或错误', "none");
return;
}
2026-06-05 17:38:10 +08:00
const body = ctx.request.body;
const result = await ctx.service.sNAdmin.searchAllocationUser(body);
if (result == "error") {
this.setResponse(ctx, 500, '服务器错误', "none");
return;
}
if (result) {
const data = {
userArr: result
};
this.setResponse(ctx, 200, '搜索可分配的单位用户成功', "deconstruction", data);
} else {
this.setResponse(ctx, 404, '搜索可分配的单位用户失败', "none");
}
} catch (error) {
const blockName = fileName + "--" + "searchAllocationUser";
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
this.setResponse(ctx, 500, '服务器错误', "none");
}
}
2026-06-11 13:15:04 +08:00
// 修改可管理的SN号数量
async editSNCount() {
const { ctx } = this;
try {
let rdmcode = await this.checkUid(ctx);
if (!rdmcode) {
this.setResponse(ctx, 401, 'token过期或错误', "none");
return;
}
const uid = JSON.parse(rdmcode).uid;
const body = ctx.request.body;
const result = await ctx.service.sNAdmin.editSNCount(uid, body);
if (result == "error") {
this.setResponse(ctx, 500, '服务器错误', "none");
return;
}
if (result) {
this.setResponse(ctx, 200, '修改可管理的SN号数量成功', "none");
} else {
this.setResponse(ctx, 404, '修改可管理的SN号数量失败', "none");
}
} catch (error) {
const blockName = fileName + "--" + "editSNCount";
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
this.setResponse(ctx, 500, '服务器错误', "none");
}
}
// B端分配SN号
async allocationSN() {
const { ctx } = this;
try {
let rdmcode = await this.checkUid(ctx);
if (!rdmcode) {
this.setResponse(ctx, 401, 'token过期或错误', "none");
return;
}
const uid = JSON.parse(rdmcode).uid;
const body = ctx.request.body;
const result = await ctx.service.sNAdmin.allocationSN(uid, body);
if (result == "error") {
this.setResponse(ctx, 500, '服务器错误', "none");
return;
}
if (result) {
this.setResponse(ctx, 200, 'B端分配SN号成功', "none");
} else {
this.setResponse(ctx, 404, 'B端分配SN号失败', "none");
}
} catch (error) {
const blockName = fileName + "--" + "allocationSN";
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
this.setResponse(ctx, 500, '服务器错误', "none");
}
}
// 查看B端SN号分配记录
async showAllocationSNRecord() {
const { ctx } = this;
try {
let rdmcode = await this.checkUid(ctx);
if (!rdmcode) {
this.setResponse(ctx, 401, 'token过期或错误', "none");
return;
}
const body = ctx.request.body;
const result = await ctx.service.sNAdmin.showAllocationSNRecord(body);
if (result == "error") {
this.setResponse(ctx, 500, '服务器错误', "none");
return;
}
if (result) {
let SNAdminMsg = [];
result.rows.forEach(element => {
SNAdminMsg.push(element.dataValues);
});
const data = {
SNAdminMsg: SNAdminMsg,
count: result.count
};
this.setResponse(ctx, 200, '搜索SN号分配记录成功', "deconstruction", data);
} else {
this.setResponse(ctx, 404, '搜索SN号分配记录失败', "none");
}
} catch (error) {
const blockName = fileName + "--" + "showAllocationSNRecord";
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
this.setResponse(ctx, 500, '服务器错误', "none");
}
}
// 删除B端SN号分配记录
async deleteAllocationSNRecord() {
const { ctx } = this;
try {
let rdmcode = await this.checkUid(ctx);
if (!rdmcode) {
this.setResponse(ctx, 401, 'token过期或错误', "none");
return;
}
const uid = JSON.parse(rdmcode).uid;
const body = ctx.request.body;
const { model, password, requestMsg } = body
const modelType = this.decryptRequest(model);
const deCodePassword = this.decryptRequest(password);
const result = await ctx.service.sNAdmin.deleteAllocationSNRecord(uid, modelType, deCodePassword, requestMsg);
if (result == "error") {
this.setResponse(ctx, 500, '服务器错误', "none");
return;
}
if (result) {
this.setResponse(ctx, 200, '删除SN号分配记录成功', "none");
} else {
this.setResponse(ctx, 404, '删除SN号分配记录失败', "none");
}
} catch (error) {
const blockName = fileName + "--" + "deleteAllocationSNRecord";
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
this.setResponse(ctx, 500, '服务器错误', "none");
}
}
2026-06-05 17:38:10 +08:00
// 管理系统导出B端SN号分配记录
async downloadAllocationSNRecord() {
const { ctx } = this;
try {
let rdmcode = await this.checkUid(ctx);
if (!rdmcode) {
this.setResponse(ctx, 401, 'token过期或错误', "none");
return;
}
const uid = JSON.parse(rdmcode).uid;
const { form, searchMsg, dataHeaders } = ctx.request.body;
const result = await ctx.service.sNAdmin.downloadAllocationSNRecord(uid, searchMsg);
if (result == "error") {
this.setResponse(ctx, 500, '服务器错误', "none");
return;
}
if (result.code) {
const file = this.downloadData(form, result.data, dataHeaders);
if (file) {
const { filePath, completeName, buf } = file;
try {
fs.writeFileSync(filePath, Buffer.from(buf));
const data = {
// 本机服务器地址
// 'http://127.0.0.1:8012'
// 'http://192.168.31.28:8012'
// 公司服务器地址
// 'http://123.57.205.46:8012'
// 'http://manager.bdrd.hwasmart.com//accountManager'
fileUrl: 'http://127.0.0.1:8012' + `/public/${completeName}?time=${new Date().getTime()}`,
fileName: completeName,
}
this.setResponse(ctx, 200, '文件已成功写入', "deconstruction", data);
setTimeout(() => {
fs.unlinkSync(filePath);
}, 600000);
} catch (error) {
this.setResponse(ctx, 500, '写入文件时发生错误', "none");
}
} else {
this.setResponse(ctx, 404, '操作失败', "none");
}
} else {
this.setResponse(ctx, 404, '操作失败', "none");
}
} catch (error) {
const blockName = fileName + "--" + "downloadAllocationSNRecord";
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
this.setResponse(ctx, 500, '服务器错误', "none");
}
}
}
module.exports = SNAdminController;