| ¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.smartor.common; |
| | | |
| | | import com.alibaba.fastjson2.JSON; |
| | | import com.smartor.domain.MtSubmitSmResp; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Value; |
| | | import org.springframework.http.HttpEntity; |
| | | import org.springframework.http.HttpHeaders; |
| | | import org.springframework.http.MediaType; |
| | | import org.springframework.http.ResponseEntity; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.web.client.RestTemplate; |
| | | |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.security.MessageDigest; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.Date; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * MTæ¶æ¯åéå·¥å
·ç±»ï¼SPåè¡ä¸æ±èå¹³å°æäº¤MTæ¶æ¯ï¼ |
| | | * æ¥å£å°åï¼http://ip:port/submitsm |
| | | * ç¼ç ï¼UTF-8ï¼é´æç®æ³ï¼MD5 |
| | | */ |
| | | @Slf4j |
| | | @Component |
| | | public class MtSubmitSmUtil { |
| | | private static final RestTemplate restTemplate = new RestTemplate(); |
| | | |
| | | @Value("${sms_password}") |
| | | private String password; |
| | | |
| | | @Value("${sms_accountName}") |
| | | private String accountName; |
| | | |
| | | @Value("${sms_url}") |
| | | private String url; |
| | | |
| | | @Value("${sms_sourceAddr}") |
| | | private String sourceAddr; |
| | | |
| | | @Value("${sms_serviceCode}") |
| | | private String serviceCode; |
| | | |
| | | @Value("${sms_appkey}") |
| | | private String appKey; |
| | | |
| | | @Value("${sms_flag}") |
| | | private String smsFlag; |
| | | |
| | | |
| | | /** |
| | | * åéMTæ¶æ¯ |
| | | * |
| | | * @param destAddr ç®æ å·ç |
| | | * @param content çä¿¡å
容 |
| | | * @return MtSubmitSmResp ååºç»æ |
| | | */ |
| | | public Map<String, Object> submitSm(String destAddr, String content) { |
| | | try { |
| | | log.info("ååéä¸åéçä¿¡ï¼destAddrï¼{}ï¼contentï¼{}", destAddr, content); |
| | | String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()); |
| | | String hashHex = buildHashHex(accountName, password, date); |
| | | |
| | | Map<String, Object> authMap = new HashMap<>(); |
| | | authMap.put("Algorithm", "MD5"); |
| | | authMap.put("Date", date); |
| | | authMap.put("HASHHEX", hashHex); |
| | | |
| | | Map<String, Object> reqMap = new HashMap<>(); |
| | | reqMap.put("accountName", accountName); |
| | | reqMap.put("Authorization", authMap); |
| | | reqMap.put("DestAddr", destAddr); |
| | | reqMap.put("SourceAddr", sourceAddr); |
| | | reqMap.put("Content", smsFlag + content); |
| | | reqMap.put("ServiceCode", serviceCode); |
| | | |
| | | HttpHeaders headers = new HttpHeaders(); |
| | | headers.setContentType(new MediaType("application", "json", StandardCharsets.UTF_8)); |
| | | headers.set("Accept-Language", "UTF-8"); |
| | | headers.set("app-key", "ak-BFq4NN0TMf92J7KAioNYGz74"); |
| | | |
| | | HttpEntity<String> entity = new HttpEntity<>(JSON.toJSONString(reqMap), headers); |
| | | log.info("MtSubmitSm 请æ±å°åï¼{}ï¼è¯·æ±åæ°ï¼{}", url + "/submitsm", JSON.toJSONString(reqMap)); |
| | | |
| | | ResponseEntity<String> response = restTemplate.postForEntity(url, entity, String.class); |
| | | log.info("MtSubmitSm ååºç»æï¼{}", response.getBody()); |
| | | |
| | | return JSON.parseObject(response.getBody(), Map.class); |
| | | } catch (Exception e) { |
| | | log.error("MtSubmitSm åé失败ï¼urlï¼{}ï¼destAddrï¼{}ï¼errorï¼{}", url, destAddr, e.getMessage(), e); |
| | | Map<String, Object> resp = new HashMap<>(); |
| | | resp.put("code", -1); |
| | | resp.put("message", "åéå¼å¸¸ï¼" + e.getMessage()); |
| | | return resp; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 计ç®HASHHEXï¼MD5(Algorithm:å¸å·å:å¯ç :Date) |
| | | * ä¾ï¼MD5:95555:12345:2016-10-09 16:19:43 |
| | | */ |
| | | private static String buildHashHex(String accountName, String password, String date) throws Exception { |
| | | String plain = "MD5:" + accountName + ":" + password + ":" + date; |
| | | MessageDigest md = MessageDigest.getInstance("MD5"); |
| | | byte[] bytes = md.digest(plain.getBytes(StandardCharsets.UTF_8)); |
| | | StringBuilder sb = new StringBuilder(); |
| | | for (byte b : bytes) { |
| | | sb.append(String.format("%02x", b)); |
| | | } |
| | | return sb.toString(); |
| | | } |
| | | } |