PushMsgTask.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. package com.yihu.wlyy.task;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import java.util.concurrent.LinkedBlockingQueue;
  5. import com.yihu.wlyy.util.SystemConf;
  6. import io.swagger.annotations.Api;
  7. import org.json.JSONArray;
  8. import org.json.JSONObject;
  9. import org.slf4j.Logger;
  10. import org.slf4j.LoggerFactory;
  11. import org.springframework.stereotype.Controller;
  12. import org.springframework.web.bind.annotation.RequestMapping;
  13. import org.springframework.web.bind.annotation.ResponseBody;
  14. import com.fasterxml.jackson.databind.ObjectMapper;
  15. import com.yihu.wlyy.entity.wechat.WechatTemplate;
  16. import com.yihu.wlyy.entity.wechat.WechatTemplateData;
  17. import com.yihu.wlyy.util.HttpUtil;
  18. @Controller
  19. @RequestMapping(value = "/pushMsgTask")
  20. @Api(description = "消息推送")
  21. public class PushMsgTask {
  22. private static Logger logger = LoggerFactory.getLogger(PushMsgTask.class);
  23. // 最大容量为50的数组堵塞队列
  24. private static LinkedBlockingQueue<JSONObject> queue = new LinkedBlockingQueue<JSONObject>();
  25. private static PushMsgTask instance;
  26. private static Object lock = new Object();
  27. public static PushMsgTask getInstance() {
  28. synchronized (lock) {
  29. if (instance == null) {
  30. instance = new PushMsgTask();
  31. instance.run();
  32. }
  33. }
  34. return instance;
  35. }
  36. @RequestMapping(value = "aa")
  37. @ResponseBody
  38. public String test() {
  39. return "1111";
  40. }
  41. /**
  42. * 添加一条推送消息
  43. * @param receiver 接收人
  44. * @param type 消息类型
  45. * @param title 消息标题
  46. * @param msg 消息内容
  47. * @param data 消息数据
  48. */
  49. public void put(String receiver, String type, String title, String msg, String data) {
  50. try {
  51. JSONObject json = new JSONObject();
  52. json.put("receiver", receiver);
  53. json.put("type", type);
  54. json.put("title", title);
  55. json.put("msg", msg);
  56. json.put("data", data);
  57. queue.put(json);
  58. } catch (Exception e) {
  59. logger.error("添加到消息队列失败!", e);
  60. e.printStackTrace();
  61. }
  62. }
  63. /**
  64. * 添加微信消息
  65. * @param type
  66. * @param data
  67. */
  68. public void putWxMsg(String access_token, int type, String openid, String name, JSONObject data) {
  69. try {
  70. JSONObject json = new JSONObject();
  71. json.put("wx", true);
  72. json.put("access_token", access_token);
  73. json.put("type", type);
  74. json.put("openid", openid);
  75. json.put("name", name);
  76. json.put("data", data);
  77. queue.put(json);
  78. } catch (Exception e) {
  79. logger.error("添加到微信消息列队列失败!", e);
  80. e.printStackTrace();
  81. }
  82. }
  83. public void put(JSONArray array) {
  84. if (array == null || array.length() == 0) {
  85. return;
  86. }
  87. for (int i = 0; i < array.length(); i++) {
  88. JSONObject json = array.getJSONObject(i);
  89. if (json == null) {
  90. continue;
  91. }
  92. try {
  93. queue.put(json);
  94. } catch (Exception e) {
  95. logger.error("批量添加到消息队列失败!", e);
  96. }
  97. }
  98. }
  99. private void run() {
  100. new Thread(new ConsumerTask()).start();
  101. }
  102. // 消费者
  103. class ConsumerTask implements Runnable {
  104. @Override
  105. public void run() {
  106. try {
  107. while (true) {
  108. // 如果queue为空,则当前线程会堵塞,直到有新数据加入
  109. JSONObject json = queue.take();
  110. boolean wx = json.has("wx") ? json.getBoolean("wx") : false;
  111. if (wx) {
  112. // 推送微信消息
  113. JSONObject data = json.has("data") ? json.getJSONObject("data") : null;
  114. if (data == null) {
  115. continue;
  116. }
  117. // 消息类型:1:签约成功 2:签约失败 3:咨询回复通知 4:健康指导提醒
  118. int type = json.has("type") ? json.getInt("type") : -1;
  119. if (type == -1) {
  120. continue;
  121. }
  122. String access_token = json.has("access_token") ? json.getString("access_token") : "";
  123. String openid = json.has("openid") ? json.getString("openid") : "";
  124. String name = json.has("name") ? json.getString("name") : "";
  125. // 发送消息到微信端
  126. sendWeixinMessage(access_token, type, openid, name, data);
  127. } else {
  128. // 推送平台消息
  129. String receiver = json.has("receiver") ? json.getString("receiver") : "";
  130. String type = json.has("type") ? json.getString("type") : "";
  131. String title = json.has("title") ? json.getString("title") : "";
  132. String msg = json.has("msg") ? json.getString("msg") : "";
  133. String data = json.has("data") ? json.getString("data") : "";
  134. boolean res = HttpUtil.pushMessage(receiver, type, title, msg, data);
  135. if (res) {
  136. logger.info("消息推送成功!");
  137. } else {
  138. logger.error("消息推送失败!");
  139. }
  140. }
  141. }
  142. } catch (Exception ex) {
  143. ex.printStackTrace();
  144. }
  145. }
  146. }
  147. // private static final String signSuccess = "0D2vYZVRzFz15p9Y_pkZ1DKutDq8UOsks79FXUKS0tA";
  148. // private static final String signFalied = "My2VNERjJt4NXR4Ibh42pdrP6B6ka8rQxZeWinQh99s";
  149. // private static final String consultNotice = "0mF_vHj-ILx8EH8DwzmAi7LqzjqYiU9IrSRRmziTZyc";
  150. // private static final String healthNotice = "uv31ES_VCmq3tBYtyGmEQvIwU_zh9LDhF3bFpbIUt5g";
  151. // private static final String termination = "C0tdXtA_8k-Cy4a1EkzQuI877vqaqAtRkc-e_Gsd7sk";
  152. // private static final String appointmentSuccess = "FY3Pqa66tHIE1Fv-irbFBPOh5cYP71fkOzfZKH4S-Fo";
  153. // private static final String appointmentCancel = "tldWEb9AN7p_RoHoD8ml0GxWW3V1V_mpEEhp2v6p56s";
  154. // private static final String url = "http://www.xmtyw.cn/wlyy/wx/html/";
  155. /**
  156. * 发送微信模板消息
  157. * @param type 1:签约成功 2:签约失败 3:咨询回复通知 4:健康指导提醒 5:解约申请通知 6:预约挂号成功通知 7:预约取消通知
  158. * @param json 当type==1||type==2时:{"first":"消息主题",”doctor":"医生code","doctorName":"医生名","date":"签约时间","content":"签约内容","remark":"消息备注"}
  159. * type==3时:{"first":"消息主题","consult":"医生咨询编号","consultcontent":"咨询内容","replycontent":"回复内容","doctorName":"医生名","remark":"消息备注"}
  160. * type==4时:{"first":"消息主题","date":"指导时间","orgName":"指导机构","doctorName":"指导医生名","content":"指导内容","remark":"消息备注"}
  161. * type==5时:{"first":"消息主题","date":"解约时间",”doctor":"医生code","doctorName":"医生名","orgName":"解约机构","remark":"消息备注"}
  162. * type==6时:{"first":"消息主题","date":"预约时间",”id":"预约ID","doctorName":"医生名","orgName":"预约医院","deptName":"预约科室","remark":"消息备注"}
  163. * type==7时:{"first":"消息主题","name":"就诊人名","date":"预约时间","doctorName":"医生名","orgName":"预约医院","remark":"消息备注"}
  164. * @return
  165. */
  166. private boolean sendWeixinMessage(String access_token, int type, String openid, String name, JSONObject json) {
  167. try {
  168. if (access_token != null) {
  169. String token_url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + access_token;
  170. String params = typeMsg(type, openid, name, json);
  171. if (params == "") {
  172. logger.error("参数错误!");
  173. return false;
  174. }
  175. String result = HttpUtil.sendPost(token_url, params);
  176. JSONObject jsonResult = new JSONObject(result);
  177. if (Integer.parseInt(jsonResult.get("errcode").toString()) == 0) {
  178. logger.info("微信信息推送成功!");
  179. return true;
  180. } else {
  181. logger.error("错误编码:" + jsonResult.get("errcode").toString() + " 错误提示:" + jsonResult.get("errmsg").toString());
  182. return false;
  183. }
  184. } else {
  185. logger.error("获取access_token失败!");
  186. return false;
  187. }
  188. } catch (Exception e) {
  189. e.printStackTrace();
  190. logger.error("微信信息推送失败!");
  191. return false;
  192. }
  193. }
  194. /**
  195. * 拼接参数
  196. * @param type
  197. * @return
  198. */
  199. public String typeMsg(int type, String openid, String name, JSONObject json) {
  200. try {
  201. String url = SystemConf.getInstance().getSystemProperties().getProperty("server_url") + "wx/html/";
  202. WechatTemplate temp = new WechatTemplate();
  203. temp.setTouser(openid);
  204. temp.setTopcolor("#000000");
  205. Map<String, WechatTemplateData> m = new HashMap<String, WechatTemplateData>();
  206. WechatTemplateData first = new WechatTemplateData();
  207. first.setColor("#000000");
  208. first.setValue(json.getString("first"));
  209. m.put("first", first);
  210. WechatTemplateData remark = new WechatTemplateData();
  211. remark.setColor("#000000");
  212. remark.setValue(json.getString("remark"));
  213. m.put("remark", remark);
  214. String temp_id = "";
  215. if (type == 3) {
  216. temp.setUrl(url + "yszx/html/consulting-doctor.html?openid=" + openid + "&consult=" + json.getString("consult"));
  217. temp_id = SystemConf.getInstance().getSystemProperties().getProperty("template_consult_notice");
  218. temp.setTemplate_id(temp_id);
  219. WechatTemplateData keyword1 = new WechatTemplateData();
  220. keyword1.setColor("#000000");
  221. keyword1.setValue(json.getString("consultcontent"));
  222. m.put("keyword1", keyword1);
  223. WechatTemplateData keyword2 = new WechatTemplateData();
  224. keyword2.setColor("#000000");
  225. keyword2.setValue(json.getString("replycontent"));
  226. m.put("keyword2", keyword2);
  227. WechatTemplateData keyword3 = new WechatTemplateData();
  228. keyword3.setColor("#000000");
  229. keyword3.setValue(json.getString("doctorName"));
  230. m.put("keyword3", keyword3);
  231. } else if (type == 1 || type == 2) {
  232. if (type == 1)
  233. temp_id = SystemConf.getInstance().getSystemProperties().getProperty("template_sign_success");
  234. else
  235. temp_id = SystemConf.getInstance().getSystemProperties().getProperty("template_sign_failed");
  236. temp.setUrl(url + "ssgg/html/doctor-homepage-new.html?openid=" + openid + "&state=" + json.getString("doctor"));
  237. temp.setTouser(openid);
  238. temp.setTopcolor("#000000");
  239. temp.setTemplate_id(temp_id);
  240. WechatTemplateData keyword1 = new WechatTemplateData();
  241. keyword1.setColor("#000000");
  242. keyword1.setValue(json.getString("doctorName"));
  243. m.put("keyword1", keyword1);
  244. WechatTemplateData keyword2 = new WechatTemplateData();
  245. keyword2.setColor("#000000");
  246. keyword2.setValue(name);
  247. m.put("keyword2", keyword2);
  248. WechatTemplateData keyword3 = new WechatTemplateData();
  249. keyword3.setColor("#000000");
  250. keyword3.setValue(json.getString("date"));
  251. m.put("keyword3", keyword3);
  252. WechatTemplateData keyword4 = new WechatTemplateData();
  253. keyword4.setColor("#000000");
  254. keyword4.setValue(json.getString("content"));
  255. m.put("keyword4", keyword4);
  256. } else if (type == 4) {
  257. temp.setUrl(url + "yszd/html/doctor-guidance.html?openid=" + openid);
  258. temp_id = SystemConf.getInstance().getSystemProperties().getProperty("template_health_notice");
  259. temp.setTemplate_id(temp_id);
  260. WechatTemplateData keyword1 = new WechatTemplateData();
  261. keyword1.setColor("#000000");
  262. keyword1.setValue(json.getString("date"));
  263. m.put("keyword1", keyword1);
  264. WechatTemplateData keyword2 = new WechatTemplateData();
  265. keyword2.setColor("#000000");
  266. keyword2.setValue(json.getString("orgName"));
  267. m.put("keyword2", keyword2);
  268. WechatTemplateData keyword3 = new WechatTemplateData();
  269. keyword3.setColor("#000000");
  270. keyword3.setValue(json.getString("doctorName"));
  271. m.put("keyword3", keyword3);
  272. WechatTemplateData keyword4 = new WechatTemplateData();
  273. keyword4.setColor("#000000");
  274. keyword4.setValue(name);
  275. m.put("keyword4", keyword4);
  276. WechatTemplateData keyword5 = new WechatTemplateData();
  277. keyword5.setColor("#000000");
  278. keyword5.setValue(json.getString("content"));
  279. m.put("keyword5", keyword5);
  280. } else if (type == 5) {
  281. temp.setUrl(url + "ssgg/html/doctor-homepage-new.html?openid=" + openid + "&state=" + json.getString("doctor"));
  282. temp_id = SystemConf.getInstance().getSystemProperties().getProperty("template_termination");
  283. temp.setTemplate_id(temp_id);
  284. WechatTemplateData keyword1 = new WechatTemplateData();
  285. keyword1.setColor("#000000");
  286. keyword1.setValue(json.getString("doctorName"));
  287. m.put("keyword1", keyword1);
  288. WechatTemplateData keyword2 = new WechatTemplateData();
  289. keyword2.setColor("#000000");
  290. keyword2.setValue(name);
  291. m.put("keyword2", keyword2);
  292. WechatTemplateData keyword3 = new WechatTemplateData();
  293. keyword3.setColor("#000000");
  294. keyword3.setValue(json.getString("orgName"));
  295. m.put("keyword3", keyword3);
  296. WechatTemplateData keyword4 = new WechatTemplateData();
  297. keyword4.setColor("#000000");
  298. keyword4.setValue(json.getString("date"));
  299. m.put("keyword4", keyword4);
  300. } else if (type == 6) {
  301. temp.setUrl(url + "wdyy/html/detail-appointment.html?openid=" + openid +"&id=" + json.getLong("id"));
  302. temp_id = SystemConf.getInstance().getSystemProperties().getProperty("template_appoint_success");
  303. temp.setTemplate_id(temp_id);
  304. WechatTemplateData keyword1 = new WechatTemplateData();
  305. keyword1.setColor("#000000");
  306. keyword1.setValue(json.getString("orgName"));
  307. m.put("hospitalname", keyword1);
  308. WechatTemplateData keyword2 = new WechatTemplateData();
  309. keyword2.setColor("#000000");
  310. keyword2.setValue(json.getString("deptName"));
  311. m.put("deptname", keyword2);
  312. WechatTemplateData keyword3 = new WechatTemplateData();
  313. keyword3.setColor("#000000");
  314. keyword3.setValue(json.getString("doctorName"));
  315. m.put("doctorname", keyword3);
  316. WechatTemplateData keyword4 = new WechatTemplateData();
  317. keyword4.setColor("#000000");
  318. keyword4.setValue(json.getString("date"));
  319. m.put("planstarttime", keyword4);
  320. } else if (type == 7) {
  321. temp.setUrl(url + "wdyy/html/my-appointment.html?openid=" + openid);
  322. temp_id = SystemConf.getInstance().getSystemProperties().getProperty("template_appoint_failed");
  323. temp.setTemplate_id(temp_id);
  324. WechatTemplateData keyword1 = new WechatTemplateData();
  325. keyword1.setColor("#000000");
  326. keyword1.setValue(json.getString("name"));
  327. m.put("keynote1", keyword1);
  328. WechatTemplateData keyword2 = new WechatTemplateData();
  329. keyword2.setColor("#000000");
  330. keyword2.setValue(json.getString("date"));
  331. m.put("keynote2", keyword2);
  332. WechatTemplateData keyword3 = new WechatTemplateData();
  333. keyword3.setColor("#000000");
  334. keyword3.setValue(json.getString("doctorName"));
  335. m.put("keynote3", keyword3);
  336. WechatTemplateData keyword4 = new WechatTemplateData();
  337. keyword4.setColor("#000000");
  338. keyword4.setValue(json.getString("orgName"));
  339. m.put("keynote4", keyword4);
  340. }
  341. temp.setData(m);
  342. ObjectMapper mapper = new ObjectMapper();
  343. String strJson = mapper.writeValueAsString(temp);
  344. return strJson;
  345. } catch (Exception e) {
  346. e.printStackTrace();
  347. return "";
  348. }
  349. }
  350. }