Browse Source

增加修改密码接口

reloaded 5 năm trước cách đây
mục cha
commit
b2d2662d1a
5 tập tin đã thay đổi với 56 bổ sung10 xóa
  1. 0 2
      app/controller/product.js
  2. 4 3
      app/controller/user.js
  3. 1 0
      app/router.js
  4. 0 2
      app/service/product.js
  5. 51 3
      app/service/user.js

+ 0 - 2
app/controller/product.js

@@ -18,8 +18,6 @@ class ProductController extends Controller {
   }
 
   async newquery() {
-    console.log('111111111111111111111111111111111111111111111111');
-
     const res = await this.service.newquery(this.ctx.query);
     this.ctx.ok({ ...res });
   }

+ 4 - 3
app/controller/user.js

@@ -7,13 +7,14 @@ const { CrudController } = require('naf-framework-mongoose/lib/controller');
 
 // 科技超市用户管理
 class UserController extends Controller {
-
   constructor(ctx) {
     super(ctx);
     this.service = this.ctx.service.user;
   }
-
-
+  async uppasswd() {
+    const res = await this.service.uppasswd(this.ctx.request.body);
+    this.ctx.ok({ data: res });
+  }
 }
 
 module.exports = CrudController(UserController, meta);

+ 1 - 0
app/router.js

@@ -8,6 +8,7 @@ module.exports = app => {
   router.get('/', controller.home.index);
 
   // 科技超市用户表设置路由
+  router.post('user', '/api/market/user/uppasswd', controller.user.uppasswd);
   router.resources('user', '/api/market/user', controller.user); // index、create、show、destroy
   router.post('user', '/api/market/user/:id', controller.user.update);
 

+ 0 - 2
app/service/product.js

@@ -28,8 +28,6 @@ class ProductService extends CrudService {
   }
 
   async newquery({ skip, limit, ...info }) {
-    console.log('222222222222222222222222222222222222222222222222');
-
     const products = await this.model.find(info);
     const _products = await this.model.find(info).skip(Number(skip)).limit(Number(limit));
     const data = [];

+ 51 - 3
app/service/user.js

@@ -27,7 +27,13 @@ class UserService extends CrudService {
     const res = await this.model.create(newdata);
     if (res) {
       const url = this.ctx.app.config.axios.auth.baseUrl;
-      const newdata = { name, phone: data.phone, passwd: password, uid: res.id, role: data.role };
+      const newdata = {
+        name,
+        phone: data.phone,
+        passwd: password,
+        uid: res.id,
+        role: data.role,
+      };
       await this.ctx.curl(url, {
         method: 'post',
         headers: {
@@ -40,6 +46,20 @@ class UserService extends CrudService {
     return res;
   }
 
+  // 用户修改密码
+  async uppasswd(data) {
+    const { uid, newpasswd } = data;
+    assert(uid && newpasswd, '缺少部分信息项');
+    // 根据用户id查询其他用户表中是否存在相应数据
+    const user = await this.model.findById(uid, '+password');
+    // 如果用户不存在抛出异常
+    if (!user) {
+      throw new BusinessError(ErrorCode.USER_NOT_EXIST);
+    }
+    user.password = { secret: data.newpasswd };
+    await user.save();
+  }
+
   async update({ id }, data) {
     const user = await this.model.findById(id);
     const { phone } = data;
@@ -97,11 +117,39 @@ class UserService extends CrudService {
   }
 
   // 创建登录Token
-  async createJwt({ id, name, phone, is_qy, img_path, email, resume, major, office_phone, profession, role, status, columnid }) {
+  async createJwt({
+    id,
+    name,
+    phone,
+    is_qy,
+    img_path,
+    email,
+    resume,
+    major,
+    office_phone,
+    profession,
+    role,
+    status,
+    columnid,
+  }) {
     const { secret, expiresIn = '1d', issuer } = this.config.jwt;
     const subject = phone;
     // const _userid = id;
-    const res = { name, phone, is_qy, id, img_path, email, resume, major, office_phone, profession, role, status, columnid };
+    const res = {
+      name,
+      phone,
+      is_qy,
+      id,
+      img_path,
+      email,
+      resume,
+      major,
+      office_phone,
+      profession,
+      role,
+      status,
+      columnid,
+    };
     const token = await jwt.sign(res, secret, { expiresIn, issuer, subject });
     return token;
   }