lrf 2 gadi atpakaļ
vecāks
revīzija
8b09af468f

+ 5 - 2
app/controller/user/config/.admin.js

@@ -1,6 +1,6 @@
 module.exports = {
   create: {
-    requestBody: ['account', 'password', 'name', 'role', 'shop'],
+    requestBody: ['email', 'account', 'password', 'name', 'role', 'shop'],
   },
   destroy: {
     params: ['!id'],
@@ -8,7 +8,7 @@ module.exports = {
   },
   update: {
     params: ['!id'],
-    requestBody: ['account', 'password', 'name', 'role', 'shop'],
+    requestBody: ['email', 'account', 'password', 'name', 'role', 'shop'],
   },
   show: {
     parameters: {
@@ -44,4 +44,7 @@ module.exports = {
     params: ['!id'],
     requestBody: ['!password'],
   },
+  emailResetPwd: {
+    requestBody: ['!account'],
+  },
 };

+ 1 - 0
app/model/user/admin.js

@@ -11,6 +11,7 @@ const admin = {
   role: { type: String, zh: '角色', ref: 'Dev.Role' },
   shop: { type: String, zh: '店铺', ref: 'Shop.Shop' },
   name: { type: String, required: false, zh: '名称' }, //
+  email: { type: String, required: false, zh: '邮箱' }, //
 };
 const schema = new Schema(admin, { toJSON: { getters: true, virtuals: true } });
 schema.index({ id: 1 });

+ 26 - 0
app/service/user/admin.js

@@ -3,12 +3,14 @@ const { CrudService } = require('naf-framework-mongoose-free/lib/service');
 const { BusinessError, ErrorCode } = require('naf-core').Error;
 const _ = require('lodash');
 const assert = require('assert');
+const Transaction = require('mongoose-transactions');
 
 //
 class AdminService extends CrudService {
   constructor(ctx) {
     super(ctx, 'admin');
     this.model = this.ctx.model.User.Admin;
+    this.tran = new Transaction();
   }
 
   async beforeCreate(data) {
@@ -51,6 +53,30 @@ class AdminService extends CrudService {
     data.password = { secret: password };
     await data.save();
   }
+  /**
+   * 邮箱重置账号密码
+   * @param {Object} body 参数体
+   * @param body.account 要重置的账号
+   */
+  async emailResetPwd({ account }) {
+    const admin = await this.model.findOne({ account });
+    if (!admin) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到用户');
+    const { email } = admin;
+    if (!email) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到账户的邮箱');
+    // 重置密码+发邮件
+    const secret = this.ctx.service.util.trade.createNonceStr();
+    try {
+      this.tran.update('Admin', admin._id, { password: { secret } });
+      await this.ctx.service.util.email.resetPwd(email, secret);
+      await this.tran.run();
+    } catch (error) {
+      this.tran.rollback();
+      throw new Error(error);
+    } finally {
+      this.tran.clean();
+    }
+
+  }
 }
 
 module.exports = AdminService;

+ 6 - 0
app/service/util/email.js

@@ -13,6 +13,12 @@ class EmailService extends CrudService {
     this.emailServiceConfig = _.get(this.app, 'config.emailConfig.config');
   }
 
+  async resetPwd(email, password) {
+    const data = { config: this.emailServiceConfig, template: 'resetPwd', receiver: email, params: { password } };
+    const url = `${this.emailServiceUrl}/sendEmail`;
+    await this.httpUtil.cpost(url, data);
+  }
+
   async errorEmail(error) {
     const data = { config: this.emailServiceConfig, params: error };
     const url = `${this.emailServiceUrl}/error`;

+ 4 - 3
app/z_router/user/admin.js

@@ -7,12 +7,13 @@ const rkey = 'admin';
 const ckey = 'user.admin';
 const keyZh = '管理员';
 const routes = [
-  { method: 'post', path: `${rkey}/resetPwd/:id`, controller: `${ckey}.resetPwd`, name: `${ckey}ResetPwd`, zh: `重置密码${keyZh}` },
+  { method: 'post', path: `${rkey}/emailResetPwd`, controller: `${ckey}.emailResetPwd`, name: `${ckey}emailResetPwd`, zh: `${keyZh}-随机重置密码并发送邮件` },
+  { method: 'post', path: `${rkey}/resetPwd/:id`, controller: `${ckey}.resetPwd`, name: `${ckey}ResetPwd`, zh: `修改密码${keyZh}` },
   { method: 'post', path: `${rkey}/login`, controller: `${ckey}.login`, name: `${ckey}Login`, zh: `${keyZh}登陆` },
   { method: 'get', path: `${rkey}`, controller: `${ckey}.index`, name: `${ckey}Query`, zh: `${keyZh}列表查询` },
   { method: 'get', path: `${rkey}/:id`, controller: `${ckey}.show`, name: `${ckey}Show`, zh: `${keyZh}查询` },
-  { method: 'post', path: `${rkey}`, controller: `${ckey}.create`, middleware: [ 'password' ], name: `${ckey}Create`, zh: `创建${keyZh}` },
-  { method: 'post', path: `${rkey}/:id`, controller: `${ckey}.update`, middleware: [ 'password' ], name: `${ckey}Update`, zh: `修改${keyZh}` },
+  { method: 'post', path: `${rkey}`, controller: `${ckey}.create`, middleware: ['password'], name: `${ckey}Create`, zh: `创建${keyZh}` },
+  { method: 'post', path: `${rkey}/:id`, controller: `${ckey}.update`, middleware: ['password'], name: `${ckey}Update`, zh: `修改${keyZh}` },
   { method: 'delete', path: `${rkey}/:id`, controller: `${ckey}.destroy`, name: `${ckey}Delete`, zh: `删除${keyZh}` },
 ];