333
schangxiang@126.com
2025-09-19 18966e02fb573c7e2bb0c6426ed792b38b910940
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
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;
  }
}