//#region import
|
import { BaseProductInfo } from "../../entity/basicInfo/base/baseProductInfo";
|
import { BaseStorage } from "../../entity/basicInfo/base/baseStorage";
|
import { TMSWayBill } from "../../entity/express/tms/tmsWayBill";
|
import { BasePostCode } from "../../entity/basicInfo/base/basePostCode";
|
import { TMSWayBillReceive } from "../../entity/express/tms/tmsWayBillReceive";
|
import { TMSIdCard } from "../../entity/express/tms/tmsIdCard";
|
import { TMSWayBillList } from "../../entity/express/tms/tmsWayBillList";
|
import { Post, Prefix } from "egg-shell-decorators";
|
|
import * as path from "path";
|
import * as fs from "fs";
|
import moment = require("moment");
|
import { aliOSSUpload } from "../../public/aliyunOssHelper";
|
import { BaseConsignor } from "../../entity/basicInfo/consignor/baseConsignor";
|
import BaseController from "../baseController";
|
//#endregion
|
|
@Prefix("/open")
|
export default class OpenController extends BaseController {
|
//#region wayBillAdd
|
/**
|
* 推送新增运单接口
|
*/
|
@Post()
|
public async wayBillAdd() {
|
let { ctx, app } = this;
|
let body = ctx.request.body;
|
|
// let userInfo = await ctx.helper.userInfo();
|
if (!body.wayBillInfo || !body.wayBillDetails) {
|
this.info.result = false;
|
this.info.msg = "传递数据不正确";
|
this.info.statusCode = 301;
|
|
ctx.body = this.info;
|
return;
|
}
|
|
let wayBillCode = body.wayBillInfo.wayBillCode;
|
// 保存到mongodb
|
try {
|
ctx.logger.info("将数据保存至MongoDB before:" + wayBillCode);
|
let db = this.app.mongodb;
|
let collection = db.collection("apiWayBill");
|
let re = await collection.insert(body);
|
ctx.logger.info("将数据保存至MongoDB after:" + JSON.stringify(re));
|
// ctx.logger.info("将数据保存至MongoDB after:" + db.url);
|
} catch (error) {
|
ctx.logger.info("将数据保存至MongoDB错误:" + error.message);
|
}
|
|
this.ctx.logger.info("推送新增运单接口:" + wayBillCode);
|
let wayBillInfo = body.wayBillInfo;
|
let wayBillDetails = body.wayBillDetails;
|
|
try {
|
//#region 校验数据
|
|
//#region 基础数据校验验证
|
let token = ctx.header.token;
|
if (!token) {
|
this.info.result = false;
|
this.info.msg = "token信息不合法";
|
this.info.statusCode = 301;
|
|
ctx.body = this.info;
|
return;
|
}
|
// 获得货主信息
|
const signTokenKey = "consignorToken_" + token;
|
let prodRedis = app.redis.clients.get("userInfo");
|
let conInfoStr = null;
|
if (prodRedis !== undefined) {
|
conInfoStr = await prodRedis.get(signTokenKey);
|
}
|
if (conInfoStr == null) {
|
this.info.result = false;
|
this.info.msg = "用户信息不合法";
|
this.info.statusCode = 302;
|
|
ctx.body = this.info;
|
return;
|
}
|
let conInfo = JSON.parse(conInfoStr);
|
ctx.logger.info("货主信息:" + conInfo.consignorName + ", " + conInfo.consignorCode);
|
|
//#endregion
|
|
//#region 校验物料数据
|
// 验证手机号或电话格式是否正确
|
let consigneeMobile = wayBillInfo.consigneeMobile;
|
let regular = /^((0\d{2,3}-\d{7,8})|(1[3569784]\d{9}))$/;
|
if (!regular.test(consigneeMobile)) {
|
this.info.result = false;
|
this.info.msg = consigneeMobile + "收货人电话格式错误!";
|
this.info.statusCode = 303;
|
|
ctx.body = this.info;
|
return;
|
}
|
|
// 包裹毛重不能为空!
|
if (!wayBillInfo.grossWeight) {
|
this.info.result = false;
|
this.info.msg = "包裹毛重不能为空!";
|
this.info.statusCode = 538;
|
ctx.body = this.info;
|
return;
|
}
|
|
// 校验明细
|
if (!wayBillDetails || wayBillDetails.Count === 0) {
|
this.info.result = false;
|
this.info.msg = "请填写运单物料明细!";
|
this.info.statusCode = 304;
|
|
ctx.body = this.info;
|
return;
|
}
|
for (let item of wayBillDetails) {
|
if (!item.productCode) {
|
this.info.result = false;
|
this.info.msg = "运单明细中,物料编号不能为空!";
|
this.info.statusCode = 305;
|
|
ctx.body = this.info;
|
return;
|
}
|
let dataInfo = await this.dbRead.findOne(BaseProductInfo, {
|
where: [
|
{
|
productCode: item.productCode,
|
auditing: 2
|
},
|
{
|
productModel: item.productCode,
|
auditing: 2
|
}
|
]
|
});
|
|
if (!dataInfo) {
|
this.info.result = false;
|
this.info.msg = `物料名称为:${item.ProductName}条形码为:${item.productCode}未进行备案,不能推送。请提供物料详情,联系客服备案!`;
|
this.info.statusCode = 306;
|
|
ctx.body = this.info;
|
return;
|
}
|
}
|
//#endregion
|
|
//#region 校验运单
|
if (!wayBillInfo.wayBillCode) {
|
this.info.result = false;
|
this.info.msg = "运单号不允许为空!";
|
this.info.statusCode = 305;
|
|
ctx.body = this.info;
|
return;
|
} else {
|
let billInfo = await this.dbRead.findOne(TMSWayBill, {
|
where: {
|
wayBillCode: wayBillInfo.wayBillCode
|
}
|
});
|
if (billInfo != null) {
|
this.info.result = false;
|
this.info.msg = wayBillInfo.wayBillCode + "运单号已存在,不允许重复导入!";
|
this.info.statusCode = 503;
|
ctx.body = this.info;
|
return;
|
}
|
}
|
//#endregion
|
|
//#region 校验仓库
|
let storage_Id = 51;
|
let storageName = "悉尼仓";
|
if (!wayBillInfo.storageName) {
|
let storage = await this.dbRead.findOne(BaseStorage, {
|
where: { storageName: wayBillInfo.storageName }
|
});
|
if (storage != null) {
|
storage_Id = storage.storage_Id;
|
storageName = storage.storageName;
|
}
|
}
|
|
if (wayBillInfo.orderType != "直邮") {
|
this.info.result = false;
|
this.info.msg = "订单类型为默认值“直邮”!";
|
this.info.statusCode = 306;
|
ctx.body = this.info;
|
return;
|
}
|
//#endregion
|
|
//#region 收货人检验
|
if (!wayBillInfo.consigneeName) {
|
this.info.result = false;
|
this.info.msg = "收货人不能为空!";
|
this.info.statusCode = 505;
|
ctx.body = this.info;
|
return;
|
}
|
if (!wayBillInfo.consigneeMobile) {
|
this.info.result = false;
|
this.info.msg = "收货人电话不能为空!";
|
this.info.statusCode = 506;
|
ctx.body = this.info;
|
return;
|
}
|
if (!wayBillInfo.consigneeAddress) {
|
this.info.result = false;
|
this.info.msg = "收货人详细地址不能为空!";
|
this.info.statusCode = 507;
|
ctx.body = this.info;
|
return;
|
}
|
//#endregion
|
|
//#region 查询省市区
|
//省
|
if (!wayBillInfo.provinceName) {
|
this.info.result = false;
|
this.info.msg = "请正确填写省!";
|
this.info.statusCode = 509;
|
ctx.body = this.info;
|
return;
|
}
|
//市
|
if (!wayBillInfo.cityName) {
|
this.info.result = false;
|
this.info.msg = "请正确填写市!";
|
this.info.statusCode = 510;
|
this.ctx.body = this.info;
|
return;
|
}
|
//区
|
if (!wayBillInfo.regionName) {
|
this.info.result = false;
|
this.info.msg = "请正确填写区!";
|
this.info.statusCode = 511;
|
ctx.body = this.info;
|
return;
|
}
|
if (!wayBillInfo.consigneeAddress) {
|
this.info.result = false;
|
this.info.msg = "请正确填写收货人详细地址!";
|
this.info.statusCode = 508;
|
ctx.body = this.info;
|
return;
|
}
|
// let _billInfo = wayBillInfo;
|
// let addr1 = _billInfo.provinceName + _billInfo.cityName + _billInfo.regionName;
|
// if (_billInfo.consigneeAddress.indexOf(addr1) != 0) {
|
// this.info.result = false;
|
// this.info.msg = "详细地址开头省市区不全。";
|
// ctx.body = this.info;
|
// return;
|
// }
|
// // 验证省市是否一致
|
// let sqlCity = `SELECT C.City_Id FROM dbo.Base_City C WHERE C.cityName='${_billInfo.cityName}'
|
// AND C.ParentId=(SELECT TOP 1 P.City_Id FROM dbo.Base_City P WHERE P.cityName='${_billInfo.provinceName}' AND P.ParentId=0)
|
// `;
|
// let cityExists = await this.dbRead.query(sqlCity);
|
// if (!cityExists) {
|
// this.info.result = false;
|
// this.info.msg = "省市不匹配,无法保存。";
|
// ctx.body = this.info;
|
// return;
|
// }
|
|
// 根据省市区详细地址获取邮编
|
let consigneePostCode = "100000";
|
let postCodeList = await this.dbRead.find(BasePostCode, {
|
where: {
|
province: wayBillInfo.provinceName,
|
city: wayBillInfo.cityName,
|
district: wayBillInfo.regionName
|
}
|
});
|
if (postCodeList.length > 0) {
|
for (let item of postCodeList) {
|
let address = item.address;
|
if (address.indexOf(wayBillInfo.consigneeAddress) >= 0) {
|
consigneePostCode = item.postNumber;
|
}
|
}
|
} else {
|
//只根据省市查询邮政编码
|
let postCodeInfo = await this.dbRead.findOne(BasePostCode, {
|
where: {
|
province: wayBillInfo.provinceName,
|
city: wayBillInfo.cityName
|
}
|
});
|
if (postCodeInfo) {
|
consigneePostCode = postCodeInfo.postNumber;
|
}
|
}
|
//#endregion
|
|
//#region 发件人校验
|
if (!wayBillInfo.billingName) {
|
this.info.result = false;
|
this.info.msg = "发货人不能为空!";
|
this.info.statusCode = 512;
|
ctx.body = this.info;
|
return;
|
}
|
if (!wayBillInfo.billingMobile) {
|
this.info.result = false;
|
this.info.msg = "发货人电话不能为空!";
|
this.info.statusCode = 513;
|
ctx.body = this.info;
|
return;
|
}
|
if (!wayBillInfo.billingAddress) {
|
this.info.result = false;
|
this.info.msg = "发件地址不能为空!";
|
this.info.statusCode = 514;
|
ctx.body = this.info;
|
return;
|
}
|
//#endregion
|
|
//#endregion
|
|
//#region 添加运单表
|
let wayInfo = new TMSWayBill();
|
wayInfo.wayBillCode = wayBillInfo.wayBillCode;
|
wayInfo.customerOrderNo = wayBillInfo.customerOrderNo;
|
wayInfo.consignor_Id = conInfo.consignor_Id;
|
wayInfo.consignorCode = conInfo.consignorCode;
|
wayInfo.consignorName = conInfo.consignorName;
|
wayInfo.consigneeName = wayBillInfo.consigneeName;
|
|
wayInfo.storage_Id = storage_Id;
|
wayInfo.storageName = storageName;
|
|
//Province_Id = Datasheng.City_Id,
|
wayInfo.provinceName = wayBillInfo.provinceName;
|
//City_Id = Datashi.City_Id,
|
wayInfo.cityName = wayBillInfo.cityName;
|
//Region_Id = Dataqu.City_Id,
|
wayInfo.regionName = wayBillInfo.regionName;
|
|
wayInfo.street = wayBillInfo.street;
|
wayInfo.consigneeAddress = wayBillInfo.consigneeAddress;
|
wayInfo.consigneePostCode = consigneePostCode;
|
wayInfo.consigneeMobile = wayBillInfo.consigneeMobile;
|
wayInfo.consigneeIdcard = wayBillInfo.consigneeIdcard;
|
wayInfo.grossWeight = wayBillInfo.grossWeight; //包裹毛重
|
wayInfo.isStoreMate = 0;
|
wayInfo.orderType = wayBillInfo.orderType;
|
wayInfo.remark = wayBillInfo.remark;
|
|
wayInfo.billingName = wayBillInfo.billingName;
|
wayInfo.billingMobile = wayBillInfo.billingMobile;
|
wayInfo.billingAddress = wayBillInfo.billingAddress;
|
|
wayInfo.platUser_Id = conInfo.platUser_Id;
|
wayInfo.platUserCode = conInfo.platUserCode;
|
wayInfo.platUserName = conInfo.platUserName;
|
wayInfo.userProduct_Id = conInfo.userProduct_Id;
|
wayInfo.userProductCode = conInfo.userProductCode;
|
wayInfo.userProductAlias = conInfo.userProductAlias;
|
wayInfo.orderStatus = "已提交";
|
wayInfo.makeWay = "API录入";
|
wayInfo.createDate = new Date();
|
wayInfo.creator = conInfo.consignorName;
|
wayInfo.createID = conInfo.consignor_Id;
|
//揽收表中是否存在该运单且状态为已揽收
|
let wayBillReceiveInfo = await this.dbRead.findOne(TMSWayBillReceive, {
|
wayBillCode: wayBillInfo.wayBillCode
|
});
|
|
// 揽收身份证不为空,以揽收为准
|
if (wayBillReceiveInfo != null && wayBillReceiveInfo.consigneeName && wayBillReceiveInfo.consigneeIdcard) {
|
wayInfo.consigneeName = wayBillReceiveInfo.consigneeName;
|
wayInfo.consigneeIdcard = wayBillReceiveInfo.consigneeIdcard;
|
}
|
|
await this.dbWrite.insert(TMSWayBill, wayInfo);
|
let newWayInfo = await this.dbRead.findOne(TMSWayBill, {
|
where: {
|
wayBillCode: wayInfo.wayBillCode
|
},
|
select: ["wayBill_Id"]
|
});
|
if (!newWayInfo) {
|
ctx.logger.info(wayInfo.wayBillCode + " 查询结果是空的");
|
} else {
|
if (wayInfo.wayBill_Id != newWayInfo.wayBill_Id) {
|
ctx.logger.info(wayInfo.wayBillCode + " 保存ID错乱" + wayInfo.wayBill_Id + ", 实际为:" + newWayInfo.wayBill_Id);
|
wayInfo.wayBill_Id = newWayInfo.wayBill_Id;
|
}
|
}
|
|
if (wayInfo.wayBill_Id > 0) {
|
if (wayBillReceiveInfo != null && wayBillReceiveInfo.collectStatus == "已揽收") {
|
await this.dbWrite.update(TMSWayBill, wayInfo.wayBill_Id, {
|
collectStatus: wayBillReceiveInfo.collectStatus
|
});
|
}
|
//添加物流轨迹
|
await this.service.tms.wayBillHelper.setStatusHistory(wayInfo, "API录入", "订单创建成功", "订单创建成功");
|
}
|
//#endregion
|
|
//#region 更新收货人手机到身份证库
|
if (wayBillInfo.consigneeIdcard) {
|
let idcard = await this.dbRead.findOne(TMSIdCard, {
|
idCardCode: wayBillInfo.consigneeIdcard
|
});
|
let consigneeMobile = wayInfo.consigneeMobile;
|
if (idcard && !(idcard.mobile === consigneeMobile || idcard.mobile2 === consigneeMobile || idcard.mobile3 === consigneeMobile)) {
|
if (!idcard.mobile) {
|
await this.dbWrite.update(TMSIdCard, idcard.idCard_Id, {
|
mobile: wayInfo.consigneeMobile
|
});
|
} else if (!idcard.mobile2) {
|
await this.dbWrite.update(TMSIdCard, idcard.idCard_Id, {
|
mobile2: wayInfo.consigneeMobile
|
});
|
} else if (!idcard.mobile3) {
|
await this.dbWrite.update(TMSIdCard, idcard.idCard_Id, {
|
mobile3: wayInfo.consigneeMobile
|
});
|
}
|
}
|
}
|
//#endregion
|
|
//#region 运单明细处理
|
for (let detailInfo of wayBillDetails) {
|
// 查询物料信息
|
let dataInfo = await this.dbRead.findOne(BaseProductInfo, {
|
where: [
|
{
|
productCode: detailInfo.productCode,
|
auditing: 2
|
},
|
{
|
productModel: detailInfo.productCode,
|
auditing: 2
|
}
|
]
|
});
|
if (dataInfo) {
|
//添加到TMS_WayBillList表
|
let listInfo = new TMSWayBillList();
|
listInfo.wayBill_Id = wayInfo.wayBill_Id;
|
listInfo.product_Id = dataInfo.product_Id;
|
listInfo.productName = dataInfo.productName;
|
listInfo.productCode = dataInfo.productCode;
|
listInfo.productModel = dataInfo.productModel;
|
listInfo.productSpec = dataInfo.productSpec;
|
listInfo.quantityOrder = detailInfo.quantityOrder;
|
listInfo.smallUnit = detailInfo.smallUnit;
|
listInfo.salePrice = detailInfo.salePrice || 0;
|
listInfo.rowTotal = detailInfo.quantityOrder * (detailInfo.salePrice || 0);
|
listInfo.weight = detailInfo.weight;
|
listInfo.grossWeight = detailInfo.weight + 0.1; //以前是加100
|
await this.dbWrite.insert(TMSWayBillList, listInfo);
|
}
|
}
|
//#endregion
|
|
//#region 修改运单合计数量和合计金额
|
let sqlwhere = `Update TMS_WayBill Set
|
GrandTotal=(select sum(rowTotal) from TMS_WayBillList where wayBill_Id =TMS_WayBill.wayBill_Id),
|
TotalQuantityOrder=(select sum(quantityOrder) from TMS_WayBillList where wayBill_Id =TMS_WayBill.wayBill_Id),
|
weight =(select sum(weight) from TMS_WayBillList where wayBill_Id =TMS_WayBill.wayBill_Id)
|
Where wayBill_Id=${wayInfo.wayBill_Id}`;
|
await this.dbWrite.query(sqlwhere);
|
//#endregion
|
|
//#region 返回结果
|
this.info.data = {
|
wayBill_Id: wayInfo.wayBill_Id
|
};
|
this.info.msg = "推送成功!";
|
this.info.result = true;
|
ctx.body = this.info;
|
this.ctx.logger.info(wayBillInfo.wayBillCode + this.info.msg);
|
//#endregion
|
} catch (error) {
|
let msg = "运单上传错误信息:" + error.message;
|
this.info.result = false;
|
this.info.statusCode = 515;
|
this.info.msg = msg;
|
console.log(msg);
|
ctx.body = this.info;
|
}
|
}
|
|
//#endregion
|
|
//#region getRouterList
|
/**
|
* 获取路由
|
*/
|
@Post()
|
public async getRouterList() {
|
let { ctx } = this;
|
let body = ctx.request.body;
|
|
if (!body.code) {
|
this.info.result = false;
|
this.info.msg = "传递数据不正确";
|
this.info.statusCode = 301;
|
|
ctx.body = this.info;
|
return;
|
}
|
|
let info = await this.service.tms.wayBillHelper.getWayBillInfo(body.code);
|
this.logger.info("GetRouterList2物流轨迹查询接口 - data:", body.code, info.result, info.msg);
|
ctx.body = info;
|
}
|
|
//#endregion
|
|
//#region productAdd
|
/**
|
* 物料提交备案接口
|
*/
|
@Post()
|
public async productAdd() {
|
let { ctx, app } = this;
|
let body = ctx.request.body;
|
let productInfoList = body.productInfoList;
|
|
if (!Array.isArray(productInfoList)) {
|
this.info.result = false;
|
this.info.msg = "传递数据物料信息必须为数组结构";
|
this.info.statusCode = 301;
|
|
ctx.body = this.info;
|
return;
|
}
|
|
ctx.logger.info("物料入库接口:", body);
|
//#region 验证并获取货主信息
|
let token = ctx.header.token;
|
if (!token) {
|
this.info.result = false;
|
this.info.statusCode = 301;
|
this.info.msg = "token信息不合法";
|
ctx.body = this.info;
|
return;
|
}
|
|
// 获得货主信息
|
const signTokenKey = "consignorToken_" + token;
|
let prodRedis = app.redis.clients.get("userInfo");
|
let conInfoStr = null;
|
if (prodRedis !== undefined) {
|
conInfoStr = await prodRedis.get(signTokenKey);
|
}
|
if (conInfoStr == null) {
|
this.info.result = false;
|
this.info.msg = "用户信息不合法";
|
this.info.statusCode = 302;
|
|
ctx.body = this.info;
|
return;
|
}
|
let conInfo = JSON.parse(conInfoStr);
|
ctx.logger.info("货主信息:" + conInfo.consignorName + ", " + conInfo.consignorCode);
|
//#endregion
|
|
try {
|
let _PProductInfoList: BaseProductInfo[] = [];
|
for (let dataInfo of productInfoList) {
|
//#region 校验数据
|
//添加物料信息
|
let data = new BaseProductInfo();
|
//校验物料编号
|
if (!dataInfo.productCode) {
|
this.info.result = false;
|
this.info.statusCode = 522;
|
this.info.msg = "物料编号不能为空!";
|
ctx.body = this.info;
|
return;
|
} else {
|
//查询编号是否已存在
|
let productInfo = this.dbWrite.findOne(BaseProductInfo, {
|
productCode: dataInfo.productCode
|
});
|
if (productInfo === null) {
|
data.productCode = dataInfo.productCode;
|
} else {
|
this.info.result = false;
|
this.info.msg = dataInfo.productCode + "物料编码已存在!";
|
ctx.body = this.info;
|
return;
|
}
|
}
|
//校验物料条码
|
if (!dataInfo.productModel) {
|
this.info.result = false;
|
this.info.statusCode = 522;
|
this.info.msg = "物料条码不能为空!";
|
ctx.body = this.info;
|
return;
|
} else {
|
//查询条码是否已存在
|
let productInfo = await this.dbWrite.findOne(BaseProductInfo, {
|
productModel: dataInfo.productModel
|
});
|
if (productInfo) {
|
this.info.result = false;
|
this.info.statusCode = 523;
|
this.info.msg = dataInfo.productModel + "物料条码已存在!";
|
ctx.body = this.info;
|
return;
|
}
|
}
|
data.productModel = dataInfo.productModel;
|
data.productName = dataInfo.ProductName;
|
data.cIQName = dataInfo.ciqName;
|
data.brand_Id = dataInfo.brand_Id;
|
data.brandName = dataInfo.brandName;
|
data.type_Id = dataInfo.type_Id;
|
data.typeName = dataInfo.typeName;
|
data.productSpec = dataInfo.productSpec;
|
data.originPlace = dataInfo.originPlace;
|
data.netWeight = dataInfo.netWeight;
|
data.weight = dataInfo.weight;
|
data.productStatus = 1;
|
data.auditing = 0;
|
data.consignor_Id = conInfo.consignor_Id;
|
data.consignorName = conInfo.consignorName;
|
data.consignorCode = conInfo.consignorCode;
|
data.platUser_Id = conInfo.platUser_Id;
|
data.platUserCode = conInfo.platUserCode;
|
data.platUserName = conInfo.platUserName;
|
data.userProduct_Id = conInfo.userProduct_Id;
|
data.userProductCode = conInfo.userProductCode;
|
data.createID = conInfo.consignor_Id;
|
data.creator = conInfo.consignorName;
|
data.createDate = new Date();
|
await this.dbWrite.insert(BaseProductInfo, data);
|
_PProductInfoList.push(data);
|
//#endregion
|
}
|
this.info.result = true;
|
this.info.data = _PProductInfoList;
|
this.info.statusCode = 524;
|
this.info.msg = "物料信息添加成功!";
|
} catch (ex) {
|
this.info.result = false;
|
this.info.msg = ex.message;
|
}
|
ctx.body = this.info;
|
}
|
|
//#endregion
|
|
//#region productSearch
|
/**
|
* 物料查询接口
|
*/
|
@Post()
|
public async productSearch() {
|
let { ctx, app } = this;
|
let body = ctx.request.body;
|
let code = body.code;
|
|
if (!code) {
|
this.info.result = false;
|
this.info.msg = "物料条码不能为空";
|
this.info.statusCode = 301;
|
|
ctx.body = this.info;
|
}
|
ctx.logger.info("物料查询接口:", body);
|
|
//#region 验证并获取货主信息
|
let token = ctx.header.token;
|
if (!token) {
|
this.info.result = false;
|
this.info.statusCode = 301;
|
this.info.msg = "token信息不合法";
|
ctx.body = this.info;
|
return;
|
}
|
|
// 获得货主信息
|
const signTokenKey = "consignorToken_" + token;
|
let prodRedis = app.redis.clients.get("userInfo");
|
let conInfoStr = null;
|
if (prodRedis !== undefined) {
|
conInfoStr = await prodRedis.get(signTokenKey);
|
}
|
if (conInfoStr == null) {
|
this.info.result = false;
|
this.info.msg = "用户信息不合法";
|
this.info.statusCode = 302;
|
|
ctx.body = this.info;
|
return;
|
}
|
let conInfo = JSON.parse(conInfoStr);
|
ctx.logger.info("货主信息:" + conInfo.consignorName + ", " + conInfo.consignorCode);
|
//#endregion
|
|
try {
|
//查询编号是否已存在
|
let productInfo = await this.dbRead.findOne(BaseProductInfo, {
|
productModel: code
|
});
|
if (productInfo != null) {
|
let _Product = {
|
productModel: productInfo.productModel,
|
auditing: productInfo.auditing
|
};
|
this.info.result = true;
|
this.info.data = _Product;
|
this.info.statusCode = 536;
|
this.info.msg = "已获取物料状态!";
|
} else {
|
this.info.result = false;
|
this.info.msg = "所查询的条码信息不存在!";
|
}
|
} catch (ex) {
|
this.info.result = false;
|
this.info.msg = ex.message;
|
}
|
ctx.body = this.info;
|
}
|
|
//#endregion
|
|
//#region idcardAdd
|
/**
|
* 上传身份证正反面照片
|
*/
|
@Post()
|
public async idcardAdd() {
|
let { ctx, app } = this;
|
let body = ctx.request.body;
|
ctx.logger.info("身份证上传接口:", body.wayBillCode);
|
|
//#region 验证并获取货主信息
|
let token = ctx.header.token;
|
if (!token) {
|
this.info.result = false;
|
this.info.statusCode = 301;
|
this.info.msg = "token信息不合法";
|
ctx.body = this.info;
|
return;
|
}
|
// 保存到mongodb
|
// try {
|
// let db = this.app.mongodb;
|
// let collection = db.collection("api_idcardAdd");
|
// let data = Object.assign({ createDate: moment(new Date()).format("YYYY-MM-DD HH:mm:ss") }, body);
|
// await collection.insert(data);
|
// } catch (error) {
|
// ctx.logger.info("api_idcardAdd将数据保存至MongoDB错误:" + error.message);
|
// }
|
|
// 获得货主信息
|
const signTokenKey = "consignorToken_" + token;
|
const consignorCode = ctx.header.consignorcode;
|
let userRedis = await app.redis.clients.get("userInfo");
|
let conInfoStr = userRedis && (await userRedis.get(signTokenKey));
|
let conInfo: BaseConsignor;
|
if (!conInfoStr) {
|
conInfo = await this.dbRead.findOne(BaseConsignor, {
|
where: {
|
consignorCode: consignorCode,
|
token: token
|
}
|
});
|
if (conInfo) {
|
userRedis && (await userRedis.set(signTokenKey, JSON.stringify(conInfo)));
|
}
|
} else {
|
conInfo = JSON.parse(conInfoStr);
|
}
|
if (!conInfo) {
|
this.info.result = false;
|
this.info.msg = "用户信息不合法";
|
this.info.statusCode = 302;
|
|
ctx.body = this.info;
|
return;
|
}
|
ctx.logger.info("货主信息:" + conInfo.consignorName + ", " + conInfo.consignorCode);
|
//#endregion
|
|
try {
|
let imageUrl = ""; //身份证正面
|
let ImageUrlCare = ""; //身份证反面
|
|
//#region 校验数据
|
if (!body.wayBillCode) {
|
this.info.result = false;
|
this.info.msg = "运单号不能为空";
|
this.info.statusCode = 301;
|
|
ctx.body = this.info;
|
}
|
if (!body.fullName) {
|
this.info.result = false;
|
this.info.statusCode = 526;
|
this.info.msg = "姓名不能为空";
|
ctx.body = this.info;
|
return;
|
}
|
if (!body.idCardCode) {
|
this.info.result = false;
|
this.info.statusCode = 527;
|
this.info.msg = "证件号码不能为空";
|
ctx.body = this.info;
|
return;
|
}
|
if (!body.validityPeriod) {
|
this.info.result = false;
|
this.info.statusCode = 530;
|
this.info.msg = "身份证有效期不能为空";
|
ctx.body = this.info;
|
return;
|
}
|
if (!body.mobile) {
|
this.info.result = false;
|
this.info.statusCode = 531;
|
this.info.msg = "手机号不能为空";
|
ctx.body = this.info;
|
return;
|
}
|
if (body.validityPeriod != "长期") {
|
let endDate = moment(body.validityPeriod);
|
let now = moment(new Date());
|
if (endDate <= now) {
|
this.info.result = false;
|
this.info.statusCode = 532;
|
this.info.msg = "身份证已过期不允许上传";
|
ctx.body = this.info;
|
return;
|
}
|
}
|
|
if (!body.positiveServerUrl) {
|
this.info.result = false;
|
this.info.statusCode = 528;
|
this.info.msg = "身份证正面图片不能为空";
|
ctx.body = this.info;
|
return;
|
} else {
|
//身份证正面图片
|
let positiveServerUrl = body.positiveServerUrl.replace("\n", "");
|
//过滤data:URL
|
let base64Data = positiveServerUrl.replace(/^data:image\/\w+;base64,/, "");
|
let dataBuffer = new Buffer(base64Data, "base64");
|
//上传到阿里云存储
|
let aliPath = "Upload/Card/" + body.idCardCode + "_1.jpg";
|
let rootPath = path.resolve();
|
let folder = rootPath + path.sep + "upload" + path.sep + "card" + path.sep;
|
ctx.helper.mkdir(folder);
|
let localPath = folder + body.idCardCode + "_1.jpg";
|
fs.writeFileSync(localPath, dataBuffer);
|
let _info = await aliOSSUpload(ctx, localPath, aliPath, true);
|
this.info.result = _info.result;
|
this.info.msg = _info.msg;
|
this.info.statusCode = _info.statusCode;
|
|
if (!this.info.result) {
|
ctx.body = this.info;
|
return;
|
}
|
imageUrl = "/" + aliPath;
|
//删除本地临时图片
|
fs.unlinkSync(localPath);
|
}
|
if (!body.negativeServerUrl) {
|
this.info.result = false;
|
this.info.statusCode = 529;
|
this.info.msg = "身份证反面图片不能为空";
|
ctx.body = this.info;
|
return;
|
} else {
|
//身份证正面图片
|
let negativeServerUrl = body.negativeServerUrl.replace("\n", "");
|
//过滤data:URL
|
let base64Data = negativeServerUrl.replace(/^data:image\/\w+;base64,/, "");
|
let dataBuffer = new Buffer(base64Data, "base64");
|
//上传到阿里云存储
|
let aliPath = "Upload/Card/" + body.idCardCode + "_2.jpg";
|
let rootPath = path.resolve();
|
let folder = rootPath + path.sep + "uploadcard" + path.sep;
|
ctx.helper.mkdir(folder);
|
let localPath = folder + body.idCardCode + "_2.jpg";
|
fs.writeFileSync(localPath, dataBuffer);
|
let _info = await aliOSSUpload(ctx, localPath, aliPath, true);
|
this.info.result = _info.result;
|
this.info.msg = _info.msg;
|
this.info.statusCode = _info.statusCode;
|
if (!this.info.result) {
|
ctx.body = this.info;
|
return;
|
}
|
ImageUrlCare = "/" + aliPath;
|
//删除本地临时图片
|
fs.unlinkSync(localPath);
|
}
|
//#endregion
|
|
//#region 身份证对应运单逻辑
|
let wayBillInfo = await this.dbRead.findOne(TMSWayBill, {
|
where: [
|
{
|
wayBillCode: body.wayBillCode
|
},
|
{
|
customerOrderNo: body.wayBillCode
|
}
|
]
|
});
|
// 在揽收查询中新建,将状态改为未揽收
|
let receiveInfo = await this.dbRead.findOne(TMSWayBillReceive, {
|
wayBillCode: body.wayBillCode
|
});
|
if (!receiveInfo) {
|
receiveInfo = new TMSWayBillReceive();
|
receiveInfo.collectStatus = "未揽收";
|
receiveInfo.consignor_Id = 0;
|
receiveInfo.createID = 0;
|
receiveInfo.creator = "API上传";
|
receiveInfo.wayBillCode = body.wayBillCode;
|
receiveInfo.userTrueName = "API上传";
|
receiveInfo.user_Id = 0;
|
receiveInfo.consigneeIdcard = body.idCardCode;
|
receiveInfo.consigneeName = body.fullName;
|
receiveInfo.userProduct_Id = 1007;
|
receiveInfo.platUser_Id = 1;
|
await this.dbWrite.insert(TMSWayBillReceive, receiveInfo);
|
} else {
|
let updateInfo = {
|
consigneeIdcard: body.idCardCode,
|
consigneeName: body.fullName
|
};
|
await this.dbWrite.update(TMSWayBillReceive, receiveInfo.wayBillReceive_Id, updateInfo);
|
}
|
//#endregion
|
|
//#region 新增身份证逻辑
|
//新增身份证逻辑
|
let _IdCard = await this.dbRead.findOne(TMSIdCard, {
|
idCardCode: body.idCardCode
|
});
|
if (!_IdCard) {
|
let new_idCard = new TMSIdCard();
|
new_idCard.idCardType = "身份证";
|
new_idCard.fullName = body.fullName;
|
new_idCard.idCardCode = body.idCardCode;
|
new_idCard.validityPeriod = body.validityPeriod;
|
new_idCard.uploadTime = new Date();
|
new_idCard.positiveServerUrl = imageUrl;
|
new_idCard.negativeServerUrl = ImageUrlCare;
|
new_idCard.createID = conInfo.consignor_Id;
|
new_idCard.creator = conInfo.consignorName;
|
new_idCard.createDate = new Date();
|
new_idCard.platUser_Id = conInfo.platUser_Id;
|
new_idCard.platUserCode = conInfo.platUserCode;
|
new_idCard.platUserName = conInfo.platUserName;
|
new_idCard.platCorpName = conInfo.platCorpName;
|
new_idCard.userProduct_Id = 1007;
|
new_idCard.userProductCode = conInfo.userProductCode;
|
new_idCard.userProductAlias = conInfo.userProductAlias;
|
new_idCard.consignor_Id = conInfo.consignor_Id;
|
new_idCard.consignorCode = conInfo.consignorCode;
|
new_idCard.consignorName = conInfo.consignorName;
|
new_idCard.mobile = body.mobile;
|
new_idCard.periodState = "有效";
|
new_idCard.provingState = "待验证";
|
new_idCard.mobile2 = body.mobile2;
|
new_idCard.mobile3 = body.mobile3;
|
new_idCard.remark = "api推送";
|
|
await this.dbWrite.insert(TMSIdCard, new_idCard);
|
if (new_idCard.idCard_Id > 0) {
|
if (wayBillInfo != null) {
|
//添加运单追踪记录
|
this.service.tms.wayBillHelper.setStatusHistory(wayBillInfo, "身份证上传", "身份证上传", "身份信息已收录");
|
|
let updateresult = await this.dbWrite.update(TMSWayBill, wayBillInfo.wayBill_Id, {
|
consigneeName: body.fullName,
|
consigneeIdcard: body.idCardCode
|
});
|
if (updateresult.raw) {
|
this.info.result = true;
|
this.info.statusCode = 533;
|
this.info.msg = "创建身份证库成功!";
|
}
|
} else {
|
let wb = new TMSWayBill();
|
wb.wayBill_Id = 0;
|
wb.wayBillCode = body.wayBillCode;
|
//添加运单追踪记录
|
this.service.tms.wayBillHelper.setStatusHistory(wb, "身份证上传", "身份证上传", "身份信息已收录");
|
this.info.result = true;
|
this.info.statusCode = 533;
|
this.info.msg = "创建身份证库成功!";
|
}
|
} else {
|
this.info.result = false;
|
this.info.statusCode = 534;
|
this.info.msg = "创建身份证库失败!";
|
ctx.body = this.info;
|
return;
|
}
|
} else {
|
let updateInfo = {
|
fullName: body.fullName,
|
idCardCode: body.idCardCode,
|
periodState: "有效",
|
provingState: "待验证",
|
positiveServerUrl: imageUrl,
|
negativeServerUrl: ImageUrlCare,
|
userProduct_Id: 1007,
|
remark: "api推送"
|
};
|
|
await this.dbWrite.update(TMSIdCard, _IdCard.idCard_Id, updateInfo);
|
if (wayBillInfo) {
|
//添加运单追踪记录
|
this.service.tms.wayBillHelper.setStatusHistory(wayBillInfo, "身份证上传", "身份证上传", "身份信息已收录");
|
|
await this.dbWrite.update(TMSWayBill, wayBillInfo.wayBill_Id, {
|
consigneeName: body.fullName,
|
consigneeIdcard: body.idCardCode
|
});
|
this.info.result = true;
|
this.info.msg = "上传成功";
|
} else {
|
this.info.result = true;
|
this.info.msg = "上传成功";
|
let wb = new TMSWayBill();
|
wb.wayBill_Id = 0;
|
wb.wayBillCode = body.wayBillCode;
|
//添加运单追踪记录
|
this.service.tms.wayBillHelper.setStatusHistory(wb, "身份证上传", "身份证上传", "身份信息已收录");
|
}
|
}
|
//#endregion
|
|
ctx.logger.info("IdcardAdd获取上传身份证接口数据:上传成功" + body.idCardCode);
|
} catch (ex) {
|
this.info.result = false;
|
this.info.msg = "错误信息:" + ex.message;
|
}
|
ctx.body = this.info;
|
return;
|
}
|
|
//#endregion
|
}
|