Browse Source

修改登录为手机和验证码

weiximei 6 years ago
parent
commit
5a30c8c4e4

+ 7
- 0
CODE/smart-community/app-api/pom.xml View File

@@ -149,6 +149,13 @@
149 149
 			<version>2.6</version>
150 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 161
 	</dependencies>

+ 34
- 6
CODE/smart-community/app-api/src/main/java/com/community/huiju/common/code/impl/PhoneCodeImpl.java View File

@@ -2,14 +2,21 @@ package com.community.huiju.common.code.impl;
2 2
 
3 3
 import com.alibaba.fastjson.JSONObject;
4 4
 import com.community.commom.constant.Constant;
5
+import com.community.commom.utils.HttpClientUtils;
5 6
 import com.community.huiju.common.code.ICode;
6 7
 import com.community.huiju.common.code.entity.CodeEntity;
7 8
 import lombok.extern.log4j.Log4j;
8 9
 import lombok.extern.slf4j.Slf4j;
10
+import okhttp3.*;
9 11
 import org.springframework.beans.factory.annotation.Autowired;
10 12
 import org.springframework.stereotype.Service;
13
+import org.springframework.util.LinkedMultiValueMap;
14
+import org.springframework.util.MultiValueMap;
11 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 22
  * @author weiximei
@@ -18,25 +25,46 @@ import org.springframework.web.client.RestTemplate;
18 25
 @Slf4j
19 26
 public class PhoneCodeImpl implements ICode {
20 27
 
28
+    public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
29
+
21 30
     @Autowired
22 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 42
     @Override
26 43
     public boolean sendCode(String phone, String code) {
27 44
         boolean bool = false;
28
-        CodeEntity codeEntity = new CodeEntity();
29
-        codeEntity.setCode(Constant.CODE);
30
-        codeEntity.setTel(phone);
31
-        codeEntity.setParams("[\""+code+"\"]");
32 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 62
             if ("发送成功".equals(result)) {
35 63
                 bool = true;
36 64
                 log.info("手机号 {} 验证码 {} 发送成功!",phone,code);
37 65
             }else {
38 66
                 bool = false;
39
-                log.error("短信发送验证码失败!{}",result);
67
+                log.error("短信发送验证码失败!{}", result);
40 68
             }
41 69
         } catch (Exception e){
42 70
             e.printStackTrace();

+ 11
- 3
CODE/smart-community/app-api/src/main/java/com/community/huiju/controller/CodeController.java View File

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

+ 10
- 11
CODE/smart-community/app-api/src/main/java/com/community/huiju/controller/UserController.java View File

@@ -12,12 +12,10 @@ import io.swagger.annotations.ApiOperation;
12 12
 import org.springframework.beans.BeanUtils;
13 13
 import org.springframework.beans.factory.annotation.Autowired;
14 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 17
 import javax.servlet.http.HttpSession;
18
+import javax.xml.crypto.dsig.keyinfo.PGPData;
21 19
 
22 20
 /**
23 21
  * 用户控制器
@@ -38,15 +36,16 @@ public class UserController {
38 36
             @ApiImplicitParam(paramType = "query",dataType = "String",name = "loginPassword",value = "密码")
39 37
     })
40 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 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 49
         if (null != taUser) {
51 50
             UserElement userElement = new UserElement();
52 51
             BeanUtils.copyProperties(taUser,userElement);

+ 8
- 0
CODE/smart-community/app-api/src/main/java/com/community/huiju/service/ITaUserService.java View File

@@ -2,6 +2,7 @@ package com.community.huiju.service;
2 2
 
3 3
 import com.community.commom.mode.ResponseBean;
4 4
 import com.community.huiju.model.TaUser;
5
+import org.springframework.web.bind.annotation.RequestParam;
5 6
 
6 7
 /**
7 8
  * 用户业务 接口
@@ -16,6 +17,13 @@ public interface ITaUserService {
16 17
      */
17 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 29
      * @param user

+ 24
- 1
CODE/smart-community/app-api/src/main/java/com/community/huiju/service/impl/TaUserServiceImpl.java View File

@@ -56,6 +56,29 @@ public class TaUserServiceImpl implements ITaUserService {
56 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 82
     @Transactional
60 83
     @Override
61 84
     public ResponseBean register(TaUser user) {
@@ -67,7 +90,7 @@ public class TaUserServiceImpl implements ITaUserService {
67 90
         user.setAcceptAgreementStatus("1");
68 91
         user.setUserName(user.getLoginName());
69 92
         user.setRemark("这是系统自动注册!");
70
-        user.setLoginPassword(MD5Utils.encode(user.getLoginPassword()));
93
+        //user.setLoginPassword(MD5Utils.encode(user.getLoginPassword()));
71 94
         taUserMapper.insertSelective(user);
72 95
 
73 96
         response.addSuccess(user);