Skip to content

Nest 修改用户信息

修改用户信息

  • user
ts
import { Controller, Post, Req } from "@nestjs/common";

import { UserUpdateService } from "../services/user.update.service";

@Controller("user")
export class UserUpdateController {
  constructor(private readonly userUpdateService: UserUpdateService) {}
  // 个人信息修改
  @Post("update")
  async upadte(@Req() req: any) {
    console.log(req.body);
    console.log(req.userId);
    const result = await this.userUpdateService.updateUser(
      req.body,
      req.userId
    );
    if (result) {
      return {
        message: "修改成功",
      };
    } else {
      return {
        message: "修改失败",
      };
    }
  }

  // 修改密码
  @Post("updatePassword")
  async updatePassword(@Req() req: any) {
    console.log(req.body);
    console.log(req.userId);
    const result = await this.userUpdateService.updatePassword(
      req.body,
      req.userId
    );
    if (result) {
      return {
        message: "修改成功",
      };
    } else {
      return {
        message: "修改失败",
      };
    }
  }
}
  • services
ts
import { Inject, Injectable } from "@nestjs/common";
// 引入数据库模块
import { PrismadbService } from "../../prisma/prisma.service";
// 引入redis模块
import { RedisService } from "../../redis/redis.service";
// 引入异常模块
import { GlobalCheckException } from "../../../common/globalcheck.exception";
// 引入md5
// 引入加密模块
import * as crypto from "crypto";

import { UpdateUserDto } from "../dto/update-user.dto";

export class UserUpdateService {
  @Inject(RedisService)
  private readonly redisService: RedisService;

  @Inject(PrismadbService)
  private readonly prismaService: PrismadbService;

  // 密码加密
  async passwordmd5(str) {
    const hash = crypto.createHash("md5");
    hash.update(str);
    return hash.digest("hex");
  }

  // 更新用户信息
  async updateUser(body: any, userId: number) {
    // 检查用户存在不存在
    const user = await this.prismaService.users.findFirst({
      where: {
        id: userId,
      },
    });
    if (user) {
      const updateUser = new UpdateUserDto();
      updateUser.head_pic = body.head_pic;
      updateUser.nick_name = body.nick_name;
      updateUser.is_admin = body.is_admin;
      updateUser.is_frozen = body.is_frozen;
      updateUser.phone_number = body.phone_number;
      const result = await this.prismaService.users.update({
        where: {
          id: userId,
        },
        data: updateUser,
      });
      if (result) {
        return true;
      } else {
        return false;
      }
    } else {
      throw new GlobalCheckException("用户不存在");
    }
  }

  // 更新密码
  async updatePassword(body: any, userId: number) {
    // 检查用户存在不存在
    const user = await this.prismaService.users.findFirst({
      where: {
        id: userId,
      },
    });
    if (user) {
      // 更新密码
      const result = await this.prismaService.users.update({
        where: {
          id: userId,
        },
        data: {
          password: await this.passwordmd5(body.password),
        },
      });
      return true;
    } else {
      throw new GlobalCheckException("用户不存在");
    }
  }
}