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
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
}