news 2026/8/4 17:16:53

飞滴网约车项目Day01

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
飞滴网约车项目Day01

今日完成

乘客用户中心服务

验证码发送

首先我们需要开发用户注册和登录功能,其中重点是乘客发送验证码,以下是发送验证码的时序图

REST ful 风格

在设计rest fuk 风格的时候,我们需要考虑以下的事情:

  1. 协议(http,https)
  2. 域名 (www.xxx.com/aoi/courses,api.xxx.com/courses)
  3. 路径
  4. 版本(v1,v2等)
  5. 动作(post,put,patch, delete, get)

nacos下载和配置

下载地址:https://github.com/alibaba/nacos/releases/tag/2.0.3

添加maven依赖

<!-- nacos--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> <version>2021.1</version> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <version>2021.1</version> </dependency>
# 启动Nacos Server: 定位到bin目录,然后执行启动命令: ## cmd窗口 startup.cmd -m standalone ## powershell窗口 .\startup.cmd -m standalone 这将以单机模式启动Nacos服务器。

获取随机验证码

@RestController public class NumberCodeController { @GetMapping("/numberCode/{size}") public String numberCode(@PathVariable("size") int size) { System.out.println("生成验证码,验证码长度:" + size); JSONObject result = new JSONObject(); result.put("code", 1); result.put("message", "success"); JSONObject data = new JSONObject(); //获取随机数 String number = "0123456789"; StringBuilder numberCode = new StringBuilder(); for (int i = 0; i < size; i++) { int index = (int) (Math.random() * number.length()); char c = number.charAt(index); numberCode.append(c); } System.out.println(numberCode.toString()); data.put("numberCode", numberCode); result.put("data", data); return result.toString(); } }

统一返回类型

public enum CommonStatusEnum { SUCCESS(1, "success"), FAIL(0, "fail"); @Getter private int code; @Getter private String value; private CommonStatusEnum(int code, String value) { this.code = code; this.value = value; } }
@Data @Accessors(chain = true) public class ResponseResult<T> { private int code; private String message; private T data; /** * 成功返回结果 */ public static <T> ResponseResult<T> success(T data) { return new ResponseResult<T>().setCode(CommonStatusEnum.SUCCESS.getCode()) .setMessage(CommonStatusEnum.SUCCESS.getValue()) .setData(data); } /** * 失败返回结果 */ public static <T> ResponseResult<T> fail(int code , String message) { return new ResponseResult<T>().setCode(code) .setMessage(message); } /** * 失败返回结果,自定义失败 */ public static <T> ResponseResult<T> fail(int code, String message,T data) { return new ResponseResult<T>().setCode(code) .setMessage(message) .setData(data); } /** * 失败:统一失败 */ public static <T> ResponseResult<T> fail(T data) { return new ResponseResult<T>().setData(data); } }

改造后的为:

@RestController public class NumberCodeController { @GetMapping("/numberCode/{size}") public ResponseResult numberCode(@PathVariable("size") int size) { System.out.println("生成验证码,验证码长度:" + size); //获取随机数 String number = "0123456789"; StringBuilder numberCode = new StringBuilder(); for (int i = 0; i < size; i++) { int index = (int) (Math.random() * number.length()); char c = number.charAt(index); numberCode.append(c); } //定义返回值 NumberCodeResponse numberCodeResponse = new NumberCodeResponse(); numberCodeResponse.setNumberCode(Integer.parseInt(numberCode.toString())); return ResponseResult.success(numberCodeResponse); } }

api-passenger和service-verificationCode之间的调用

<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-loadbalancer</artifactId> </dependency>
@FeignClient("service-verificationcode") public interface ServiceVerificationCodeClient { @GetMapping("/numberCode/{size}") ResponseResult<NumberCodeResponse> numberCode(@PathVariable("size") int size); }

验证码redis使用

maven依赖

<!--redis使用--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>

yml编写

server: port: 8081 spring: cloud: nacos: discovery: server-addr: 127.0.0.1:8848 application: name: api-passenger redis: host: 127.0.0.1 port: 6379

代码编写

@Service public class VerificationCodeService { // 验证码前缀 private String verificationCodePrefix = "passenger-verification-code-"; @Autowired private StringRedisTemplate stringRedisTemplate; @Autowired private ServiceVerificationCodeClient serviceVerificationCodeClient; public ResponseResult generateCode(String passengerPhone){ // 调用验证码服务 System.out.println("调用生成验证码服务"); ResponseResult<NumberCodeResponse> numberCode = serviceVerificationCodeClient.numberCode(6); System.out.println("获取验证码为:" + numberCode.getData().getNumberCode()); //存入redis System.out.println("将验证码存入redis"); String key = verificationCodePrefix + passengerPhone; stringRedisTemplate.opsForValue().set(key, numberCode.getData().toString(),1, TimeUnit.MINUTES); //通过第三发短信商务发送验证码 TODO //返回值 return ResponseResult.success(numberCode); } }

校验验证码

骨架

@PostMapping("/verification-code-check") public ResponseResult verificationCodeCheck(@RequestBody VerificationCodeDTO verificationCodeDTO){ String passengerPhone = verificationCodeDTO.getPassengerPhone(); String verificationCode = verificationCodeDTO.getVerificationCode(); return verificationCodeService.checkCode(passengerPhone, verificationCode); }
/** * 校验验证码 * @param passengerPhone * @param verificationCode * @return */ public ResponseResult checkCode(String passengerPhone, String verificationCode){ //1.根据手机号获取redis中的验证码 //2.校验验证码 //3.判断原来是否存在用户,如果不存在,则创建用户 //4.颁发令牌 TokenResponse tokenResponse = new TokenResponse(); tokenResponse.setToken("token value"); //5.返回结果 return ResponseResult.success(tokenResponse); }

校验redis验证码

/** * 校验验证码 * @param passengerPhone * @param verificationCode * @return */ public ResponseResult checkCode(String passengerPhone, String verificationCode){ //1.根据手机号获取redis中的验证码 String key = generatorKeyByPhone(passengerPhone); String codeRedis = stringRedisTemplate.opsForValue().get(key); System.out.println("redis中的key:"+codeRedis); if (codeRedis == null){ return ResponseResult.fail(CommonStatusEnum.VERIFICATION_CODE_ERROR.getCode(), CommonStatusEnum.VERIFICATION_CODE_ERROR.getValue()); } if (!codeRedis.equals(verificationCode)){ return ResponseResult.fail(CommonStatusEnum.VERIFICATION_CODE_ERROR.getCode(), CommonStatusEnum.VERIFICATION_CODE_ERROR.getValue()); } stringRedisTemplate.delete(key); //2.校验验证码 //3.判断原来是否存在用户,如果不存在,则创建用户 //4.颁发令牌 TokenResponse tokenResponse = new TokenResponse(); tokenResponse.setToken("token value"); //5.返回结果 return ResponseResult.success(tokenResponse); } private String generatorKeyByPhone(String phone){ return verificationCodePrefix + phone; } }

乘客用户服务

骨架

@RestController public class UserController { @Autowired private UserService userService; @PostMapping("/user") public ResponseResult loginOrRegister(@RequestBody VerificationCodeDTO verificationCodeDTO){ String passengerPhone = verificationCodeDTO.getPassengerPhone(); return userService.loginOrRegister(passengerPhone); } }
@Service public class UserService { public ResponseResult loginOrRegister(String passengerPhone){ System.out.println("注册手机号被调用,手机号为:"+passengerPhone); //根据手机号查询用户 //判断用户是否存在 return ResponseResult.success(passengerPhone); } }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/8/4 13:13:46

DeepSeek辅助总结的GizmoSQL数据库自述文件

&#x1f680; GizmoSQL — 面向云端的高性能 SQL 服务器 &#x1f31f; 什么是 GizmoSQL&#xff1f; GizmoSQL 是一个轻量级、高性能的 SQL 服务器&#xff0c;构建于&#xff1a; &#x1f986; DuckDB 或 &#x1f5c3;️ SQLite&#xff0c;用于查询执行&#x1f680; A…

作者头像 李华
网站建设 2026/8/4 13:17:16

MyBatis执行流程

1. SqlSession初始化与获取SqlSession是MyBatis的核心对象&#xff0c;用于执行SQL操作。SqlSessionFactory是创建SqlSession的工厂类&#xff0c;通过build()方法初始化&#xff0c;并通过openSession()方法获取SqlSession。SqlSessionFactoryBuilder是构建SqlSessionFactory的…

作者头像 李华
网站建设 2026/8/4 13:16:12

Leetcode会员尊享100题:270.最接近的二叉树值

给你二叉搜索树的根节点 root 和一个目标值 target &#xff0c;请在该二叉搜索树中找到最接近目标值 target 的数值。如果有多个答案&#xff0c;返回最小的那个。示例 1&#xff1a;输入&#xff1a;root [4,2,5,1,3], target 3.714286 输出&#xff1a;4示例 2&#xff1a…

作者头像 李华
网站建设 2026/8/4 13:16:57

三步让阿里云配置好 clawdbot(moltbot)附上专属优惠

就在刚刚&#xff08;2026年1月28日&#xff09;&#xff0c;阿里云正式宣布上线 Moltbot&#xff08;原名&#xff1a;Clawdbot&#xff09; 全套云服务&#xff01;这对于想做AI Agent的开发者来说是个重磅消息。它全面提供了Agent所需的算力、模型和消息应用支持 。 简单来…

作者头像 李华
网站建设 2026/8/4 13:17:16

腾讯地图:2026 年的技术革新与应用拓展

在 2026 年&#xff0c;数字化浪潮席卷各行各业&#xff0c;地图服务作为连接物理与数字世界的关键纽带&#xff0c;其重要性愈发凸显。腾讯地图凭借一系列技术革新与应用优化&#xff0c;成为了众多企业和开发者的首选。 强大技术架构支撑 腾讯地图拥有从 LBS 到时空引擎的进…

作者头像 李华
网站建设 2026/8/4 14:14:05

宠物寄养小程序前端功能版块详解

宠物寄养小程序以场景化前端功能为支撑&#xff0c;打通宠主寄养需求与机构服务能力的对接通道&#xff0c;既缓解宠主外出时的照料焦虑&#xff0c;又助力寄养机构规范服务流程。其前端设计围绕需求精准匹配、服务过程可视化、操作便捷化三大核心&#xff0c;构建全周期服务功…

作者头像 李华