UserController

 1 package com.miaoshaProject.controller;
 2 
 3 import com.miaoshaProject.controller.viewobject.UserVO;
 4 import com.miaoshaProject.error.BusinessException;
 5 import com.miaoshaProject.error.EnumBusinessError;
 6 import com.miaoshaProject.response.CommonReturnType;
 7 import com.miaoshaProject.service.UesrService;
 8 import com.miaoshaProject.service.model.UserModel;
 9 import org.springframework.beans.BeanUtils;
10 import org.springframework.beans.factory.annotation.Autowired;
11 import org.springframework.http.HttpStatus;
12 import org.springframework.stereotype.Controller;
13 import org.springframework.web.bind.annotation.*;
14 
15 import javax.servlet.http.HttpServletRequest;
16 import java.util.HashMap;
17 import java.util.Random;
18 
19 /**
20  * @Author wangshuo
21  * @Date 2022/4/12, 18:42
22  * Please add a comment
23  */
24 @Controller("user")
25 @RequestMapping("/user")
26 @CrossOrigin()
27 public class UserController extends BaseController{
28 
29     @Autowired
30     UesrService uesrService;
31     @Autowired
32     HttpServletRequest httpServletRequest;
33 
34     //用户获取otp短信接口
35     @RequestMapping(value = "/getotp",method = {RequestMethod.POST},consumes = {CONTENT_TYPE_FORMED})
36     @ResponseBody
37     public CommonReturnType getOtp(@RequestParam(name="telphone")String telphone){
38 
39         //需要按照一定的规则生成otp验证码
40         Random random = new Random();
41         int randomInt = random.nextInt(99999);
42         randomInt += 1;
43         String otpCode = String.valueOf(randomInt);
44         //将opt验证码同用户的手机号码关联  使用httpSession的方式绑定他的手机号与optCode
45         httpServletRequest.getSession().setAttribute(telphone,otpCode);
46 
47         System.out.println("tel:"+telphone+"  验证码:"+httpServletRequest.getSession().getAttribute(telphone));
48 
49         //通过短信通道将验证码发送给用户(省略)
50         return CommonReturnType.create(otpCode);
51     }
52 
53     @RequestMapping("/get")
54     @ResponseBody
55     public CommonReturnType getUser(@RequestParam(name = "id") Integer id) throws BusinessException {
56 
57         //调用service服务获取对应id的用户对象并返回给前端
58         UserModel userModel = uesrService.getById(id);
59 
60         if (userModel == null) {
61             //返回用户不存在异常
62             throw new BusinessException(EnumBusinessError.USER_NOT_EXISTS);
63             //空指针异常
64             // userModel.setAge(1514);
65         }
66         //将核心领域模型对象转化为可供UI使用的viewObject
67         //返回通用对象
68         return CommonReturnType.create(convertFromModel(userModel));
69     }
70 
71     //BeanCopy
72     private UserVO convertFromModel(UserModel userModel) {
73 
74         if (userModel == null)
75             return null;
76         UserVO userVO = new UserVO();
77         BeanUtils.copyProperties(userModel, userVO);
78         return userVO;
79     }
80 }

静态文件下载

链接: https://caiyun.139.com/m/i?145CGKG4cvOEp  提取码:5G6U

getopt.html

 1 <html>
 2 <head>
 3     <meta charset="UTF-8">
 4     <link rel="stylesheet" href="assets/global/plugins/bootstrap/css/bootstrap.min.css" type="text/css"/>
 5     <link rel="stylesheet" href="assets/global/css/components.css" type="text/css"/>
 6     <link rel="stylesheet" href="assets/admin/pages/css/login.css" type="text/css"/>
 7     <script rel="stylesheet" src="assets/global/plugins/jquery-1.11.0.min.js" type="text/javascript"></script>
 8 </head>
 9 <body class="login" background="edge_bj_1.jpg">
10 
11 <div class="content">
12     <h3 class="form-title">用户登录</h3>
13     <div>
14         <label class="control-label">手机号</label>
15         <div class="form-group">
16             <input type="text" class="form-control" placeholder="手机号" name="telphone" id="telphone">
17         </div>
18     </div>
19     <div>
20         <button class="btn blue" id="getotp" type="submit">
21             获取验证码
22         </button>
23     </div>
24 </div>
25 
26 </body>
27 <script>
28 
29     jQuery(document).ready(function () {
30 
31         //绑定otp的click事件用于向后端发送手机验证码的请求
32         $("#getotp").on("click", function () {
33 
34             var tel = $("#telphone").val();
35             if (tel == null || tel == '') {
36                 alert("用户手机号不能为空");
37                 return false;
38             }
39             $.ajax({
40                 type: "POST",
41                 contentType: "application/x-www-form-urlencoded",
42                 url: "http://localhost:8080/user/getotp",
43                 data: {
44                     "telphone": $("#telphone").val(),
45                 },
46                 success: function (data) {
47 
48                     if (data.status == "success") {
49                         alert("验证码已发送至您的手机,请注意查收")
50                     } else {
51                         alert("请求失败  原因为:" + data.data.errorMsg)
52                     }
53                 },
54                 error:function (data) {
55                     alert("请求失败   原因为:"+data.responseText)
56                 }
57             });
58             return false;
59         })
60     })
61 
62 </script>
63 </html>