forked from kidgrow-microservices-platform

zhaoxiaohao
2020-05-22 f21c78ae0e3c410c6ba5be77277b5b491aca3af1
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
package com.kidgrow.sms.util;
 
import com.aliyuncs.CommonResponse;
import com.google.gson.Gson;
import com.kidgrow.sms.exception.SmsException;
import com.kidgrow.sms.template.BatchSmsTemplate;
import com.kidgrow.sms.template.SmsTemplate;
import org.apache.commons.lang3.StringUtils;
 
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;
 
/**
 * 石家庄喜高科技有限责任公司 版权所有 © Copyright 2020<br>
 *
 * @Description: 阿里云短信服务工具类<br>
 * @Project: <br>
 * @CreateDate: Created in 2020/2/17 08:53 <br>
 * @Author: <a href="4345453@kidgrow.com">liuke</a>
 */
public class AliyunSmsUtils {
    private static final String SUCCESS_CODE = "OK";
    /**
     * 宽松校验即可.
     */
    private static final String PHONE_NUMBER_REGEX = "\\d{5,}";
 
    /**
     * 生成随机验证码.
     *
     * @return 随机数
     */
    public static int randomCode() {
        return 100_000 + ThreadLocalRandom.current().nextInt(1_000_000 - 100_000);
    }
 
    /**
     * Map 转 json 字符串的简单实现.
     *
     * @param map the map
     *
     * @return the json string
     */
    public static String toJsonStr(final Map<String, String> map) {
        if (null == map || map.isEmpty()) {
            return null;
        }
 
        final StringBuilder sb = new StringBuilder();
        sb.append('{');
        for (final Map.Entry<String, String> entry : map.entrySet()) {
            sb.append('"')
                    .append(entry.getKey().replace("\"", "\\\""))
                    .append('"')
                    .append(':')
                    .append('"')
                    .append(entry.getValue().replace("\"", "\\\""))
                    .append('"')
                    .append(',');
        }
        sb.deleteCharAt(sb.length() - 1);
        sb.append('}');
        return sb.toString();
    }
 
    /**
     * 校验 SmsTemplate.
     *
     * @param template the SmsTemplate
     */
    public static void checkSmsTemplate(final SmsTemplate template) {
 
        checkNotEmpty(template.getSignName(), "SmsTemplate signName must be not empty");
        checkNotEmpty(template.getTemplateCode(), "SmsTemplate templateCode must be not empty");
        checkNotEmpty(template.getPhoneNumbers(), "SmsTemplate phoneNumbers must be not empty");
 
    }
 
    /**
     * 校验 BatchSmsTemplate.
     *
     * @param template the BatchSmsTemplate
     */
    public static void checkBatchSmsTemplate(final BatchSmsTemplate template) {
 
        checkNotEmpty(template.getSignNames(), "BatchSmsTemplate signNames must be not empty");
        checkNotEmpty(template.getPhoneNumbers(), "BatchSmsTemplate phoneNumbers must be not empty");
        checkNotEmpty(template.getTemplateCode(), "BatchSmsTemplate templateCode must be not empty");
        checkNotEmpty(template.getTemplateParams(), "BatchSmsTemplate templateParams must be not empty");
 
        if (template.getSignNames().size() != template.getPhoneNumbers().size()
                && template.getPhoneNumbers().size() != template.getTemplateParams().size()) {
            throw new IllegalArgumentException("BatchSmsTemplate phoneNumbers, signNames, templateParams size must be the same");
        }
    }
 
    /**
     * 校验 SendSmsResponse 状态.
     *
     * @param response the SendSmsResponse
     */
    public static void checkSmsResponse(final CommonResponse response) {
        if (null == response) {
            throw new SmsException("Response is null");
        }
        final Gson gson = new Gson();
        final Map<String, String> json = gson.fromJson(response.getData(), Map.class);
        if (!SUCCESS_CODE.equalsIgnoreCase(json.get("Code"))) {
            //throw new SmsException("Http status: " + response.getHttpStatus() + ", response: " + response.getData());
            throw new SmsException(response.getData());
        }
    }
 
    /**
     * 校验手机号码(中国).
     *
     * @param phoneNumber the phone number
     */
    public static void checkPhoneNumber(final String phoneNumber) {
        if (null == phoneNumber || !phoneNumber.matches(PHONE_NUMBER_REGEX)) {
            throw new IllegalArgumentException("Invalid phone number");
        }
    }
 
    /**  <br>     2018年3月已知
     中国电信号段
     133,149,153,173,177,180,181,189,199
     中国联通号段
     130,131,132,145,155,156,166,175,176,185,186
     中国移动号段
     134(0-8),135,136,137,138,139,147,150,151,152,157,158,159,178,182,183,184,187,188,198
     其他号段
     14号段以前为上网卡专属号段,如中国联通的是145,中国移动的是147等等。
     虚拟运营商
     电信:1700,1701,1702
     移动:1703,1705,1706
     联通:1704,1707,1708,1709,171
     卫星通信:148(移动) 1349
     */
    public static boolean isMobile(String str) {
        boolean b = false;
        String s2="^[1](([3][0-9])|([4][5,7,9])|([5][0-9])|([6][6])|([7][3,5,6,7,8])|([8][0-9])|([9][8,9]))[0-9]{8}$";// 验证手机号
        if(StringUtils.isNotBlank(str)){
            return str.matches(s2);
        }
        return b;
    }
 
 
 
    /**
     * 校验字符串不为空.
     *
     * @param str the str
     * @param message the message
     */
    public static void checkNotEmpty(final String str, final String message) {
        if (null == str || str.isEmpty()) {
            throw new IllegalArgumentException(message);
        }
    }
 
    /**
     * 校验集合不为空.
     *
     * @param coll the Collection
     * @param message the message
     */
    public static void checkNotEmpty(final Collection coll, final String message) {
        if (null == coll || coll.isEmpty()) {
            throw new IllegalArgumentException(message);
        }
    }
}