| package com.matrix.component.tools; | 
|   | 
| import com.matrix.core.tools.LogUtil; | 
| import com.matrix.core.tools.StringUtils; | 
| import com.matrix.system.shopXcx.api.WeChatApiTools; | 
| import net.sf.json.JSONObject; | 
|   | 
| import java.io.IOException; | 
| import java.util.Arrays; | 
| import java.util.HashMap; | 
| import java.util.List; | 
| import java.util.Map; | 
|   | 
| /** | 
|  * 小程序消息模板 | 
|  * | 
|  * @author jiangyouyao | 
|  * @email 512061637@qq.com | 
|  * @date 2019年2月25日 | 
|  */ | 
| public class WxTempLateMsgUtil { | 
|   | 
|   | 
|     /** | 
|      * 模板内容first | 
|      */ | 
|     public static String TEMP_FIRST = "first"; | 
|     /** | 
|      * 模板内容keyword | 
|      */ | 
|     public static String TEMP_KEYWORD = "keyword"; | 
|   | 
|     /** | 
|      * 模板内容keyword1 | 
|      */ | 
|     public static String TEMP_KEYWORD1 = "keyword1"; | 
|   | 
|     /** | 
|      * 模板内容keyword2 | 
|      */ | 
|     public static String TEMP_KEYWORD2 = "keyword2"; | 
|   | 
|     /** | 
|      * 模板内容keyword3 | 
|      */ | 
|     public static String TEMP_KEYWORD3 = "keyword3"; | 
|   | 
|     /** | 
|      * 模板内容keyword4 | 
|      */ | 
|     public static String TEMP_KEYWORD4 = "keyword4"; | 
|   | 
|     /** | 
|      * 模板内容keyword5 | 
|      */ | 
|     public static String TEMP_KEYWORD5 = "keyword5"; | 
|   | 
|     /** | 
|      * 模板内容keyword6 | 
|      */ | 
|     public static String TEMP_KEYWORD6 = "keyword6"; | 
|   | 
|     /** | 
|      * 模板内容keyword7 | 
|      */ | 
|     public static String TEMP_KEYWORD7 = "keyword7"; | 
|   | 
|     /** | 
|      * 模板内容remark | 
|      */ | 
|     public static String TEMP_REMARK = "remark"; | 
|   | 
|     /** | 
|      * 模板文字颜色1 | 
|      */ | 
|     public static String TEMP_COLOR1 = "#0044BB"; | 
|   | 
|     /** | 
|      * 模板文字颜色2 | 
|      */ | 
|     public static String TEMP_COLOR2 = "#2E2E2E"; | 
|   | 
|   | 
|     /** | 
|      * 常量errcode | 
|      */ | 
|     public static String ERRCODE = "errcode"; | 
|     /** | 
|      * 40001 | 
|      **/ | 
|     public static final String SEND_ERROR_40001 = "40001"; | 
|   | 
|   | 
|     /** | 
|      * 订单发货提醒 | 
|      */ | 
|     public static final String ORDER_DELIVERY = "rLITY0JWVkmU0N9UQKBLf8uj62TheD6TejBJ6OkeptI"; | 
|   | 
|     /** | 
|      * 订单支付成功通知 | 
|      */ | 
|     public static final String ORDER_PAY_SUCCESS = "Onmy7iGMlGaV54v4CEXt5kOCR0oumAr9-3cF-Qx57B4"; | 
|   | 
|   | 
|     /** | 
|      * 退款成功提醒 | 
|      */ | 
|     public static final String REFUND_SUCCESS = "13EFbbABfkWuqTfRmKiqadP8LMCMgxD7CFwm3JYrVms"; | 
|   | 
|     /** | 
|      * 拒绝退款提醒 | 
|      */ | 
|     public static final String REFUND_FAILED = "nRe9Ha3RY3SVUahANxUZS_YJBsFoGskKoIDeqEg3uO4"; | 
|   | 
|     /** | 
|      * 填写物流提醒 | 
|      */ | 
|     public static final String WRITE_LOGISTICS = "rLITY0JWVkmU0N9UQKBLf8uj62TheD6TejBJ6OkeptI"; | 
|   | 
|     /** | 
|      * 订单发货提醒 | 
|      * | 
|      * @param orderOn     订单编号 | 
|      * @param goodsName   商品名称 | 
|      * @param address     收货地址 | 
|      * @param companyName 物流公司 | 
|      * @param trackingNo  物流编号 | 
|      * @return | 
|      */ | 
|     public static int sendOrderDeliveryNotice(String orderOn, String goodsName, String address, String companyName, String trackingNo, String openId, | 
|                                               String page, String formId) { | 
|         return sendWxTemplateMsg(Arrays.asList(orderOn, goodsName, address, companyName, trackingNo), openId, page, ORDER_DELIVERY, formId); | 
|     } | 
|   | 
|     ; | 
|   | 
|   | 
|     /** | 
|      * 通用模板消息接口 | 
|      */ | 
|     public static int sendWxTemplateMsg(List<String> valueList, String openId, | 
|                                         String page, String tempId, String formId) { | 
|   | 
|         if (StringUtils.isBlank(openId)) { | 
|             return 0; | 
|         } | 
|   | 
|         String access_token = ""; | 
|         try { | 
|             access_token = WeChatApiTools.getAccessToken(); | 
|         } catch (IOException e1) { | 
|             LogUtil.error("获取accesstoken失败", e1.getMessage()); | 
|         } | 
|         // 发送模板URL | 
|         String url = WxApiConstants.XCX_TEMPLATEMSG_SEN.replace("ACCESS_TOKEN", access_token); | 
|         WxTemplate temp = new WxTemplate(); | 
|         // 跳转地址 | 
|         if (StringUtils.isNotBlank(page)) { | 
|             temp.setPage(page); | 
|         } | 
|         temp.setTouser(openId); | 
|         // 消息模版ID | 
|         temp.setTemplateId(tempId); | 
|   | 
|         Map<String, Object> tempLateMsgMap = new HashMap<String, Object>(); | 
|         // 设置模板消息的值 | 
|         for (int i = 0; i < valueList.size(); i++) { | 
|             TemplateData keyword = new TemplateData(); | 
|             keyword.setValue(valueList.get(i)); | 
|             tempLateMsgMap.put(TEMP_KEYWORD + (i + 1), keyword); | 
|         } | 
|         temp.setData(tempLateMsgMap); | 
|         String jsonString = JSONObject.fromObject(temp).toString(); | 
|   | 
|         LogUtil.info("组装模板消息={}", jsonString); | 
|         HttpRequest reqObj = new HttpRequest(); | 
|         HttpResponse result = null; | 
|         try { | 
|             result = reqObj.sendHttpsPost(url, null, jsonString); | 
|         } catch (IOException e) { | 
|             LogUtil.error("发送模板通知出错了", e); | 
|         } | 
|         JSONObject json = JSONObject.fromObject(result.getDataString()); | 
|         LogUtil.info("发送模板消息结果={}", json); | 
|         int res = 0; | 
|         if (json == null) { | 
|             return res; | 
|         } else { | 
|             res = json.getInt(ERRCODE); | 
|             if (res == Integer.parseInt(SEND_ERROR_40001)) { | 
|                 String new_access_token = ""; | 
|                 try { | 
|                     new_access_token = WeChatApiTools.getAccessToken(); | 
|                 } catch (IOException e1) { | 
|                     LogUtil.error("获取new_access_token失败", e1.getMessage()); | 
|                 } | 
|                 url = WxApiConstants.XCX_TEMPLATEMSG_SEN.replace("ACCESS_TOKEN", new_access_token); | 
|                 try { | 
|                     result = reqObj.sendHttpsPost(url, null, jsonString); | 
|                 } catch (IOException e) { | 
|                     LogUtil.error("发送new_access_token模板通知出错了", e); | 
|                 } | 
|                 json = JSONObject.fromObject(result.getDataString()); | 
|                 if (json == null) { | 
|                     return res; | 
|                 } else { | 
|                     res = json.getInt(ERRCODE); | 
|                     if (res != 0) { | 
|                         // 添加发送失败 信息 | 
|                         LogUtil.info("发送new_access_token模板消息错误错误 errcode:{} errmsg:{}" + "," + json.getInt("errcode") | 
|                                 + "," + json.getString("errmsg")); | 
|                     } else { | 
|                         return 1; | 
|                     } | 
|                 } | 
|             } else if (res != 0) { | 
|                 // 添加发送失败 信息 | 
|                 LogUtil.info("发送模板消息错误错误 errcode:{} errmsg:{}" + "," + json.getInt("errcode") + "," | 
|                         + json.getString("errmsg")); | 
|             } else { | 
|                 return 1; | 
|             } | 
|         } | 
|         LogUtil.info("模板消息发送结果:" + res); | 
|         return 0; | 
|     } | 
|   | 
|   | 
|     /** | 
|      * 发送订阅消息 | 
|      * @param temp {temp} | 
|      */ | 
|     public static void  sendSubscribeMsg(WxSubscribeDto temp) { | 
|         String access_token = ""; | 
|         try { | 
|             access_token = WeChatApiTools.getAccessToken(); | 
|         } catch (IOException e1) { | 
|             LogUtil.error("获取accesstoken失败", e1.getMessage()); | 
|         } | 
|         // 发送模板URL | 
|         String url = WxApiConstants.XCX_TEMPLATEMSG_SEN.replace("ACCESS_TOKEN", access_token); | 
|         String jsonString = JSONObject.fromObject(temp).toString(); | 
|         LogUtil.info("组装模板消息={}", jsonString); | 
|         HttpRequest reqObj = new HttpRequest(); | 
|         HttpResponse result = null; | 
|         try { | 
|             result = reqObj.sendHttpsPost(url, null, jsonString); | 
|         } catch (IOException e) { | 
|             LogUtil.error("发送模板通知出错了", e); | 
|         } | 
|         JSONObject json = JSONObject.fromObject(result.getDataString()); | 
|         LogUtil.info("发送模板消息结果={}", json); | 
|     } | 
|   | 
|   | 
| } |