地质所 沉降监测网建设项目
chenhuan
2024-05-16 0fdd42e318f51f9e3c6581473416af1cca69877f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package com.javaweb.platform.utils;
 
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import com.javaweb.common.utils.IdGenerate;
import com.javaweb.common.utils.StringUtils;
import com.javaweb.platform.domain.Validcode;
import com.javaweb.platform.mapper.ValidcodeMapper;
 
import com.aliyun.tea.*;
import com.aliyun.dysmsapi20170525.*;
import com.aliyun.dysmsapi20170525.models.*;
import com.aliyun.teaopenapi.*;
import com.aliyun.teaopenapi.models.*;
 
@Service
public class ValidcodeUtils {
    
    @Autowired
    private ValidcodeMapper validcodeMapper;
    
    private static final String accessKeyId="LTAI5tJjstk9c2qZs43fbGx9";
     
    private static final String accessKeySecret="sWHErgvNkain06tOBIxmyht1kMwq2H";
    
    private static final String SignName="北京梦之岩";
    
    private static  final String SignName2="梦之岩";
    
    private static String  Regist="1";//注册用户
    private static String Forgot ="2";//忘记密码
    private static String Approve ="3";//通过申请
    private static String Reject ="4";//不通过审核
    
    private static String Approve_Hole="5";
    private static String Reject_Hole="6";
    
    private static int phonelength = 11;
 
 
    /**
     * 向手机发送验证码
     * @param type 验证码的类型,1:注册用户,2:修改密码,3:通过审核,4:不通过审核 , 5 钻孔申请通过  6 钻孔申请不通过 
     * @param phone
     * @param code
     * @return 0:发送过于频繁,1:正常发送
     * @throws Exception 
     */
    public int sendMesgToPhone(String type,String phone) throws Exception {
        if(phone.length() != phonelength){
            return 0;
        }
        if(hasExistsCode(phone,type))
        {
            return 0;
        }
        String code = createCode();
        String templateParam = "{code:"+code+"}";
        //创建客户端连接
        com.aliyun.dysmsapi20170525.Client client = createClient();
        SendSmsRequest sendSmsRequest = null;
        if(type.equals(Regist)||type.equals(Forgot)){
            
            sendSmsRequest = createSendSmsRequest(phone, "SMS_237405548", templateParam,SignName);
            
        }else if(type.equals(Approve)){
      
            sendSmsRequest = createSendSmsRequest(phone, "SMS_238461833", null,SignName2);
            
        }else if(type.equals(Reject)){
 
            sendSmsRequest = createSendSmsRequest(phone, "SMS_238466927",  null,SignName2);
            
        }else if(type.equals(Approve_Hole)){
            
            sendSmsRequest = createSendSmsRequest(phone, "SMS_240396449",  null,SignName2);
            
        }else if(type.equals(Reject_Hole)){
            
            sendSmsRequest = createSendSmsRequest(phone, "SMS_240376549",  null,SignName2);
        }
        
        //发送数据
        sendMsg(client,sendSmsRequest);
 
        //写入数据库
        Validcode validcode = new Validcode();
        validcode.setId(IdGenerate.nextId());
        validcode.setPhone(phone);
        validcode.setType(type);
        validcode.setCreateTime(new Date());
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MINUTE, 5);
        validcode.setSendTime(new Date());
        validcode.setUntilTime(calendar.getTime());
        validcode.setCode(code);
        validcodeMapper.insertValidcode(validcode);
        return 1;
    }
    
    private void  sendMsg(com.aliyun.dysmsapi20170525.Client client, SendSmsRequest sendSmsRequest){
        SendSmsResponse r = new SendSmsResponse();
        // 复制代码运行请自行打印 API 的返回值
        try {
            r = client.sendSms(sendSmsRequest);
            System.out.println(r.getBody().getMessage());
 
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    /**
     * 验证码是否有效
     * @param code
     * @param phone
     * @param type
     * @return
     */
    public boolean validCode(String code,String phone,String type) {
        Validcode validcode = new Validcode();
        validcode.setCode(code);
        validcode.setPhone(phone);
        validcode.setType(type);
        validcode.setUntilTime(new Date());
        List<Validcode> codeList = validcodeMapper.selectValidcodeList(validcode);
        if(codeList.size() > 0)
        {
            return true;
        }
        return false;
    }
    /**
     * 查询数据库中,最近50秒是否发送了短信
     * @param phone
     * @return
     */
    private boolean hasExistsCode(String phone,String type) {
        Validcode validcode = new Validcode();
        validcode.setPhone(phone);
        validcode.setType(type);
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.SECOND, -50);
        validcode.setSendTime(calendar.getTime());
        List<Validcode> exisList=validcodeMapper.selectValidcodeList(validcode);
        if(exisList.size()>0)
        {
            return true;
        }
        return false;
    }
    private String createCode()
    {
        //生成六位随机数验证码
        String ret = "";
        for(int i = 0;i<6;i++)
        {
            Random r = new Random();
            int j = r.nextInt(10);
            if(i == 0 && j == 0)
            {
                ret += 1;
            }
            else
            {
                ret += j;
            }
        }
        return ret;
    }
     /**
     * 使用AK&SK初始化账号Client
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     * @throws Exception
     */
    private com.aliyun.dysmsapi20170525.Client createClient() throws Exception {
        Config config = new Config()
                // 您的AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 您的AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new com.aliyun.dysmsapi20170525.Client(config);
    }
    /**
     * 创建发送命令的参数
     * @return
     */
    private SendSmsRequest createSendSmsRequest(String phone,String templateCode,String templateParam,String signname){
        
        SendSmsRequest sendSmsRequest = new SendSmsRequest();
        sendSmsRequest.setSignName(signname);
        sendSmsRequest.setPhoneNumbers(phone);
 
        sendSmsRequest.setTemplateCode(templateCode);
        if(!StringUtils.isEmpty(templateParam)){
            sendSmsRequest.setTemplateParam(templateParam);
        }
        return sendSmsRequest;
     
    }
    
}