MessageSendUtil.java 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package cn.com.lzt.message.send.util;
  2. import cn.com.lzt.message.send.entity.MMessageEntity;
  3. import cn.com.lzt.message.send.entity.MMessageSendLogEntity;
  4. import cn.com.lzt.message.send.entity.MessageSendCallBack;
  5. import cn.com.lzt.message.send.entity.MessageWxTemplateConfig;
  6. import cn.com.lzt.message.send.service.MMessageSendServiceI;
  7. import cn.com.lzt.message.send.service.impl.MMessageSendServiceImpl;
  8. import cn.com.lzt.warehouse.entity.WarehouseEntity;
  9. import com.aliyuncs.DefaultAcsClient;
  10. import com.aliyuncs.IAcsClient;
  11. import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
  12. import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
  13. import com.aliyuncs.exceptions.ClientException;
  14. import com.aliyuncs.http.MethodType;
  15. import com.aliyuncs.profile.DefaultProfile;
  16. import com.aliyuncs.profile.IClientProfile;
  17. import com.xcgl.utils.XcglConstant;
  18. import com.xcgl.weixin.service.WXServiceI;
  19. import com.xcgl.weixin.service.WXServiceImpl;
  20. import net.sf.json.JSONObject;
  21. import org.apache.commons.lang3.StringUtils;
  22. import org.apache.http.HttpResponse;
  23. import org.apache.http.HttpStatus;
  24. import org.apache.http.client.methods.HttpPost;
  25. import org.apache.http.entity.StringEntity;
  26. import org.apache.http.impl.client.DefaultHttpClient;
  27. import org.apache.log4j.Logger;
  28. import org.jeecgframework.core.constant.Globals;
  29. import org.jeecgframework.core.util.ApplicationContextUtil;
  30. import org.jeecgframework.core.util.DateUtils;
  31. import org.jeecgframework.core.util.ResourceUtil;
  32. import org.jeecgframework.web.system.pojo.base.TSUser;
  33. import org.jeecgframework.web.system.service.SystemService;
  34. import java.nio.charset.Charset;
  35. import java.util.*;
  36. import java.util.regex.Matcher;
  37. import java.util.regex.Pattern;
  38. public class MessageSendUtil
  39. {
  40. private static final Logger logger = Logger.getLogger(MessageSendUtil.class);
  41. public static MMessageSendLogEntity sendSMS(String phone, String templateCode, Map<String,Object> params) {
  42. MMessageSendLogEntity cb = new MMessageSendLogEntity();
  43. try {
  44. //设置超时时间-可自行调整
  45. System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
  46. System.setProperty("sun.net.client.defaultReadTimeout", "10000");
  47. //初始化ascClient需要的几个参数
  48. final String product = "Dysmsapi";//短信API产品名称(短信产品名固定,无需修改)
  49. final String domain = "dysmsapi.aliyuncs.com";//短信API产品域名(接口地址固定,无需修改)
  50. //替换成你的AK
  51. final String accessKeyId = "LTAImsMOGNQTqWvX";//你的accessKeyId,参考本文档步骤2
  52. final String accessKeySecret = "zdf5cux8RzTl531tQQ1rIlESTBBiBe";//你的accessKeySecret,参考本文档步骤2
  53. //初始化ascClient,暂时不支持多region(请勿修改)
  54. IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId,
  55. accessKeySecret);
  56. DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
  57. IAcsClient acsClient = new DefaultAcsClient(profile);
  58. //组装请求对象
  59. SendSmsRequest request = new SendSmsRequest();
  60. //使用post提交
  61. request.setMethod(MethodType.POST);
  62. //必填:待发送手机号。支持以逗号分隔的形式进行批量调用,批量上限为1000个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式;发送国际/港澳台消息时,接收号码格式为00+国际区号+号码,如“0085200000000”
  63. if(!ResourceUtil.getConfigByName(Globals.SERVER_TYPE).equals(Globals.SERVER_TYPE_LINE)){
  64. phone = ResourceUtil.getConfigByName(Globals.SERVER_TEST_MOBILE);
  65. }
  66. request.setPhoneNumbers(phone);
  67. //必填:短信签名-可在短信控制台中找到
  68. request.setSignName("美都环卫物业");
  69. //必填:短信模板-可在短信控制台中找到,发送国际/港澳台消息时,请使用国际/港澳台短信模版
  70. request.setTemplateCode(templateCode);//SMS_144450234
  71. //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
  72. //友情提示:如果JSON中需要带换行符,请参照标准的JSON协议对换行符的要求,比如短信内容中包含\r\n的情况在JSON中需要表示成\\r\\n,否则会导致JSON在服务端解析失败
  73. JSONObject obj = JSONObject.fromObject(params);
  74. request.setTemplateParam(obj.toString());
  75. //可选-上行短信扩展码(扩展码字段控制在7位或以下,无特殊需求用户请忽略此字段)
  76. //request.setSmsUpExtendCode("90997");
  77. //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
  78. // request.setOutId("yourOutId");
  79. //请求失败这里会抛ClientException异常
  80. SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
  81. cb.setCallback(sendSmsResponse.getMessage());
  82. if(sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK")) {
  83. cb.setStatus(1);
  84. }else {
  85. cb.setStatus(0);
  86. }
  87. }catch (Exception e){
  88. cb.setStatus(0);
  89. cb.setCallback(e.getMessage());
  90. }
  91. // return false;
  92. cb.setSendWay(MMessageEntity.MessageSendWay.short_message);
  93. cb.setCreateTime(new Date());
  94. return cb;
  95. }
  96. public static boolean sendMsgToWX(String templateID,String userId,String openId,
  97. String url,Map<String, Object> userMsgParms) {
  98. return sendMsgToWX(templateID, userId, openId, url, userMsgParms, true);
  99. }
  100. /**
  101. * 注:此方法立即发送不会通过mq,不会产生发送日志及已读状态
  102. * @param checkTestMode 是否检测测试服模式 有些功能需要直接发给执行人执行任务
  103. * @see MMessageSendServiceI#sendMessage
  104. * @return
  105. */
  106. public static boolean sendMsgToWX(String templateID,String userId,String openId,
  107. String url,Map<String, Object> userMsgParms, boolean checkTestMode) {
  108. List<Map<String,Object>> listParams = new ArrayList<Map<String,Object>>();
  109. Map<String, Object> oneMsg = new HashMap<String, Object>();
  110. oneMsg.put("openid", openId);
  111. oneMsg.put("userid", userId);
  112. if(StringUtils.isNotBlank(url)) {
  113. oneMsg.put("url", url);
  114. }
  115. oneMsg.put("tplParams", userMsgParms);
  116. listParams.add(oneMsg);
  117. return sendMsgToWX(listParams,templateID, checkTestMode);
  118. }
  119. public static boolean sendMsgToWX(List<Map<String,Object>> listParams,String templateID) {
  120. return sendMsgToWX(listParams, templateID, true);
  121. }
  122. /**
  123. *
  124. * @param listParams
  125. * @param templateID
  126. * @return
  127. */
  128. public static boolean sendMsgToWX(List<Map<String,Object>> listParams,String templateID, boolean checkTestMode) {
  129. logger.info("sendMsgToWX templateID=" + templateID);
  130. Map<String,Object> reqParams = new HashMap<String,Object>();
  131. reqParams.put("templateId", templateID);
  132. reqParams.put("list", listParams);
  133. if(checkTestMode && !ResourceUtil.getConfigByName(Globals.SERVER_TYPE).equals(Globals.SERVER_TYPE_LINE)){
  134. String openId = ResourceUtil.getConfigByName(Globals.SERVER_TEST_OPENID);
  135. for(Map<String,Object> oneMsg :listParams){
  136. oneMsg.put("openid", openId);
  137. }
  138. }
  139. org.apache.http.client.HttpClient client = new DefaultHttpClient();
  140. try {
  141. String wxurl = "http://www.shenqin.work/api/wx/sendTplMsg";
  142. HttpPost httpPost = new HttpPost(wxurl);
  143. httpPost.addHeader("Content-type","application/json; charset=utf-8");
  144. httpPost.setEntity(new StringEntity(com.alibaba.fastjson.JSONObject.toJSONString(reqParams), Charset.forName("UTF-8")));
  145. HttpResponse response = client.execute(httpPost);
  146. // 检验返回码
  147. int statusCode = response.getStatusLine().getStatusCode();
  148. if (statusCode != HttpStatus.SC_OK) {
  149. logger.error("发送微信msg错误:" + statusCode);
  150. return true;
  151. }
  152. }catch (Exception e) {
  153. logger.error("发送微信msg错误:" + e.getMessage());
  154. return false;
  155. }
  156. return false;
  157. }
  158. public static void main(String[] ars){
  159. }
  160. }