stage_shoufang.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. 'use strict';
  2. /**
  3. * 收方单-数据模型
  4. *
  5. * @author ellisran
  6. * @date 2021/09/23
  7. * @version
  8. */
  9. const accountGroup = require('../const/account_group').group;
  10. const fs = require('fs');
  11. const path = require('path');
  12. const qr = require('qr-image');
  13. const sendToWormhole = require('stream-wormhole');
  14. const auditConst = require('../const/audit').stage;
  15. module.exports = app => {
  16. class stageShoufang extends app.BaseService {
  17. constructor(ctx) {
  18. super(ctx);
  19. this.tableName = 'stage_shoufang';
  20. }
  21. async add(datas) {
  22. const transaction = await this.db.beginTransaction();
  23. try {
  24. const data = {
  25. tid: this.ctx.tender.id,
  26. order: this.ctx.stage.order,
  27. sid: this.ctx.stage.id,
  28. lid: datas.lid,
  29. pid: datas.pid ? datas.pid : null,
  30. extra_upload: this.ctx.stage.status === auditConst.status.checked ? 1 : 0,
  31. create_time: new Date(),
  32. };
  33. const result = await transaction.insert(this.tableName, data);
  34. // 生成二维码并保存
  35. const size = 5;
  36. const margin = 1;
  37. const text = this.ctx.protocol + '://' + this.ctx.host + '/wap/shoufang/upload' +
  38. '?tid=' + this.ctx.tender.id + '&order=' + this.ctx.stage.order + '&sfid=' + result.insertId;
  39. // 大小默认5,二维码周围间距默认1
  40. const qr_png = await qr.image(text || '', { type: 'png', size: size || 5, margin: margin || 1 });
  41. const qrcodePath = '/public/upload/' + this.ctx.tender.id + '/stage/shoufang/qrcode/' + result.insertId + '.png';
  42. await this.ctx.helper.saveStreamFile(qr_png, path.resolve(this.app.baseDir, 'app/' + qrcodePath));
  43. await transaction.update(this.tableName, { id: result.insertId, qrcode: qrcodePath });
  44. await transaction.commit();
  45. return result;
  46. } catch (err) {
  47. await transaction.rollback();
  48. throw err;
  49. }
  50. }
  51. async del(sfid) {
  52. const transaction = await this.db.beginTransaction();
  53. try {
  54. // 删除附件再删除收方单
  55. const sfInfo = await this.getDataById(sfid);
  56. const sfAttList = await this.ctx.service.stageShoufangAtt.getAllDataByCondition({ where: { sfid } });
  57. if (sfAttList) {
  58. if (sfAttList.length !== 0) {
  59. for (const att of sfAttList) {
  60. if (fs.existsSync(path.join(this.app.baseDir, att.filepath))) {
  61. await fs.unlinkSync(path.join(this.app.baseDir, att.filepath));
  62. }
  63. }
  64. }
  65. }
  66. // 删除二维码
  67. if (fs.existsSync(path.join(this.app.baseDir, 'app/' + sfInfo.qrcode))) {
  68. await fs.unlinkSync(path.join(this.app.baseDir, 'app/' + sfInfo.qrcode));
  69. }
  70. await transaction.delete(this.ctx.service.stageShoufangAtt.tableName, { sfid });
  71. await transaction.delete(this.tableName, { id: sfid });
  72. await transaction.commit();
  73. return true;
  74. } catch (err) {
  75. await transaction.rollback();
  76. throw err;
  77. }
  78. }
  79. }
  80. return stageShoufang;
  81. };