wuxw
2025-03-11 a8d78bf321dab845132fe9ce2a612cb813e51063
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package com.java110.job.msgNotify;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.java110.core.client.OutRestTemplate;
import com.java110.core.factory.WechatFactory;
import com.java110.core.log.LoggerFactory;
import com.java110.dto.wechat.SmallWeChatDto;
import com.java110.intf.store.ISmallWeChatInnerServiceSMO;
import com.java110.utils.cache.CommonCache;
import com.java110.utils.cache.MappingCache;
import com.java110.utils.constant.MappingConstant;
import com.java110.utils.constant.WechatConstant;
import com.java110.utils.util.ListUtil;
import com.java110.utils.util.StringUtil;
import com.java110.vo.ResultVo;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
 
import java.util.List;
import java.util.Map;
 
@Service
public class WechatTemplateImpl implements IWechatTemplate {
 
    public static final String industry_id1 = "30"; // 房地产物业
    public static final String industry_id2 = "2"; // IT 科技 IT软件与服务
 
 
    private static Logger logger = LoggerFactory.getLogger(WechatTemplateImpl.class);
 
    /**
     * 获取 行业
     */
    public static final String GET_INDUSTRY = "https://api.weixin.qq.com/cgi-bin/template/get_industry?access_token=ACCESS_TOKEN";
 
 
    /**
     * 设置 行业
     */
    public static final String SET_INDUSTRY = "https://api.weixin.qq.com/cgi-bin/template/api_set_industry?access_token=ACCESS_TOKEN";
 
 
    /**
     * 获取模板列表
     */
    public static final String GET_ALL_PRIVATE_TEMPLATE = "https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token=ACCESS_TOKEN";
 
 
    /**
     * 添加模板
     */
    public static final String ADD_TEMPLATE = "https://api.weixin.qq.com/cgi-bin/template/api_add_template?access_token=ACCESS_TOKEN";
 
 
    public static final String WECHAT_TEMPLATE = "WECHAT_TEMPLATE_";
 
 
    @Autowired
    private ISmallWeChatInnerServiceSMO smallWeChatInnerServiceSMOImpl;
 
    @Autowired
    private RestTemplate outRestTemplate;
 
    /**
     * 设置行业为 物业
     *
     * @param communityId 小区ID
     */
    private void setIndustry(String communityId) {
 
        //todo 查询公众号设置的行业
        String url = GET_INDUSTRY.replace("ACCESS_TOKEN", getAccessToken(communityId));
 
        ResponseEntity<String> responseEntity = outRestTemplate.getForEntity(url, String.class);
 
        logger.debug("查询行业返回参数:{}", responseEntity);
 
        if (responseEntity.getStatusCode() != HttpStatus.OK) {
            throw new IllegalArgumentException("获取公众号行业失败");
        }
 
        JSONObject paramOut = JSONObject.parseObject(responseEntity.getBody());
 
       // String industryName = paramOut.getJSONObject("primary_industry").getString("second_class");
 
        JSONObject primaryIndustry = paramOut.getJSONObject("primary_industry");
        if (primaryIndustry != null && "物业".equals(primaryIndustry.getString("second_class"))) { //如果是物业 直接 返回 无需设置
            return;
        }
 
        //todo 设置公众号设置的行业
        url = SET_INDUSTRY.replace("ACCESS_TOKEN", getAccessToken(communityId));
 
        JSONObject paramIn = new JSONObject();
        paramIn.put("industry_id1", industry_id1);
        paramIn.put("industry_id2", industry_id2);
 
        responseEntity = outRestTemplate.postForEntity(url, paramIn.toJSONString(), String.class);
 
        logger.debug("设置行业返回参数:{}", responseEntity);
 
        if (responseEntity.getStatusCode() != HttpStatus.OK) {
            throw new IllegalArgumentException("设置公众号行业失败");
        }
 
    }
 
    /**
     * 获取模板列表
     *
     * @param communityId
     * @return
     */
    private String getAllPrivateTemplate(String communityId) {
        String url = GET_ALL_PRIVATE_TEMPLATE.replace("ACCESS_TOKEN", getAccessToken(communityId));
 
        ResponseEntity<String> responseEntity = outRestTemplate.getForEntity(url, String.class);
 
        logger.debug("查询行业返回参数:{}", responseEntity);
 
        if (responseEntity.getStatusCode() != HttpStatus.OK) {
            throw new IllegalArgumentException("获取模板列表失败");
        }
 
        String templateList = responseEntity.getBody();
        CommonCache.setValue(WECHAT_TEMPLATE + communityId, templateList, CommonCache.TOKEN_EXPIRE_TIME);
        return templateList;
    }
 
    /**
     * 添加模板
     *
     * @param communityId
     * @param templateIdShort
     * @param keys
     */
    private void addTemplate(String communityId, String templateIdShort, String[] keys) {
 
        //todo 设置行业
        setIndustry(communityId);
 
        //todo 设置公众号设置的行业
        String url = ADD_TEMPLATE.replace("ACCESS_TOKEN", getAccessToken(communityId));
 
        JSONObject paramIn = new JSONObject();
        paramIn.put("template_id_short", templateIdShort);
        paramIn.put("keyword_name_list", keys);
 
        ResponseEntity<String> responseEntity = outRestTemplate.postForEntity(url, paramIn.toJSONString(), String.class);
 
        logger.debug("添加模板返回参数:{}", responseEntity);
 
        if (responseEntity.getStatusCode() != HttpStatus.OK) {
            throw new IllegalArgumentException("添加模板失败");
        }
 
        JSONObject paramOut = JSONObject.parseObject(responseEntity.getBody());
 
        if (paramOut.getIntValue("errcode") != 0) {
            throw new IllegalArgumentException(paramOut.getString("errmsg"));
        }
 
    }
 
    private void deletePrivateTemplate(String communityId, String templateId) {
 
    }
 
    /**
     * 获取模板ID
     *
     * @param communityId     小区ID
     * @param templateIdShort 模板库中模板的编号
     * @param title           模板标题
     * @return
     */
    @Override
    public String getTemplateId(String communityId, String templateIdShort, String title, String[] keys) {
 
        String templateList = CommonCache.getValue(WECHAT_TEMPLATE + communityId);
        //todo 不存在 调用微信查询
        if (StringUtil.isEmpty(templateList)) {
            templateList = getAllPrivateTemplate(communityId);
        }
 
        //todo 如果还是空 则直接 添加
        if (StringUtil.isEmpty(templateList)) {
            addTemplate(communityId, templateIdShort, keys);
            templateList = getAllPrivateTemplate(communityId);
        }
 
        //todo 循环校验
        JSONObject templateListObj = JSONObject.parseObject(templateList);
        if (templateListObj == null || !templateListObj.containsKey("template_list") || templateListObj.getJSONArray("template_list").size() < 1) {
            addTemplate(communityId, templateIdShort, keys);
            templateList = getAllPrivateTemplate(communityId);
            templateListObj = JSONObject.parseObject(templateList);
        }
 
        //todo 寻找 templateId
        JSONArray templateLists = templateListObj.getJSONArray("template_list");
        JSONObject template = null;
        for (int templateIndex = 0; templateIndex < templateLists.size(); templateIndex++) {
            template = templateLists.getJSONObject(templateIndex);
            if (title.equals(template.getString("title")) && templateHasKey(template.getString("content"), keys)) {
                return template.getString("template_id");
            }
        }
 
        //todo 说明没有寻找到
        addTemplate(communityId, templateIdShort, keys);
        templateList = getAllPrivateTemplate(communityId);
        templateListObj = JSONObject.parseObject(templateList);
 
        templateLists = templateListObj.getJSONArray("template_list");
        for (int templateIndex = 0; templateIndex < templateLists.size(); templateIndex++) {
            template = templateLists.getJSONObject(templateIndex);
            if (title.equals(template.getString("title")) && templateHasKey(template.getString("content"), keys)) {
                return template.getString("template_id");
            }
        }
 
 
        return "-1";
    }
 
    /**
     * 关键字是否都包含
     * @param content
     * @param keys
     * @return
     */
    private boolean templateHasKey(String content, String[] keys) {
        for (String key : keys) {
            if (!content.contains(key)) {
                return false;
            }
        }
 
        return true;
    }
 
    public String getAccessToken(String communityId) {
        SmallWeChatDto smallWeChatDto = new SmallWeChatDto();
        smallWeChatDto.setWeChatType(SmallWeChatDto.WECHAT_TYPE_PUBLIC);
        smallWeChatDto.setObjType(SmallWeChatDto.OBJ_TYPE_COMMUNITY);
        smallWeChatDto.setObjId(communityId);
        List<SmallWeChatDto> smallWeChatDtos = smallWeChatInnerServiceSMOImpl.querySmallWeChats(smallWeChatDto);
 
        if (ListUtil.isNull(smallWeChatDtos)) {
            String appIdCache = MappingCache.getValue(WechatConstant.WECHAT_DOMAIN, "appId");
            String appSecretCache = MappingCache.getValue(WechatConstant.WECHAT_DOMAIN, "appSecret");
            String mchIdCache = MappingCache.getValue(WechatConstant.WECHAT_DOMAIN, "mchId");
            String keyCache = MappingCache.getValue(MappingConstant.WECHAT_STORE_DOMAIN, "key");
            smallWeChatDto = new SmallWeChatDto();
            smallWeChatDto.setAppId(appIdCache);
            smallWeChatDto.setAppSecret(appSecretCache);
            smallWeChatDto.setMchId(mchIdCache);
            smallWeChatDto.setPayPassword(keyCache);
        } else {
            smallWeChatDto = smallWeChatDtos.get(0);
        }
 
        String accessToken = WechatFactory.getAccessToken(smallWeChatDto.getAppId(), smallWeChatDto.getAppSecret());
 
        return accessToken;
    }
 
    @Override
    public String getAppId(String communityId) {
        SmallWeChatDto smallWeChatDto = new SmallWeChatDto();
        smallWeChatDto.setWeChatType(SmallWeChatDto.WECHAT_TYPE_PUBLIC);
        smallWeChatDto.setObjType(SmallWeChatDto.OBJ_TYPE_COMMUNITY);
        smallWeChatDto.setObjId(communityId);
        List<SmallWeChatDto> smallWeChatDtos = smallWeChatInnerServiceSMOImpl.querySmallWeChats(smallWeChatDto);
 
        if (ListUtil.isNull(smallWeChatDtos)) {
            String appIdCache = MappingCache.getValue(WechatConstant.WECHAT_DOMAIN, "appId");
            String appSecretCache = MappingCache.getValue(WechatConstant.WECHAT_DOMAIN, "appSecret");
            String mchIdCache = MappingCache.getValue(WechatConstant.WECHAT_DOMAIN, "mchId");
            String keyCache = MappingCache.getValue(MappingConstant.WECHAT_STORE_DOMAIN, "key");
            smallWeChatDto = new SmallWeChatDto();
            smallWeChatDto.setAppId(appIdCache);
            smallWeChatDto.setAppSecret(appSecretCache);
            smallWeChatDto.setMchId(mchIdCache);
            smallWeChatDto.setPayPassword(keyCache);
        } else {
            smallWeChatDto = smallWeChatDtos.get(0);
        }
        return smallWeChatDto.getAppId();
    }
}