import BaseController from "../../baseController";
|
import { Post } from "egg-shell-decorators";
|
import * as crypto from "crypto";
|
import { BaseProductInfo } from "../../../entity/basicInfo/base/baseProductInfo";
|
import { TMSWeiXinWayBill } from "../../../entity/express/tms/tmsWeiXinWayBill";
|
import { TMSWeiXinWayBillList } from "../../../entity/express/tms/tmsWeiXinWayBillList";
|
import { TMSWeiXinConsignee } from "../../../entity/express/tms/tmsWeiXinConsignee";
|
import { BaseConsignor } from "../../../entity/basicInfo/consignor/baseConsignor";
|
/**
|
* 微信端
|
*/
|
export default class WeixinController extends BaseController {
|
// #region 获取用户openid
|
@Post()
|
public async getopenid() {
|
let redis = this.ctx.app.redis.clients.get("common"); // 将消息放入redis缓存
|
const ACCESS_TOKEN_OPENID_KEY = "wechat_access_token_openid"; // 缓存数据key
|
try {
|
let access_token = await redis.get(ACCESS_TOKEN_OPENID_KEY);
|
if (!access_token) {
|
let appid = "wx302e66bff6a12043";
|
let secret = "0850fcca9bca2312cc2e0ff71bc0a417";
|
// access_token和jsapi_ticket得有效期为7200秒
|
// 获取微信公众号全局唯一接口凭证
|
var url =
|
"https://open.weixin.qq.com/connect/oauth2/authorize?appid=" +
|
appid +
|
"&redirect_uri=" +
|
encodeURIComponent("http://auod-webchat2.99gyl.cn/#/way-search") +
|
"&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect&connect_redirect=1#wechat_redirect";
|
var result = await this.ctx.curl(url);
|
let data = Buffer.from(result.data).toString();
|
let strJson = JSON.parse(data);
|
// 获取jsapi_ticket微信公众号调用微信JS接口的临时票据
|
let tokenurl =
|
"https://api.weixin.qq.com/sns/oauth2/access_token?appid=" +
|
appid +
|
"&secret=" +
|
secret +
|
"&code=" +
|
strJson.code +
|
"&grant_type=authorization_code";
|
|
// let ticketUrl =
|
// "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + strJson.access_token + "&type=jsapi";
|
var api_ticket = await this.ctx.curl(tokenurl);
|
let dataticket = Buffer.from(api_ticket.data).toString();
|
let jsapi_ticket = JSON.parse(dataticket);
|
// 缓存
|
access_token = jsapi_ticket.ticket;
|
await redis.set(ACCESS_TOKEN_OPENID_KEY, access_token);
|
await redis.expire(ACCESS_TOKEN_OPENID_KEY, 7000);
|
}
|
|
this.info.data = access_token;
|
this.info.result = true;
|
this.ctx.body = this.info;
|
return;
|
} catch (ex) {
|
let msg = "异常错误信息:" + ex.message;
|
this.info.result = false;
|
this.info.msg = msg;
|
this.ctx.body = this.info;
|
return;
|
}
|
}
|
// #endregion
|
|
// #region 新增或修改订单信息
|
@Post()
|
public async addWayBill() {
|
let { ctx } = this;
|
let body = ctx.request.body;
|
let weixininfo; //运单主表
|
let weixinList; //运单明细表
|
if (body.formData) {
|
try {
|
weixininfo = new TMSWeiXinWayBill();
|
weixininfo.wayBillCode = await ctx.service.common.getCodeRegular(1767, 1); // 微信运单主表id
|
weixininfo.billingAddress = body.formData.send.sendAddress;
|
weixininfo.billingMobile = body.formData.send.sendMobile;
|
weixininfo.billingName = body.formData.send.sendUser;
|
weixininfo.regionName = body.formData.consign.consignAddress;
|
weixininfo.consigneeMobile = body.formData.consign.consignMobile;
|
weixininfo.consigneeName = body.formData.consign.consignUser;
|
weixininfo.grossWeight = body.formData.grossWeight;
|
weixininfo.shipping = body.formData.shipping;
|
weixininfo.remark = body.formData.remark;
|
weixininfo.openid = "";
|
await this.dbWrite.save(weixininfo);
|
|
if (weixininfo.wayBill_Id) {
|
for (let item of body.formData.dataList) {
|
weixinList = new TMSWeiXinWayBillList();
|
weixinList.wayBill_Id = weixininfo.wayBill_Id;
|
weixinList.productName = item.productName;
|
weixinList.productModel = item.productModel;
|
weixinList.productCode = item.productCode;
|
weixinList.quantityOrder = item.quantityOrder;
|
weixinList.salePrice = item.salePrice;
|
weixinList.rowTotal = item.rowTotal;
|
weixinList.productSpec = item.productSpec;
|
weixinList.weight = item.weight;
|
weixinList.grossWeight = item.grossWeight;
|
weixinList.smallUnit = item.smallUnit;
|
await this.dbWrite.save(weixinList);
|
}
|
}
|
this.info.result = true;
|
this.info.msg = "运单生成成功!";
|
ctx.body = this.info;
|
return;
|
} catch (error) {
|
this.info.result = false;
|
this.info.msg = error.message;
|
ctx.body = this.info;
|
return;
|
}
|
} else {
|
this.info.result = false;
|
this.info.msg = "未传入运单信息";
|
ctx.body = this.info;
|
return;
|
}
|
}
|
// #endregion
|
|
//#region 获取运单信息
|
/**
|
* 获取运单信息
|
*/
|
@Post()
|
public async getwayBill() {
|
let { ctx } = this;
|
let body = ctx.request.body;
|
let addressList;
|
try {
|
if (body.printStatus) {
|
addressList = await this.dbRead.find(TMSWeiXinWayBill, {
|
printStatus: body.printStatus
|
});
|
} else {
|
addressList = await this.dbRead.find(TMSWeiXinWayBill);
|
}
|
if (!addressList) {
|
this.info.result = false;
|
this.info.msg = "运单信息不存在";
|
ctx.body = this.info;
|
return;
|
}
|
this.info.result = true;
|
this.info.data = addressList;
|
} catch (error) {
|
this.info.result = false;
|
this.info.data = error.message;
|
}
|
ctx.body = this.info;
|
}
|
//#endregion
|
|
//#region 获取门店名称
|
/**
|
* 获取门店名称
|
*/
|
@Post()
|
public async getConsignorName() {
|
let { ctx } = this;
|
let body = ctx.request.body;
|
try {
|
let consignorInfo = await this.dbRead.findOne(BaseConsignor, {
|
consignorCode: body.consignorCode
|
});
|
if (!consignorInfo) {
|
this.info.result = false;
|
this.info.msg = "运单信息不存在";
|
ctx.body = this.info;
|
return;
|
}
|
this.info.result = true;
|
this.info.data = consignorInfo;
|
} catch (error) {
|
this.info.result = false;
|
this.info.data = error.message;
|
}
|
ctx.body = this.info;
|
}
|
//#endregion
|
|
//#region 获取地址信息
|
/**
|
* 获取地址信息
|
*/
|
@Post()
|
public async getAddress() {
|
let { ctx } = this;
|
let body = ctx.request.body;
|
try {
|
let dataList = [];
|
let datainfo = {};
|
let addressList = await this.dbRead.find(TMSWeiXinConsignee);
|
if (!addressList) {
|
this.info.result = false;
|
this.info.msg = "地址簿信息不存在";
|
ctx.body = this.info;
|
return;
|
}
|
for (let info of addressList) {
|
datainfo = {
|
title: info.consigneeName + "," + info.consigneeMobile,
|
desc: info.consigneeAddress,
|
url: {
|
path: "/addressdetails",
|
replace: false,
|
query: {
|
name: info.consigneeName,
|
mobile: info.consigneeMobile,
|
address: info.consigneeAddress,
|
title: body.title
|
}
|
}
|
};
|
dataList.push(datainfo);
|
}
|
this.info.result = true;
|
this.info.data = dataList;
|
} catch (error) {
|
this.info.result = false;
|
this.info.data = error.message;
|
}
|
ctx.body = this.info;
|
}
|
//#endregion
|
|
//#region 新增地址信息
|
/**
|
* 新增地址信息
|
*/
|
@Post()
|
public async addaddress() {
|
let { ctx } = this;
|
let body = ctx.request.body;
|
try {
|
let addressInfo = await this.dbRead.findOne(TMSWeiXinConsignee, {
|
consigneeName: body.consigneeName,
|
consigneeMobile: body.consigneeMobile,
|
consigneeAddress: body.consigneeAddress
|
});
|
if (!addressInfo) {
|
addressInfo = new TMSWeiXinConsignee();
|
addressInfo.consigneeName = body.consigneeName;
|
addressInfo.consigneeMobile = body.consigneeMobile;
|
addressInfo.consigneeAddress = body.consigneeAddress;
|
await this.dbWrite.save(addressInfo);
|
this.info.result = true;
|
this.info.msg = "添加地址成功";
|
} else {
|
this.info.result = false;
|
this.info.msg = "已添加地址,无需重复添加";
|
}
|
} catch (error) {
|
this.info.result = false;
|
this.info.data = error.message;
|
}
|
ctx.body = this.info;
|
return;
|
}
|
//#endregion
|
|
//#region 根据物料条码获取物料信息
|
/**
|
* 根据物料条码获取物料信息
|
*/
|
@Post()
|
public async getByModel() {
|
let { ctx } = this;
|
let body = ctx.request.body;
|
// let userInfo = await ctx.helper.userInfo();
|
// let userProduct_Id = userInfo.userProduct_Id;
|
// if (!body.productModel) {
|
// this.info.result = false;
|
// this.info.msg = "物料条码查询条件不存在";
|
// ctx.body = this.info;
|
// return;
|
// }
|
|
try {
|
let dataInfo;
|
if (body.productModel) {
|
dataInfo = await this.dbRead.findOne(BaseProductInfo, {
|
where: {
|
productModel: body.productModel
|
}
|
});
|
} else {
|
dataInfo = await this.dbRead.find(BaseProductInfo, {
|
where: {
|
auditing: 2
|
}
|
});
|
}
|
if (!dataInfo) {
|
this.info.result = false;
|
this.info.msg = "物料条码不存在";
|
ctx.body = this.info;
|
return;
|
}
|
|
this.info.result = true;
|
this.info.data = dataInfo;
|
} catch (error) {
|
this.info.result = false;
|
this.info.data = error.message;
|
}
|
ctx.body = this.info;
|
}
|
//#endregion
|
|
//#region 获取扫描签名
|
/**
|
* 扫描-JSTicket
|
*/
|
@Post()
|
public async getJSTicket() {
|
let redis = this.ctx.app.redis.clients.get("common"); // 将消息放入redis缓存
|
const ACCESS_TOKEN_KEY = "wechat_access_token"; // 缓存数据key
|
try {
|
let access_token = await redis.get(ACCESS_TOKEN_KEY);
|
if (!access_token) {
|
let appid = "wx0436f6ea0c214969";
|
let secret = "c9c5271f972cd61dea40d8de4f752e8f";
|
// access_token和jsapi_ticket得有效期为7200秒
|
// 获取微信公众号全局唯一接口凭证
|
let url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret;
|
var result = await this.ctx.curl(url);
|
let data = Buffer.from(result.data).toString();
|
let strJson = JSON.parse(data);
|
// 获取jsapi_ticket微信公众号调用微信JS接口的临时票据
|
let ticketUrl = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + strJson.access_token + "&type=jsapi";
|
var api_ticket = await this.ctx.curl(ticketUrl);
|
let dataticket = Buffer.from(api_ticket.data).toString();
|
let jsapi_ticket = JSON.parse(dataticket);
|
// 缓存
|
access_token = jsapi_ticket.ticket;
|
await redis.set(ACCESS_TOKEN_KEY, access_token);
|
await redis.expire(ACCESS_TOKEN_KEY, 7000);
|
}
|
// 获取时间戳
|
var timestamp = new Date().valueOf();
|
// 获取随机数
|
var nonceStr = Math.random().toString(36).substr(2, 15);
|
let dataInfo = {
|
jsapi_ticket: access_token,
|
timestamp: timestamp,
|
nonceStr: nonceStr,
|
url: this.body.url
|
};
|
var strintdata = this.raw(dataInfo);
|
// sha1后的签名
|
var signature = this.sha1(strintdata);
|
let data2 = {
|
appId: "wx0436f6ea0c214969",
|
timestamp: timestamp,
|
nonceStr: nonceStr,
|
signature: signature
|
};
|
this.info.data = data2;
|
this.info.result = true;
|
this.ctx.body = this.info;
|
return;
|
} catch (ex) {
|
let msg = "异常错误信息:" + ex.message;
|
this.info.result = false;
|
this.info.msg = msg;
|
this.ctx.body = this.info;
|
return;
|
}
|
}
|
//#endregion
|
|
// /**
|
// * 对参数对象进行字典排序
|
// * @param {对象} args 签名所需参数对象
|
// * @return {字符串} 排序后生成字符串
|
// */
|
public raw(args) {
|
var keys = Object.keys(args);
|
keys = keys.sort();
|
var newArgs = {};
|
keys.forEach(function (key) {
|
newArgs[key.toLowerCase()] = args[key];
|
});
|
|
var string = "";
|
for (var k in newArgs) {
|
string += "&" + k + "=" + newArgs[k];
|
}
|
string = string.substr(1);
|
return string;
|
}
|
|
// sha1加密
|
public sha1(str) {
|
let shasum = crypto.createHash("sha1");
|
shasum.update(str);
|
str = shasum.digest("hex");
|
return str;
|
}
|
}
|