Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * 签名的参数名
- */
- private static final String SIGN_PARAM = "user_sign";
- /**
- * 渠道的参数名
- */
- private static final String CHANNEL_PARAM = "sendposition";
- public void sendMessage(String mobile, String content) {
- logger.debug("apply to send message, mobile : " + mobile + ", content : " + content);
- String ip = this.randomIp();
- try { //empty block
- Map<String, Object> map = new HashMap<>();
- map.put("method","sendIntegralWall"); // what is it?
- map.put("modetype",0); // what is it?
- map.put("linkUrl",""); // what is it? empty param will be removed
- map.put("sendposition", productId); // what is it? use your constant CHANNEL_PARAM
- map.put("phone",mobile);
- map.put("title", encodeChinese("短信发送"));
- map.put("content", encodeChinese(content));
- map.put("pageurl","");
- map.put("userid","");
- map.put("msgid",""); // what is it?
- map.put("vericode",""); // what is it?
- map.put("sign","");
- map.put("ip", ip);
- map.put("timestamp", System.currentTimeMillis());
- map.put(SIGN_PARAM, this.sign(map));
- StringBuilder sb = new StringBuilder(sendSmsUrl).append("?");
- for(Map.Entry<String, Object> entry : map.entrySet()){
- sb.append(entry.getKey()).append("=")
- .append(entry.getValue() == null ? "" : String.valueOf(entry.getValue()))
- .append("&");
- }
- sb.deleteCharAt(sb.length() - 1);
- String url = sb.toString();
- logger.debug("send message url : " + url);
- String response = restTemplate.getForObject(url, String.class); //restTemplate.getForObject(url, Map.class);
- logger.info("send message, response : " + response);
- Map<String, String> result = JsonUtil.fromJson(response, Map.class); // not needed
- if(!"200".equals(result.get("code"))){
- throw new RuntimeException("send sms error ! response : " + response);
- }
- } finally { //empty block
- }
- }
- public String sign(Map<String, Object> params){
- if(params == null || params.size() == 0){
- return null;
- }
- Object channel = String.valueOf(params.get(CHANNEL_PARAM));
- if(channel == null) // will be never null: String.valueOf(null) == "null"
- return null;
- params.remove(SIGN_PARAM); // params.get(SIGN_PARAM) always null
- List<String> paramNames = new ArrayList<>(params.keySet());
- Collections.sort(paramNames);
- StringBuilder sb = new StringBuilder();
- for(String paramName : paramNames){
- Object obj = params.get(paramName);
- sb.append(paramName).append("=").append(obj == null ? "" : String.valueOf(obj)).append("&");
- }
- if(sb.length() > 0)
- sb.deleteCharAt(sb.length() - 1);
- return EncryptUtil.HMAC_SHA1(sb.toString(), key);
- }
- private String randomIp(){
- StringBuilder sb = new StringBuilder();
- for(int i = 0; i < 4; i++){
- sb.append(random.nextInt(255)).append("."); // not valid ip, Class A addresses have range from 1-126 in the first octet
- }
- return sb.deleteCharAt(sb.length() - 1).toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement