xuekang
2024-05-11 bac0878349a1db23e7b420ea164e22fb9db73a99
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
package com.nuvole.util.sms;
 
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.nuvole.common.domain.emnu.SMSResultEmnu;
import com.nuvole.common.domain.result.SMSResult;
 
import java.util.HashMap;
import java.util.Map;
 
/**
 * 短信(云片)
 *
 * @Author: lc
 * @Date: 2019/6/1 11:09
 */
public class YpSMSUtil {
 
 
    private static String YP_SMS_K = "faf531146ca1e38abacd3862fb3fc32b";
    private static String YP_SMS_URL = "https://sms.yunpian.com/v2/sms/single_send.json";
 
 
    /**
     * 发送短信
     *
     * @param mobile  手机号码字符串 多个号码以逗号分割
     * @param message 短信内容
     * @Author: lc
     * @Date: 2019/6/1 11:27
     */
    public static SMSResult sendSMS(String mobile, String message) {
 
        if (StrUtil.isEmpty(mobile)) {
            return new SMSResult(SMSResultEmnu.ERROR, "手机号不能为空!");
        }
        if (StrUtil.isEmpty(message)) {
            return new SMSResult(SMSResultEmnu.ERROR, "短信内容不能为空!");
        }
        try {
            Map<String, Object> param = new HashMap<>();
            param.put("apikey", YP_SMS_K);
            param.put("mobile", mobile);
            param.put("text", message);
            String msg = HttpUtil.post(YP_SMS_URL, param);
            JSONObject jsonObject = JSONObject.parseObject(msg);
            if (!"0".equals(jsonObject.getString("code"))) {
                return new SMSResult(SMSResultEmnu.ERROR, jsonObject.getString("msg"));
            }
            return new SMSResult(SMSResultEmnu.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return new SMSResult(SMSResultEmnu.ERROR, "发送异常!");
        }
    }
 
    public static void main(String[] args) {
        System.out.println(sendSMS("17803846500", SMSTemplate.getVaildCodeMsg(RandomUtil.randomNumbers(6))));
    }
 
}