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;
  }
相关推荐
LEE6 分钟前
前端转型全栈 03:接口失败也返回 200,OpenAPI 契约与错误码怎么定
前端·后端·全栈
我命由我1234533 分钟前
CSS - CSS 媒体查询 orientation
前端·javascript·css·html·css3·html5·js
lichenyang4531 小时前
让 VK 小程序调用 HarmonyOS 原生能力:一次跨端 Bridge SDK 的设计与实践
前端
阳光宅男@李光熠1 小时前
【电子通识】一起学习TDK的EMC基础——电池兼容设计方法概述
java·前端·数据库
南雨北斗2 小时前
vue3项目中的env.d.ts文件
前端
烈风逍遥2 小时前
第五篇:通用 LLM 流式对话:前后端联接的完整实现
前端·后端·架构
aixingpan2 小时前
aixingpan.cn API开发文档:api_docs_trichart_natal_progression_sec_transit2接口指南
前端·php
flash俊杰2 小时前
Electron 打包后窗口 30 秒不出现:一个 ABI 不匹配的血案
electron·node.js
江华森2 小时前
Web 安全实战:验证机制、会话管理、SQL 注入、XSS 与 CSRF
前端
江华森2 小时前
HTTPS 实战:TLS 握手成本、自制 CA 与证书、三大常见故障
前端