Message3.java 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package com.happy.common.http;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.google.gson.Gson;
  4. import com.google.gson.reflect.TypeToken;
  5. import com.happy.Model.Message.Mp_template_msg;
  6. import com.happy.Model.weixin.AccessToken;
  7. import com.happy.Model.weixin.WechatResult;
  8. import com.happy.Model.weixin.WeiXinUtil;
  9. import com.happy.Until.HttpUtils;
  10. import java.util.HashMap;
  11. public class Message3 {
  12. /** 小程序推送消息到公众号
  13. * openid:用户小程序openid, order_num订单号,
  14. * h_type房型, live_end入住和离店日期:2023-08-02~2023-08-06
  15. * user_name:用户名, hotel_name:酒店名
  16. * **/
  17. public static String send(String openid,String order_num,
  18. String h_type,String live_end,
  19. String user_name,String hotel_name,String id)
  20. throws Exception {
  21. JSONObject jsonObject = new JSONObject();
  22. jsonObject.put("grant_type", "client_credential");
  23. jsonObject.put("appid", WeiXinUtil.appid_c); // 小程序
  24. jsonObject.put("secret",WeiXinUtil.screct_c);
  25. String msg3 = HttpsClient.sendJson2("https://api.weixin.qq.com/cgi-bin/stable_token",jsonObject);
  26. Gson gson = new Gson();
  27. HashMap<String, String> userMap = gson.fromJson(msg3.toString(), new TypeToken<HashMap<String, String>>() {}.getType());
  28. JSONObject message = new JSONObject();
  29. String token = userMap.get("access_token");
  30. message.put("touser", openid);
  31. Mp_template_msg mp_template_msg = new Mp_template_msg();
  32. mp_template_msg.setAppid(WeiXinUtil.appid_g); // 公众号
  33. mp_template_msg.setTemplate_id("pQLY-9pKW-lD-6PuOlxieatGmuCHOezubTxI99cs7Zo");
  34. mp_template_msg.setUrl("index");
  35. JSONObject jsonObject1 = new JSONObject();
  36. jsonObject1.put("appid",WeiXinUtil.appid_c);
  37. jsonObject1.put("pagepath","/pages/push/push?id="+id);
  38. mp_template_msg.setMiniprogram(jsonObject1);
  39. // 订单号
  40. JSONObject character_string1_ = new JSONObject();
  41. character_string1_.put("value", order_num);
  42. character_string1_.put("color", "#173177");
  43. // 房型名称
  44. JSONObject thing6 = new JSONObject();
  45. thing6.put("value", h_type);
  46. thing6.put("color", "#0000FF");
  47. // 入离时间
  48. JSONObject time11 = new JSONObject();
  49. time11.put("value", live_end);
  50. time11.put("color", "#173177");
  51. // 客户名称
  52. JSONObject thing10 = new JSONObject();
  53. thing10.put("value", user_name);
  54. thing10.put("color", "#173177");
  55. // 酒店名称
  56. JSONObject thing9_ = new JSONObject();
  57. thing9_.put("value", hotel_name);
  58. thing9_.put("color", "#173177");
  59. // 封装data
  60. JSONObject data = new JSONObject();
  61. // 订单号
  62. data.put("character_string1",character_string1_);
  63. // 房型名称
  64. data.put("thing6",thing6);
  65. // 入离时间
  66. data.put("time11",time11);
  67. // 客户名称
  68. data.put("thing10",thing10);
  69. // 酒店名称
  70. data.put("thing9",thing9_);
  71. mp_template_msg.setData(data);
  72. message.put("mp_template_msg",mp_template_msg);
  73. return HttpsClient.sendJson("https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token="+token, message);
  74. }
  75. }