Pārlūkot izejas kodu

feat: 计量前台修改账号同步至项目管理

lanjianrong 3 gadi atpakaļ
vecāks
revīzija
765e9e2fb0
1 mainītis faili ar 36 papildinājumiem un 0 dzēšanām
  1. 36 0
      app/service/project_account.js

+ 36 - 0
app/service/project_account.js

@@ -583,6 +583,10 @@ module.exports = app => {
                 if (accountData.account === undefined) {
                     throw '不存在对应账号';
                 }
+                const projectData = await this.ctx.service.project.getProjectById(accountData.project_id);
+                if (!projectData) {
+                    throw '不存在对应项目';
+                }
                 // 加密密码
                 const encryptPassword = account ? crypto.createHmac('sha1', account).update(password)
                     .digest().toString('base64') : crypto.createHmac('sha1', accountData.account).update(password)
@@ -615,6 +619,11 @@ module.exports = app => {
                     }, SmsAliConst.template.mmcz);
                 }
 
+                // 判断是否更改了账号
+                if (accountData.account !== account) {
+                    await this.syncAccount(projectData.code, accountData.account, account);
+                }
+
                 this.transaction.commit();
             } catch (error) {
                 this.transaction.rollback();
@@ -823,6 +832,33 @@ module.exports = app => {
             const sql = 'Select `account`, `name`, `company`, `role`, `mobile`, `telephone`, `is_admin` as `isAdmin`, `account_group` as `accountGroup` From ' + this.tableName + ' where project_id = ?';
             return await this.db.query(sql, [project_id]);
         }
+
+        /**
+         * 同步修改项目管理的账号
+         * @param {String} code - 项目编号
+         * @param {String} account - 旧账号
+         * @param {String} newAccount - 新账号
+         * @return {Promise} -
+         */
+        async syncAccount(code, account, newAccount) {
+            return new Promise(resolve => {
+                this.ctx.curl(`${app.config.managementProxyPath}/api/external/jl/account/update`, {
+                    method: 'POST',
+                    data: {
+                        token: this.ctx.helper.createJWT({ code, account, newAccount }),
+                    },
+                }).then(({ status, data }) => {
+                    if (status === 200) {
+                        const result = JSON.parse(data.toString());
+                        if (!result || result.code !== 0) {
+                            return resolve();
+                        }
+                        return resolve();
+                    }
+                    return resolve();
+                });
+            });
+        }
     }
 
     return ProjectAccount;