wx_work.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Ellisran
  6. * @date 2020/7/6
  7. * @version
  8. */
  9. module.exports = app => {
  10. class WxWork extends app.BaseService {
  11. /**
  12. * 构造函数
  13. *
  14. * @param {Object} ctx - egg全局变量
  15. * @return {void}
  16. */
  17. constructor(ctx) {
  18. super(ctx);
  19. this.tableName = 'wx_work';
  20. }
  21. async insertCorp(permanent_code, agentid, auth_corp_info, auth_user_info) {
  22. const corpInfo = await this.getDataByCondition({ corpid: auth_corp_info.corpid });
  23. const pushData = {
  24. agentid,
  25. corpid: auth_corp_info.corpid,
  26. corp_name: auth_corp_info.corp_name,
  27. permanent_code,
  28. auth_user_info: JSON.stringify(auth_user_info),
  29. in_time: new Date(),
  30. };
  31. if (corpInfo) {
  32. pushData.id = corpInfo.id;
  33. return await this.db.update(this.tableName, pushData);
  34. }
  35. return this.db.insert(this.tableName, pushData);
  36. }
  37. async delCorp(corpid) {
  38. const transaction = await this.db.beginTransaction();
  39. try {
  40. const corpInfo = await this.getDataByCondition({ corpid });
  41. if (corpInfo) {
  42. // 删除用户已绑定企业微信信息
  43. const userList = await this.ctx.service.projectAccount.getAllDataByCondition({ where: { qywx_corpid: corpid } });
  44. if (userList.length !== 0) {
  45. const updateUserList = [];
  46. for (const u of userList) {
  47. updateUserList.push({
  48. id: u.id,
  49. qywx_corpid: null,
  50. qywx_userid: null,
  51. qywx_user_info: null,
  52. });
  53. }
  54. await transaction.updateRows(this.ctx.service.projectAccount.tableName, updateUserList);
  55. }
  56. await transaction.delete(this.tableName, { id: corpInfo.id });
  57. } else {
  58. throw '该企业已删除';
  59. }
  60. await transaction.commit();
  61. return true;
  62. } catch (err) {
  63. console.log(err);
  64. await transaction.rollback();
  65. throw err;
  66. }
  67. }
  68. }
  69. return WxWork;
  70. }