stage_shoufang.js 3.4 KB

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