|
@@ -0,0 +1,387 @@
|
|
|
+package com.jpsoft.bus.modules.common.utils;
|
|
|
+
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
+import com.jpsoft.bus.modules.wechat.entity.AccessToken;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import net.sf.json.JSONObject;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.io.BufferedReader;
|
|
|
+import java.io.InputStreamReader;
|
|
|
+import java.io.OutputStream;
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.net.HttpURLConnection;
|
|
|
+import java.net.URL;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.regex.Matcher;
|
|
|
+import java.util.regex.Pattern;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+@Component
|
|
|
+public class WechatMessageUtil {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private WeixinUtil weixinUtil;
|
|
|
+
|
|
|
+ public static final String send_template = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 微信发送模版
|
|
|
+ *
|
|
|
+ * @param sendData 发送数据
|
|
|
+ * @param templateId 模版Id
|
|
|
+ * @param openId 发送人
|
|
|
+ * @param url 详情跳转地址
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public boolean sendTemplate(JSONObject sendData, String appId, String appSecret, String templateId, String openId, String url) {
|
|
|
+ boolean result = false;
|
|
|
+
|
|
|
+ AccessToken accessToken = weixinUtil.getCommonAccessToken(appId, appSecret);
|
|
|
+
|
|
|
+ if (accessToken != null) {
|
|
|
+ //发送模版内容
|
|
|
+ String sendTemplateUrl = send_template.replace("ACCESS_TOKEN", accessToken.getToken());
|
|
|
+
|
|
|
+ JSONObject sendTemplateData = new JSONObject();
|
|
|
+
|
|
|
+ sendTemplateData.put("touser", openId);
|
|
|
+ sendTemplateData.put("template_id", templateId);
|
|
|
+ sendTemplateData.put("url", url);
|
|
|
+ sendTemplateData.put("data", sendData);
|
|
|
+
|
|
|
+ log.warn("模版发送Id>>>>" + templateId + ">>>模版发送数据>>>>>" + sendTemplateData.toString());
|
|
|
+
|
|
|
+ String sendTemplateRet = HttpConnectionUtil.requestByPost(sendTemplateUrl, sendTemplateData.toString());
|
|
|
+
|
|
|
+ log.warn("模版发送返回数据>>>>>" + sendTemplateRet);
|
|
|
+
|
|
|
+ if (StringUtils.isNotBlank(sendTemplateRet)) {
|
|
|
+ JSONObject sendTemplateJson = JSONObject.fromObject(sendTemplateRet);
|
|
|
+
|
|
|
+ if ("0".equals(sendTemplateJson.getString("errcode"))) {
|
|
|
+ //发送成功
|
|
|
+ result = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String sendNativeTemplate(String appId, String appSecret, String templateId, String openId, JSONObject sendData, String urlPath) {
|
|
|
+ String result = "";
|
|
|
+
|
|
|
+ try {
|
|
|
+ AccessToken accessToken = weixinUtil.getCommonAccessToken(appId, appSecret);
|
|
|
+
|
|
|
+ if (accessToken == null) {
|
|
|
+ throw new Exception("token无法获取");
|
|
|
+ }
|
|
|
+
|
|
|
+ //发送模版内容
|
|
|
+ String sendTemplateUrl = send_template.replace("ACCESS_TOKEN", accessToken.getToken());
|
|
|
+
|
|
|
+ JSONObject sendTemplateData = new JSONObject();
|
|
|
+
|
|
|
+ sendTemplateData.put("touser", openId);
|
|
|
+ sendTemplateData.put("template_id", templateId);
|
|
|
+ sendTemplateData.put("url", urlPath);
|
|
|
+ sendTemplateData.put("data", sendData);
|
|
|
+
|
|
|
+ URL url = new URL(sendTemplateUrl);
|
|
|
+ HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
|
|
+ conn.setRequestMethod("POST");
|
|
|
+ conn.setDoOutput(true);
|
|
|
+ conn.setDoInput(true);
|
|
|
+ conn.setUseCaches(false);
|
|
|
+ conn.setRequestProperty("Connection", "Keep-Alive");
|
|
|
+ conn.setRequestProperty("Charset", "UTF-8");
|
|
|
+
|
|
|
+ // 设置文件类型:
|
|
|
+ conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
|
|
|
+
|
|
|
+ // 设置接收类型否则返回415错误
|
|
|
+ //conn.setRequestProperty("accept","*/*")此处为暴力方法设置接受所有类型,以此来防范返回415;
|
|
|
+ conn.setRequestProperty("accept", "application/json");
|
|
|
+ conn.setConnectTimeout(5000);
|
|
|
+ conn.setReadTimeout(5000);
|
|
|
+
|
|
|
+ String dataStr = sendTemplateData.toString();
|
|
|
+
|
|
|
+ log.warn("发送数据:" + dataStr);
|
|
|
+ // 往服务器里面发送数据
|
|
|
+ byte[] buffer = dataStr.getBytes("UTF-8");
|
|
|
+
|
|
|
+ // 设置文件长度
|
|
|
+ conn.setRequestProperty("Content-Length", String.valueOf(buffer.length));
|
|
|
+ OutputStream output = conn.getOutputStream();
|
|
|
+ output.write(buffer);
|
|
|
+ output.flush();
|
|
|
+ output.close();
|
|
|
+
|
|
|
+ if (conn.getResponseCode() == 200) {
|
|
|
+ BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
|
|
|
+
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ String line = "";
|
|
|
+
|
|
|
+ while ((line = reader.readLine()) != null) {
|
|
|
+ sb.append(line);
|
|
|
+ }
|
|
|
+
|
|
|
+ result = sb.toString();
|
|
|
+
|
|
|
+ log.warn("接收数据:" + result);
|
|
|
+
|
|
|
+ reader.close();
|
|
|
+ }
|
|
|
+ } catch (Exception ex) {
|
|
|
+ log.error(ex.getMessage(), ex);
|
|
|
+ }
|
|
|
+
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getContent(String content) {
|
|
|
+ String finallyContent = content;
|
|
|
+ String regex = "<p.*?>(.*?)</p>";
|
|
|
+ Pattern p = Pattern.compile(regex);
|
|
|
+ Matcher m = p.matcher(content);
|
|
|
+ while (m.find()) {
|
|
|
+ finallyContent = m.group(1);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ return finallyContent;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 老师退回学生发送模板消息
|
|
|
+ *
|
|
|
+ * @param openId
|
|
|
+ * @param teacherName
|
|
|
+ * @param appId
|
|
|
+ * @param appSecret
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public boolean sendReturnExamineStudent(String openId, String teacherName, String reason, String appId, String appSecret) {
|
|
|
+
|
|
|
+
|
|
|
+ String templateId = "MKELH20mSQHRKlvD9FbQPizzEI7jNV-9Gv5OsB6WOc8";
|
|
|
+ JSONObject sendData = new JSONObject();
|
|
|
+
|
|
|
+ JSONObject first = new JSONObject();
|
|
|
+ first.put("value", "申请处理提醒!");
|
|
|
+ first.put("color", "#FF0000");
|
|
|
+
|
|
|
+ JSONObject keyword1 = new JSONObject();
|
|
|
+ keyword1.put("value", DateUtil.format(new Date(), "yyyy-MM-dd HH:mm"));
|
|
|
+ keyword1.put("color", "#173177");
|
|
|
+
|
|
|
+ JSONObject keyword2 = new JSONObject();
|
|
|
+ keyword2.put("value", teacherName);
|
|
|
+ keyword2.put("color", "#173177");
|
|
|
+
|
|
|
+ // String remarkStr = "发现有一例体温异常情况,点击查看详情";
|
|
|
+ JSONObject remark = new JSONObject();
|
|
|
+ remark.put("value", reason);
|
|
|
+ remark.put("color", "#173177");
|
|
|
+
|
|
|
+ sendData.put("first", first);
|
|
|
+ sendData.put("keyword1", keyword1);
|
|
|
+ sendData.put("keyword2", keyword2);
|
|
|
+ sendData.put("remark", remark);
|
|
|
+
|
|
|
+
|
|
|
+ boolean ret = sendTemplate(sendData, appId, appSecret, templateId, openId, "");
|
|
|
+
|
|
|
+ return ret;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 学平险发送模板消息
|
|
|
+ * @param openId
|
|
|
+ * @param detail 首行内容
|
|
|
+ * @param policyNo 保单号
|
|
|
+ * @param insuredName 被保人
|
|
|
+ * @param insurance 保险方案
|
|
|
+ * @param startTime 开始时间
|
|
|
+ * @param endTime 结束时间
|
|
|
+ * @param appId
|
|
|
+ * @param appSecret
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public boolean sendInsuranceMessage(String openId, String detail, String policyNo, String insuredName,String insurance,String startTime,String endTime,String reason ,String url,String appId, String appSecret) {
|
|
|
+
|
|
|
+
|
|
|
+ String templateId = "2mmM4l5Xh1ViB112EvkWkhJj8k7HQLtXxTeCqWr8vBA";
|
|
|
+ JSONObject sendData = new JSONObject();
|
|
|
+
|
|
|
+ JSONObject first = new JSONObject();
|
|
|
+ first.put("value", detail);
|
|
|
+ first.put("color", "#FF0000");
|
|
|
+
|
|
|
+ JSONObject keyword1 = new JSONObject();
|
|
|
+ keyword1.put("value", policyNo);
|
|
|
+ keyword1.put("color", "#173177");
|
|
|
+
|
|
|
+ JSONObject keyword2 = new JSONObject();
|
|
|
+ keyword2.put("value", insuredName);
|
|
|
+ keyword2.put("color", "#173177");
|
|
|
+
|
|
|
+ JSONObject keyword3 = new JSONObject();
|
|
|
+ keyword3.put("value", insurance);
|
|
|
+ keyword3.put("color", "#173177");
|
|
|
+
|
|
|
+ JSONObject keyword4 = new JSONObject();
|
|
|
+ keyword4.put("value", startTime);
|
|
|
+ keyword4.put("color", "#173177");
|
|
|
+
|
|
|
+ JSONObject keyword5 = new JSONObject();
|
|
|
+ keyword5.put("value", endTime);
|
|
|
+ keyword5.put("color", "#173177");
|
|
|
+
|
|
|
+ // String remarkStr = "前往签字";
|
|
|
+ JSONObject remark = new JSONObject();
|
|
|
+ remark.put("value", reason);
|
|
|
+ remark.put("color", "#ed1414");
|
|
|
+
|
|
|
+ sendData.put("first", first);
|
|
|
+ sendData.put("keyword1", keyword1);
|
|
|
+ sendData.put("keyword2", keyword2);
|
|
|
+ sendData.put("keyword3", keyword3);
|
|
|
+ sendData.put("keyword4", keyword4);
|
|
|
+ sendData.put("keyword5", keyword5);
|
|
|
+ sendData.put("remark", remark);
|
|
|
+
|
|
|
+
|
|
|
+ boolean ret = sendTemplate(sendData, appId, appSecret, templateId, openId, url);
|
|
|
+
|
|
|
+ return ret;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * vip移动充值模板消息
|
|
|
+ * @param openId
|
|
|
+ * @param detail 首行内容
|
|
|
+ * @param appId
|
|
|
+ * @param appSecret
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public boolean sendVipMoveMessage(String openId, String detail, String payPersonName, String result,String reason ,String url,String appId, String appSecret) {
|
|
|
+
|
|
|
+
|
|
|
+ String templateId = "9SsNV33hxxtlJuTYN4jo2toIkrxFJSKyf4KZ-g2Z6Io";
|
|
|
+ JSONObject sendData = new JSONObject();
|
|
|
+
|
|
|
+ JSONObject first = new JSONObject();
|
|
|
+ first.put("value", detail);
|
|
|
+ first.put("color", "#FF0000");
|
|
|
+
|
|
|
+ JSONObject keyword1 = new JSONObject();
|
|
|
+ keyword1.put("value", payPersonName);
|
|
|
+ keyword1.put("color", "#173177");
|
|
|
+
|
|
|
+ JSONObject keyword2 = new JSONObject();
|
|
|
+ keyword2.put("value", result);
|
|
|
+ keyword2.put("color", "#173177");
|
|
|
+
|
|
|
+ JSONObject keyword3 = new JSONObject();
|
|
|
+ keyword3.put("value", DateUtil.format(new Date(),"yyyy年MM月dd日"));
|
|
|
+ keyword3.put("color", "#173177");
|
|
|
+
|
|
|
+ JSONObject remark = new JSONObject();
|
|
|
+ remark.put("value", reason);
|
|
|
+ remark.put("color", "#ed1414");
|
|
|
+
|
|
|
+ sendData.put("first", first);
|
|
|
+ sendData.put("keyword1", keyword1);
|
|
|
+ sendData.put("keyword2", keyword2);
|
|
|
+ sendData.put("keyword3", keyword3);
|
|
|
+ sendData.put("remark", remark);
|
|
|
+
|
|
|
+
|
|
|
+ boolean ret = sendTemplate(sendData, appId, appSecret, templateId, openId, url);
|
|
|
+
|
|
|
+ return ret;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * @param schoolName
|
|
|
+ * @param studentName
|
|
|
+ * @param amount
|
|
|
+ * @param openId
|
|
|
+ * @param createTime
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public boolean sendOrderInfo(String name,String orderRemark,String schoolName,String studentName,BigDecimal amount,String openId,Date createTime,String url,String appId, String appSecret) {
|
|
|
+
|
|
|
+
|
|
|
+ String templateId = "EtUairBxzoipyp8xMEVjctnDKGCfU4v5PxjSGUGNH2E";
|
|
|
+ JSONObject sendData = new JSONObject();
|
|
|
+
|
|
|
+ JSONObject first = new JSONObject();
|
|
|
+ first.put("value", "您有一个新的订单,点击查看详情!");
|
|
|
+ first.put("color", "#FF0000");
|
|
|
+
|
|
|
+ JSONObject keyword1 = new JSONObject();
|
|
|
+ keyword1.put("value", name);
|
|
|
+ keyword1.put("color", "#173177");
|
|
|
+
|
|
|
+ JSONObject keyword2 = new JSONObject();
|
|
|
+ keyword2.put("value", studentName);
|
|
|
+ keyword2.put("color", "#173177");
|
|
|
+
|
|
|
+ JSONObject keyword3 = new JSONObject();
|
|
|
+
|
|
|
+ keyword3.put("value", "¥" + amount.setScale(2) + "元");
|
|
|
+ keyword3.put("color", "#173177");
|
|
|
+
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+
|
|
|
+ JSONObject keyword4 = new JSONObject();
|
|
|
+ keyword4.put("value", sdf.format(createTime));
|
|
|
+ keyword4.put("color", "#173177");
|
|
|
+
|
|
|
+ /* String remarkStr = "账单来源:" + schoolName;
|
|
|
+
|
|
|
+ if(StringUtils.isNotBlank(billRemark)){
|
|
|
+ remarkStr = remarkStr + ",备注:"+ billRemark +"";
|
|
|
+ }
|
|
|
+*/
|
|
|
+ JSONObject remark = new JSONObject();
|
|
|
+ remark.put("value", orderRemark);
|
|
|
+ remark.put("color", "#173177");
|
|
|
+
|
|
|
+ sendData.put("first", first);
|
|
|
+ sendData.put("keyword1", keyword1);
|
|
|
+ sendData.put("keyword2", keyword2);
|
|
|
+ sendData.put("keyword3", keyword3);
|
|
|
+ sendData.put("keyword4", keyword4);
|
|
|
+ sendData.put("remark", remark);
|
|
|
+
|
|
|
+ boolean ret = false;
|
|
|
+ try{
|
|
|
+ //w8Zk_VQMFIEVSIBPZid7zSrvHmBdrgnqF76u8PLCZEs cs
|
|
|
+ ret = sendTemplate(sendData, appId, appSecret, templateId, openId, url);
|
|
|
+
|
|
|
+ }catch (Exception ex){
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ return ret;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|