Browse Source

上传文件

陈士柏 2 years ago
parent
commit
65ec3c4c15
1 changed files with 77 additions and 0 deletions
  1. 77 0
      mhotel/src/com/happy/common/http/Message2.java

+ 77 - 0
mhotel/src/com/happy/common/http/Message2.java

@@ -0,0 +1,77 @@
+package com.happy.common.http;
+
+import com.alibaba.fastjson.JSONObject;
+import com.google.gson.Gson;
+import com.google.gson.reflect.TypeToken;
+import com.happy.Model.Message.Mp_template_msg;
+import java.util.HashMap;
+
+public class Message2 {
+
+    /** 小程序推送消息到公众号
+     * openid:用户小程序openid, order_num订单号,
+     *  h_type房型, live_end入住和离店日期:2023-08-02~2023-08-06
+     *  user_name:用户名, hotel_name:酒店名
+     * **/
+    public static String send(String openid,String order_num,
+                              String h_type,String live_end,
+                              String user_name,String hotel_name)
+            throws Exception {
+        JSONObject jsonObject = new JSONObject();
+        jsonObject.put("grant_type", "client_credential");
+        jsonObject.put("appid", "wx2fc3f45732fae5d3"); // 小程序
+        jsonObject.put("secret","7eee4a49a4470a77f9222995e8511547");
+        String msg3 = HttpsClient.sendJson2("https://api.weixin.qq.com/cgi-bin/stable_token",jsonObject);
+        Gson gson = new Gson();
+        HashMap<String, String> userMap = gson.fromJson(msg3.toString(), new TypeToken<HashMap<String, String>>() {}.getType());
+        JSONObject message = new JSONObject();
+        String token = userMap.get("access_token");
+        message.put("touser", openid);
+
+        Mp_template_msg mp_template_msg = new Mp_template_msg();
+        mp_template_msg.setAppid("wxa46ef222053a1047");  // 公众号
+        mp_template_msg.setTemplate_id("2qQLkDrxBpMcao35jONQSo8GIY6wwzFna-LLU7Q7qRM");
+        mp_template_msg.setUrl("index");
+        JSONObject jsonObject1 = new JSONObject();
+        jsonObject1.put("appid","wx2fc3f45732fae5d3");
+        jsonObject1.put("pagepath","pages/index/index");
+        mp_template_msg.setMiniprogram(jsonObject1);
+        // 订单号
+        JSONObject character_string1_ = new JSONObject();
+        character_string1_.put("value", order_num);
+        character_string1_.put("color", "#173177");
+        // 房型名称
+        JSONObject thing6 = new JSONObject();
+        thing6.put("value", h_type);
+        thing6.put("color", "#0000FF");
+        // 入离时间
+        JSONObject time11 = new JSONObject();
+        time11.put("value", live_end);
+        time11.put("color", "#173177");
+        // 客户名称
+        JSONObject thing10 = new JSONObject();
+        thing10.put("value", user_name);
+        thing10.put("color", "#173177");
+        // 酒店名称
+        JSONObject thing9_ = new JSONObject();
+        thing9_.put("value", hotel_name);
+        thing9_.put("color", "#173177");
+        // 封装data
+        JSONObject data = new JSONObject();
+        // 订单号
+        data.put("character_string1",character_string1_);
+        // 房型名称
+        data.put("thing6",thing6);
+        // 入离时间
+        data.put("time11",time11);
+        // 客户名称
+        data.put("thing10",thing10);
+        // 酒店名称
+        data.put("thing9",thing9_);
+        mp_template_msg.setData(data);
+        message.put("mp_template_msg",mp_template_msg);
+        return HttpsClient.sendJson("https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token="+token, message);
+    }
+
+
+}