package com.dobbinsoft.fw.support.sms; import com.dobbinsoft.fw.core.exception.CoreExceptionDefinition; import com.dobbinsoft.fw.core.exception.ServiceException; import com.dobbinsoft.fw.core.exception.ThirdPartServiceException; import com.dobbinsoft.fw.support.properties.FwSMSProperties; import com.fasterxml.jackson.databind.ObjectMapper; import org.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.annotation.Autowired; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * Created with IntelliJ IDEA. * Description: * User: rize * Date: 2019/11/17 * Time: 15:50 */ public class ZjunSMSClient implements SMSClient, InitializingBean { private static final Logger logger = LoggerFactory.getLogger(ZjunSMSClient.class); @Autowired private FwSMSProperties properties; private String userName; private String password; private String content; private String url; @Override public void afterPropertiesSet() throws Exception { this.userName = properties.getZjunUserName(); this.password = properties.getZjunPassword(); this.content = properties.getZjunContent(); this.url = "http://sms.izjun.com:8001/sms/api/sendMessageMass"; } @Override public SMSResult sendRegisterVerify(String phone, String verifyCode) throws ServiceException { return send(phone, verifyCode); } @Override public SMSResult sendBindPhoneVerify(String phone, String verifyCode) throws ServiceException { return send(phone, verifyCode); } @Override public SMSResult sendResetPasswordVerify(String phone, String verifyCode) throws ServiceException { return send(phone, verifyCode); } @Override public SMSResult sendAdminLoginVerify(String phone, String verifyCode) throws ServiceException { return send(phone, verifyCode); } private SMSResult send(String phone, String verifyCode) throws ServiceException { logger.info("[掌骏短信发送] phone=" + phone + "; verifyCode=" + verifyCode); try { // 手机号 List phoneList = new ArrayList<>(); phoneList.add(phone); // 替换内容中的验证码占位符 String content = this.content.replace("${code}", verifyCode); // 时间戳 long timestamp = System.currentTimeMillis(); // 计算 sign 参数 String sign = md5(this.userName + timestamp + md5(this.password)); // 请求参数 Map data = new HashMap<>(); data.put("userName", this.userName); data.put("phoneList", phoneList); data.put("content", content); data.put("timestamp", timestamp); data.put("sign", sign); // 将字典转换为 JSON 字符串 String json = new JSONObject(data).toString(); logger.info("[掌骏短信发送] " + json); // 发送 POST 请求 ZjunSMSResult zjunSMSResult = post(this.url, json); SMSResult smsResult = new SMSResult(); if (zjunSMSResult.getCode() == 0) { smsResult.setSucc(true); smsResult.setMsg("OK"); } else { smsResult.setSucc(false); smsResult.setMsg(zjunSMSResult.getMessage()); } return smsResult; } catch (Exception e) { logger.error("[掌骏短信发送] 异常", e); throw new ThirdPartServiceException("掌骏短信发送未知异常", CoreExceptionDefinition.THIRD_PART_SERVICE_EXCEPTION.getCode()); } } private static String md5(String str) throws NoSuchAlgorithmException { MessageDigest md = MessageDigest.getInstance("MD5"); byte[] array = md.digest(str.getBytes(StandardCharsets.UTF_8)); StringBuilder sb = new StringBuilder(); for (byte item : array) { sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3)); } return sb.toString(); } public static ZjunSMSResult post(String url, String data) throws IOException, ThirdPartServiceException { HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/json"); connection.setDoOutput(true); try (OutputStream os = connection.getOutputStream()) { os.write(data.getBytes(StandardCharsets.UTF_8)); os.flush(); } StringBuilder response = new StringBuilder(); try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) { String line; while ((line = br.readLine()) != null) { response.append(line); } } ObjectMapper mapper = new ObjectMapper(); try { return mapper.readValue(response.toString(), ZjunSMSResult.class); } catch (Exception e) { logger.error("[掌骏短信发送JSON转换] 异常", e); throw new ThirdPartServiceException("掌骏短信发送JSON转换异常", CoreExceptionDefinition.THIRD_PART_SERVICE_EXCEPTION.getCode()); } } }