234 lines
8.5 KiB
JavaScript
234 lines
8.5 KiB
JavaScript
'use strict';
|
|
|
|
const Controller = require('../core/base_controller');
|
|
|
|
const fs = require('fs');
|
|
|
|
const fileName = "Organization";
|
|
|
|
class OrganizationController extends Controller {
|
|
// 注册B端用户单位
|
|
async addOrganization() {
|
|
const { ctx } = this;
|
|
try {
|
|
let rdmcode = await this.checkUid(ctx);
|
|
if (!rdmcode) {
|
|
this.setResponse(ctx, 401, 'token过期或错误', "none");
|
|
return;
|
|
}
|
|
const requestMsg = ctx.request.body.requestMsg;
|
|
requestMsg.forEach(element => {
|
|
element.uid = this.generateNumberString('O');
|
|
element.organization_name = this.decryptRequest(element.organization_name);
|
|
element.address = element.address == "" ? "" : this.decryptRequest(element.address);
|
|
element.liaison_name = element.liaison_name == "" ? "" : this.decryptRequest(element.liaison_name);
|
|
element.phone = element.phone == "" ? "" : this.decryptRequest(element.phone);
|
|
element.email = element.email == "" ? "" : this.decryptRequest(element.email);
|
|
element.other_information = element.other_information == "" ? "" : this.decryptRequest(element.other_information);
|
|
element.post_code = element.post_code == "" ? "" : this.decryptRequest(element.post_code);
|
|
element.created_at = new Date();
|
|
element.updated_at = new Date();
|
|
});
|
|
const result = await ctx.service.organization.addOrganization(requestMsg);
|
|
if (result == "error") {
|
|
this.setResponse(ctx, 500, '服务器错误', "none");
|
|
return;
|
|
}
|
|
if (result) {
|
|
this.setResponse(ctx, 200, '新增B端用户单位成功', "none");
|
|
} else {
|
|
this.setResponse(ctx, 404, '新增B端用户单位失败', "none");
|
|
}
|
|
} catch (error) {
|
|
const blockName = fileName + "--" + "addOrganization";
|
|
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
|
|
this.setResponse(ctx, 500, '服务器错误', "none");
|
|
}
|
|
}
|
|
|
|
// 查看B端用户单位
|
|
async showOrganization() {
|
|
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.organization.showOrganization(body);
|
|
if (result == "error") {
|
|
this.setResponse(ctx, 500, '服务器错误', "none");
|
|
return;
|
|
}
|
|
if (result) {
|
|
let OrganizationMsg = [];
|
|
result.rows.forEach(element => {
|
|
OrganizationMsg.push(element.dataValues);
|
|
});
|
|
const data = {
|
|
OrganizationMsg: OrganizationMsg,
|
|
count: result.count
|
|
};
|
|
this.setResponse(ctx, 200, '搜索B端用户单位成功', "deconstruction", data);
|
|
} else {
|
|
this.setResponse(ctx, 404, '搜索B端用户单位失败', "none");
|
|
}
|
|
} catch (error) {
|
|
const blockName = fileName + "--" + "showOrganization";
|
|
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
|
|
this.setResponse(ctx, 500, '服务器错误', "none");
|
|
}
|
|
}
|
|
|
|
// 远程搜索B端用户单位
|
|
async SearchOrganization() {
|
|
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.organization.SearchOrganization(body);
|
|
if (result == "error") {
|
|
this.setResponse(ctx, 500, '服务器错误', "none");
|
|
return;
|
|
}
|
|
if (result) {
|
|
let organization = [];
|
|
result.forEach(element => {
|
|
organization.push(element.dataValues);
|
|
});
|
|
const data = {
|
|
organization: organization,
|
|
};
|
|
this.setResponse(ctx, 200, '搜索B端用户信息成功', "deconstruction", data);
|
|
} else {
|
|
this.setResponse(ctx, 404, '搜索B端用户信息失败', "none");
|
|
}
|
|
} catch (error) {
|
|
const blockName = fileName + "--" + "showBSide";
|
|
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
|
|
this.setResponse(ctx, 500, '服务器错误', "none");
|
|
}
|
|
}
|
|
|
|
// 编辑B端用户单位
|
|
async editOrganization() {
|
|
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 } = body
|
|
const modelType = this.decryptRequest(model);
|
|
const result = await ctx.service.organization.editOrganization(uid, modelType, updateMsg, requestMsg);
|
|
if (result == "error") {
|
|
this.setResponse(ctx, 500, '服务器错误', "none");
|
|
return;
|
|
}
|
|
if (result) {
|
|
this.setResponse(ctx, 200, '编辑B端用户单位成功', "none");
|
|
} else {
|
|
this.setResponse(ctx, 404, '编辑B端用户单位失败', "none");
|
|
}
|
|
} catch (error) {
|
|
const blockName = fileName + "--" + "editOrganization";
|
|
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
|
|
this.setResponse(ctx, 500, '服务器错误', "none");
|
|
}
|
|
}
|
|
|
|
// 删除B端用户单位
|
|
async deleteOrganization() {
|
|
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.organization.deleteOrganization(uid, modelType, deCodePassword, requestMsg);
|
|
if (result == "error") {
|
|
this.setResponse(ctx, 500, '服务器错误', "none");
|
|
return;
|
|
}
|
|
if (result) {
|
|
this.setResponse(ctx, 200, '删除B端用户单位成功', "none");
|
|
} else {
|
|
this.setResponse(ctx, 404, '删除B端用户单位失败', "none");
|
|
}
|
|
} catch (error) {
|
|
const blockName = fileName + "--" + "deleteOrganization";
|
|
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
|
|
this.setResponse(ctx, 500, '服务器错误', "none");
|
|
}
|
|
}
|
|
|
|
// 管理系统导出B端用户单位
|
|
async downloadOrganization() {
|
|
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.organization.downloadOrganization(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 + "--" + "downloadOrganization";
|
|
ctx.app.middleware.log('../../controllerLog', blockName + ">>>>" + error);
|
|
this.setResponse(ctx, 500, '服务器错误', "none");
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = OrganizationController; |