package com.doumee.service.business.third; import cn.emay.sdk.client.SmsSDKClient; import cn.emay.sdk.core.dto.sms.common.ResultModel; import cn.emay.sdk.core.dto.sms.request.SmsSingleRequest; import cn.emay.sdk.core.dto.sms.response.SmsResponse; import com.alibaba.fastjson.JSONObject; import com.doumee.biz.system.SystemDictDataBiz; import com.doumee.core.constants.ResponseStatus; import com.doumee.core.exception.BusinessException; import com.doumee.core.haikang.model.HKConstants; import com.doumee.core.haikang.model.param.BaseResponse; import com.doumee.core.haikang.service.HKService; import com.doumee.core.utils.Constants; import com.doumee.dao.business.model.InterfaceLog; import com.doumee.service.business.InterfaceLogService; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; import javax.annotation.PostConstruct; import java.util.Date; import java.util.HashMap; import java.util.Map; @Service public class EmayService { private static SmsSDKClient client ; public SmsSDKClient getClient() { return client; } public void setClient(SmsSDKClient client) { this.client = client; } @Autowired @Lazy private InterfaceLogService interfaceLogService ; @Autowired @Lazy private SystemDictDataBiz systemDictDataBiz; private void saveInterfaceLog(String path,String name,String req ,String res) { if(interfaceLogService !=null){ InterfaceLog hkMonitoryLogDO=new InterfaceLog(); hkMonitoryLogDO.setType(0); hkMonitoryLogDO.setCreateDate(new Date()); hkMonitoryLogDO.setIsdeleted(0); hkMonitoryLogDO.setRequest(req); hkMonitoryLogDO.setRepose(res); hkMonitoryLogDO.setName(name); hkMonitoryLogDO.setUrl(path); interfaceLogService.create(hkMonitoryLogDO); } } private static String ip; private static int port; private static String appSecret; private static String appKey; @PostConstruct//启动初始化 public int initClient(){ ip = systemDictDataBiz.queryByCode(Constants.SMS,Constants.SMS_IP).getCode(); port = Integer.parseInt(systemDictDataBiz.queryByCode(Constants.SMS,Constants.SMS_PORT).getCode()); appKey = systemDictDataBiz.queryByCode(Constants.SMS,Constants.SMS_APPKEY).getCode(); appSecret = systemDictDataBiz.queryByCode(Constants.SMS,Constants.SMS_APPSECRET).getCode(); return 0; } public boolean sendSingleSms(String mobile,String content) { try { client = new SmsSDKClient(ip,port,appKey,appSecret); if(client == null){ return false; } String customSmsId = "1"; String extendedCode = "01"; SmsSingleRequest request = new SmsSingleRequest(mobile, content, customSmsId, extendedCode, ""); ResultModel result = client.sendSingleSms(request); saveInterfaceLog(ip+"/"+port,"【短信】发送", JSONObject.toJSONString(result),JSONObject.toJSONString(result)); if (result.getCode().equals("SUCCESS")) { System.out.println("请求成功"); SmsResponse response = result.getResult(); System.out.println("sendSingleSms:" + response.toString()); return true; } else { System.out.println("请求失败"); return false; } } catch (Exception e) { } return false; } public void sendSmsByHk(String phone,String content){ try { String url = systemDictDataBiz.queryByCode(Constants.HK_PARAM,Constants.HK_HTTPS).getCode() + systemDictDataBiz.queryByCode(Constants.HK_PARAM,Constants.HK_HOST).getCode() + HKConstants.ARTEMIS_PATH + HKConstants.InterfacePath.sendSms[0]; Map map = new HashMap<>(); map.put("phoneNo",phone.split(",")); map.put("content",content); BaseResponse result = HKService.sendSmsByHk(map,url); if(result !=null){ if(!StringUtils.equals(result.getCode(),HKConstants.RESPONSE_SUCCEE)){ throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(),result.getMsg()); } }else{ throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(),"对不起,短信发送失败,请稍后重试!"); } }catch (Exception e){ e.printStackTrace(); } throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(),"对不起,短信发送失败,请稍后重试!"); } }