Просмотр исходного кода

修改登录为手机和验证码

weiximei 6 лет назад
Родитель
Сommit
5a30c8c4e4

+ 7
- 0
CODE/smart-community/app-api/pom.xml Просмотреть файл

149
 			<version>2.6</version>
149
 			<version>2.6</version>
150
 		</dependency>
150
 		</dependency>
151
 
151
 
152
+		<!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp -->
153
+		<dependency>
154
+			<groupId>com.squareup.okhttp3</groupId>
155
+			<artifactId>okhttp</artifactId>
156
+			<version>3.11.0</version>
157
+		</dependency>
158
+
152
 
159
 
153
 
160
 
154
 	</dependencies>
161
 	</dependencies>

+ 34
- 6
CODE/smart-community/app-api/src/main/java/com/community/huiju/common/code/impl/PhoneCodeImpl.java Просмотреть файл

2
 
2
 
3
 import com.alibaba.fastjson.JSONObject;
3
 import com.alibaba.fastjson.JSONObject;
4
 import com.community.commom.constant.Constant;
4
 import com.community.commom.constant.Constant;
5
+import com.community.commom.utils.HttpClientUtils;
5
 import com.community.huiju.common.code.ICode;
6
 import com.community.huiju.common.code.ICode;
6
 import com.community.huiju.common.code.entity.CodeEntity;
7
 import com.community.huiju.common.code.entity.CodeEntity;
7
 import lombok.extern.log4j.Log4j;
8
 import lombok.extern.log4j.Log4j;
8
 import lombok.extern.slf4j.Slf4j;
9
 import lombok.extern.slf4j.Slf4j;
10
+import okhttp3.*;
9
 import org.springframework.beans.factory.annotation.Autowired;
11
 import org.springframework.beans.factory.annotation.Autowired;
10
 import org.springframework.stereotype.Service;
12
 import org.springframework.stereotype.Service;
13
+import org.springframework.util.LinkedMultiValueMap;
14
+import org.springframework.util.MultiValueMap;
11
 import org.springframework.web.client.RestTemplate;
15
 import org.springframework.web.client.RestTemplate;
12
 
16
 
17
+import java.io.IOException;
18
+import java.util.concurrent.TimeUnit;
19
+
13
 /**
20
 /**
14
  * 手机验证码
21
  * 手机验证码
15
  * @author weiximei
22
  * @author weiximei
18
 @Slf4j
25
 @Slf4j
19
 public class PhoneCodeImpl implements ICode {
26
 public class PhoneCodeImpl implements ICode {
20
 
27
 
28
+    public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
29
+
21
     @Autowired
30
     @Autowired
22
     private RestTemplate restTemplate;
31
     private RestTemplate restTemplate;
23
 
32
 
33
+    public OkHttpClient getClient() {
34
+        OkHttpClient client  = new OkHttpClient.Builder()
35
+                .connectTimeout(10, TimeUnit.SECONDS)
36
+                .writeTimeout(10,TimeUnit.SECONDS)
37
+                .readTimeout(20, TimeUnit.SECONDS)
38
+                .build();
39
+        return client;
40
+    }
24
 
41
 
25
     @Override
42
     @Override
26
     public boolean sendCode(String phone, String code) {
43
     public boolean sendCode(String phone, String code) {
27
         boolean bool = false;
44
         boolean bool = false;
28
-        CodeEntity codeEntity = new CodeEntity();
29
-        codeEntity.setCode(Constant.CODE);
30
-        codeEntity.setTel(phone);
31
-        codeEntity.setParams("[\""+code+"\"]");
32
         try {
45
         try {
33
-            String result = restTemplate.postForObject(Constant.REQUEST_URL, JSONObject.toJSONString(codeEntity),String.class);
46
+            String result = null;
47
+            OkHttpClient client = getClient();
48
+            RequestBody body = RequestBody.create(JSON, "{\"code\":\""+Constant.CODE+"\"," + "\"tel\":\""+phone+"\"," + "\"params\":[\""+code+"\"]}");
49
+            Request request = new Request.Builder()
50
+                    .url(Constant.REQUEST_URL)
51
+                    .post(body)
52
+                    .build();
53
+            Response response = client.newCall(request).execute();
54
+            if (response.isSuccessful()) {
55
+                result = response.body().string();
56
+            } else {
57
+                throw new IOException("Unexpected code " + response);
58
+            }
59
+
60
+
61
+
34
             if ("发送成功".equals(result)) {
62
             if ("发送成功".equals(result)) {
35
                 bool = true;
63
                 bool = true;
36
                 log.info("手机号 {} 验证码 {} 发送成功!",phone,code);
64
                 log.info("手机号 {} 验证码 {} 发送成功!",phone,code);
37
             }else {
65
             }else {
38
                 bool = false;
66
                 bool = false;
39
-                log.error("短信发送验证码失败!{}",result);
67
+                log.error("短信发送验证码失败!{}", result);
40
             }
68
             }
41
         } catch (Exception e){
69
         } catch (Exception e){
42
             e.printStackTrace();
70
             e.printStackTrace();

+ 11
- 3
CODE/smart-community/app-api/src/main/java/com/community/huiju/controller/CodeController.java Просмотреть файл

1
 package com.community.huiju.controller;
1
 package com.community.huiju.controller;
2
 
2
 
3
+import com.community.commom.constant.Constant;
3
 import com.community.commom.mode.ResponseBean;
4
 import com.community.commom.mode.ResponseBean;
4
 import com.community.huiju.common.code.ICode;
5
 import com.community.huiju.common.code.ICode;
5
 import io.swagger.annotations.Api;
6
 import io.swagger.annotations.Api;
6
 import io.swagger.annotations.ApiImplicitParam;
7
 import io.swagger.annotations.ApiImplicitParam;
7
 import io.swagger.annotations.ApiImplicitParams;
8
 import io.swagger.annotations.ApiImplicitParams;
8
 import io.swagger.annotations.ApiOperation;
9
 import io.swagger.annotations.ApiOperation;
10
+import lombok.extern.slf4j.Slf4j;
9
 import org.springframework.beans.factory.annotation.Autowired;
11
 import org.springframework.beans.factory.annotation.Autowired;
10
 import org.springframework.beans.factory.annotation.Qualifier;
12
 import org.springframework.beans.factory.annotation.Qualifier;
11
 import org.springframework.cloud.context.config.annotation.RefreshScope;
13
 import org.springframework.cloud.context.config.annotation.RefreshScope;
12
 import org.springframework.web.bind.annotation.RequestMapping;
14
 import org.springframework.web.bind.annotation.RequestMapping;
13
 import org.springframework.web.bind.annotation.RequestMethod;
15
 import org.springframework.web.bind.annotation.RequestMethod;
16
+import org.springframework.web.bind.annotation.RequestParam;
14
 import org.springframework.web.bind.annotation.RestController;
17
 import org.springframework.web.bind.annotation.RestController;
18
+import sun.rmi.runtime.Log;
15
 
19
 
16
 import javax.servlet.http.HttpSession;
20
 import javax.servlet.http.HttpSession;
17
 
21
 
22
 @RequestMapping("/code")
26
 @RequestMapping("/code")
23
 @RefreshScope
27
 @RefreshScope
24
 @Api("获取验证码的API")
28
 @Api("获取验证码的API")
29
+@Slf4j
25
 public class CodeController {
30
 public class CodeController {
26
 
31
 
27
     @Autowired
32
     @Autowired
30
 
35
 
31
     @ApiOperation(value = "获取手机验证码", notes = "根据手机号发送验证码")
36
     @ApiOperation(value = "获取手机验证码", notes = "根据手机号发送验证码")
32
     @ApiImplicitParams({@ApiImplicitParam(paramType = "query",dataType = "String",name = "phone",value = "手机号")})
37
     @ApiImplicitParams({@ApiImplicitParam(paramType = "query",dataType = "String",name = "phone",value = "手机号")})
33
-    @RequestMapping(value = "/sendCode",method = RequestMethod.GET)
34
-    public ResponseBean sendCode(String phone, HttpSession session) {
38
+    @RequestMapping(value = "/sendCode",method = RequestMethod.POST)
39
+    public ResponseBean sendCode(@RequestParam String phone, HttpSession session) {
35
         ResponseBean response = new ResponseBean();
40
         ResponseBean response = new ResponseBean();
36
         int code = (int) ((Math.random()*9+1)*1000);
41
         int code = (int) ((Math.random()*9+1)*1000);
37
-        boolean result = iCode.sendCode(phone,String.valueOf(code));
42
+        //boolean result = iCode.sendCode(phone,String.valueOf(code));
43
+        boolean result = true;
38
         if (result) {
44
         if (result) {
45
+            log.info("{} 验证码: {}",phone,code);
46
+            session.setAttribute(Constant.SESSION_PHONE_CODE,String.valueOf(code));
39
             response.addSuccess("发送成功!");
47
             response.addSuccess("发送成功!");
40
         } else {
48
         } else {
41
             response.addError("发送失败!");
49
             response.addError("发送失败!");

+ 10
- 11
CODE/smart-community/app-api/src/main/java/com/community/huiju/controller/UserController.java Просмотреть файл

12
 import org.springframework.beans.BeanUtils;
12
 import org.springframework.beans.BeanUtils;
13
 import org.springframework.beans.factory.annotation.Autowired;
13
 import org.springframework.beans.factory.annotation.Autowired;
14
 import org.springframework.cloud.context.config.annotation.RefreshScope;
14
 import org.springframework.cloud.context.config.annotation.RefreshScope;
15
-import org.springframework.web.bind.annotation.RequestBody;
16
-import org.springframework.web.bind.annotation.RequestMapping;
17
-import org.springframework.web.bind.annotation.RequestMethod;
18
-import org.springframework.web.bind.annotation.RestController;
15
+import org.springframework.web.bind.annotation.*;
19
 
16
 
20
 import javax.servlet.http.HttpSession;
17
 import javax.servlet.http.HttpSession;
18
+import javax.xml.crypto.dsig.keyinfo.PGPData;
21
 
19
 
22
 /**
20
 /**
23
  * 用户控制器
21
  * 用户控制器
38
             @ApiImplicitParam(paramType = "query",dataType = "String",name = "loginPassword",value = "密码")
36
             @ApiImplicitParam(paramType = "query",dataType = "String",name = "loginPassword",value = "密码")
39
     })
37
     })
40
     @RequestMapping(value = "/login",method = RequestMethod.POST)
38
     @RequestMapping(value = "/login",method = RequestMethod.POST)
41
-    public ResponseBean login(@RequestBody TaUser user, HttpSession session){
42
-        ResponseBean responseBean = iTaUserService.login(user);
39
+    public ResponseBean login(@RequestParam(value = "loginName") String loginName,@RequestParam(value = "code",defaultValue = "") String code, HttpSession session){
40
+        ResponseBean responseBean = new ResponseBean();
41
+        String phondeCode = (String) session.getAttribute(Constant.SESSION_PHONE_CODE);
42
+        if (!code.equals(phondeCode)) {
43
+            responseBean.addError("验证码错误!");
44
+            return responseBean;
45
+        }
46
+        responseBean = iTaUserService.login(loginName);
43
         TaUser taUser = (TaUser) responseBean.getData();
47
         TaUser taUser = (TaUser) responseBean.getData();
44
 
48
 
45
-//        userElement.setStatus(taUser.getStatus());
46
-//        userElement.setLoginName(taUser.getLoginName());
47
-//        userElement.setUserName(taUser.getUserName());
48
-//        userElement.setEmail(taUser.getEmail());
49
-//        userElement.setId(taUser.getId());
50
         if (null != taUser) {
49
         if (null != taUser) {
51
             UserElement userElement = new UserElement();
50
             UserElement userElement = new UserElement();
52
             BeanUtils.copyProperties(taUser,userElement);
51
             BeanUtils.copyProperties(taUser,userElement);

+ 8
- 0
CODE/smart-community/app-api/src/main/java/com/community/huiju/service/ITaUserService.java Просмотреть файл

2
 
2
 
3
 import com.community.commom.mode.ResponseBean;
3
 import com.community.commom.mode.ResponseBean;
4
 import com.community.huiju.model.TaUser;
4
 import com.community.huiju.model.TaUser;
5
+import org.springframework.web.bind.annotation.RequestParam;
5
 
6
 
6
 /**
7
 /**
7
  * 用户业务 接口
8
  * 用户业务 接口
16
      */
17
      */
17
     ResponseBean login(TaUser user);
18
     ResponseBean login(TaUser user);
18
 
19
 
20
+    /**
21
+     * 用户登录
22
+     * @param loginName
23
+     * @return 用户对象
24
+     */
25
+    ResponseBean login(String loginName);
26
+
19
     /**
27
     /**
20
      * 注册用户
28
      * 注册用户
21
      * @param user
29
      * @param user

+ 24
- 1
CODE/smart-community/app-api/src/main/java/com/community/huiju/service/impl/TaUserServiceImpl.java Просмотреть файл

56
         return response;
56
         return response;
57
     }
57
     }
58
 
58
 
59
+    @Override
60
+    public ResponseBean login(String loginName) {
61
+        ResponseBean response = new ResponseBean();
62
+        log.info("{} 手机号校验结果: {}",loginName,AccountValidatorUtil.isPhone(loginName));
63
+        if (!AccountValidatorUtil.isPhone(loginName)){
64
+            response.addError("请输入正取的手机号!");
65
+            return response;
66
+        }
67
+        Map<String,Object> map = Maps.newHashMap();
68
+        map.put("loginName",loginName);
69
+        TaUser currentUser = taUserMapper.selectByLoginName(map);
70
+        if (null != currentUser){
71
+            response.addSuccess(currentUser);
72
+            return response;
73
+        }else {
74
+            TaUser user = new TaUser();
75
+            user.setLoginName(loginName);
76
+            ResponseBean result = register(user);
77
+            response.addSuccess(result.getData());
78
+        }
79
+        return response;
80
+    }
81
+
59
     @Transactional
82
     @Transactional
60
     @Override
83
     @Override
61
     public ResponseBean register(TaUser user) {
84
     public ResponseBean register(TaUser user) {
67
         user.setAcceptAgreementStatus("1");
90
         user.setAcceptAgreementStatus("1");
68
         user.setUserName(user.getLoginName());
91
         user.setUserName(user.getLoginName());
69
         user.setRemark("这是系统自动注册!");
92
         user.setRemark("这是系统自动注册!");
70
-        user.setLoginPassword(MD5Utils.encode(user.getLoginPassword()));
93
+        //user.setLoginPassword(MD5Utils.encode(user.getLoginPassword()));
71
         taUserMapper.insertSelective(user);
94
         taUserMapper.insertSelective(user);
72
 
95
 
73
         response.addSuccess(user);
96
         response.addSuccess(user);