Helius
2020-12-15 2914588a65371a3ce43f678cde0a26cd8da26611
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
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);
    }
 
 
}