付智勇

删除用户

@@ -103,4 +103,17 @@ userController.prototype.updateUserByUserId = async(ctx, next) =>{ @@ -103,4 +103,17 @@ userController.prototype.updateUserByUserId = async(ctx, next) =>{
103 } 103 }
104 } 104 }
105 105
  106 +userController.prototype.delUserByUserId = async(ctx,next) => {
  107 + const userId = ctx.params.userId;
  108 + if(!userId){
  109 + return status.paramError('userId');
  110 + }
  111 + try {
  112 + let deluser = userService.delUserByUserId(userId);
  113 + return deluser
  114 + } catch (error) {
  115 + throw new Error(error)
  116 + }
  117 +}
  118 +
106 module.exports = new userController(); 119 module.exports = new userController();
@@ -41,6 +41,16 @@ router.put('/updateUserByUserId/:userId',async(ctx, next)=>{ @@ -41,6 +41,16 @@ router.put('/updateUserByUserId/:userId',async(ctx, next)=>{
41 } 41 }
42 }) 42 })
43 43
  44 +router.delete('/delUserByUserId/:userId',async(ctx, next)=>{
  45 + try{
  46 + let delData = await userContoller.delUserByUserId(ctx, next);
  47 + status.successTemp(ctx,200,delData);
  48 + }catch(e){
  49 + console.log(e)
  50 + status.catchError(ctx,400,e.message);
  51 + }
  52 +})
  53 +
44 54
45 55
46 56
@@ -82,5 +82,23 @@ userService.prototype.updateUserByUserId = async(userId,data)=>{ @@ -82,5 +82,23 @@ userService.prototype.updateUserByUserId = async(userId,data)=>{
82 } 82 }
83 } 83 }
84 84
  85 +userService.prototype.delUserByUserId = async(userId) => {
  86 + let t = await sequelize.transaction({ autocommit: true })
  87 + try {
  88 + let User = await userModel.find({where:{id:userId}},{transaction: t});
  89 + if(!User){
  90 + t.rollback()
  91 + return {msg:'用户不存在'}
  92 + }
  93 + let delUser = await userModel.destroy({where:{id:userId}},{transaction: t});
  94 + await t.commit();
  95 + return delUser
  96 + } catch (error) {
  97 + t.rollback();
  98 + throw new Error(error);
  99 + }
  100 +
  101 +}
  102 +
85 103
86 module.exports = new userService(); 104 module.exports = new userService();
@@ -31,7 +31,7 @@ module.exports = { @@ -31,7 +31,7 @@ module.exports = {
31 successTemp: (ctx,code,data)=>{ 31 successTemp: (ctx,code,data)=>{
32 ctx.response.status= 200; 32 ctx.response.status= 200;
33 ctx.response.body= { 33 ctx.response.body= {
34 - code:data.code ? data.code:200, 34 + code:!data.code ? 200:data.code,
35 returnData:{ 35 returnData:{
36 data:data 36 data:data
37 } 37 }