灏天阁

Node/Koa2[37]: test 环境下删除用户

· Yin灏

测试数据要及时删除

删除用户

src/routes/api/user.js

//...
const { isTest } = require("../../utils/env");
const { deleteCurUser } = require("../../constroller/user");
const { loginCheck } = require("../../middlewares/loginChecks");
// 删除
router.post("/delete", loginCheck, async (ctx, next) => {
  if (isTest) {
    // 测试环境下,测试账号登录之后,删除自己
    const { userName } = ctx.session.userInfo;
    ctx.body = await deleteCurUser(userName);
  }
});

删除用户 controller

src/controller/user.js

//...
const { deleteUser } = require("../services/user");
const { deleteUserFailInfo } = require("../model/ErrorInfo");
//...
async function deleteCurUser(userName) {
  // services
  const result = await deleteUser(userName);
  if (result) {
    return new SuccessModel();
  }
  return new ErrorModel(deleteUserFailInfo);
}
module.exports = {
  //...
  deleteCurUser,
};

删除用户 services

src/services/user.js

//...
// 删除用户,数据库层
async function deleteUser(userName) {
  const result = await User.destroy({
    where: {
      userName,
    },
  });
  return result > 0;
}

module.exports = {
  //...
  deleteUser,
};

- Book Lists -