import BaseController from "../baseController";
|
import { Post } from "egg-shell-decorators";
|
import { TMSWayBill } from "../../entity/express/tms/tmsWayBill";
|
import moment = require("moment");
|
import { In } from "typeorm";
|
import { TMSIdCard } from "../../entity/express/tms/tmsIdCard";
|
import * as XLSX from "xlsx";
|
import * as path from "path";
|
import * as fs from "fs";
|
// import * as unzip from "unzip";
|
import { aliOSSUpload } from "../../public/aliyunOssHelper";
|
|
/**
|
* 身份证管理
|
*/
|
export default class IdCardController extends BaseController {
|
//#region 删除身份证信息
|
@Post()
|
public async deleteIdCard() {
|
try {
|
//修改运单信息
|
await this.dbWrite.update(
|
TMSWayBill,
|
{
|
consigneeIdcard: In(this.body.idCardCodes),
|
orderStatus: In(["新建", "已提交", "审核成功", "已处理", "处理完成", "组板异常", "取消组板"])
|
},
|
{
|
consigneeIdcard: null
|
}
|
);
|
|
//修改成功之后删除身份证信息
|
await this.dbWrite.delete(TMSIdCard, {
|
idCard_Id: In(this.body.ids)
|
});
|
|
this.info.result = true;
|
this.info.msg = "删除成功!";
|
|
this.info.result = true;
|
this.info.msg = "删除成功!";
|
} catch (ex) {
|
this.info.result = false;
|
this.info.msg = "错误消息:" + ex.message;
|
}
|
this.ctx.body = this.info;
|
}
|
//#endregion
|
|
//#region addIdCard 上传身份证
|
@Post()
|
public async addIdCard() {
|
try {
|
await this.ctx.service.tms.idCard.addIdCard();
|
} catch (ex) {
|
this.info.result = false;
|
this.info.msg = "错误信息:" + ex.message;
|
}
|
this.ctx.body = this.info;
|
}
|
//#endregion
|
|
//#region isIdCardinformation 更新正面照片弹框
|
@Post()
|
public async isIdCardinformation() {
|
try {
|
let cardInfo = await this.dbRead.findOne(TMSIdCard, {
|
idCardCode: this.body.idCardCode
|
});
|
if (cardInfo != null) {
|
this.info.result = true;
|
} else {
|
this.info.result = false;
|
this.info.msg = "请先保存用户信息,然后再上传省份证!";
|
}
|
} catch (ex) {
|
this.info.result = false;
|
this.info.msg = "错误信息:" + ex.message;
|
}
|
this.ctx.body = this.info;
|
}
|
//#endregion
|
|
//#region UpdateIdCard 阿里修改替换身份证图片
|
@Post()
|
public async updateIdCard() {
|
let domain = "http://auod-bucket-wms.oss-ap-southeast-2.aliyuncs.com";
|
try {
|
let cardInfo = await this.dbRead.findOne(TMSIdCard, { idCardCode: this.body.idCardCode });
|
if (cardInfo != null) {
|
if (this.body.positiveServerUrl) {
|
cardInfo.positiveServerUrl = this.body.positiveServerUrl.replace(domain, "");
|
cardInfo.uploadTime = new Date();
|
}
|
if (this.body.negativeServerUrl) {
|
cardInfo.negativeServerUrl = this.body.negativeServerUrl.replace(domain, "");
|
cardInfo.uploadTime = new Date();
|
}
|
await this.dbWrite.save(cardInfo);
|
this.info.result = true;
|
this.info.msg = "身份证上传成功";
|
} else {
|
this.info.result = false;
|
this.info.msg = "身份证上传失败";
|
}
|
} catch (ex) {
|
this.info.result = false;
|
this.info.msg = "错误信息:" + ex.message;
|
}
|
this.ctx.body = this.info;
|
}
|
//#endregion
|
|
//#region SingleStart
|
/// <summary>
|
/// GetToken
|
/// </summary>
|
/// <param name="this.body"></param>
|
/// <returns></returns>
|
@Post()
|
public async singleStart() {
|
try {
|
// provingState = 验证状态
|
let cardList = await this.dbRead.find(TMSIdCard, {
|
idCard_Id: In(this.body.ids)
|
});
|
let startDate = moment(new Date());
|
let index = 0;
|
for (let item of cardList) {
|
index++;
|
this.ctx.logger.info("手动开始执行:" + index);
|
this.info = await this.ctx.service.tms.idCard.singleAnalysis(item, this.info);
|
}
|
let endDate = moment(new Date());
|
let span = endDate.diff(startDate, "seconds");
|
this.ctx.logger.info("身份证手动分析总时长:" + span);
|
} catch (ex) {
|
this.ctx.logger.info("分析错误:" + ex.message);
|
}
|
|
this.ctx.body = this.info;
|
}
|
//#endregion
|
|
//#region IdCardDistinguish_Inner 身份证识别
|
@Post()
|
public async idCardUpload() {
|
try {
|
this.ctx.body = await this.ctx.service.tms.idCard.idCardUpload();
|
} catch (ex) {
|
this.info.result = false;
|
this.info.msg = "错误信息:" + ex.message;
|
}
|
this.ctx.body = this.info;
|
}
|
//#endregion
|
|
//#region 批量上传身份证
|
@Post()
|
public async importExcel() {
|
setTimeout(async () => {
|
await this.importExcelWork();
|
}, 0);
|
|
this.info.result = true;
|
this.ctx.body = this.info;
|
}
|
|
private async importExcelWork() {
|
await this.unzip();
|
let insertCount = 0;
|
let msg = "";
|
let userInfo = await this.userInfo;
|
let { ctx } = this;
|
let body = ctx.request.body;
|
let redis = ctx.app.redis.clients.get("common"); // 将消息放入redis缓存
|
redis.expire(body.key, 5 * 60);
|
let rootPath = path.resolve(); // 获得根目录
|
let unzipPath = rootPath + path.sep + "upload" + path.sep + "unzip_" + userInfo.user_Id;
|
let filePath = unzipPath + path.sep + 'data.xlsx'; // 上传文件路径
|
if (!fs.existsSync(filePath)) {
|
this.setMsg(`解压后文件夹中不存在excel文件,excel文件名必须命名为data.xlsx`, "red");
|
return;
|
}
|
// 写导入业务逻辑和上传身份证到oss上
|
var workbook = XLSX.readFile(filePath); //整个 excel 文档
|
var sheetNames = workbook.SheetNames; //获取所有工作薄名
|
var sheet1 = workbook.Sheets[sheetNames[0]]; //根据工作薄名获取工作薄
|
let dataList = XLSX.utils.sheet_to_json(sheet1); // 获得当前sheet表单数据转为json格式
|
// 图片
|
// let imageList = [];
|
//#region 验证数据正确性
|
this.info.result = true;
|
if (!dataList.length) {
|
this.setMsg(`没有可导入的数据`, "red");
|
return;
|
}
|
this.isMsgError = false;
|
let i = 0;
|
for (let row of dataList) {
|
i++;
|
if (i % 500 === 1) {
|
this.setMsg(`${i}、开始验证...`);
|
}
|
if (!row["姓名"]) {
|
this.setMsg(`${i}、第${i + 1}姓名不能为空`, "red");
|
}
|
if (!row["联系电话"]) {
|
this.setMsg(`${i}、第${i + 1}联系电话不能为空`, "red");
|
}
|
if (!row["身份证号"]) {
|
this.setMsg(`${i}、第${i + 1}身份证号不能为空`, "red");
|
}
|
if (!row["身份证正面"]) {
|
this.setMsg(`${i}、第${i + 1}身份证正面不能为空`, "red");
|
}
|
if (!row["身份证反面"]) {
|
this.setMsg(`${i}、第${i + 1}身份证反面不能为空`, "red");
|
}
|
let frontcard = row["身份证正面"];
|
let reversecard = row["身份证反面"];
|
let imgFront = unzipPath + path.sep + frontcard;
|
if (!fs.existsSync(imgFront)) {
|
this.setMsg(`${i}、第${i + 1}身份证正面图片不存在`, "red");
|
}
|
let imgReverse = unzipPath + path.sep + reversecard;
|
if (!fs.existsSync(imgReverse)) {
|
this.setMsg(`${i}、第${i + 1}身份证反面图片不存在`, "red");
|
}
|
}
|
if (this.isMsgError) {
|
this.setMsg("-1");
|
return;
|
}
|
|
let domain = "https://auod-bucket-wms.oss-ap-southeast-2.aliyuncs.com/";
|
for (let dataInfo of dataList) {
|
insertCount += 1;
|
let names = dataInfo["姓名"];
|
let mobile = dataInfo["联系电话"];
|
let idCardcode = dataInfo["身份证号"];
|
let frontcard = dataInfo["身份证正面"];
|
let reversecard = dataInfo["身份证反面"];
|
let imgFront = unzipPath + path.sep + frontcard;
|
let imgReverse = unzipPath + path.sep + reversecard;
|
|
|
let tmscard = await this.dbRead.findOne(TMSIdCard, {
|
idCardCode: idCardcode
|
});
|
if (tmscard) {
|
this.setMsg(`${insertCount}、第${insertCount + 1}身份证号已存在`, "red");
|
continue;
|
};
|
// 上传图片到阿里云OSS
|
let aliUrlFront = `/Upload/Card/${idCardcode}_1.jpg`;
|
let info = await aliOSSUpload(this.ctx, imgFront, aliUrlFront, true);
|
if (!info.result) {
|
this.setMsg(`${insertCount}、第${insertCount + 1}身份证正面上传失败`, "red");
|
continue;
|
}
|
|
let aliUrlReverse = `/Upload/Card/${idCardcode}_2.jpg`;
|
info = await aliOSSUpload(this.ctx, imgReverse, aliUrlReverse, true);
|
if (!info.result) {
|
this.setMsg(`${insertCount}、第${insertCount + 1}身份证反面上传失败`, "red");
|
continue;
|
}
|
let idcardInfo = new TMSIdCard();
|
idcardInfo.negativeServerUrl = domain + aliUrlReverse;
|
idcardInfo.positiveServerUrl = domain + aliUrlFront;
|
idcardInfo.idCardType = "身份证";
|
idcardInfo.fullName = names;
|
idcardInfo.idCardCode = idCardcode;
|
idcardInfo.mobile = mobile;
|
idcardInfo.uploadTime = new Date();
|
idcardInfo.provingState = "待验证";
|
idcardInfo.periodState = "有效";
|
idcardInfo.userProduct_Id = userInfo.userProduct_Id;
|
idcardInfo.userProductCode = userInfo.userProductCode;
|
idcardInfo.userProductAlias = userInfo.userProductAlias;
|
idcardInfo.platUser_Id = userInfo.platUser_Id;
|
idcardInfo.platUserCode = userInfo.platUserCode;
|
idcardInfo.platUserName = userInfo.platUserName;
|
idcardInfo.platCorpName = userInfo.platCorpName;
|
idcardInfo.createID = userInfo.userProduct_Id;
|
|
idcardInfo.createID = userInfo.user_Id;
|
idcardInfo.creator = userInfo.userName;
|
idcardInfo.createDate = new Date();
|
|
idcardInfo.consignor_Id = userInfo.consignor_Id;;
|
idcardInfo.consignorCode = userInfo.consignorCode;
|
idcardInfo.consignorName = userInfo.consignorName;
|
// if (insertCount % 50 === 0) {
|
await this.dbWrite.save(idcardInfo);
|
msg = "导入第" + insertCount + "行成功";
|
this.setMsg(msg, "blue");
|
// }
|
}
|
msg = `共导入${insertCount}条成功`;
|
this.setMsg(msg, "blue");
|
this.setMsg("-1");
|
}
|
|
/**
|
* 解压文件
|
*/
|
private async unzip() {
|
let { ctx } = this;
|
let userInfo = await this.userInfo;
|
let body = ctx.request.body;
|
let redis = ctx.app.redis.clients.get("common"); // 将消息放入redis缓存
|
let fileUrl = body.url;
|
redis.expire(body.key, 5 * 60);
|
if (!fileUrl) {
|
redis.rpush(body.key, "上传文件不存在");
|
return;
|
}
|
|
let rootPath = path.resolve(); // 获得根目录
|
let filePath = rootPath + path.sep + fileUrl.replace(/\//gi, path.sep); // 上传文件路径
|
let unzipPath = rootPath + path.sep + "upload" + path.sep + "unzip_" + userInfo.user_Id;
|
this.ctx.helper.deldir(unzipPath); // 先删除数据
|
this.ctx.helper.mkdir(unzipPath); // 创建目录
|
|
var promise = new Promise(resolve => {
|
let { unzip } = require("cross-unzip");
|
unzip(filePath, unzipPath, err => {
|
resolve(err);
|
});
|
});
|
return promise;
|
}
|
//#endregion
|
}
|