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;
  }
相关推荐
前端摸鱼匠1 小时前
Vue 3 的v-bind合并行为:讲解v-bind与普通属性合并的规则
前端·javascript·vue.js·前端框架·ecmascript
REDcker2 小时前
浏览器端Web程序性能分析与优化实战 DevTools指标与工程清单
开发语言·前端·javascript·vue·ecmascript·php·js
donecoding3 小时前
一个 sudo 引发的血案:npm 全局包权限错乱彻底修复
前端·node.js·前端工程化
风骏时光牛马3 小时前
Raku正则匹配与数据批量处理实操案例
前端
nbwenren3 小时前
2026实测:Gemini 3 镜像站视觉能力实践——拍照原型图,一键生成 HTML+CSS 代码
前端·css·html
Lee川3 小时前
Prisma 实战指南:像搭积木一样设计古诗词数据库
前端·数据库·后端
Linsk3 小时前
Java和JavaScript的关系真是雷峰和雷峰塔的关系吗?
java·javascript·oracle
当时只道寻常3 小时前
浏览器文本复制到剪贴板:企业级最佳实践
javascript
jinanwuhuaguo4 小时前
(第二十九篇)OpenClaw 实时与具身的跃迁——从异步孤岛到数字世界的“原住民”
前端·网络·人工智能·重构·openclaw