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;
  }
相关推荐
小桥风满袖2 分钟前
Three.js-硬要自学系列38之专项学习缓冲几何体
前端·css·three.js
Rubin932 分钟前
埋点方案实现
前端
斯~内克7 分钟前
Centrifugo 深度解析:构建高性能实时应用的开源引擎
前端·开源
tianchang18 分钟前
策略模式(Strategy Pattern)深入解析与实战应用
前端·javascript·代码规范
best66621 分钟前
JavaScript的Math内置对象,到底是何方神圣?
javascript
掘金安东尼27 分钟前
技术解析:高级 Excel 财务报表解析器的架构与实现
前端·javascript·面试
Hilaku28 分钟前
深入CSS层叠的本质:@layer如何优雅地解决样式覆盖与!important滥用问题
前端·css·html
天天扭码31 分钟前
AI时代,前端如何处理大模型返回的多模态数据?
前端·人工智能·面试
每天开心33 分钟前
一文教你掌握事件机制
前端·javascript·ai编程
LeeAt40 分钟前
真的!真的就一句话就能明白this指向问题
前端·javascript