Prechádzať zdrojové kódy

删除用户;规格排序查询

lrf 2 rokov pred
rodič
commit
57fb29187c

+ 3 - 0
app/controller/user/config/.admin.js

@@ -47,4 +47,7 @@ module.exports = {
   emailResetPwd: {
     requestBody: ['!account'],
   },
+  toMakeKey: {
+    requestBody: ['!password', '!target'],
+  },
 };

+ 1 - 1
app/controller/user/config/.user.js

@@ -3,7 +3,7 @@ module.exports = {
     requestBody: ['name', 'phone', 'password', 'icon', 'birth', 'gender', 'email', 'openid', 'status'],
   },
   destroy: {
-    params: ['!id'],
+    requestBody: ['!key', '!target'],
     service: 'delete',
   },
   update: {

+ 9 - 1
app/middleware/errorEmail.js

@@ -9,14 +9,21 @@ module.exports = options => {
         errmsg = '服务发生错误',
         details;
       const emailData = {};
-      if (_.isNumber(e.errcode)) {
+      const ekeys = Object.keys(e);
+      if (ekeys.includes('errcode')) {
+        // 人为抛出异常
         errcode = _.get(e, 'errcode');
         errmsg = _.get(e, 'errmsg');
         details = _.get(e, 'details');
         emailData.errmsg = errmsg;
         emailData.details = details;
       } else {
+        // 由assert/参数判断抛出的
+        errmsg = e.message;
         emailData.errmsg = e.stack;
+        if (process.env.NODE_ENV === 'development') {
+          details = e.stack;
+        }
       }
       emailData.errcode = errcode;
       const request = ctx.request;
@@ -24,6 +31,7 @@ module.exports = options => {
       let body = _.get(request, 'body');
       if (body) body = JSON.stringify(body, null, 1);
       emailData.errmsg = `${url}\n${body}\n${emailData.errmsg}`;
+      // console.log(emailData.errmsg);
       // 未定义的错误需要发邮件,定义的错误不需要
       if (!_.isNumber(e.errcode)) {
         // 非开发模式发送邮件

+ 0 - 1
app/service/shop/goodsSpec.js

@@ -12,7 +12,6 @@ class GoodsSpecService extends CrudService {
   }
   async query(filter, { skip = 0, limit, sort, projection } = {}) {
     const { sort: fs } = filter;
-    console.log(fs);
     if (fs) {
       sort.sort = parseInt(fs);
       delete filter.sort;

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

@@ -75,8 +75,25 @@ class AdminService extends CrudService {
     } finally {
       this.tran.clean();
     }
+  }
 
+  /**
+   * 检验当前管理员密码,生成操作key
+   * @param {Object} body 参数体
+   * @param {String} body.password 管理员密码
+   * @param {String} body.target 目标key
+   */
+  async toMakeKey({ password, target }) {
+    const admin = _.get(this.ctx, 'admin');
+    const _id = _.get(admin, '_id');
+    const taData = await this.model.findById(_id, '+password');
+    if (!taData) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到当前操作管理人');
+    const { password: upwd } = taData;
+    if (password !== upwd.secret) throw new BusinessError(ErrorCode.BAD_PASSWORD);
+    const key = this.ctx.service.util.user.getDeleteUserKey(_id, target);
+    return key;
   }
+
 }
 
 module.exports = AdminService;

+ 15 - 0
app/service/user/user.js

@@ -166,6 +166,21 @@ class UserService extends CrudService {
     const token = this.ctx.service.util.jwt.encrypt(user);
     return token;
   }
+
+  /**
+   * 删除用户
+   * @param {Object} param 地址参数
+   * @param {String} param.key 就是key
+   * @param {String} param.target 操作对象
+   */
+  async delete({ key, target }) {
+    const { opera_id, target: rt } = await this.ctx.service.util.user.getKeyData(key);
+    const admin = _.get(this.ctx, 'admin');
+    const _id = _.get(admin, '_id');
+    if (opera_id !== _id) throw new BusinessError(ErrorCode.DATA_INVALID, '不是同一个操作人,操作无效');
+    if (target !== rt) throw new BusinessError(ErrorCode.DATA_INVALID, '操作对象不是同一个数据,操作无效');
+    await this.model.deleteOne({ _id: target });
+  }
 }
 
 module.exports = UserService;

+ 41 - 0
app/service/util/user.js

@@ -0,0 +1,41 @@
+'use strict';
+const { CrudService } = require('naf-framework-mongoose-free/lib/service');
+const { BusinessError, ErrorCode } = require('naf-core').Error;
+const _ = require('lodash');
+const assert = require('assert');
+
+//
+class UserService extends CrudService {
+  constructor(ctx) {
+    super(ctx, 'user');
+    this.redis = this.app.redis;
+    this.deleteUserKey = 'deleteUser:';
+    this.redisTimeout = this.app.config.redisTimeout;
+  }
+  /**
+   * 生成操作key
+   * @param {String} opera_id 操作人id
+   * @param {String} target 操作对象数据
+   */
+  async getDeleteUserKey(opera_id, target) {
+    // 生成key
+    const key = `${this.deleteUserKey}${opera_id}`;
+    // 存储该key可操作的人
+    await this.redis.set(key, target, 'EX', this.redisTimeout);
+    // 返回key
+    return key;
+  }
+  /**
+   * 解析key数据
+   * @param {Object} key 操作key
+   */
+  async getKeyData(key) {
+    const target = await this.redis.get(key);
+    if (target) await this.redis.del(key);
+    const arr = key.split(this.deleteUserKey);
+    const opera_id = _.last(arr);
+    return { opera_id, target };
+  }
+}
+
+module.exports = UserService;

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

@@ -7,6 +7,7 @@ const rkey = 'admin';
 const ckey = 'user.admin';
 const keyZh = '管理员';
 const routes = [
+  { method: 'post', path: `${rkey}/toMakeKey`, controller: `${ckey}.toMakeKey`, name: `${ckey}toMakeKey`, zh: `${keyZh}-生成管理员操作key` },
   { 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}登陆` },

+ 1 - 1
app/z_router/user/user.js

@@ -21,7 +21,7 @@ const routes = [
   { 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: 'delete', path: `${rkey}/:id`, controller: `${ckey}.destroy`, name: `${ckey}Delete`, zh: `删除${keyZh}` },
+  { method: 'delete', path: `${rkey}`, controller: `${ckey}.destroy`, name: `${ckey}Delete`, zh: `删除${keyZh}` },
 ];
 
 module.exports = app => {