WeiXinEventProcess.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. package com.yihu.wlyy.wechat.process;
  2. import com.yihu.wlyy.util.SystemConf;
  3. import com.yihu.wlyy.wechat.util.WeiXinMessageReplyUtils;
  4. import com.yihu.wlyy.wechat.util.WeiXinMessageUtils;
  5. import org.apache.commons.lang3.StringUtils;
  6. import org.springframework.stereotype.Component;
  7. import java.util.*;
  8. /**
  9. * 微信事件处理
  10. *
  11. * Created by lyr on 2016/08/11.
  12. */
  13. @Component
  14. public class WeiXinEventProcess {
  15. /**
  16. * 微信推送事件处理
  17. *
  18. * @param message
  19. * @return
  20. */
  21. public static String eventProcess(Map<String, String> message) throws Exception {
  22. String result = "";
  23. String event = message.get("Event");
  24. switch (event) {
  25. case WeiXinMessageUtils.EVENT_TYPE_SCAN: // 扫描二维码事件
  26. result = scanEventProcess(message);
  27. break;
  28. case WeiXinMessageUtils.EVENT_TYPE_SUBSCRIBE: // 订阅事件
  29. if (message.containsKey("EventKey") && StringUtils.isNotEmpty(message.get("EventKey")) && message.containsKey("Ticket")
  30. && StringUtils.isNotEmpty(message.get("Ticket"))) {
  31. result = scanEventProcess(message);
  32. }else{
  33. result = subscribeEventProcess(message);
  34. }
  35. break;
  36. case WeiXinMessageUtils.EVENT_TYPE_CLICK: //菜单点击
  37. result = WeiXinEventProcess.clickProcess(message);
  38. default:
  39. break;
  40. }
  41. return result;
  42. }
  43. /**
  44. * 微信菜单点击处理
  45. *
  46. * @param message
  47. * @return
  48. */
  49. public static String clickProcess(Map<String, String> message) throws Exception {
  50. String result = "";
  51. if(message.get("EventKey").equals("caozuoshuoming")){
  52. String tempStr = clickEventProcess(message);
  53. /*int firsIndex = tempStr.indexOf("<item>");
  54. String result1 = tempStr.substring(0,firsIndex-1);
  55. int secondIndex = tempStr.indexOf("<item>",firsIndex+10);
  56. String result2 = tempStr.substring(secondIndex-1);*/
  57. result=tempStr.replace("<ArticleCount>5</ArticleCount>","<ArticleCount>4</ArticleCount>");
  58. }
  59. return result;
  60. }
  61. /**
  62. * 菜单点击消息发送
  63. *
  64. * @param message
  65. * @return
  66. * @throws Exception
  67. */
  68. private static String clickEventProcess(Map<String,String> message) throws Exception{
  69. String result = "";
  70. // 配置信息
  71. Properties systemConf = SystemConf.getInstance().getSystemProperties();
  72. // 图文信息
  73. List<Map<String,String>> articles = new ArrayList<>();
  74. Map<String,String> articleConsult = new HashMap<>();
  75. // 图文URL
  76. String urlConsult = systemConf.getProperty("patient_consult_url");
  77. // 图文消息图片URL
  78. String picUrlConsult = systemConf.getProperty("patient_operatinginstrutions_pic_url");
  79. // URL设置服务器URL、AppId
  80. urlConsult = urlConsult.replace("{server}", systemConf.getProperty("wechat_base_url"))
  81. .replace("{appId}", systemConf.getProperty("appId"));
  82. //图片地址
  83. picUrlConsult = picUrlConsult.replace("{server}", systemConf.getProperty("server_url"));
  84. articleConsult.put("Url",urlConsult);
  85. articleConsult.put("Title", "医生咨询功能使用说明");
  86. articleConsult.put("Description","功能使用说明");
  87. articleConsult.put("PicUrl",picUrlConsult);
  88. articles.add(articleConsult);
  89. Map<String,String> articleBooking = new HashMap<>();
  90. // 图文URL
  91. String urlBooking = systemConf.getProperty("patient_booking_url");
  92. // 图文消息图片URL
  93. String picUrlBooking = systemConf.getProperty("patient_booking_pic_url");
  94. // URL设置服务器URL、AppId
  95. urlBooking = urlBooking.replace("{server}", systemConf.getProperty("wechat_base_url"))
  96. .replace("{appId}", systemConf.getProperty("appId"));
  97. //图片地址
  98. picUrlBooking = picUrlBooking.replace("{server}", systemConf.getProperty("server_url"));
  99. articleBooking.put("Url",urlBooking);
  100. articleBooking.put("Title", "预约挂号功能使用说明");
  101. articleBooking.put("Description","功能使用说明");
  102. articleBooking.put("PicUrl",picUrlBooking);
  103. articles.add(articleBooking);
  104. Map<String,String> articleDevice = new HashMap<>();
  105. // 图文URL
  106. String urlDevice = systemConf.getProperty("patient_device_url");
  107. // 图文消息图片URL
  108. String picUrlDevice = systemConf.getProperty("patient_device_pic_url");
  109. // URL设置服务器URL、AppId
  110. urlDevice = urlDevice.replace("{server}", systemConf.getProperty("wechat_base_url"))
  111. .replace("{appId}", systemConf.getProperty("appId"));
  112. //图片地址
  113. picUrlDevice = picUrlDevice.replace("{server}", systemConf.getProperty("server_url"));
  114. articleDevice.put("Url",urlDevice);
  115. articleDevice.put("Title", "体征设备管理功能使用说明");
  116. articleDevice.put("Description","功能使用说明");
  117. articleDevice.put("PicUrl",picUrlDevice);
  118. articles.add(articleDevice);
  119. Map<String,String> articleFamily = new HashMap<>();
  120. // 图文URL
  121. String urlFamily = systemConf.getProperty("patient_family_url");
  122. // 图文消息图片URL
  123. String picUrlFamily = systemConf.getProperty("patient_family_pic_url");
  124. // URL设置服务器URL、AppId
  125. urlFamily = urlFamily.replace("{server}", systemConf.getProperty("wechat_base_url"))
  126. .replace("{appId}", systemConf.getProperty("appId"));
  127. //图片地址
  128. picUrlFamily = picUrlFamily.replace("{server}", systemConf.getProperty("server_url"));
  129. articleFamily.put("Url",urlFamily);
  130. articleFamily.put("Title", "我的家庭功能使用说明");
  131. articleFamily.put("Description","功能使用说明");
  132. articleFamily.put("PicUrl",picUrlFamily);
  133. articles.add(articleFamily);
  134. // 构建回复消息XML
  135. result = WeiXinMessageReplyUtils.replyNewsMessage(message.get("FromUserName"),message.get("ToUserName"),articles);
  136. return result;
  137. }
  138. /**
  139. * 关注事件消息发送
  140. * @param message
  141. * @return
  142. * @throws Exception
  143. */
  144. private static String subscribeEventProcess(Map<String,String> message) throws Exception{
  145. String result = "";
  146. // 配置信息
  147. Properties systemConf = SystemConf.getInstance().getSystemProperties();
  148. // 图文信息
  149. List<Map<String,String>> articles = new ArrayList<>();
  150. Map<String,String> article = new HashMap<>();
  151. // 图文URL
  152. String url = systemConf.getProperty("doctor_subscribe_url");
  153. // 图文消息图片URL
  154. String picUrl = systemConf.getProperty("doctor_qrcode_pic_url");
  155. // URL设置服务器URL、AppId
  156. url = url.replace("{server}", systemConf.getProperty("wechat_base_url"))
  157. .replace("{appId}", systemConf.getProperty("appId"));
  158. //图片地址
  159. picUrl = picUrl.replace("{server}", systemConf.getProperty("server_url"));
  160. article.put("Url",url);
  161. article.put("Title", "欢迎关注厦门i健康,快来签约家庭医生吧~");
  162. article.put("Description","请点击查看家庭签约");
  163. article.put("PicUrl",picUrl);
  164. articles.add(article);
  165. Map<String,String> articleConsult = new HashMap<>();
  166. // 图文URL
  167. String urlConsult = systemConf.getProperty("patient_consult_url");
  168. // 图文消息图片URL
  169. String picUrlConsult = systemConf.getProperty("patient_operatinginstrutions_pic_url");
  170. // URL设置服务器URL、AppId
  171. urlConsult = urlConsult.replace("{server}", systemConf.getProperty("wechat_base_url"))
  172. .replace("{appId}", systemConf.getProperty("appId"));
  173. //图片地址
  174. picUrlConsult = picUrlConsult.replace("{server}", systemConf.getProperty("server_url"));
  175. articleConsult.put("Url",urlConsult);
  176. articleConsult.put("Title", "医生咨询功能使用说明");
  177. articleConsult.put("Description","功能使用说明");
  178. articleConsult.put("PicUrl",picUrlConsult);
  179. articles.add(articleConsult);
  180. Map<String,String> articleBooking = new HashMap<>();
  181. // 图文URL
  182. String urlBooking = systemConf.getProperty("patient_booking_url");
  183. // 图文消息图片URL
  184. String picUrlBooking = systemConf.getProperty("patient_booking_pic_url");
  185. // URL设置服务器URL、AppId
  186. urlBooking = urlBooking.replace("{server}", systemConf.getProperty("wechat_base_url"))
  187. .replace("{appId}", systemConf.getProperty("appId"));
  188. //图片地址
  189. picUrlBooking = picUrlBooking.replace("{server}", systemConf.getProperty("server_url"));
  190. articleBooking.put("Url",urlBooking);
  191. articleBooking.put("Title", "预约挂号功能使用说明");
  192. articleBooking.put("Description","功能使用说明");
  193. articleBooking.put("PicUrl",picUrlBooking);
  194. articles.add(articleBooking);
  195. Map<String,String> articleDevice = new HashMap<>();
  196. // 图文URL
  197. String urlDevice = systemConf.getProperty("patient_device_url");
  198. // 图文消息图片URL
  199. String picUrlDevice = systemConf.getProperty("patient_device_pic_url");
  200. // URL设置服务器URL、AppId
  201. urlDevice = urlDevice.replace("{server}", systemConf.getProperty("wechat_base_url"))
  202. .replace("{appId}", systemConf.getProperty("appId"));
  203. //图片地址
  204. picUrlDevice = picUrlDevice.replace("{server}", systemConf.getProperty("server_url"));
  205. articleDevice.put("Url",urlDevice);
  206. articleDevice.put("Title", "体征设备管理功能使用说明");
  207. articleDevice.put("Description","功能使用说明");
  208. articleDevice.put("PicUrl",picUrlDevice);
  209. articles.add(articleDevice);
  210. Map<String,String> articleFamily = new HashMap<>();
  211. // 图文URL
  212. String urlFamily = systemConf.getProperty("patient_family_url");
  213. // 图文消息图片URL
  214. String picUrlFamily = systemConf.getProperty("patient_family_pic_url");
  215. // URL设置服务器URL、AppId
  216. urlFamily = urlFamily.replace("{server}", systemConf.getProperty("wechat_base_url"))
  217. .replace("{appId}", systemConf.getProperty("appId"));
  218. //图片地址
  219. picUrlFamily = picUrlFamily.replace("{server}", systemConf.getProperty("server_url"));
  220. articleFamily.put("Url",urlFamily);
  221. articleFamily.put("Title", "我的家庭功能使用说明");
  222. articleFamily.put("Description","功能使用说明");
  223. articleFamily.put("PicUrl",picUrlFamily);
  224. articles.add(articleFamily);
  225. // 构建回复消息XML
  226. result = WeiXinMessageReplyUtils.replyNewsMessage(message.get("FromUserName"),message.get("ToUserName"),articles);
  227. return result;
  228. }
  229. /**
  230. * 微信扫码事件处理
  231. *
  232. * @param message
  233. * @return
  234. * @throws Exception
  235. */
  236. private static String scanEventProcess(Map<String, String> message) throws Exception {
  237. String result = "";
  238. // 场景值
  239. String eventKey = message.get("EventKey");
  240. if(StringUtils.isNotEmpty(eventKey) && (eventKey.startsWith("qr_") ||
  241. eventKey.startsWith("qrscene_qr_"))) {
  242. // 配置
  243. Properties systemConf = SystemConf.getInstance().getSystemProperties();
  244. // 图文信息
  245. List<Map<String,String>> articles = new ArrayList<>();
  246. Map<String,String> article = new HashMap<>();
  247. // 二维码跳转URL
  248. String url = systemConf.getProperty("doctor_qrcode_url");
  249. // 图片URL
  250. String picUrl = systemConf.getProperty("doctor_qrcode_pic_url");
  251. // 二维码参数
  252. String[] keys = eventKey.replace("qrscene_qr_","").replace("qr_","").split("_");
  253. // 图文消息URL
  254. url = url.replace("{server}", systemConf.getProperty("wechat_base_url"))
  255. .replace("{appId}", systemConf.getProperty("appId"))
  256. .replace("{doctorCode}",keys[0]);
  257. //图片地址
  258. picUrl = picUrl.replace("{server}", systemConf.getProperty("server_url"));
  259. article.put("Url",url);
  260. article.put("Title",keys[1] + "医生签约");
  261. article.put("Description","请点击查看医生详情并申请签约");
  262. article.put("PicUrl",picUrl);
  263. articles.add(article);
  264. // 消息XML
  265. result = WeiXinMessageReplyUtils.replyNewsMessage(message.get("FromUserName"),message.get("ToUserName"),articles);
  266. }else if(StringUtils.isNotEmpty(eventKey) && (eventKey.startsWith("hs_") ||
  267. eventKey.startsWith("qrscene_hs_"))){
  268. // 配置信息
  269. Properties systemConf = SystemConf.getInstance().getSystemProperties();
  270. // 图文信息
  271. List<Map<String,String>> articles = new ArrayList<>();
  272. Map<String,String> article = new HashMap<>();
  273. // 社区二维码跳转URL
  274. String url = systemConf.getProperty("doctor_hos_qrcode_url");
  275. // 图片URL
  276. String picUrl = systemConf.getProperty("doctor_qrcode_pic_url");
  277. // 二维码参数
  278. String[] keys = eventKey.replace("qrscene_hs_","").replace("hs_","").split("_");
  279. // 图文消息URL
  280. url = url.replace("{server}", systemConf.getProperty("wechat_base_url"))
  281. .replace("{appId}", systemConf.getProperty("appId"))
  282. .replace("{hospital}",keys[0] + ":" + keys[1]);
  283. //图片地址
  284. picUrl = picUrl.replace("{server}", systemConf.getProperty("server_url"));
  285. article.put("Url",url);
  286. article.put("Title",keys[1]);
  287. article.put("Description","请点击查看社区详情");
  288. article.put("PicUrl",picUrl);
  289. articles.add(article);
  290. // 消息XML
  291. result = WeiXinMessageReplyUtils.replyNewsMessage(message.get("FromUserName"),message.get("ToUserName"),articles);
  292. } else if(StringUtils.isNotEmpty(eventKey) && (eventKey.startsWith("tw_") ||
  293. eventKey.startsWith("qrscene_tw_"))){
  294. // 配置信息
  295. Properties systemConf = SystemConf.getInstance().getSystemProperties();
  296. // 图文信息
  297. List<Map<String,String>> articles = new ArrayList<>();
  298. Map<String,String> article = new HashMap<>();
  299. // 社区二维码跳转URL
  300. String url = systemConf.getProperty("doctor_town_qrcode_url");
  301. // 图片URL
  302. String picUrl = systemConf.getProperty("doctor_qrcode_pic_url");
  303. // 二维码参数
  304. String[] keys = eventKey.replace("qrscene_tw_","").replace("tw_","").split("_");
  305. // 图文消息URL
  306. url = url.replace("{server}", systemConf.getProperty("wechat_base_url"))
  307. .replace("{appId}", systemConf.getProperty("appId"))
  308. .replace("{town}",keys[0]);
  309. //图片地址
  310. picUrl = picUrl.replace("{server}", systemConf.getProperty("server_url"));
  311. article.put("Url",url);
  312. article.put("Title",keys[1]);
  313. article.put("Description","请点击查看区详情");
  314. article.put("PicUrl",picUrl);
  315. articles.add(article);
  316. // 消息XML
  317. result = WeiXinMessageReplyUtils.replyNewsMessage(message.get("FromUserName"),message.get("ToUserName"),articles);
  318. }
  319. return result;
  320. }
  321. }