WeiXinEventProcess.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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. if(message.get("EventKey").equals("jiankangdangan")){
  60. result = clickEventProcessMenu(message);
  61. }
  62. return result;
  63. }
  64. /**
  65. * 菜单点击消息发送
  66. *
  67. * @param message
  68. * @return
  69. * @throws Exception
  70. */
  71. private static String clickEventProcess(Map<String,String> message) throws Exception{
  72. String result = "";
  73. // 配置信息
  74. Properties systemConf = SystemConf.getInstance().getSystemProperties();
  75. // 图文信息
  76. List<Map<String,String>> articles = new ArrayList<>();
  77. Map<String,String> articleConsult = new HashMap<>();
  78. // 图文URL
  79. String urlConsult = systemConf.getProperty("patient_consult_url");
  80. // 图文消息图片URL
  81. String picUrlConsult = systemConf.getProperty("patient_operatinginstrutions_pic_url");
  82. // URL设置服务器URL、AppId
  83. urlConsult = urlConsult.replace("{server}", systemConf.getProperty("wechat_base_url"))
  84. .replace("{appId}", systemConf.getProperty("appId"));
  85. //图片地址
  86. picUrlConsult = picUrlConsult.replace("{server}", systemConf.getProperty("server_url"));
  87. articleConsult.put("Url",urlConsult);
  88. articleConsult.put("Title", "医生咨询功能使用说明");
  89. articleConsult.put("Description","功能使用说明");
  90. articleConsult.put("PicUrl",picUrlConsult);
  91. articles.add(articleConsult);
  92. Map<String,String> articleBooking = new HashMap<>();
  93. // 图文URL
  94. String urlBooking = systemConf.getProperty("patient_booking_url");
  95. // 图文消息图片URL
  96. String picUrlBooking = systemConf.getProperty("patient_booking_pic_url");
  97. // URL设置服务器URL、AppId
  98. urlBooking = urlBooking.replace("{server}", systemConf.getProperty("wechat_base_url"))
  99. .replace("{appId}", systemConf.getProperty("appId"));
  100. System.out.println("picUrlBooking ===>"+picUrlBooking);
  101. System.out.println("server_url ===>"+systemConf.getProperty("server_url"));
  102. //图片地址
  103. picUrlBooking = picUrlBooking.replace("{server}", systemConf.getProperty("server_url"));
  104. articleBooking.put("Url",urlBooking);
  105. articleBooking.put("Title", "预约挂号功能使用说明");
  106. articleBooking.put("Description","功能使用说明");
  107. articleBooking.put("PicUrl",picUrlBooking);
  108. articles.add(articleBooking);
  109. Map<String,String> articleDevice = new HashMap<>();
  110. // 图文URL
  111. String urlDevice = systemConf.getProperty("patient_device_url");
  112. // 图文消息图片URL
  113. String picUrlDevice = systemConf.getProperty("patient_device_pic_url");
  114. // URL设置服务器URL、AppId
  115. urlDevice = urlDevice.replace("{server}", systemConf.getProperty("wechat_base_url"))
  116. .replace("{appId}", systemConf.getProperty("appId"));
  117. //图片地址
  118. picUrlDevice = picUrlDevice.replace("{server}", systemConf.getProperty("server_url"));
  119. articleDevice.put("Url",urlDevice);
  120. articleDevice.put("Title", "体征设备管理功能使用说明");
  121. articleDevice.put("Description","功能使用说明");
  122. articleDevice.put("PicUrl",picUrlDevice);
  123. articles.add(articleDevice);
  124. Map<String,String> articleFamily = new HashMap<>();
  125. // 图文URL
  126. String urlFamily = systemConf.getProperty("patient_family_url");
  127. // 图文消息图片URL
  128. String picUrlFamily = systemConf.getProperty("patient_family_pic_url");
  129. // URL设置服务器URL、AppId
  130. urlFamily = urlFamily.replace("{server}", systemConf.getProperty("wechat_base_url"))
  131. .replace("{appId}", systemConf.getProperty("appId"));
  132. //图片地址
  133. picUrlFamily = picUrlFamily.replace("{server}", systemConf.getProperty("server_url"));
  134. articleFamily.put("Url",urlFamily);
  135. articleFamily.put("Title", "我的家庭功能使用说明");
  136. articleFamily.put("Description","功能使用说明");
  137. articleFamily.put("PicUrl",picUrlFamily);
  138. articles.add(articleFamily);
  139. // 构建回复消息XML
  140. result = WeiXinMessageReplyUtils.replyNewsMessage(message.get("FromUserName"),message.get("ToUserName"),articles);
  141. return result;
  142. }
  143. /**
  144. * 菜单点击消息发送
  145. *
  146. * @param message
  147. * @return
  148. * @throws Exception
  149. */
  150. private static String clickEventProcessMenu(Map<String,String> message) throws Exception{
  151. String result = "";
  152. // 配置信息
  153. Properties systemConf = SystemConf.getInstance().getSystemProperties();
  154. // 图文信息
  155. List<Map<String,String>> articles = new ArrayList<>();
  156. //就诊记录
  157. Map<String,String> articleVisit = new HashMap<>();
  158. String visit = systemConf.getProperty("patient_visit_url");
  159. // 图文消息图片URL
  160. String visit_pic = systemConf.getProperty("patient_visit_pic");
  161. // URL设置服务器URL、AppId
  162. visit = visit.replace("{server}", systemConf.getProperty("wechat_base_url"))
  163. .replace("{appId}", systemConf.getProperty("appId"));
  164. //图片地址
  165. visit_pic = visit_pic.replace("{server}", systemConf.getProperty("server_url"));
  166. articleVisit.put("Url",visit);
  167. articleVisit.put("Title", "就诊记录");
  168. articleVisit.put("Description","就诊记录");
  169. articleVisit.put("PicUrl",visit_pic);
  170. articles.add(articleVisit);
  171. //#检查检验
  172. Map<String,String> articleInspect = new HashMap<>();
  173. // 图文URL
  174. String inspect = systemConf.getProperty("patient_inspect_url");
  175. // 图文消息图片URL
  176. String inspect_pic = systemConf.getProperty("patient_inspect_pic");
  177. // URL设置服务器URL、AppId
  178. inspect = inspect.replace("{server}", systemConf.getProperty("wechat_base_url"))
  179. .replace("{appId}", systemConf.getProperty("appId"));
  180. //图片地址
  181. inspect_pic = inspect_pic.replace("{server}", systemConf.getProperty("server_url"));
  182. articleInspect.put("Url",inspect);
  183. articleInspect.put("Title", "检查检验");
  184. articleInspect.put("Description","检查检验");
  185. articleInspect.put("PicUrl",inspect_pic);
  186. articles.add(articleInspect);
  187. Map<String,String> articleExamination = new HashMap<>();
  188. // 图文URL
  189. String examination = systemConf.getProperty("patient_examination_url");
  190. // 图文消息图片URL
  191. String examination_pic = systemConf.getProperty("patient_examination_pic");
  192. // URL设置服务器URL、AppId
  193. examination = examination.replace("{server}", systemConf.getProperty("wechat_base_url"))
  194. .replace("{appId}", systemConf.getProperty("appId"));
  195. //图片地址
  196. examination_pic = examination_pic.replace("{server}", systemConf.getProperty("server_url"));
  197. articleExamination.put("Url",examination);
  198. articleExamination.put("Title", "社区体检");
  199. articleExamination.put("Description","社区体检");
  200. articleExamination.put("PicUrl",examination_pic);
  201. articles.add(articleExamination);
  202. // 构建回复消息XML
  203. result = WeiXinMessageReplyUtils.replyNewsMessage(message.get("FromUserName"),message.get("ToUserName"),articles);
  204. return result;
  205. }
  206. /**
  207. * 关注事件消息发送
  208. * @param message
  209. * @return
  210. * @throws Exception
  211. */
  212. private static String subscribeEventProcess(Map<String,String> message) throws Exception{
  213. String result = "";
  214. // 配置信息
  215. Properties systemConf = SystemConf.getInstance().getSystemProperties();
  216. // 图文信息
  217. List<Map<String,String>> articles = new ArrayList<>();
  218. Map<String,String> article = new HashMap<>();
  219. // 图文URL
  220. String url = systemConf.getProperty("doctor_subscribe_url");
  221. // 图文消息图片URL
  222. String picUrl = systemConf.getProperty("doctor_qrcode_pic_url");
  223. // URL设置服务器URL、AppId
  224. url = url.replace("{server}", systemConf.getProperty("wechat_base_url"))
  225. .replace("{appId}", systemConf.getProperty("appId"));
  226. //图片地址
  227. picUrl = picUrl.replace("{server}", systemConf.getProperty("server_url"));
  228. article.put("Url",url);
  229. article.put("Title", "欢迎关注厦门i健康,快来签约家庭医生吧~");
  230. article.put("Description","请点击查看家庭签约");
  231. article.put("PicUrl",picUrl);
  232. articles.add(article);
  233. //设置URL
  234. setUrlItems(articles,systemConf);
  235. // 构建回复消息XML
  236. result = WeiXinMessageReplyUtils.replyNewsMessage(message.get("FromUserName"),message.get("ToUserName"),articles);
  237. return result;
  238. }
  239. /**
  240. * 设置微信公共链接
  241. * @param articles
  242. */
  243. public static void setUrlItems(List<Map<String,String>> articles,Properties systemConf){
  244. Map<String,String> articleConsult = new HashMap<>();
  245. // 图文URL
  246. String urlConsult = systemConf.getProperty("patient_consult_url");
  247. // 图文消息图片URL
  248. String picUrlConsult = systemConf.getProperty("patient_operatinginstrutions_pic_url");
  249. // URL设置服务器URL、AppId
  250. urlConsult = urlConsult.replace("{server}", systemConf.getProperty("wechat_base_url"))
  251. .replace("{appId}", systemConf.getProperty("appId"));
  252. //图片地址
  253. picUrlConsult = picUrlConsult.replace("{server}", systemConf.getProperty("server_url"));
  254. articleConsult.put("Url",urlConsult);
  255. articleConsult.put("Title", "医生咨询功能使用说明");
  256. articleConsult.put("Description","功能使用说明");
  257. articleConsult.put("PicUrl",picUrlConsult);
  258. articles.add(articleConsult);
  259. Map<String,String> articleBooking = new HashMap<>();
  260. // 图文URL
  261. String urlBooking = systemConf.getProperty("patient_booking_url");
  262. // 图文消息图片URL
  263. String picUrlBooking = systemConf.getProperty("patient_booking_pic_url");
  264. // URL设置服务器URL、AppId
  265. urlBooking = urlBooking.replace("{server}", systemConf.getProperty("wechat_base_url"))
  266. .replace("{appId}", systemConf.getProperty("appId"));
  267. //图片地址
  268. picUrlBooking = picUrlBooking.replace("{server}", systemConf.getProperty("server_url"));
  269. articleBooking.put("Url",urlBooking);
  270. articleBooking.put("Title", "预约挂号功能使用说明");
  271. articleBooking.put("Description","功能使用说明");
  272. articleBooking.put("PicUrl",picUrlBooking);
  273. articles.add(articleBooking);
  274. Map<String,String> articleDevice = new HashMap<>();
  275. // 图文URL
  276. String urlDevice = systemConf.getProperty("patient_device_url");
  277. // 图文消息图片URL
  278. String picUrlDevice = systemConf.getProperty("patient_device_pic_url");
  279. // URL设置服务器URL、AppId
  280. urlDevice = urlDevice.replace("{server}", systemConf.getProperty("wechat_base_url"))
  281. .replace("{appId}", systemConf.getProperty("appId"));
  282. //图片地址
  283. picUrlDevice = picUrlDevice.replace("{server}", systemConf.getProperty("server_url"));
  284. articleDevice.put("Url",urlDevice);
  285. articleDevice.put("Title", "体征设备管理功能使用说明");
  286. articleDevice.put("Description","功能使用说明");
  287. articleDevice.put("PicUrl",picUrlDevice);
  288. articles.add(articleDevice);
  289. Map<String,String> articleFamily = new HashMap<>();
  290. // 图文URL
  291. String urlFamily = systemConf.getProperty("patient_family_url");
  292. // 图文消息图片URL
  293. String picUrlFamily = systemConf.getProperty("patient_family_pic_url");
  294. // URL设置服务器URL、AppId
  295. urlFamily = urlFamily.replace("{server}", systemConf.getProperty("wechat_base_url"))
  296. .replace("{appId}", systemConf.getProperty("appId"));
  297. //图片地址
  298. picUrlFamily = picUrlFamily.replace("{server}", systemConf.getProperty("server_url"));
  299. articleFamily.put("Url",urlFamily);
  300. articleFamily.put("Title", "我的家庭功能使用说明");
  301. articleFamily.put("Description","功能使用说明");
  302. articleFamily.put("PicUrl",picUrlFamily);
  303. articles.add(articleFamily);
  304. }
  305. /**
  306. * 微信扫码事件处理
  307. *
  308. * @param message
  309. * @return
  310. * @throws Exception
  311. */
  312. private static String scanEventProcess(Map<String, String> message) throws Exception {
  313. String result = "";
  314. // 场景值
  315. String eventKey = message.get("EventKey");
  316. if(StringUtils.isNotEmpty(eventKey) && (eventKey.startsWith("qr_") ||
  317. eventKey.startsWith("qrscene_qr_"))) {
  318. // 配置
  319. Properties systemConf = SystemConf.getInstance().getSystemProperties();
  320. // 图文信息
  321. List<Map<String,String>> articles = new ArrayList<>();
  322. Map<String,String> article = new HashMap<>();
  323. // 二维码跳转URL
  324. String url = systemConf.getProperty("doctor_qrcode_url");
  325. // 图片URL
  326. String picUrl = systemConf.getProperty("doctor_qrcode_pic_url");
  327. // 二维码参数
  328. String[] keys = eventKey.replace("qrscene_qr_","").replace("qr_","").split("_");
  329. // 图文消息URL
  330. url = url.replace("{server}", systemConf.getProperty("wechat_base_url"))
  331. .replace("{appId}", systemConf.getProperty("appId"))
  332. .replace("{doctorCode}",keys[0]);
  333. //图片地址
  334. picUrl = picUrl.replace("{server}", systemConf.getProperty("server_url"));
  335. article.put("Url",url);
  336. article.put("Title",keys[1] + "医生签约");
  337. article.put("Description","请点击查看医生详情并申请签约");
  338. article.put("PicUrl",picUrl);
  339. articles.add(article);
  340. //设置通用链接
  341. setUrlItems(articles,systemConf);
  342. // 消息XML
  343. result = WeiXinMessageReplyUtils.replyNewsMessage(message.get("FromUserName"),message.get("ToUserName"),articles);
  344. }else if(StringUtils.isNotEmpty(eventKey) && (eventKey.startsWith("hs_") ||
  345. eventKey.startsWith("qrscene_hs_"))){
  346. // 配置信息
  347. Properties systemConf = SystemConf.getInstance().getSystemProperties();
  348. // 图文信息
  349. List<Map<String,String>> articles = new ArrayList<>();
  350. Map<String,String> article = new HashMap<>();
  351. // 社区二维码跳转URL
  352. String url = systemConf.getProperty("doctor_hos_qrcode_url");
  353. // 图片URL
  354. String picUrl = systemConf.getProperty("doctor_qrcode_pic_url");
  355. // 二维码参数
  356. String[] keys = eventKey.replace("qrscene_hs_","").replace("hs_","").split("_");
  357. // 图文消息URL
  358. url = url.replace("{server}", systemConf.getProperty("wechat_base_url"))
  359. .replace("{appId}", systemConf.getProperty("appId"))
  360. .replace("{hospital}",keys[0] + ":" + keys[1]);
  361. //图片地址
  362. picUrl = picUrl.replace("{server}", systemConf.getProperty("server_url"));
  363. article.put("Url",url);
  364. article.put("Title",keys[1]);
  365. article.put("Description","请点击查看社区详情");
  366. article.put("PicUrl",picUrl);
  367. articles.add(article);
  368. //设置通用链接
  369. setUrlItems(articles,systemConf);
  370. // 消息XML
  371. result = WeiXinMessageReplyUtils.replyNewsMessage(message.get("FromUserName"),message.get("ToUserName"),articles);
  372. } else if(StringUtils.isNotEmpty(eventKey) && (eventKey.startsWith("tw_") ||
  373. eventKey.startsWith("qrscene_tw_"))){
  374. // 配置信息
  375. Properties systemConf = SystemConf.getInstance().getSystemProperties();
  376. // 图文信息
  377. List<Map<String,String>> articles = new ArrayList<>();
  378. Map<String,String> article = new HashMap<>();
  379. // 社区二维码跳转URL
  380. String url = systemConf.getProperty("doctor_town_qrcode_url");
  381. // 图片URL
  382. String picUrl = systemConf.getProperty("doctor_qrcode_pic_url");
  383. // 二维码参数
  384. String[] keys = eventKey.replace("qrscene_tw_","").replace("tw_","").split("_");
  385. // 图文消息URL
  386. url = url.replace("{server}", systemConf.getProperty("wechat_base_url"))
  387. .replace("{appId}", systemConf.getProperty("appId"))
  388. .replace("{town}",keys[0]);
  389. //图片地址
  390. picUrl = picUrl.replace("{server}", systemConf.getProperty("server_url"));
  391. article.put("Url",url);
  392. article.put("Title",keys[1]);
  393. article.put("Description","请点击查看区详情");
  394. article.put("PicUrl",picUrl);
  395. articles.add(article);
  396. //设置通用链接
  397. setUrlItems(articles,systemConf);
  398. // 消息XML
  399. result = WeiXinMessageReplyUtils.replyNewsMessage(message.get("FromUserName"),message.get("ToUserName"),articles);
  400. }
  401. return result;
  402. }
  403. }