|
@@ -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();
|