郑永安
2023-09-19 69185134fcfaf913ea45f1255677225a2cc311a4
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
package com.gk.hotwork.Sms.Supplier;
 
import cn.hutool.crypto.symmetric.AES;
import com.alibaba.fastjson.JSONObject;
import com.gk.hotwork.Domain.Exception.BusinessException;
import com.gk.hotwork.Domain.Utils.HttpUtils;
import com.gk.hotwork.Sms.Supplier.Abstract.Supplier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
 
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
/**
* @Description: 聚合
* @date 2022/5/18 16:49
*/
@Component("jh")
@ConfigurationProperties(prefix = "sms.supplier.jh")
public class Jh extends Supplier {
 
 
    private AES aes = new AES("CBC", "PKCS7Padding",
            // 密钥,不可更改
            "kikokiko00012345".getBytes(),
            // iv加盐
            "wakuwaku00012345".getBytes());
 
    public boolean sendSms(String mobile, String tplId, Map<String,Object> paramsContent) {
        try {
            Map<String, Object> params = new HashMap<>();
            params.put("mobile", mobile);//接收短信的手机号码
            params.put("tpl_id", tplId);//短信模板ID,请参考个人中心短信模板设置
            StringBuilder sb = new StringBuilder();
            for (Map.Entry<String, Object> entry : paramsContent.entrySet()) {
                //包含特殊字符# 使用转义字符代替
                String value = entry.getValue().toString().replace("#", "%23");
                sb.append("&#").append(entry.getKey())
                        .append("#=").append(value);
            }
            params.put("tpl_value", sb.deleteCharAt(0).toString());
            params.put("key", this.keyDecoder(this.getKey()));//应用APPKEY(应用详细页查询)
            params.put("dtype", "json");//返回数据的格式,xml或json,默认json
            String response = HttpUtils.net(this.getUrl(), params, "GET");
            JSONObject jsonResult = JSONObject.parseObject(response);
            String errorCode = jsonResult.getString("error_code");
            String reason = jsonResult.getString("reason");
            if ("0".equals(errorCode)) {
                return true;
            } else {
                throw new BusinessException(reason);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("系统错误");
        }
    }
 
    @Override
    public String keyDecoder(String key)  {
       return aes.decryptStr(key);
    }
 
 
}