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