微信自动回复接口

  1 //1.微信自动回复接口
  2 public void receiveMSG(HttpServletRequest request, HttpServletResponse response, @PathVariable(value = "comId")
  3     String comId) throws Exception {
  4         try {
  5             boolean isGet = request.getMethod().toLowerCase().equals("get");
  6             request.setCharacterEncoding("UTF-8");
  7             response.setCharacterEncoding("UTF-8");
  8             if (isGet) {
  9                 // 验证URL真实性
 10                 String token = Utils.getParmValueByName(request, comId, "WeChat_Token", ""); // "rrtxxvafgargae";//
 11                 String signature = request.getParameter("signature");// 微信加密签名
 12                 String timestamp = request.getParameter("timestamp");// 时间戳
 13                 String nonce = request.getParameter("nonce");// 随机数
 14                 String echostr = request.getParameter("echostr");// 随机字符串
 15                 List<String> params = new ArrayList<String>();
 16                 params.add(token);// Token
 17                 params.add(timestamp);
 18                 params.add(nonce);
 19                 // 1. 将token、timestamp、nonce三个参数进行字典序排序
 20                 Collections.sort(params, new Comparator<String>() {
 21                     @Override
 22                     public int compare(String o1, String o2) {
 23                         return o1.compareTo(o2);
 24                     }
 25                 });
 26                 // 2. 将三个参数字符串拼接成一个字符串进行sha1加密
 27                 String temp = Utils.toShaString(params.get(0) + params.get(1) + params.get(2));
 28                 // log.debug("temp:"+temp);
 29                 // log.debug("signature:"+signature);
 30                 if (temp.equals(signature)) {
 31                     response.getWriter().write(echostr);
 32                 }
 33             } else {
 34                 // 处理接收消息
 35                 /** 读取接收到的xml消息 */
 36                 StringBuffer sb = new StringBuffer();
 37                 InputStream is = request.getInputStream();
 38                 InputStreamReader isr = new InputStreamReader(is, "UTF-8");
 39                 BufferedReader br = new BufferedReader(isr);
 40                 String s = "";
 41                 while ((s = br.readLine()) != null) {
 42                     sb.append(s);
 43                 }
 44                 String xml = sb.toString(); // 次即为接收到微信端发送过来的xml数据
 45                 HashMap<String, String> xmlmap = WeChatUtils.xml2HashMap(xml);
 46                 log.debug(xmlmap.toString());
 47                 // 接收事件推送
 48                 String msgType = xmlmap.get("MsgType");
 49                 if ("event".equals(msgType)) {
 50                     String event = xmlmap.get("Event");
 51                     if ("subscribe".equals(event)) {
 52                         String msg = Utils.getParmValueByName(request, comId, "WeChat_SubscribeMsg", "您好,欢迎关注!");
 53                         String xmlTo = "<xml>";
 54                         xmlTo += "<ToUserName>" + xmlmap.get("FromUserName") + "</ToUserName>";
 55                         xmlTo += "<FromUserName>" + xmlmap.get("ToUserName") + "</FromUserName>";
 56                         xmlTo += "<CreateTime>" + xmlmap.get("CreateTime") + "</CreateTime>";
 57                         xmlTo += "<MsgType>text</MsgType>";
 58                         xmlTo += "<Content>" + msg + "</Content>";
 59                         xmlTo += "</xml>";
 60                         response.getWriter().write(xmlTo);
 61                     }
 62                     if ("subscribe".equals(event) || "SCAN".equals(event)) {
 63                         log.debug("订阅");
 64 
 65                         String sql = " select cust_id from WECHAT_CUST where com_id = ? and OPENID = ?  order by TYPE_BIND desc  ";
 66                         ArrayList parms = new ArrayList();
 67                         parms.add(comId);
 68                         parms.add(xmlmap.get("FromUserName"));
 69                         String custId = utilsService.getValueBySql(sql, parms, "");
 70                         if (StringUtils.isBlank(custId)) {
 71                             String accessToken = WeChatUtils.getAccessToken(request, comId);
 72                             String usermsg = WeChatUtils.getUserInfo(accessToken, xmlmap.get("FromUserName"));
 73                             JSONObject jsonUser = WeChatUtils.string2JSON(usermsg);
 74                             if (jsonUser.containsKey("errcode"))
 75                                 throw new Exception(jsonUser.toString());
 76 
 77                             Cust cust = new Cust();
 78                             String cust_id = utilsService.getMemNum(comId);
 79                             cust.setCustId(cust_id);
 80                             cust.setCustName(jsonUser.getString("nickname").replaceAll("'", "").replaceAll("\"", ""));
 81                             cust.setCustShortName("");
 82                             cust.setReturnlimitdays(99999);
 83                             cust.setCustPwd("");
 84                             cust.setComId(comId);
 85                             cust.setCallRut(comId);
 86                             cust.setInnerType("05");
 87                             cust.setIsMkt("0");
 88                             cust.setCustType5("1");
 89                             cust.setStatus("02");
 90                             cust.setCallState("M");
 91                             cust.setTel("");
 92                             cust.setIsTel("0");
 93                             cust.setEmail("");
 94                             cust.setIsWeb("0");
 95                             if (!jsonUser.getString("sex").equals("1")) {
 96                                 cust.setCallState("F");
 97                             }
 98                             sql = " select min(MCP_CLS_ID)  from MEM_CLASS_POLICE where MCP_COM_ID = ? ";
 99                             parms.clear();
100                             parms.add(comId);
101                             String mcp_cls_id = utilsService.getValueBySql(sql, parms, "");
102                             cust.setCustType4(mcp_cls_id);
103                             CustOther custOther = new CustOther();
104                             custOther.setCustId(cust_id);
105                             if (xmlmap.containsKey("Ticket")) {
106                                 sql = " select cust_id  from WECHAT_CUST where TICKET = ? ";
107                                 parms.clear();
108                                 parms.add(xmlmap.get("Ticket"));
109                                 String CorCusNum = utilsService.getValueBySql(sql, parms, "");
110                                 cust.setCorCusNum(CorCusNum);
111                             } else {
112                                 cust.setCorCusNum("");
113                             }
114                             WechatCust wechatcust = new WechatCust();
115                             wechatcust.setComId(comId);
116                             wechatcust.setCustId(cust_id);
117                             wechatcust.setOpenid(xmlmap.get("FromUserName"));
118                             wechatcust.setTicket("");
119                             wechatcust.setHeadimgurl(jsonUser.getString("headimgurl"));
120                             wechatcust.setStatus("1");
121                             wechatcust.setCrtDate(Utils.GetCurrentDate());
122                             wechatcust.setTypeBind("0");
123                             wechatcust.setCancelDate("");
124                             weChatBaseService.addCust(cust, custOther, wechatcust);
125 
126                         } else if ("subscribe".equals(event)) {
127                             WechatCust wechatcust = new WechatCust();
128                             wechatcust.setComId(comId);
129                             wechatcust.setCustId(custId);
130                             wechatcust = wechatCustService.findByPrimaryKey(wechatcust.getPrimaryKey());
131                             Cust cust = custService.findByPrimaryKey(custId);
132                             if (StringUtils.isBlank(wechatcust.getHeadimgurl())) {
133                                 String accessToken = WeChatUtils.getAccessToken(request, comId);
134                                 String usermsg = WeChatUtils.getUserInfo(accessToken, xmlmap.get("FromUserName"));
135                                 JSONObject jsonUser = WeChatUtils.string2JSON(usermsg);
136                                 if (jsonUser.containsKey("errcode"))
137                                     throw new Exception(jsonUser.toString());
138                                 String nickname = "";
139                                 String sex = "1";
140                                 String headimgurl = "";
141                                 String ticket = "";
142                                 try {
143                                     nickname = jsonUser.getString("nickname").replaceAll("'", "").replaceAll("\"", "");
144                                     sex = jsonUser.getString("sex");
145                                     headimgurl = jsonUser.getString("headimgurl");
146                                     ticket = WeChatUtils.getQRCode(accessToken, custId).get("ticket").toString();
147                                 } catch (Throwable e) {
148 
149                                 }
150 
151                                 wechatcust.setOpenid(xmlmap.get("FromUserName"));
152                                 wechatcust.setTicket(ticket);
153                                 wechatcust.setHeadimgurl(headimgurl);
154                                 wechatcust.setCrtDate(Utils.GetCurrentDate());
155                                 cust.setCustName(nickname);
156                                 if (!sex.equals("1")) {
157                                     cust.setCallState("F");
158                                 }
159                             }
160                             wechatcust.setStatus("1");
161                             weChatBaseService.updateCust(cust, wechatcust);
162                             String updatesql = " update WECHAT_CUST set STATUS = '1' where com_id = ? and OPENID = ? ";
163                             String nowDate = Utils.GetCurrentDate();
164                             parms.clear();
165                             parms.add(comId);
166                             parms.add(xmlmap.get("FromUserName"));
167                             utilsService.updateSql(updatesql, parms);
168                         }
169 
170                     } else if ("unsubscribe".equals(event)) {
171                         log.debug("取消订阅");
172                         String sql = " update WECHAT_CUST set STATUS = '0', CANCEL_DATE = ? where com_id = ? and OPENID = ? ";
173                         ArrayList parms = new ArrayList();
174                         String nowDate = Utils.GetCurrentDate();
175                         parms.add(nowDate);
176                         parms.add(comId);
177                         parms.add(xmlmap.get("FromUserName"));
178 
179                         utilsService.updateSql(sql, parms);
180 
181                     }else if ("SCAN".equals(event)) {
182                         log.debug("用户已关注时的事件推送  ");
183                     } else if ("LOCATION".equals(event)) {
184                         log.debug("上报地理位置事件");
185                     } else if ("CLICK".equals(event)) {
186                         log.debug("点击菜单拉取消息时的事件推送");
187                         String eventKey = xmlmap.get("EventKey");
188                         log.debug("eventKey:"+eventKey);
189                         if (StringUtils.equalsIgnoreCase("cust_service", eventKey)) {
190                             String fromUserName = xmlmap.get("FromUserName");
191                             String sql = " select COR_CUS_NUM from  cust , wechat_cust where wechat_cust.cust_id = cust.cust_id "
192                                     + "    and wechat_cust.com_id = ? and wechat_cust.openid = ?  " + " order by wechat_cust.TYPE_BIND desc ";
193                             String mSql = " select MEDIA_ID from WECHAT_CUST_MEDIA where cust_id = ? and com_id = ? ";
194                             String custSql = "select COR_CUS_NUM from  cust where cust_id = ? ";
195                             ArrayList parms = new ArrayList();
196                             parms.add(comId);
197                             parms.add(fromUserName);
198 
199                             String mediaId = "";
200                             String custId = utilsService.getValueBySql(sql, parms, "");
201                             int count = 0;
202                             while (StringUtils.isBlank(mediaId) && StringUtils.isNotBlank(custId) && count < 3) {
203                                 count++;
204                                 parms.clear();
205                                 parms.add(custId);
206                                 parms.add(comId);
207                                 mediaId = utilsService.getValueBySql(mSql, parms, "");
208                                 if (StringUtils.isBlank(mediaId)) {
209                                     parms.clear();
210                                     parms.add(custId);
211                                     custId = utilsService.getValueBySql(custSql, parms, "");
212                                 }
213                             }
214                             if (StringUtils.isBlank(mediaId)) {
215                                 mediaId = Utils.getParmValueByName(request, comId, "WeChat_ServiceMediaId", "");
216                             }
217                             if (StringUtils.isNotBlank(mediaId)) {
218                                 log.debug("mediaId:"+mediaId);
219                                 String xmlTo = "<xml>";
220                                 xmlTo += "<ToUserName>" + xmlmap.get("FromUserName") + "</ToUserName>";
221                                 xmlTo += "<FromUserName>" + xmlmap.get("ToUserName") + "</FromUserName>";
222                                 xmlTo += "<CreateTime>" + xmlmap.get("CreateTime") + "</CreateTime>";
223                                 xmlTo += "<MsgType>image</MsgType>";
224                                 xmlTo += "<Image>";
225                                 xmlTo += "<MediaId>" + mediaId + "</MediaId>";
226                                 xmlTo += "</Image>";
227                                 xmlTo += "</xml>";
228                                 response.getWriter().write(xmlTo);
229                             }else {
230                                 String xmlTo = "<xml>";
231                                 xmlTo += "<ToUserName>" + xmlmap.get("FromUserName") + "</ToUserName>";
232                                 xmlTo += "<FromUserName>" + xmlmap.get("ToUserName") + "</FromUserName>";
233                                 xmlTo += "<CreateTime>" + xmlmap.get("CreateTime") + "</CreateTime>";
234                                 xmlTo += "<MsgType>text</MsgType>";
235                                 xmlTo += "<Content>还未绑定客服资料</Content>";
236                                 xmlTo += "</xml>";
237                                 response.getWriter().write(xmlTo);
238                             }
239                         }
240                     }
241                 } else if ("text".equals(msgType)) {//回复文本
242                     String key = xmlmap.get("Content");
243                     String xmlTo = "<xml>";
244                     xmlTo += "<ToUserName>" + xmlmap.get("ToUserName") + "</ToUserName>";
245                     xmlTo += "<FromUserName>" + xmlmap.get("FromUserName") + "</FromUserName>";
246                     xmlTo += "<CreateTime>" + xmlmap.get("CreateTime") + "</CreateTime>";
247                     xmlTo += "<MsgType>text</MsgType>";
248                     String repltSql="select * from  WECHAT_AUTO_KEY where WECHAT_AUTO_KEY.KEY_NAME = ? and WECHAT_AUTO_KEY.COM_ID=? ";
249                     ArrayList parms = new ArrayList();
250                     parms.add(key);
251                     parms.add(comId);
252                     List replylist=utilsService.getDataList(repltSql, parms);
253                     String desc="";
254                     if(replylist!=null && replylist.size()>0){
255                          HashMap replyLine = (HashMap)replylist.get(0);
256                          desc=(String) replyLine.get("DESC");
257                     }
258                     if(StringUtils.isNotBlank(desc))
259                         xmlTo += "<Content>" + desc + "</Content>";
260                     else 
261                         xmlTo += "<Content>" + "查无内容" + "</Content>";
262                     xmlTo += "</xml>";
263                     response.getWriter().write(xmlTo);
264                 }else if ("image".equals(msgType)) {//回复图片
265                     String key = xmlmap.get("MediaId");
266                     String xmlTo = "<xml>";
267                     xmlTo += "<ToUserName>" + xmlmap.get("ToUserName") + "</ToUserName>";
268                     xmlTo += "<FromUserName>" + xmlmap.get("FromUserName") + "</FromUserName>";
269                     xmlTo += "<CreateTime>" + xmlmap.get("CreateTime") + "</CreateTime>";
270                     xmlTo += "<MsgType>image</MsgType>";
271                     xmlTo += "<Image>";
272                     String repltSql="select * from  WECHAT_AUTO_KEY where WECHAT_AUTO_KEY.KEY_NAME = ? and WECHAT_AUTO_KEY.COM_ID=? ";
273                     ArrayList parms = new ArrayList();
274                     parms.add(key);
275                     parms.add(comId);
276                     List replylist=utilsService.getDataList(repltSql, parms);
277                     String mediaId="";
278                     if(replylist!=null && replylist.size()>0){
279                          HashMap replyLine = (HashMap)replylist.get(0);
280                          mediaId=(String) replyLine.get("MEDIA_ID");
281                     }
282                     if(StringUtils.isNotBlank(mediaId))
283                         xmlTo += "<MediaId>" + mediaId + "</MediaId>";
284                     else 
285                         xmlTo += "<MediaId>" + "查无内容" + "</MediaId>";
286                     xmlTo += "</Image>";
287                     xmlTo += "</xml>";
288                     response.getWriter().write(xmlTo);
289                 }else if ("voice".equals(msgType)) {//回复语音
290                     String key = xmlmap.get("MediaId");
291                     String xmlTo = "<xml>";
292                     xmlTo += "<ToUserName>" + xmlmap.get("ToUserName") + "</ToUserName>";
293                     xmlTo += "<FromUserName>" + xmlmap.get("FromUserName") + "</FromUserName>";
294                     xmlTo += "<CreateTime>" + xmlmap.get("CreateTime") + "</CreateTime>";
295                     xmlTo += "<MsgType>voice</MsgType>";
296                     xmlTo += "<Voice>";
297                     String repltSql="select * from  WECHAT_AUTO_KEY where WECHAT_AUTO_KEY.KEY_NAME = ? and WECHAT_AUTO_KEY.COM_ID=? ";
298                     ArrayList parms = new ArrayList();
299                     parms.add(key);
300                     parms.add(comId);
301                     List replylist=utilsService.getDataList(repltSql, parms);
302                     String mediaId="";
303                     if(replylist!=null && replylist.size()>0){
304                          HashMap replyLine = (HashMap)replylist.get(0);
305                          mediaId=(String) replyLine.get("MEDIA_ID");
306                     }
307                     if(StringUtils.isNotBlank(mediaId))
308                         xmlTo += "<MediaId>" + mediaId + "</MediaId>";
309                     else 
310                         xmlTo += "<MediaId>" + "查无内容" + "</MediaId>";
311                     xmlTo += "</Voice>";
312                     xmlTo += "</xml>";
313                     response.getWriter().write(xmlTo);
314                 }else if ("video".equals(msgType)) {//回复视频
315                     String key = xmlmap.get("MediaId");
316                     String xmlTo = "<xml>";
317                     xmlTo += "<ToUserName>" + xmlmap.get("ToUserName") + "</ToUserName>";
318                     xmlTo += "<FromUserName>" + xmlmap.get("FromUserName") + "</FromUserName>";
319                     xmlTo += "<CreateTime>" + xmlmap.get("CreateTime") + "</CreateTime>";
320                     xmlTo += "<MsgType>video</MsgType>";
321                     xmlTo += "<Video>";
322                     String repltSql="select * from  WECHAT_AUTO_KEY where WECHAT_AUTO_KEY.KEY_NAME = ? and WECHAT_AUTO_KEY.COM_ID=? ";
323                     ArrayList parms = new ArrayList();
324                     parms.add(key);
325                     parms.add(comId);
326                     List replylist=utilsService.getDataList(repltSql, parms);
327                     String mediaId="";
328                     String title="";
329                     String desc="";
330                     if(replylist!=null && replylist.size()>0){
331                          HashMap replyLine = (HashMap)replylist.get(0);
332                          mediaId=(String) replyLine.get("MEDIA_ID");
333                          title=(String) replyLine.get("TITLE");
334                          desc=(String) replyLine.get("DESC");
335                     }
336                     if(StringUtils.isNotBlank(mediaId))
337                         xmlTo += "<MediaId>" + mediaId + "</MediaId>";
338                     else 
339                         xmlTo += "<MediaId>" + "查无内容" + "</MediaId>";
340                     xmlTo += "<Title>" + title + "</Title>";
341                     xmlTo += "<Description>" + desc + "</Title>";
342                     xmlTo += "</Video> ";
343                     xmlTo += "</xml>";
344                     response.getWriter().write(xmlTo);
345                 }else if ("music".equals(msgType)) {//回复音乐
346                     String key = xmlmap.get("ThumbMediaId");
347                     String xmlTo = "<xml>";
348                     xmlTo += "<ToUserName>" + xmlmap.get("ToUserName") + "</ToUserName>";
349                     xmlTo += "<FromUserName>" + xmlmap.get("FromUserName") + "</FromUserName>";
350                     xmlTo += "<CreateTime>" + xmlmap.get("CreateTime") + "</CreateTime>";
351                     xmlTo += "<MsgType>music</MsgType>";
352                     xmlTo += "<Music>";
353                     String repltSql="select * from  WECHAT_AUTO_KEY where WECHAT_AUTO_KEY.KEY_NAME = ? and WECHAT_AUTO_KEY.COM_ID=? ";
354                     ArrayList parms = new ArrayList();
355                     parms.add(key);
356                     parms.add(comId);
357                     List replylist=utilsService.getDataList(repltSql, parms);
358                     String mediaId="";
359                     String title="";
360                     String desc="";
361                     String url="";
362                     if(replylist!=null && replylist.size()>0){
363                          HashMap replyLine = (HashMap)replylist.get(0);
364                          mediaId=(String) replyLine.get("MEDIA_ID");
365                          title=(String) replyLine.get("TITLE");
366                          desc=(String) replyLine.get("DESC");
367                          url=(String) replyLine.get("URL");
368                     }
369                     xmlTo += "<Title>" + title + "</Title>";
370                     xmlTo += "<Description>" + desc + "</Title>";
371                     xmlTo += "<MusicUrl>" + url + "</MusicUrl>";
372                     if(StringUtils.isNotBlank(mediaId))
373                         xmlTo += "<ThumbMediaId>" + mediaId + "</ThumbMediaId>";
374                     else 
375                         xmlTo += "<ThumbMediaId>" + "查无内容" + "</ThumbMediaId>";
376                     
377                     xmlTo += "</Music> ";
378                     xmlTo += "</xml>";
379                     response.getWriter().write(xmlTo);
380                 }else if ("news".equals(msgType)) {//回复图文
381                     String key = xmlmap.get("Articles");
382                     String xmlTo = "<xml>";
383                     xmlTo += "<ToUserName>" + xmlmap.get("ToUserName") + "</ToUserName>";
384                     xmlTo += "<FromUserName>" + xmlmap.get("FromUserName") + "</FromUserName>";
385                     xmlTo += "<CreateTime>" + xmlmap.get("CreateTime") + "</CreateTime>";
386                     xmlTo += "<MsgType>news</MsgType>";
387                     xmlTo += "<ArticleCount>1</ArticleCount>";
388                     xmlTo += "<Articles>";
389                     xmlTo += "<item>";
390                     String repltSql="select * from  WECHAT_AUTO_KEY where WECHAT_AUTO_KEY.KEY_NAME = ? and WECHAT_AUTO_KEY.COM_ID=? ";
391                     ArrayList parms = new ArrayList();
392                     parms.add(key);
393                     parms.add(comId);
394                     List replylist=utilsService.getDataList(repltSql, parms);
395                     String mediaId="";
396                     String title="";
397                     String desc="";
398                     String url="";
399                     String goUrl="";
400                     if(replylist!=null && replylist.size()>0){
401                          HashMap replyLine = (HashMap)replylist.get(0);
402                          mediaId=(String) replyLine.get("MEDIA_ID");
403                          title=(String) replyLine.get("TITLE");
404                          desc=(String) replyLine.get("DESC");
405                          url=(String) replyLine.get("URL");
406                          goUrl=(String) replyLine.get("GO_URL");
407                     }
408                     xmlTo += "<Title>" + title + "</Title>";
409                     if(StringUtils.isNotBlank(desc))
410                         xmlTo += "<Description>" + desc + "</Description>";
411                     else 
412                         xmlTo += "<Description>" + "查无内容" + "</Description>";
413                     xmlTo += "<PicUrl>" + url + "</PicUrl>";
414                     xmlTo += "<Url>" + goUrl + "</Url>";
415                     xmlTo += "</item> ";
416                     xmlTo += "</Articles> ";
417                     xmlTo += "</xml>";
418                     response.getWriter().write(xmlTo);
419                 }
420             }
421         } catch (Exception e) {
422             log.debug("", e);
423         }
424     }

 

posted @ 2016-10-26 10:43  GL_BKY  阅读(5198)  评论(0编辑  收藏  举报