261 lines
9.3 KiB
JavaScript
261 lines
9.3 KiB
JavaScript
'use strict';
|
|
|
|
const Controller = require('../core/base_controller');
|
|
|
|
const fs = require('fs');
|
|
|
|
const fileName = "Business";
|
|
|
|
class BusinessController extends Controller {
|
|
// 注册公司业务
|
|
async addBusiness() {
|
|
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 requestMsg = ctx.request.body.requestMsg;
|
|
requestMsg.forEach(element => {
|
|
const salt = this.generateRandomString();
|
|
element.salt = this.encryptRequest(salt);
|
|
element.login_url = this.decryptRequest(element.login_url);
|
|
element.client_url = this.decryptRequest(element.client_url);
|
|
element.temporary_id = this.decryptRequest(element.temporary_id) == 'true' ? this.shaForm(this.generateRandomString(), salt) : this.decryptRequest(element.temporary_id);
|
|
element.client_id = this.generateClientId();
|
|
element.client_secret = this.shaForm(this.generateRandomString(), salt);
|
|
element.name = this.decryptRequest(element.name);
|
|
element.type = element.type ? this.decryptRequest(element.type) : 2;
|
|
element.level = element.level ? this.decryptRequest(element.level) : 0;
|
|
element.status = element.status ? this.decryptRequest(element.status) : 0;
|
|
element.created_at = new Date();
|
|
element.updated_at = new Date();
|
|
});
|
|
const result = await ctx.service.business.addBusiness(uid, requestMsg);
|
|
if (result == "error") {
|
|
this.setResponse(ctx, 500, '服务器错误', "none");
|
|
return;
|
|
}
|
|
if (result) {
|
|
this.setResponse(ctx, 200, '新增公司业务成功', "none");
|
|
} else {
|
|
this.setResponse(ctx, 404, '新增公司业务失败', "none");
|
|
}
|
|
} catch (error) {
|
|
const blockName = fileName + "--" + "addBusiness";
|
|
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
|
|
this.setResponse(ctx, 500, '服务器错误', "none");
|
|
}
|
|
}
|
|
|
|
// 查看公司业务
|
|
async showBusiness() {
|
|
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.business.showBusiness(body);
|
|
if (result == "error") {
|
|
this.setResponse(ctx, 500, '服务器错误', "none");
|
|
return;
|
|
}
|
|
if (result) {
|
|
let BusinessMsg = [];
|
|
result.rows.forEach(element => {
|
|
BusinessMsg.push(element.dataValues);
|
|
});
|
|
const data = {
|
|
BusinessMsg: BusinessMsg,
|
|
count: result.count
|
|
};
|
|
this.setResponse(ctx, 200, '搜索公司业务成功', "deconstruction", data);
|
|
} else {
|
|
this.setResponse(ctx, 404, '搜索公司业务失败', "none");
|
|
}
|
|
} catch (error) {
|
|
const blockName = fileName + "--" + "showBusiness";
|
|
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
|
|
this.setResponse(ctx, 500, '服务器错误', "none");
|
|
}
|
|
}
|
|
|
|
// 远程搜索公司业务
|
|
async SearchBusiness() {
|
|
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.business.SearchBusiness(body);
|
|
if (result == "error") {
|
|
this.setResponse(ctx, 500, '服务器错误', "none");
|
|
return;
|
|
}
|
|
if (result) {
|
|
let business = [];
|
|
result.forEach(element => {
|
|
business.push(element.dataValues);
|
|
});
|
|
const data = {
|
|
business: business,
|
|
};
|
|
this.setResponse(ctx, 200, '搜索公司业务信息成功', "deconstruction", data);
|
|
} else {
|
|
this.setResponse(ctx, 404, '搜索公司业务信息失败', "none");
|
|
}
|
|
} catch (error) {
|
|
const blockName = fileName + "--" + "showBSide";
|
|
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
|
|
this.setResponse(ctx, 500, '服务器错误', "none");
|
|
}
|
|
}
|
|
|
|
// 编辑公司业务
|
|
async editBusiness() {
|
|
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, updateMsg, requestMsg, password } = body
|
|
const modelType = this.decryptRequest(model);
|
|
const deCodePassword = this.decryptRequest(password);
|
|
let salt;
|
|
if (updateMsg.salt) {
|
|
salt = this.generateRandomString();
|
|
updateMsg.salt = this.encryptRequest(salt);
|
|
} else {
|
|
delete updateMsg.salt;
|
|
}
|
|
if (!updateMsg.temporary_id) {
|
|
delete updateMsg.temporary_id;
|
|
} else if (updateMsg.temporary_id == 'true') {
|
|
updateMsg.temporary_id = this.shaForm(this.generateRandomString(), salt);
|
|
}
|
|
if (updateMsg.client_id) {
|
|
updateMsg.client_id = this.generateClientId();
|
|
} else {
|
|
delete updateMsg.client_id;
|
|
}
|
|
if (updateMsg.client_secret) {
|
|
updateMsg.client_secret = this.shaForm(this.generateRandomString(), salt);
|
|
} else {
|
|
delete updateMsg.client_secret;
|
|
}
|
|
const result = await ctx.service.business.editBusiness(uid, modelType, deCodePassword, updateMsg, requestMsg);
|
|
if (result == "error") {
|
|
this.setResponse(ctx, 500, '服务器错误', "none");
|
|
return;
|
|
}
|
|
if (result) {
|
|
this.setResponse(ctx, 200, '编辑公司业务成功', "none");
|
|
} else {
|
|
this.setResponse(ctx, 404, '编辑公司业务失败', "none");
|
|
}
|
|
} catch (error) {
|
|
const blockName = fileName + "--" + "editBusiness";
|
|
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
|
|
this.setResponse(ctx, 500, '服务器错误', "none");
|
|
}
|
|
}
|
|
|
|
// 删除公司业务
|
|
async deleteBusiness() {
|
|
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.business.deleteBusiness(uid, modelType, deCodePassword, requestMsg);
|
|
if (result == "error") {
|
|
this.setResponse(ctx, 500, '服务器错误', "none");
|
|
return;
|
|
}
|
|
if (result) {
|
|
this.setResponse(ctx, 200, '删除公司业务成功', "none");
|
|
} else {
|
|
this.setResponse(ctx, 404, '删除公司业务失败', "none");
|
|
}
|
|
} catch (error) {
|
|
const blockName = fileName + "--" + "deleteBusiness";
|
|
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
|
|
this.setResponse(ctx, 500, '服务器错误', "none");
|
|
}
|
|
}
|
|
|
|
// 管理系统导出公司业务
|
|
async downloadBusiness() {
|
|
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.business.downloadBusiness(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 + "--" + "downloadBusiness";
|
|
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
|
|
this.setResponse(ctx, 500, '服务器错误', "none");
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = BusinessController; |