notice_again.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. 'use strict';
  2. /**
  3. * 审批信息重新发送modal
  4. *
  5. * @author EllisRan
  6. * @date 2024/01/18
  7. * @version
  8. */
  9. const smsTypeConst = require('../const/sms_type');
  10. module.exports = app => {
  11. class NoticeAgain extends app.BaseService {
  12. /**
  13. * 构造函数
  14. *
  15. * @param {Object} ctx - egg全局变量
  16. * @return {void}
  17. */
  18. constructor(ctx) {
  19. super(ctx);
  20. this.tableName = 'notice_again';
  21. }
  22. // 入库并判断
  23. async addNoticeAgain(transaction, type, datas) {
  24. // 判断用户是否已绑定公众号或者企业微信, 并判断用户是否开启了重新发送并且钩上对应的审批
  25. const userInfo = await this.ctx.service.projectAccount.getDataById(datas.uid);
  26. if (!userInfo.wx_type || userInfo.wx_type === '') return;
  27. const wxType = JSON.parse(userInfo.wx_type);
  28. // 没开启发送功能不应该生成重新发送入库数据
  29. if (wxType[type] && wxType[type].indexOf(smsTypeConst.judge.approval.toString()) !== -1) {
  30. datas.status = 0;
  31. const notice_again = userInfo.notice_again ? JSON.parse(userInfo.notice_again) : null;
  32. if ((userInfo.wx_openid || userInfo.qywx_userid) && notice_again && notice_again.checked && notice_again.sp[datas.sp_type]) {
  33. datas.status = 1;
  34. }
  35. datas.in_time = new Date();
  36. datas.last_time = this.ctx.helper.dateTran(new Date(), 'YYYY-MM-DD HH:mm');
  37. datas.times = 0;
  38. datas.wx_data = JSON.stringify(datas.wx_data);
  39. datas.sms_type = type;
  40. datas.origin_url = JSON.stringify({
  41. host: this.ctx.host,
  42. protocol: 'https', // 改为固定防止发送有问题
  43. });
  44. await transaction.insert(this.tableName, datas);
  45. }
  46. }
  47. async stopNoticeAgain(transaction, table_name, sp_id, status = 2) {
  48. // 判断sp_id是字符还是数组
  49. // if (sp_id instanceof Array) {
  50. // for (const id of sp_id) {
  51. // await transaction.update(this.tableName, { status }, { where: { table_name, sp_id: id } });
  52. // }
  53. // } else {
  54. await transaction.update(this.tableName, { status }, { where: { table_name, sp_id } });
  55. // }
  56. }
  57. // 针对撤回删除对应的重新发送数据
  58. async deleteNoticeAgain(transaction, table_name, sp_id) {
  59. // 判断sp_id是字符还是数组
  60. await transaction.delete(this.tableName, { table_name, sp_id });
  61. }
  62. async getSpResult(table_name, sp_id) {
  63. const sql = 'SELECT * FROM ?? WHERE id = ?';
  64. const sqlParam = [table_name, sp_id];
  65. return await this.db.queryOne(sql, sqlParam);
  66. }
  67. async updateUserNoticeAgain(transaction, uid, notice_again) {
  68. const list = await transaction.select(this.tableName, { where: { uid } });
  69. const uncheckedList = this._.filter(list, function(item) {
  70. return item.status !== 2;
  71. });
  72. if (uncheckedList.length > 0) {
  73. if (!notice_again.checked) { // 关闭时所有数据暂停为0
  74. const updateData = uncheckedList.map(x => { return { id: x.id, status: 0 }; });
  75. await transaction.updateRows(this.tableName, updateData);
  76. } else {
  77. const updateData = [];
  78. // 分批次查看并是否插入
  79. for (const sp in notice_again.sp) {
  80. const splist = this._.filter(uncheckedList, function(item) {
  81. return item.sp_type === sp;
  82. });
  83. if (splist.length > 0) {
  84. const spUpData = splist.map(x => { return { id: x.id, status: notice_again.sp[sp] ? 1 : 0 }; });
  85. updateData.push(...spUpData);
  86. }
  87. }
  88. if (updateData.length > 0) {
  89. await transaction.updateRows(this.tableName, updateData);
  90. }
  91. }
  92. }
  93. }
  94. }
  95. return NoticeAgain;
  96. };