mirror of
				https://gitee.com/coder-xiaomo/flashsale
				synced 2025-11-04 14:13:14 +08:00 
			
		
		
		
	otp验证码获取
This commit is contained in:
		
							
								
								
									
										56
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										56
									
								
								README.md
									
									
									
									
									
								
							@@ -14,6 +14,19 @@
 | 
			
		||||
 | 
			
		||||
### 本次修改通过以下方式查看👇
 | 
			
		||||
 | 
			
		||||
> http://localhost:8090/user/getotp?telephone=1
 | 
			
		||||
 | 
			
		||||
预期效果:
 | 
			
		||||
 | 
			
		||||
```json
 | 
			
		||||
{"data":null,"status":"success"}
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
控制台:
 | 
			
		||||
```sql
 | 
			
		||||
telephone = 1 & otpCode = 90824
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
> http://localhost:8090/user/get?id=2
 | 
			
		||||
 | 
			
		||||
预期效果:
 | 
			
		||||
@@ -21,27 +34,30 @@
 | 
			
		||||
```json
 | 
			
		||||
{"data":{"errCode":10002,"errMsg":"未知错误"},"status":"fail"}
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
### 层次结构(以User为例,自上向下)
 | 
			
		||||
 | 
			
		||||
|                               | 目录                  | Java对象类型      | 说明                                     | 举例                         |
 | 
			
		||||
| ----------------------------- | --------------------- | ----------------- | ---------------------------------------- | ---------------------------- |
 | 
			
		||||
| **<nobr>Controller层</nobr>** |                       |                   |                                          |                              |
 | 
			
		||||
| Controller                    | controller            | 类 class          |                                          | controller.UserController    |
 | 
			
		||||
| View Object                   | controller/viewobject | 类 class          | 将用户Model转化为可供UI使用的View Object | controller.UserController    |
 | 
			
		||||
|                               |                       |                   |                                          |                              |
 | 
			
		||||
| **<nobr>Service层</nobr>**    | 转换成业务模型        |                   |                                          |                              |
 | 
			
		||||
| Service                       | service               | 接口 interface    |                                          | service.UserService          |
 | 
			
		||||
| ServiceImpl                   | service/impl          | Service接口实现类 | 将DataObject组装成Model                  | service.impl.UserServiceImpl |
 | 
			
		||||
| Model                         | service/model         | 类 class          | 用户模型Model                            | service.model.UserModel      |
 | 
			
		||||
|                               |                       |                   |                                          |                              |
 | 
			
		||||
| **<nobr>Dao层</nobr>**        | 对数据库的映射        |                   |                                          |                              |
 | 
			
		||||
| Mapper                        | dao                   | 接口 interface    |                                          | dao.UserDOMapper             |
 | 
			
		||||
| Mapping                       | resources/mapping     | Mapper接口实现类  | xml格式;SQL语句                         | mapping/UserDOMapper.xml     |
 | 
			
		||||
| Data Object                   | dataobject            | 类 class          |                                          | dataobject.UserDO            |
 | 
			
		||||
|                   | 目录                    | Java对象类型                        | 说明                            | 举例                           |
 | 
			
		||||
|-------------------|-----------------------|---------------------------------|-------------------------------|------------------------------|
 | 
			
		||||
| **Controller层**   |                       |                                 |                               |                              |
 | 
			
		||||
| Controller        | controller            | 类 class                         |                               | controller.UserController    |
 | 
			
		||||
| View Object       | controller/viewobject | 类 class                         | 将用户Model转化为可供UI使用的View Object | controller.UserController    |
 | 
			
		||||
|                   |                       |                                 |                               |                              |
 | 
			
		||||
| **Service层**      | 转换成业务模型               |                                 |                               |                              |
 | 
			
		||||
| Service           | service               | 接口 interface                    |                               | service.UserService          |
 | 
			
		||||
| ServiceImpl       | service/impl          | Service接口实现类                    | 将DataObject组装成Model           | service.impl.UserServiceImpl |
 | 
			
		||||
| Model             | service/model         | 类 class                         | 用户模型Model                     | service.model.UserModel      |
 | 
			
		||||
|                   |                       |                                 |                               |                              |
 | 
			
		||||
| **Dao层**          | 对数据库的映射               |                                 |                               |                              |
 | 
			
		||||
| Mapper            | dao                   | 接口 interface                    |                               | dao.UserDOMapper             |
 | 
			
		||||
| Mapping           | resources/mapping     | Mapper接口实现类                     | xml格式;SQL语句                   | mapping/UserDOMapper.xml     |
 | 
			
		||||
| Data Object       | dataobject            | 类 class                         |                               | dataobject.UserDO            |
 | 
			
		||||
|                   |                       |                                 |                               |                              |
 | 
			
		||||
| **其他**            |                       |                                 |                               |                              |
 | 
			
		||||
| response          | response              |                                 | 用于处理HTTP请求返回                  | response.CommonReturnType    |
 | 
			
		||||
|                   |                       |                                 |                               |                              |
 | 
			
		||||
| **异常处理**          | 用于返回错误信息              |                                 |                               |                              |
 | 
			
		||||
| CommonError       | error                 | 接口 interface                    |                               | error.CommonError            |
 | 
			
		||||
| EmBusinessError   | error                 | 枚举 enum & CommonError接口实现类      |                               | error.EmBusinessError        |
 | 
			
		||||
| BusinessException | error                 | CommonError接口实现类 & 继承自Exception |                               | error.BusinessException      |
 | 
			
		||||
 | 
			
		||||
**Tips:** Model与Data Object并非完全一一对应,例如UserModel是由ServiceImpl将UserDO和UserPasswordDO组装而成的。
 | 
			
		||||
 | 
			
		||||
response: 用于处理HTTP请求返回
 | 
			
		||||
 | 
			
		||||
error: 用于返回错误信息
 | 
			
		||||
@@ -10,6 +10,9 @@ import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
import org.springframework.stereotype.Controller;
 | 
			
		||||
import org.springframework.web.bind.annotation.*;
 | 
			
		||||
 | 
			
		||||
import javax.servlet.http.HttpServletRequest;
 | 
			
		||||
import java.util.Random;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@Controller("user") // 允许被SpringBoot扫描到
 | 
			
		||||
@RequestMapping("/user") // 通过 "/user" 访问到
 | 
			
		||||
@@ -18,6 +21,27 @@ public class UserController extends BaseController {
 | 
			
		||||
    @Autowired
 | 
			
		||||
    private UserService userService;
 | 
			
		||||
 | 
			
		||||
    @Autowired
 | 
			
		||||
    private HttpServletRequest httpServletRequest;
 | 
			
		||||
 | 
			
		||||
    // 用户获取OTP短信接口
 | 
			
		||||
    @RequestMapping("/getotp")
 | 
			
		||||
    @ResponseBody
 | 
			
		||||
    public CommonReturnType getOtp(@RequestParam(name = "telephone") String telephone) {
 | 
			
		||||
        // 需要按照一定的规则生成OTP验证码
 | 
			
		||||
        Random random = new Random();
 | 
			
		||||
        int randomInt = random.nextInt(99999);// [0,99999)
 | 
			
		||||
        String otpCode = String.format("%05d", randomInt);
 | 
			
		||||
 | 
			
		||||
        // 将OTP验证码同对应用户的手机号关联  使用
 | 
			
		||||
        httpServletRequest.getSession().setAttribute(telephone, otpCode);
 | 
			
		||||
 | 
			
		||||
        // 将OTP验证码通过短信通道发送给用户
 | 
			
		||||
        System.out.println("telephone = " + telephone + " & otpCode = " + otpCode);
 | 
			
		||||
 | 
			
		||||
        return CommonReturnType.create(null);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @RequestMapping("/get")
 | 
			
		||||
    @ResponseBody
 | 
			
		||||
    public CommonReturnType getUser(@RequestParam(name = "id") Integer id) throws BusinessException {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user