cool node.js 后端接口实现账号密码登录和注册

1.实现H5 的账号密码 登录和注册功能

2.登录和注册代码

controller

复制代码
@Post('/h5Login', { summary: 'H5登录' })
  async LoginByH5(@Body() body) {
    const res: any = await this.businessLoginService.H5Login(body);
    return this.ok(res);
  }

  @Post('/h5Register', { summary: 'H5注册' })
  async RegisterByH5(@Body() body) {
    const res: any = await this.businessLoginService.H5Register(body);
    return this.ok(res);
  }

service

复制代码
 // H5 登录

  async H5Login(login) {
    if (!login.password || !login.phone) {
      throw new CoolCommException('参数不能为空~');
    }
    const user = await this.businessStudentEntity.findOneBy({
      phone: login?.phone,
      password: md5(login?.password),
    });
    if (!user) {
      // if (user.password !== md5(login.password)) {
      //   // 校验用户状态及密码
      //   throw new CoolCommException('账户或密码不正确~');
      // }
      // if (!user.phone) {
      //   throw new CoolCommException('账户不存在~');
      // }
      throw new CoolCommException('账户或密码不正确~');
    } else {
      // 生成token
      const { expire, refreshExpire } = this.coolConfig.jwt.token;
      const result = {
        expire,
        token: await this.generateToken(user, expire),
        refreshExpire,
        refreshToken: await this.generateToken(user, refreshExpire, true),
      };

      // 将用户相关信息保存到缓存
      await this.cacheManager.set(`business:token:${user.id}`, result.token, {
        ttl: expire,
      });
      await this.cacheManager.set(
        `business:token:refresh:${user.id}`,
        result.token,
        { ttl: refreshExpire }
      );

      return { token: result.token, user };
    }
  }

  //H5注册
  async H5Register(register) {
    if (
      !register.phone ||
      !register.password ||
      !register.confirmPassword ||
      !register.yzmCode ||
      !register.registerCode
    ) {
      throw new CoolCommException('参数不能为空~');
    }
    const phoneRegex = /^1[3-9]\d{9}$/; // 手机号码的正则表达式

    if (!phoneRegex.test(register.phone)) {
      throw new CoolCommException('手机号码格式不正确~');
    }
    if (register.password !== register.confirmPassword) {
      throw new CoolCommException('两次密码不一致');
    }
    const inviteCode = await this.businessUserEntity.findOneBy({
      inviteCode: register.registerCode,
    });
    if (!inviteCode) {
      throw new CoolCommException('导师不存在~');
    }
    const user = await this.businessStudentEntity.findOneBy({
      phone: register?.phone,
    });
    if (user) {
      throw new CoolCommException('账户已存在~');
    }

    await this.businessStudentEntity.save({
      phone: register.phone,
      password: md5(register.password),
      membershipLevel: 0,
      balance: 0,
      userId: inviteCode.id,
      nickName: `蹲票用户${register.phone}`,
    });
    return 1;
  }
相关推荐
朴shu7 小时前
Delta数据结构:深入剖析高效数据同步的奥秘
javascript·算法·架构
该用户已不存在7 小时前
Vibe Coding 入门指南:从想法到产品的完整路径
前端·人工智能·后端
Pedro7 小时前
Flutter - 日志不再裸奔:pd_log 让打印有型、写盘有序
前端·flutter
申阳7 小时前
Day 3:01. 基于Nuxt开发个人呢博客项目-初始化项目
前端·后端·程序员
三小河7 小时前
解决 React + SSE 流式输出卡顿:Nginx 关键配置实战
前端·架构·前端框架
玖月晴空7 小时前
Uniapp 速查文档
前端·微信小程序·uni-app
琉-璃7 小时前
vue3+ts 任意组件间的通信 mitt的使用
前端·javascript·vue.js
FogLetter8 小时前
React Fiber 机制:让渲染变得“有礼貌”的魔法
前端·react.js
不想说话的麋鹿8 小时前
「项目前言」从配置程序员到动手造轮子:我用Vue3+NestJS复刻低代码平台的初衷
前端·程序员·全栈
JunpengHu8 小时前
esri-leaflet介绍
前端