|
@@ -0,0 +1,440 @@
|
|
|
+package com.jpsoft.enterprise.modules.common.utils;
|
|
|
+
|
|
|
+import com.jpsoft.enterprise.modules.wechat.entity.AccessToken;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import net.sf.json.JSONObject;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+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
|
|
|
+@Service
|
|
|
+public class WechatMessageUtil {
|
|
|
+ public static final String send_template = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN";
|
|
|
+
|
|
|
+ private static final String TEMPLATE_REDIRECT_PRE_URL = "http://wuyeopenlocktest-wecat.sudaonline.net";
|
|
|
+
|
|
|
+ private static final String TEMPERATURE_ALARM_PRE_URL = "http://wisdomhousewechat.sudaonline.net/prevention/addetail_fll.html?id=";
|
|
|
+
|
|
|
+ public static boolean sendBillInfo(String fullName, BigDecimal amount, String openId,
|
|
|
+ String detailAmount, String propertyName, String billRemark, String billDetailId, String billTemplateId, String appId, String appSecret) {
|
|
|
+ JSONObject sendData = new JSONObject();
|
|
|
+
|
|
|
+ JSONObject first = new JSONObject();
|
|
|
+ first.put("value", "您有一个新的账单,点击查看详情!");
|
|
|
+ first.put("color", "#FF0000");
|
|
|
+
|
|
|
+ JSONObject keyword1 = new JSONObject();
|
|
|
+ keyword1.put("value", fullName);
|
|
|
+ keyword1.put("color", "#173177");
|
|
|
+
|
|
|
+ JSONObject keyword2 = new JSONObject();
|
|
|
+ keyword2.put("value", "¥" + amount.setScale(2) + "元");
|
|
|
+ keyword2.put("color", "#173177");
|
|
|
+
|
|
|
+ JSONObject keyword3 = new JSONObject();
|
|
|
+ keyword3.put("value", detailAmount);
|
|
|
+ keyword3.put("color", "#173177");
|
|
|
+
|
|
|
+
|
|
|
+ String remarkStr = "账单来源:" + propertyName;
|
|
|
+
|
|
|
+ if (StringUtils.isNotBlank(billRemark)) {
|
|
|
+ remarkStr = remarkStr + "\n备注:" + billRemark + "";
|
|
|
+ }
|
|
|
+
|
|
|
+ JSONObject remark = new JSONObject();
|
|
|
+ remark.put("value", remarkStr);
|
|
|
+ remark.put("color", "#173177");
|
|
|
+
|
|
|
+ sendData.put("first", first);
|
|
|
+ sendData.put("keyword1", keyword1);
|
|
|
+ sendData.put("keyword2", keyword2);
|
|
|
+ sendData.put("keyword3", keyword3);
|
|
|
+ sendData.put("remark", remark);
|
|
|
+
|
|
|
+ //w8Zk_VQMFIEVSIBPZid7zSrvHmBdrgnqF76u8PLCZEs cs
|
|
|
+ boolean ret = sendTemplate(sendData, appId, appSecret, billTemplateId, openId, TEMPLATE_REDIRECT_PRE_URL + "/billDetail/" + billDetailId + "payTime=0&payType=bill");
|
|
|
+
|
|
|
+ return ret;
|
|
|
+ }
|
|
|
+
|
|
|
+ //
|
|
|
+ public static boolean sendBillPayedMessage(String fullName, String feeName, Date createTime, BigDecimal amount, String openId, String billDetailId, String payedTemplateId, String appId, String appSecret) {
|
|
|
+
|
|
|
+ JSONObject sendData = new JSONObject();
|
|
|
+
|
|
|
+ JSONObject first = new JSONObject();
|
|
|
+ first.put("value", "账单支付成功!");
|
|
|
+ first.put("color", "#FF0000");
|
|
|
+
|
|
|
+ JSONObject keyword1 = new JSONObject();
|
|
|
+ keyword1.put("value", fullName);
|
|
|
+ keyword1.put("color", "#173177");
|
|
|
+
|
|
|
+ JSONObject keyword2 = new JSONObject();
|
|
|
+ keyword2.put("value", feeName);
|
|
|
+ keyword2.put("color", "#173177");
|
|
|
+
|
|
|
+
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+
|
|
|
+ JSONObject keyword3 = new JSONObject();
|
|
|
+ keyword3.put("value", sdf.format(createTime));
|
|
|
+ keyword3.put("color", "#173177");
|
|
|
+
|
|
|
+ JSONObject keyword4 = new JSONObject();
|
|
|
+
|
|
|
+ keyword4.put("value", "¥" + amount.setScale(2) + "元");
|
|
|
+ keyword4.put("color", "#173177");
|
|
|
+
|
|
|
+
|
|
|
+ String remarkStr = "感谢您的支持,点击查看缴费详情";
|
|
|
+
|
|
|
+ JSONObject remark = new JSONObject();
|
|
|
+ remark.put("value", remarkStr);
|
|
|
+ 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);
|
|
|
+
|
|
|
+ //w8Zk_VQMFIEVSIBPZid7zSrvHmBdrgnqF76u8PLCZEs cs
|
|
|
+ boolean ret = sendTemplate(sendData, appId, appSecret, payedTemplateId, openId, TEMPLATE_REDIRECT_PRE_URL + "/billDetail/" + billDetailId + "payTime=1&payType=bill");
|
|
|
+
|
|
|
+ return ret;
|
|
|
+ }
|
|
|
+
|
|
|
+ //
|
|
|
+ public static boolean sendScanPayedMessage(String fullName, String feeName, Date createTime, BigDecimal amount, String openId, String independentPayId, String payedTemplateId, String appId, String appSecret) {
|
|
|
+
|
|
|
+ JSONObject sendData = new JSONObject();
|
|
|
+
|
|
|
+ JSONObject first = new JSONObject();
|
|
|
+ first.put("value", "账单支付成功!");
|
|
|
+ first.put("color", "#FF0000");
|
|
|
+
|
|
|
+ JSONObject keyword1 = new JSONObject();
|
|
|
+ keyword1.put("value", fullName);
|
|
|
+ keyword1.put("color", "#173177");
|
|
|
+
|
|
|
+ JSONObject keyword2 = new JSONObject();
|
|
|
+ keyword2.put("value", feeName);
|
|
|
+ keyword2.put("color", "#173177");
|
|
|
+
|
|
|
+
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+
|
|
|
+ JSONObject keyword3 = new JSONObject();
|
|
|
+ keyword3.put("value", sdf.format(createTime));
|
|
|
+ keyword3.put("color", "#173177");
|
|
|
+
|
|
|
+ JSONObject keyword4 = new JSONObject();
|
|
|
+
|
|
|
+ keyword4.put("value", "¥" + amount.setScale(2) + "元");
|
|
|
+ keyword4.put("color", "#173177");
|
|
|
+
|
|
|
+
|
|
|
+ String remarkStr = "感谢您的支持,点击查看缴费详情";
|
|
|
+
|
|
|
+ JSONObject remark = new JSONObject();
|
|
|
+ remark.put("value", remarkStr);
|
|
|
+ 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 = sendTemplate(sendData, appId, appSecret, payedTemplateId, openId, TEMPLATE_REDIRECT_PRE_URL + "/billDetail/" + independentPayId + "payTime=1&payType=wechat");
|
|
|
+
|
|
|
+ return ret;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送通知信息
|
|
|
+ *
|
|
|
+ * @param noticeName 通知名称名称
|
|
|
+ * @param noticeDetailId 通知详情ID
|
|
|
+ * @param openId 发送人
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static boolean sendNoticeMessage(String noticeName, String content, String openId, String noticeDetailId, String noticeTemplateId, String appId, String appSecret) {
|
|
|
+
|
|
|
+
|
|
|
+ JSONObject sendData = new JSONObject();
|
|
|
+ String finallyContent = getContent(content);
|
|
|
+ JSONObject first = new JSONObject();
|
|
|
+ first.put("value", "您好,园区通知如下:");
|
|
|
+ first.put("color", "#173177");
|
|
|
+
|
|
|
+ JSONObject keyword1 = new JSONObject();
|
|
|
+ keyword1.put("value", noticeName);
|
|
|
+ keyword1.put("color", "#173177");
|
|
|
+
|
|
|
+
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+
|
|
|
+ JSONObject keyword2 = new JSONObject();
|
|
|
+ keyword2.put("value", sdf.format(new Date()));
|
|
|
+ keyword2.put("color", "#173177");
|
|
|
+
|
|
|
+ JSONObject keyword3 = new JSONObject();
|
|
|
+ keyword3.put("value", finallyContent);
|
|
|
+ keyword3.put("color", "#173177");
|
|
|
+
|
|
|
+ JSONObject remark = new JSONObject();
|
|
|
+ remark.put("value", "点击查看详情");
|
|
|
+ remark.put("color", "#173177");
|
|
|
+
|
|
|
+ sendData.put("first", first);
|
|
|
+ sendData.put("keyword1", keyword1);
|
|
|
+ sendData.put("keyword2", keyword2);
|
|
|
+ sendData.put("keyword3", keyword3);
|
|
|
+ sendData.put("remark", remark);
|
|
|
+
|
|
|
+ //noticeDetailId
|
|
|
+ boolean ret = sendTemplate(sendData, appId, appSecret, noticeTemplateId, openId, TEMPLATE_REDIRECT_PRE_URL + "/#/noticeInfo/" + noticeDetailId);
|
|
|
+
|
|
|
+ return ret;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送物业管理通知信息
|
|
|
+ *
|
|
|
+ * @param noticeName 通知名称
|
|
|
+ * @param noticeDetailId 通知详情ID
|
|
|
+ * @param openId 发送人
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static boolean sendPropertyNoticeMessage(String noticeName, String content, String openId, String noticeDetailId, String noticeTemplateId, String appId, String appSecret) {
|
|
|
+
|
|
|
+
|
|
|
+ JSONObject sendData = new JSONObject();
|
|
|
+ String finallyContent = getContent(content);
|
|
|
+ JSONObject first = new JSONObject();
|
|
|
+ first.put("value", "您好,物业管理通知如下:");
|
|
|
+ first.put("color", "#173177");
|
|
|
+
|
|
|
+ JSONObject keyword1 = new JSONObject();
|
|
|
+ keyword1.put("value", noticeName);
|
|
|
+ keyword1.put("color", "#173177");
|
|
|
+
|
|
|
+
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
+
|
|
|
+ JSONObject keyword2 = new JSONObject();
|
|
|
+ keyword2.put("value", sdf.format(new Date()));
|
|
|
+ keyword2.put("color", "#173177");
|
|
|
+
|
|
|
+ JSONObject keyword3 = new JSONObject();
|
|
|
+ keyword3.put("value", finallyContent);
|
|
|
+ keyword3.put("color", "#173177");
|
|
|
+
|
|
|
+ JSONObject remark = new JSONObject();
|
|
|
+ remark.put("value", "点击查看详情");
|
|
|
+ remark.put("color", "#173177");
|
|
|
+
|
|
|
+ sendData.put("first", first);
|
|
|
+ sendData.put("keyword1", keyword1);
|
|
|
+ sendData.put("keyword2", keyword2);
|
|
|
+ sendData.put("keyword3", keyword3);
|
|
|
+ sendData.put("remark", remark);
|
|
|
+
|
|
|
+ //noticeDetailId
|
|
|
+ boolean ret = sendTemplate(sendData, appId, appSecret, noticeTemplateId, openId, TEMPERATURE_ALARM_PRE_URL + "/#/noticeInfo/" + noticeDetailId);
|
|
|
+
|
|
|
+ return ret;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送警告通知信息
|
|
|
+ *
|
|
|
+ * @param noticeName 通知名称
|
|
|
+ * @param noticeDetailId 通知详情ID
|
|
|
+ * @param openId 发送人
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static boolean sendAlarmNoticeMessage(String noticeName, String content, Date createTime, String openId, String noticeDetailId, String noticeTemplateId, String appId, String appSecret) {
|
|
|
+
|
|
|
+
|
|
|
+ JSONObject sendData = new JSONObject();
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
|
|
|
+ String finallyContent = getContent(content);
|
|
|
+ JSONObject first = new JSONObject();
|
|
|
+ first.put("value", "您好,入侵警告通知如下:");
|
|
|
+ first.put("color", "#173177");
|
|
|
+
|
|
|
+ JSONObject keyword1 = new JSONObject();
|
|
|
+ keyword1.put("value", sdf.format(createTime));
|
|
|
+ keyword1.put("color", "#173177");
|
|
|
+
|
|
|
+
|
|
|
+ JSONObject keyword2 = new JSONObject();
|
|
|
+ keyword2.put("value", finallyContent);
|
|
|
+ keyword2.put("color", "#173177");
|
|
|
+
|
|
|
+ JSONObject remark = new JSONObject();
|
|
|
+ remark.put("value", "请尽快查看");
|
|
|
+ remark.put("color", "#173177");
|
|
|
+
|
|
|
+ sendData.put("first", first);
|
|
|
+ sendData.put("keyword1", keyword1);
|
|
|
+ sendData.put("keyword2", keyword2);
|
|
|
+ //sendData.put("remark", remark);
|
|
|
+
|
|
|
+ //noticeDetailId
|
|
|
+ boolean ret = sendTemplate(sendData, appId, appSecret, noticeTemplateId, openId, "");
|
|
|
+
|
|
|
+ return ret;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 微信发送模版
|
|
|
+ *
|
|
|
+ * @param sendData 发送数据
|
|
|
+ * @param templateId 模版Id
|
|
|
+ * @param openId 发送人
|
|
|
+ * @param url 详情跳转地址
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static boolean sendTemplate(JSONObject sendData, String appId, String appSecret, String templateId, String openId, String url) {
|
|
|
+ boolean result = false;
|
|
|
+
|
|
|
+ AccessToken accessToken = WeixinUtil.getAccessToken(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 static String sendNativeTemplate(String appId,String appSecret,String templateId, String openId,JSONObject sendData,String urlPath) {
|
|
|
+ String result = "";
|
|
|
+
|
|
|
+ try {
|
|
|
+ AccessToken accessToken = WeixinUtil.getAccessToken(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;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|