| | |
| | | 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; |
| | | |
| | | /** |
| | | * 小程序消息模板 |
| | |
| | | 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); |
| | | |
| | | } |
| | | |
| | | |