12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- 'use strict';
- /**
- *
- *
- * @author Ellisran
- * @date 2020/7/6
- * @version
- */
- module.exports = app => {
- class WxWork extends app.BaseService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- this.tableName = 'wx_work';
- }
- async insertCorp(permanent_code, agentid, auth_corp_info, auth_user_info) {
- const corpInfo = await this.getDataByCondition({ corpid: auth_corp_info.corpid });
- const pushData = {
- agentid,
- corpid: auth_corp_info.corpid,
- corp_name: auth_corp_info.corp_name,
- permanent_code,
- auth_user_info: JSON.stringify(auth_user_info),
- in_time: new Date(),
- };
- if (corpInfo) {
- pushData.id = corpInfo.id;
- return await this.db.update(this.tableName, pushData);
- }
- return this.db.insert(this.tableName, pushData);
- }
- async delCorp(corpid) {
- const transaction = await this.db.beginTransaction();
- try {
- const corpInfo = await this.getDataByCondition({ corpid });
- if (corpInfo) {
- // 删除用户已绑定企业微信信息
- const userList = await this.ctx.service.projectAccount.getAllDataByCondition({ where: { qywx_corpid: corpid } });
- if (userList.length !== 0) {
- const updateUserList = [];
- for (const u of userList) {
- updateUserList.push({
- id: u.id,
- qywx_corpid: null,
- qywx_userid: null,
- qywx_user_info: null,
- });
- }
- await transaction.updateRows(this.ctx.service.projectAccount.tableName, updateUserList);
- }
- await transaction.delete(this.tableName, { id: corpInfo.id });
- } else {
- throw '该企业已删除';
- }
- await transaction.commit();
- return true;
- } catch (err) {
- console.log(err);
- await transaction.rollback();
- throw err;
- }
- }
- }
- return WxWork;
- }
|