WeiXinEventProcess.java 16 KB

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