代金券

分类: /

时间:

****如果太过分恢复规划" class="reference-link"># > ****如果太过分恢复规划

第三方第三方
舒服点
sdfsdfdsf

  1. package com.jeecg.p3.weixin.service.impl;
  2. import java.text.SimpleDateFormat;
  3. import java.util.ArrayList;
  4. import java.util.Date;
  5. import java.util.HashMap;4444444
  6. import java.util.List;
  7. import java.util.Map;
  8. import java.util.regex.Pattern;
  9. import javax.servlet.http.HttpServletRequest;
  10. import net.sf.json.JSONObject;
  11. import org.jeecgframework.p3.core.logger.Logger;
  12. import org.jeecgframework.p3.core.logger.LoggerFactory;
  13. import org.jeecgframework.p3.core.util.oConvertUtils;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.stereotype.Service;
  16. import com.jeecg.p3.commonweixin.entity.MyJwWebJwid;
  17. import com.jeecg.p3.commonweixin.util.DateUtils;
  18. import com.jeecg.p3.system.service.MyJwWebJwidService;
  19. import com.jeecg.p3.weixin.entity.WeixinAutoresponse;
  20. import com.jeecg.p3.weixin.entity.WeixinAutoresponseDefault;
  21. import com.jeecg.p3.weixin.entity.WeixinGzuser;
  22. import com.jeecg.p3.weixin.entity.WeixinMenu;
  23. import com.jeecg.p3.weixin.entity.WeixinNewsitem;
  24. import com.jeecg.p3.weixin.entity.WeixinNewstemplate;
  25. import com.jeecg.p3.weixin.entity.WeixinReceivetext;
  26. import com.jeecg.p3.weixin.entity.WeixinSubscribe;
  27. import com.jeecg.p3.weixin.entity.WeixinTexttemplate;
  28. import com.jeecg.p3.weixin.service.WechatService;
  29. import com.jeecg.p3.weixin.service.WeixinAutoresponseDefaultService;
  30. import com.jeecg.p3.weixin.service.WeixinAutoresponseService;
  31. import com.jeecg.p3.weixin.service.WeixinGzuserService;
  32. import com.jeecg.p3.weixin.service.WeixinMenuService;
  33. import com.jeecg.p3.weixin.service.WeixinNewsitemService;
  34. import com.jeecg.p3.weixin.service.WeixinNewstemplateService;
  35. import com.jeecg.p3.weixin.service.WeixinReceivetextService;
  36. import com.jeecg.p3.weixin.service.WeixinSubscribeService;
  37. import com.jeecg.p3.weixin.service.WeixinTexttemplateService;
  38. import com.jeecg.p3.weixin.util.EmojiFilter;
  39. import com.jeecg.p3.weixin.util.MessageUtil;
  40. import com.jeecg.p3.weixin.util.WeiXinConstants;
  41. import com.jeecg.p3.weixin.util.WeixinUtil;
  42. import com.jeecg.p3.weixin.vo.WeixinMessageDTO;
  43. import com.jeecg.p3.weixin.vo.resp.Article;
  44. import com.jeecg.p3.weixin.vo.resp.NewsMessageResp;
  45. import com.jeecg.p3.weixin.vo.resp.TextMessageResp;
  46. /**
  47. * 微信消息处理
  48. * @author LeeShaoQing
  49. * @since 20180723
  50. */
  51. @Service("wechatService")
  52. public class WechatServiceImpl implements WechatService {
  53. public final static Logger log = LoggerFactory.getLogger(WechatServiceImpl.class);
  54. @Autowired
  55. private WeixinAutoresponseService weixinAutoresponseService;
  56. @Autowired
  57. private WeixinTexttemplateService weixinTexttemplateService;
  58. @Autowired
  59. private WeixinNewstemplateService weixinNewstemplateService;
  60. @Autowired
  61. private WeixinNewsitemService weixinNewsitemService;
  62. @Autowired
  63. private MyJwWebJwidService myJwWebJwidService;
  64. @Autowired
  65. private WeixinAutoresponseDefaultService weixinAutoresponseDefaultService;
  66. @Autowired
  67. private WeixinMenuService weixinMenuService;
  68. @Autowired
  69. private WeixinSubscribeService weixinSubscribeService;
  70. @Autowired
  71. private WeixinReceivetextService weixinReceivetextService;
  72. @Autowired
  73. private WeixinGzuserService weixinGzuserService;
  74. @Override
  75. public String coreService(HttpServletRequest request) {
  76. String respMessage = "";
  77. try {
  78. // xml请求解析
  79. Map<String, String> requestMap = MessageUtil.parseXml(request);
  80. //消息存储-非事件消息
  81. if(!requestMap.get("MsgType").equals(MessageUtil.REQ_MESSAGE_TYPE_EVENT)) {
  82. saveReceiveMessage(requestMap);
  83. }
  84. //写入通用DTO类中方便传递参数
  85. WeixinMessageDTO weixinMessageDTO = new WeixinMessageDTO();
  86. // 发送方帐号(OpenId)
  87. weixinMessageDTO.setFromUserName(requestMap.get("FromUserName"));
  88. // 公众帐号(JWID)
  89. weixinMessageDTO.setToUserName(requestMap.get("ToUserName"));
  90. // 消息类型
  91. weixinMessageDTO.setMsgType(requestMap.get("MsgType"));
  92. //消息ID
  93. weixinMessageDTO.setMsgId(requestMap.get("MsgId"));
  94. //消息内容
  95. weixinMessageDTO.setContent(requestMap.get("Content"));
  96. //多媒体ID
  97. weixinMessageDTO.setMediaId(requestMap.get("MediaId"));
  98. //菜单key
  99. weixinMessageDTO.setKey(requestMap.get("EventKey"));
  100. //Event(事件)
  101. weixinMessageDTO.setEvent(requestMap.get("Event"));
  102. //【微信触发类型】文本消息
  103. if (weixinMessageDTO.getMsgType().equals(MessageUtil.REQ_MESSAGE_TYPE_TEXT)) {
  104. respMessage = doTextResponse(weixinMessageDTO, request);
  105. }
  106. //【微信触发类型】图片消息
  107. else if (weixinMessageDTO.getMsgType().equals(MessageUtil.REQ_MESSAGE_TYPE_IMAGE)) {
  108. respMessage = doImageResponse(weixinMessageDTO, request);
  109. }
  110. //【微信触发类型】地理位置消息
  111. else if (weixinMessageDTO.getMsgType().equals(MessageUtil.REQ_MESSAGE_TYPE_LOCATION)) {
  112. respMessage = doLocationeResponse(weixinMessageDTO, request);
  113. }
  114. //【微信触发类型】链接消息
  115. else if (weixinMessageDTO.getMsgType().equals(MessageUtil.REQ_MESSAGE_TYPE_LINK)) {
  116. respMessage = doLinkResponse(weixinMessageDTO, request);
  117. }
  118. //【微信触发类型】音频消息
  119. else if (weixinMessageDTO.getMsgType().equals(MessageUtil.REQ_MESSAGE_TYPE_VOICE)) {
  120. respMessage = doVoiceResponse(weixinMessageDTO, request);
  121. }
  122. //【微信触发类型】视频消息
  123. else if (weixinMessageDTO.getMsgType().equals(MessageUtil.RESP_MESSAGE_TYPE_VIDEO)) {
  124. respMessage = doVideoResponse(weixinMessageDTO, request);
  125. }
  126. //【微信触发类型】小视频消息
  127. else if (weixinMessageDTO.getMsgType().equals(MessageUtil.REQ_MESSAGE_TYPE_SHORTVIDEO)) {
  128. respMessage = doShortVideoResponse(weixinMessageDTO, request);
  129. }
  130. //【微信触发类型】事件推送
  131. else if (weixinMessageDTO.getMsgType().equals(MessageUtil.REQ_MESSAGE_TYPE_EVENT)) {
  132. // 事件类型
  133. String eventType = weixinMessageDTO.getEvent();
  134. // 订阅
  135. if (eventType.equals(MessageUtil.EVENT_TYPE_SUBSCRIBE)) {
  136. try {
  137. respMessage = doSubscribeEventResponse(weixinMessageDTO, request);
  138. } catch (Exception e) {
  139. log.error("-----处理订阅事件异常:--------------"+e.toString());
  140. }
  141. //扫描二维码
  142. }else if(eventType.equals(MessageUtil.EVENT_TYPE_SCAN)){
  143. try {
  144. respMessage = doScanEventResponse(weixinMessageDTO, request);
  145. } catch (Exception e) {
  146. log.error("-----处理扫描二维码事件异常:--------------"+e.toString());
  147. }
  148. }
  149. // 取消订阅
  150. else if (eventType.equals(MessageUtil.EVENT_TYPE_UNSUBSCRIBE)) {
  151. try {
  152. respMessage = doUnsubscribeEventResponse(weixinMessageDTO, request);
  153. } catch (Exception e) {
  154. log.error("-----处理取消订阅事件异常:--------------"+e.toString());
  155. }
  156. }
  157. //上报地理位置事件
  158. else if(eventType.equals(MessageUtil.EVENT_TYPE_LOCATION)){
  159. try {
  160. respMessage = doLocationEventResponse(weixinMessageDTO, request);
  161. } catch (Exception e) {
  162. log.error("-----处理上报地理位置事件异常:--------------"+e.toString());
  163. }
  164. }
  165. // 自定义菜单点击事件(Click)
  166. else if (eventType.equals(MessageUtil.EVENT_TYPE_CLICK)) {
  167. try {
  168. respMessage = doMyMenuEventResponse(weixinMessageDTO, request);
  169. } catch (Exception e) {
  170. log.error("-----处理微信请求[Click]事件异常:--------------"+e.toString());
  171. }
  172. }
  173. // 自定义菜单点击事件(VIEW)
  174. else if (eventType.equals(MessageUtil.EVENT_TYPE_VIEW)) {
  175. try {
  176. respMessage = doMyMenuViewEventResponse(weixinMessageDTO, request);
  177. } catch (Exception e) {
  178. log.error("-----处理微信请求[View]事件异常:--------------"+e.toString());
  179. }
  180. }
  181. // 其他事件
  182. else {
  183. try {
  184. respMessage = doOtherEventResponse(eventType, requestMap);
  185. } catch (Exception e) {
  186. log.error("-----处理其他事件异常:--------------"+e.toString());
  187. }
  188. }
  189. }
  190. } catch (Exception e) {
  191. log.error("-----处理微信请求异常:--------------"+e.toString());
  192. respMessage = "";
  193. }
  194. return respMessage;
  195. }
  196. /**
  197. * 【微信触发类型】输入内容为文本消息的返回处理
  198. * @param weixinMessageDTO, request
  199. * @return respMessage
  200. */
  201. private String doTextResponse(WeixinMessageDTO weixinMessageDTO, HttpServletRequest request) {
  202. String respMessage = "";
  203. //将当前输入内容匹配关键字,如果找到则走关键字回复
  204. WeixinAutoresponse autoResponse = findKey(weixinMessageDTO.getContent(), weixinMessageDTO.getToUserName());
  205. if(autoResponse != null) {
  206. weixinMessageDTO.setTemplateId(autoResponse.getTemplateId());
  207. //调用共通方法回复语
  208. respMessage = getRespMessage(weixinMessageDTO, autoResponse.getMsgType(), request);
  209. } else {
  210. //调用未识别回复语通用方法
  211. respMessage = unrecognizedReply(weixinMessageDTO, request);
  212. }
  213. return respMessage;
  214. }
  215. /**
  216. * 【微信触发类型】图片消息
  217. * @param weixinMessageDTO, request
  218. * @return respMessage
  219. */
  220. private String doImageResponse(WeixinMessageDTO weixinMessageDTO, HttpServletRequest request) {
  221. //调用未识别回复语通用方法
  222. String respMessage = unrecognizedReply(weixinMessageDTO, request);
  223. return respMessage;
  224. }
  225. /**
  226. * 【微信触发类型】地理位置消息
  227. * @param weixinMessageDTO, request
  228. * @return respMessage
  229. */
  230. private String doLocationeResponse(WeixinMessageDTO weixinMessageDTO, HttpServletRequest request) {
  231. //TODO
  232. String respMessage = "";
  233. return respMessage;
  234. }
  235. /**
  236. * 【微信触发类型】链接消息
  237. * @param weixinMessageDTO, request
  238. * @return respMessage
  239. */
  240. private String doLinkResponse(WeixinMessageDTO weixinMessageDTO, HttpServletRequest request) {
  241. //调用未识别回复语通用方法
  242. String respMessage = unrecognizedReply(weixinMessageDTO, request);
  243. return respMessage;
  244. }
  245. /**
  246. * 【微信触发类型】音频消息
  247. * @param weixinMessageDTO, request
  248. * @return respMessage
  249. */
  250. private String doVoiceResponse(WeixinMessageDTO weixinMessageDTO, HttpServletRequest request) {
  251. //调用未识别回复语通用方法
  252. String respMessage = unrecognizedReply(weixinMessageDTO, request);
  253. return respMessage;
  254. }
  255. /**
  256. * 【微信触发类型】视频消息
  257. * @param weixinMessageDTO, request
  258. * @return respMessage
  259. */
  260. private String doVideoResponse(WeixinMessageDTO weixinMessageDTO, HttpServletRequest request) {
  261. //调用未识别回复语通用方法
  262. String respMessage = unrecognizedReply(weixinMessageDTO, request);
  263. return respMessage;
  264. }
  265. /**
  266. * 【微信触发类型】小视频消息
  267. * @param weixinMessageDTO, request
  268. * @return respMessage
  269. */
  270. private String doShortVideoResponse(WeixinMessageDTO weixinMessageDTO, HttpServletRequest request) {
  271. //调用未识别回复语通用方法
  272. String respMessage = unrecognizedReply(weixinMessageDTO, request);
  273. return respMessage;
  274. }
  275. /**
  276. * 【微信触发类型】事件推送-订阅
  277. * @param weixinMessageDTO
  278. * @return
  279. */
  280. private String doSubscribeEventResponse(WeixinMessageDTO weixinMessageDTO, HttpServletRequest request) {
  281. String respMessage = "";
  282. WeixinSubscribe weixinSubscribe = weixinSubscribeService.querySubscribeByJwid(weixinMessageDTO.getToUserName());
  283. if(weixinSubscribe != null) {
  284. weixinMessageDTO.setTemplateId(weixinSubscribe.getTemplateId());
  285. //调用共通方法回复语
  286. respMessage = getRespMessage(weixinMessageDTO, weixinSubscribe.getMsgType(), request);
  287. }
  288. //关注,根据用户OpenId和Jwid添加关注用户
  289. saveGzUserInfoByOpenId(weixinMessageDTO.getFromUserName(), weixinMessageDTO.getToUserName());
  290. return respMessage;
  291. }
  292. /**
  293. * 【微信触发类型】事件推送-扫描二维码
  294. * @param weixinMessageDTO, request
  295. * @return respMessage
  296. */
  297. private String doScanEventResponse(WeixinMessageDTO weixinMessageDTO, HttpServletRequest request) {
  298. //TODO
  299. String respMessage = "";
  300. return respMessage;
  301. }
  302. /**
  303. * 【微信触发类型】事件推送-取消订阅
  304. * @param weixinMessageDTO, request
  305. * @return respMessage
  306. */
  307. private String doUnsubscribeEventResponse(WeixinMessageDTO weixinMessageDTO, HttpServletRequest request) {
  308. String respMessage = "";
  309. //取消关注,更新粉丝表状态
  310. WeixinGzuser weixinGzuser = weixinGzuserService.queryByOpenId(weixinMessageDTO.getFromUserName());
  311. if(weixinGzuser != null) {
  312. weixinGzuser.setSubscribe("0");//未关注
  313. weixinGzuser.setTagidList("");//标签ID置为空
  314. weixinGzuserService.doEdit(weixinGzuser);
  315. }
  316. return respMessage;
  317. }
  318. /**
  319. * 【微信触发类型】事件推送-接收地理位置信息
  320. * @param weixinMessageDTO, request
  321. * @return respMessage
  322. */
  323. private String doLocationEventResponse(WeixinMessageDTO weixinMessageDTO, HttpServletRequest request) {
  324. //TODO
  325. String respMessage = "";
  326. return respMessage;
  327. }
  328. /**
  329. * 【微信触发类型】事件推送-自定义菜单点击事件(Click)
  330. * @param weixinMessageDTO, request
  331. * @return respMessage
  332. */
  333. private String doMyMenuEventResponse(WeixinMessageDTO weixinMessageDTO, HttpServletRequest request) {
  334. String respMessage = "";
  335. //根据菜单KEY和JWID查询到菜单信息
  336. List<WeixinMenu> weixinMenus = weixinMenuService.queryMenuByKeyAndJwid(weixinMessageDTO.getKey(), weixinMessageDTO.getToUserName());
  337. if(weixinMenus != null && weixinMenus.size() > 0) {
  338. WeixinMenu weixinMenu = weixinMenus.get(0);
  339. if(weixinMenu != null && oConvertUtils.isNotEmpty(weixinMenu.getTemplateId())) {
  340. weixinMessageDTO.setTemplateId(weixinMenu.getTemplateId());
  341. //调用共通方法回复语
  342. respMessage = getRespMessage(weixinMessageDTO, weixinMenu.getMsgtype(), request);
  343. }
  344. }
  345. return respMessage;
  346. }
  347. /**
  348. * 【微信触发类型】事件推送-自定义菜单点击事件(VIEW)
  349. * @param weixinMessageDTO, request
  350. * @return respMessage
  351. */
  352. private String doMyMenuViewEventResponse(WeixinMessageDTO weixinMessageDTO, HttpServletRequest request) {
  353. //TODO
  354. String respMessage = "";
  355. return respMessage;
  356. }
  357. /**
  358. * 文本素材消息返回-根据文本素材ID
  359. * @param weixinMessageDTO
  360. * @return respMessage
  361. */
  362. private String getTextRespMessage(WeixinMessageDTO weixinMessageDTO) {
  363. String respMessage = "";
  364. //根据TemplateID查询数据返回TemplateContent文本内容。
  365. WeixinTexttemplate textTemplate = weixinTexttemplateService.queryById(weixinMessageDTO.getTemplateId());
  366. TextMessageResp textMessage = new TextMessageResp();
  367. if (textTemplate != null) {
  368. String content = textTemplate.getTemplateContent();
  369. textMessage.setContent(content);
  370. textMessage.setCreateTime(new Date().getTime());
  371. textMessage.setFromUserName(weixinMessageDTO.getToUserName());
  372. textMessage.setToUserName(weixinMessageDTO.getFromUserName());
  373. textMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_TEXT);
  374. //把文本素材转换为XML返回
  375. respMessage = MessageUtil.textMessageToXml(textMessage);
  376. }
  377. return respMessage;
  378. }
  379. /**
  380. * 图文素材消息返回-根据图文素材ID
  381. * @param weixinMessageDTO, request
  382. * @return respMessage
  383. */
  384. private String getNewsRespMessage(WeixinMessageDTO weixinMessageDTO, HttpServletRequest request) {
  385. String respMessage = "";
  386. String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
  387. //根据TemplateID查询图文记录
  388. WeixinNewstemplate weixinNewstemplate = weixinNewstemplateService.queryById(weixinMessageDTO.getTemplateId());
  389. //根据图文ID查询关联表记录
  390. List<WeixinNewsitem> weixinNewsitems = weixinNewsitemService.queryByNewstemplateId(weixinNewstemplate.getId());
  391. //包装为NewMessageResp类
  392. NewsMessageResp newsMessageResp = new NewsMessageResp();
  393. if(weixinNewsitems != null && weixinNewsitems.size() > 0) {
  394. newsMessageResp.setArticleCount(weixinNewsitems.size());
  395. List<Article> articles = new ArrayList<Article>();
  396. for (WeixinNewsitem weixinNewsitem : weixinNewsitems) {
  397. Article article = new Article();
  398. article.setTitle(weixinNewsitem.getTitle());
  399. article.setDescription(weixinNewsitem.getDescription());
  400. String url = "";
  401. if ("news".equals(weixinNewsitem.getNewType())) {
  402. url = basePath + "/weixin/back/weixinCommon/goContent.do?id=" + weixinNewsitem.getId() + "&jwid=" + weixinMessageDTO.getToUserName();
  403. } else {
  404. //图文外部链接参数替换
  405. url = weixinNewsitem.getExternalUrl();
  406. if(oConvertUtils.isNotEmpty(url)) {
  407. if (url.indexOf("${openid}") != -1) {
  408. url = url.replace("${openid}", weixinMessageDTO.getFromUserName());
  409. }
  410. if (url.indexOf("${wxid}") != -1) {
  411. url = url.replace("${wxid}", weixinMessageDTO.getToUserName());
  412. }
  413. if (url.indexOf("${appid}") != -1) {
  414. MyJwWebJwid jwWeb = myJwWebJwidService.queryByJwid(weixinMessageDTO.getToUserName());
  415. url = url.replace("${appid}", jwWeb.getWeixinAppId());
  416. }
  417. }
  418. }
  419. article.setUrl(url);
  420. if(weixinNewsitem.getImagePath()!=null&&weixinNewsitem.getImagePath().indexOf("http://")!=-1){
  421. article.setPicUrl(weixinNewsitem.getImagePath());
  422. }else{
  423. article.setPicUrl(basePath + "/"+ weixinNewsitem.getImagePath());
  424. }
  425. articles.add(article);
  426. }
  427. newsMessageResp.setArticles(articles);
  428. newsMessageResp.setCreateTime(new Date().getTime());
  429. newsMessageResp.setFromUserName(weixinMessageDTO.getToUserName());
  430. newsMessageResp.setToUserName(weixinMessageDTO.getFromUserName());
  431. newsMessageResp.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_NEWS);
  432. //把图文素材转换为XML返回
  433. respMessage = MessageUtil.newsMessageToXml(newsMessageResp);
  434. }
  435. return respMessage;
  436. }
  437. /**
  438. * 扩展接口业务消息返回- 扩展接口业务 (自定义菜单使用)
  439. * @param weixinMessageDTO, request
  440. * @return respMessage
  441. */
  442. private String getExpandRespMessage(WeixinMessageDTO weixinMessageDTO, HttpServletRequest request) {
  443. //TODO
  444. String respMessage = "";
  445. return respMessage;
  446. }
  447. /**
  448. * 语音素材消息返回-根据语音素材ID
  449. * @param weixinMessageDTO, request
  450. * @return respMessage
  451. */
  452. private String getVoiceRespMessage(WeixinMessageDTO weixinMessageDTO, HttpServletRequest request) {
  453. //TODO
  454. String respMessage = "";
  455. return respMessage;
  456. }
  457. /**
  458. * 图片素材消息返回-根据图片素材ID
  459. * @param weixinMessageDTO, request
  460. * @return respMessage
  461. */
  462. private String getImageRespMessage(WeixinMessageDTO weixinMessageDTO, HttpServletRequest request) {
  463. //TODO
  464. String respMessage = "";
  465. return respMessage;
  466. }
  467. /**
  468. * 视频素材消息返回-根据视频素材ID
  469. * @param weixinMessageDTO, request
  470. * @return respMessage
  471. */
  472. private String getVideoRespMessage(WeixinMessageDTO weixinMessageDTO, HttpServletRequest request) {
  473. //TODO
  474. String respMessage = "";
  475. return respMessage;
  476. }
  477. /**
  478. * 遍历关键字管理中是否存在用户输入的关键字信息
  479. *
  480. * @param content
  481. * @return WeixinAutoresponse
  482. */
  483. private WeixinAutoresponse findKey(String content, String toUsername) {
  484. List<WeixinAutoresponse> autoResponses = weixinAutoresponseService.queryByJwid(toUsername);
  485. for (WeixinAutoresponse r : autoResponses) {
  486. //------关键字支持模糊匹配功能------
  487. //全匹配
  488. if("1".equals(r.getKeywordType())){
  489. // 如果包含关键字
  490. String kw = r.getKeyword();
  491. //通用写法,页面支持中文逗号
  492. kw = kw.replace(",", ",");
  493. String[] allkw = kw.split(",");
  494. for (String k : allkw) {
  495. if (k.equalsIgnoreCase(content)) {
  496. return r;
  497. }
  498. }
  499. //模糊匹配
  500. }else if("2".equals(r.getKeywordType())){
  501. String kw = r.getKeyword();
  502. //通用写法,页面支持中文逗号
  503. kw = kw.replace(",", ",");
  504. String[] allkw = kw.split(",");
  505. for (String k : allkw) {
  506. Pattern p = Pattern.compile(".*?"+k+".*?");
  507. if (p.matcher(content).matches()) {
  508. return r;
  509. }
  510. }
  511. }
  512. }
  513. return null;
  514. }
  515. /**
  516. * 共通方法 - 【微信触发类型】未识别回复语
  517. * @param weixinMessageDTO, request
  518. * @return respMessage
  519. */
  520. private String unrecognizedReply(WeixinMessageDTO weixinMessageDTO, HttpServletRequest request) {
  521. String respMessage = "";
  522. //根据输入内容和微信ID查询未识别回复语记录
  523. WeixinAutoresponseDefault autoresponseDefault = weixinAutoresponseDefaultService.queryBymsgTriggerType(weixinMessageDTO.getMsgType(), weixinMessageDTO.getToUserName());
  524. if(autoresponseDefault != null && oConvertUtils.isNotEmpty(autoresponseDefault.getTemplateId())) {
  525. String templateId = autoresponseDefault.getTemplateId();
  526. weixinMessageDTO.setTemplateId(templateId);
  527. //调用共通回复语
  528. respMessage = getRespMessage(weixinMessageDTO, autoresponseDefault.getMsgType(), request);
  529. }
  530. return respMessage;
  531. }
  532. /**
  533. * 共通方法 - 根据平台设置返回不同类型的回复语
  534. * @param weixinMessageDTO
  535. * @param msgType
  536. * @param request
  537. * @return respMessage
  538. */
  539. private String getRespMessage(WeixinMessageDTO weixinMessageDTO, String msgType, HttpServletRequest request) {
  540. String respMessage = "";
  541. if(MessageUtil.RESP_MESSAGE_TYPE_TEXT.equals(msgType)) {
  542. //调用文本素材消息回复
  543. respMessage = getTextRespMessage(weixinMessageDTO);
  544. } else if(MessageUtil.RESP_MESSAGE_TYPE_NEWS.equals(msgType)) {
  545. //调用图文素材消息回复
  546. respMessage = getNewsRespMessage(weixinMessageDTO, request);
  547. } else if(MessageUtil.RESP_MESSAGE_TYPE_IMAGE.equals(msgType)) {
  548. //调用图片素材消息回复
  549. respMessage = getImageRespMessage(weixinMessageDTO, request);
  550. } else if(MessageUtil.RESP_MESSAGE_TYPE_VOICE.equals(msgType)) {
  551. //调用语音素材消息回复
  552. respMessage = getVoiceRespMessage(weixinMessageDTO, request);
  553. } else if(MessageUtil.RESP_MESSAGE_TYPE_VIDEO.equals(msgType)) {
  554. //调用视频素材消息回复
  555. respMessage = getVideoRespMessage(weixinMessageDTO, request);
  556. } else if(MessageUtil.RESP_MESSAGE_TYPE_EXPAND.equals(msgType)) {
  557. //调用扩展接口业务素材消息回复
  558. respMessage = getExpandRespMessage(weixinMessageDTO, request);
  559. }
  560. return respMessage;
  561. }
  562. /**
  563. * 消息存储 - 每次请求开始往消息存储表中写入记录
  564. * @param requestMap
  565. */
  566. private void saveReceiveMessage(Map<String, String> requestMap) {
  567. try {
  568. //消息类型
  569. String msgType = requestMap.get("MsgType");
  570. //拼装消息存储
  571. WeixinReceivetext weixinReceivetext = new WeixinReceivetext();
  572. weixinReceivetext.setCreateTime(new Date());
  573. weixinReceivetext.setFromUserName(requestMap.get("FromUserName"));
  574. weixinReceivetext.setToUserName(requestMap.get("ToUserName"));
  575. weixinReceivetext.setMsgId(requestMap.get("MsgId"));
  576. weixinReceivetext.setMsgType(msgType);
  577. weixinReceivetext.setMediaId(requestMap.get("MediaId"));
  578. //根据不同类型存储Content不统一,改为存储JSON格式
  579. Map<String,Object> map = new HashMap<String,Object>();
  580. boolean contentFlag = false;
  581. //【微信触发类型】文本消息
  582. if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_TEXT)) {
  583. weixinReceivetext.setContent(requestMap.get("Content"));
  584. contentFlag = true;
  585. }
  586. //【微信触发类型】图片消息
  587. else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_IMAGE)) {
  588. map.put("PicUrl", requestMap.get("PicUrl"));
  589. }
  590. //【微信触发类型】地理位置消息
  591. else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_LOCATION)) {
  592. map.put("Location_X", requestMap.get("Location_X"));
  593. map.put("Location_Y", requestMap.get("Location_Y"));
  594. map.put("Scale", requestMap.get("Scale"));
  595. map.put("Label", requestMap.get("Label"));
  596. }
  597. //【微信触发类型】链接消息
  598. else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_LINK)) {
  599. map.put("Title", requestMap.get("Title"));
  600. map.put("Description", requestMap.get("Description"));
  601. map.put("Url", requestMap.get("Url"));
  602. }
  603. //【微信触发类型】音频消息
  604. else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_VOICE)) {
  605. map.put("Format", requestMap.get("Format"));
  606. map.put("Recognition", requestMap.get("Recognition"));
  607. }
  608. //【微信触发类型】视频消息
  609. else if (msgType.equals(MessageUtil.RESP_MESSAGE_TYPE_VIDEO)) {
  610. map.put("ThumbMediaId", requestMap.get("ThumbMediaId"));
  611. }
  612. //【微信触发类型】小视频消息
  613. else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_SHORTVIDEO)) {
  614. map.put("ThumbMediaId", requestMap.get("ThumbMediaId"));
  615. }
  616. //如果不是文本消息则转换JSON存储
  617. if(!contentFlag) {
  618. JSONObject jsonObject = JSONObject.fromObject(map);
  619. weixinReceivetext.setContent(jsonObject.toString());
  620. }
  621. weixinReceivetextService.doAdd(weixinReceivetext);
  622. } catch (Exception e) {
  623. log.error("-----消息存储异常:--------------"+e.toString());
  624. }
  625. }
  626. /**
  627. * 其他事件处理
  628. * @param eventType
  629. * @param requestMap
  630. * @return
  631. */
  632. private String doOtherEventResponse(String eventType, Map<String, String> requestMap) {
  633. if (oConvertUtils.isEmpty(eventType)) {
  634. return "";
  635. }
  636. eventType = eventType.trim();
  637. if (eventType.equals(MessageUtil.EVENT_CARD_PASS_CHECK)) {
  638. // 卡劵: 审核事件推送
  639. return "";
  640. } else if (eventType.equals(MessageUtil.EVENT_USER_GET_CARD)) {
  641. // 卡劵: 领取事件推送
  642. return "";
  643. } else if (eventType.equals(MessageUtil.EVENT_CARD_NOT_PASS_CHECK)) {
  644. // 卡劵: 领取失败事件推送
  645. return "";
  646. } else if (eventType.equals(MessageUtil.EVENT_USER_GIFTING_CARD)) {
  647. // 卡劵: 转赠事件推送
  648. return "";
  649. } else if (eventType.equals(MessageUtil.EVENT_USER_DEL_CARD)) {
  650. // 卡劵: 删除事件推送
  651. return "";
  652. } else if (eventType.equals(MessageUtil.EVENT_USER_CONSUME_CARD)) {
  653. // 卡劵: 核销事件推送
  654. return "";
  655. } else if (eventType.equals(MessageUtil.EVENT_USER_PAY_FROM_PAY_CELL)) {
  656. // 卡劵: 买单事件推送
  657. return "";
  658. } else if (eventType.equals(MessageUtil.EVENT_USER_VIEW_CARD)) {
  659. // 卡劵: 进入会员卡事件推送
  660. return "";
  661. } else if (eventType.equals(MessageUtil.EVENT_USER_ENTER_SESSION_FROM_CARD)) {
  662. // 卡劵:从卡券进入公众号会话事件推送
  663. return "";
  664. } else if (eventType.equals(MessageUtil.EVENT_UPDATE_MEMBER_CARD)) {
  665. // 卡劵: 会员卡内容更新事件
  666. return "";
  667. } else if (eventType.equals(MessageUtil.EVENT_CARD_SKU_REMIND)) {
  668. // 卡劵: 库存报警事件
  669. return "";
  670. } else if (eventType.equals(MessageUtil.EVENT_CARD_PAY_ORDER)) {
  671. // 卡劵: 券点流水详情事件
  672. return "";
  673. } else if (eventType.equals(MessageUtil.EVENT_SUBMIT_MEMBERCARD_USER_INFO)) {
  674. // 卡劵: 会员卡激活事件推送
  675. return "";
  676. } else if (eventType.equals(MessageUtil.EVENT_MASSSENDJOBFINISH)) {
  677. // 群发: 事件推送群发结果
  678. return "";
  679. // TODO
  680. } else if (eventType.equals(MessageUtil.EVENT_SHAKEAROUNDUSERSHAKE)) {
  681. // 摇一摇: 事件通知
  682. return "";
  683. } else if (eventType.equals(MessageUtil.EVENT_SHAKEAROUNDLOTTERYBIND)) {
  684. // 红包: 事件通知
  685. return "";
  686. } else if (eventType.equals(MessageUtil.EVENT_USER_SCAN_PRODUCT)) {
  687. // 扫一扫: 打开商品主页事件推送
  688. return "";
  689. } else if (eventType.equals(MessageUtil.EVENT_USER_SCAN_PRODUCT_ENTER_SESSION)) {
  690. // 扫一扫: 进入公众号事件推送
  691. return "";
  692. } else if (eventType.equals(MessageUtil.EVENT_USER_SCAN_PRODUCT_ASYNC)) {
  693. // 扫一扫: 地理位置信息异步推送
  694. return "";
  695. } else if (eventType.equals(MessageUtil.EVENT_USER_SCAN_PRODUCT_VERIFY_ACTION)) {
  696. // 扫一扫: 商品审核结果推送
  697. return "";
  698. } else if (eventType.equals(MessageUtil.EVENT_TEMPLATESENDJOBFINISH)) {
  699. // 模版消息: 模版消息发送任务完成后
  700. return "";
  701. } else if (requestMap.containsKey(MessageUtil.EVENT_TRANSACTION_ID)) {
  702. // 对支付事件进行处理。
  703. return "";
  704. }
  705. return "";
  706. }
  707. /**
  708. * 根据用户OpenId和Jwid添加关注用户
  709. * @param openId
  710. * @param jwid
  711. */
  712. public void saveGzUserInfoByOpenId(String openId, String jwid) {
  713. WeixinGzuser weixinGzuser = weixinGzuserService.queryByOpenId(openId);
  714. // 获取请求的微信公众账号信息
  715. MyJwWebJwid myJwWebJwid = myJwWebJwidService.queryByJwid(jwid);
  716. // 通过微信接口,抓取关注用户信息
  717. WeixinGzuser gzUser = weixinGzuserService.getGzUserInfo(openId, jwid, myJwWebJwid.getAccessToken());
  718. if (weixinGzuser == null) {
  719. if (gzUser != null) {
  720. WeixinGzuser gzUserInfo = new WeixinGzuser();
  721. gzUserInfo.setCity(EmojiFilter.filterEmoji(gzUser.getCity()));
  722. // 增加表情过滤
  723. gzUserInfo.setCountry(EmojiFilter.filterEmoji(gzUser.getCountry()));
  724. gzUserInfo.setHeadimgurl(gzUser.getHeadimgurl());
  725. String nickName = WeixinUtil.encode(gzUser.getNickname().getBytes());
  726. gzUserInfo.setNickname(nickName);
  727. gzUserInfo.setNicknameTxt(gzUser.getNickname());
  728. gzUserInfo.setOpenid(gzUser.getOpenid());
  729. // 增加表情过滤
  730. gzUserInfo.setProvince(EmojiFilter.filterEmoji(gzUser.getProvince()));
  731. gzUserInfo.setSex(gzUser.getSex());
  732. gzUserInfo.setSubscribe(gzUser.getSubscribe());
  733. //update-begin--Author:zhangweijian Date: 20180807 for:关注时间修改
  734. gzUserInfo.setSubscribeTime(gzUser.getSubscribeTime());
  735. //update-end--Author:zhangweijian Date: 20180807 for:关注时间修改
  736. gzUserInfo.setSubscribeScene(gzUser.getSubscribeScene());
  737. gzUserInfo.setQrScene(gzUser.getQrScene());
  738. gzUserInfo.setQrSceneStr(gzUser.getQrSceneStr());
  739. gzUserInfo.setGroupid(gzUser.getGroupid());
  740. gzUserInfo.setLanguage(gzUser.getLanguage());
  741. gzUserInfo.setBzname(gzUser.getBzname());
  742. gzUserInfo.setTagidList(gzUser.getTagidList());
  743. gzUserInfo.setCreateTime(new Date());
  744. gzUserInfo.setJwid(jwid);
  745. this.weixinGzuserService.doAdd(gzUserInfo);
  746. } else {
  747. try {
  748. // 获取不到用户详细信息情况下
  749. WeixinGzuser temp = new WeixinGzuser();
  750. temp.setOpenid(openId);
  751. // 默认设为关注
  752. temp.setSubscribe(WeiXinConstants.WEIXIN_SUBSCRIBE_TYPE);
  753. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  754. temp.setSubscribeTime(sdf.format(new Date()));
  755. temp.setCreateTime(new Date());
  756. temp.setJwid(myJwWebJwid.getJwid());
  757. this.weixinGzuserService.doAdd(temp);
  758. } catch (Exception e) {
  759. log.info("---------------保存粉丝信息异常-------------------" + e.toString());
  760. }
  761. }
  762. } else {
  763. if (gzUser != null) {
  764. String nickName = WeixinUtil.encode(gzUser.getNickname().getBytes());
  765. weixinGzuser.setNickname(nickName);
  766. weixinGzuser.setHeadimgurl(gzUser.getHeadimgurl());
  767. }
  768. // 默认设为关注
  769. weixinGzuser.setSubscribe(WeiXinConstants.WEIXIN_SUBSCRIBE_TYPE);
  770. weixinGzuser.setSubscribeTime(DateUtils.getDataString(DateUtils.datetimeFormat));
  771. this.weixinGzuserService.doEdit(weixinGzuser);
  772. }
  773. }
  774. }

最后更新:2018-08-26 13:30:07.0

—— 原创内容,转载请注明:[ 文章转载自:ZrLog demo site   //118.24.153.47/ ] ——