package com.stylefeng.guns.modular.system.util;
|
|
import cn.hutool.http.HttpUtil;
|
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSONObject;
|
import org.springframework.stereotype.Component;
|
|
import java.time.LocalDateTime;
|
import java.time.format.DateTimeFormatter;
|
|
/**
|
* @BelongsProject: xxx
|
* @BelongsPackage: com.xxx.commons.utils
|
* @ClassName WxMaSubscribeMessageUtil
|
* @Author: handsome boy__LiuWenCheng
|
* @CreateTime: 2024-09-05 15:58
|
*/
|
|
@Component
|
public class WxMaSubscribeMessageUtil {
|
public static String appId = "wx4d36488fa2cd2718";
|
public static String appSecret = "f3874e9c813f31a2e90c37346eb74d82";
|
|
|
/**
|
* 活动即将开始通知
|
* @param openId
|
*/
|
public static void push(String openId,String name ,Double money) {
|
String accessToken = getAccessToken();
|
String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + accessToken;
|
JSONObject data = new JSONObject();
|
data.put("touser", openId);
|
data.put("template_id", "loQAaLfhJvkkvZL0ILKbwKJiXiHq5db6SLqeLX_V54c ");
|
data.put("page", "/pages/index/index/index" );
|
|
// 设置消息内容
|
JSONObject messageData = new JSONObject();
|
|
|
JSONObject thing1 = new JSONObject();
|
thing1.put("value", name);
|
messageData.put("thing1", thing1);
|
|
|
|
JSONObject time2 = new JSONObject();
|
time2.put("value", "您的专属优惠券已到账,快点打车用掉吧~");
|
messageData.put("thing2", time2);
|
|
// 转化成年月日时分的时间格式
|
JSONObject thing3 = new JSONObject();
|
thing3.put("value", money);
|
messageData.put("amount3", thing3);
|
|
|
// 打印 messageData 的内容
|
System.out.println("messageData: " + messageData.toJSONString());
|
|
data.put("data", messageData);
|
// 打印 data 的内容
|
System.out.println("data: " + data.toJSONString());
|
|
// 发送消息
|
String result = HttpUtil.post(url, data.toString());
|
System.out.println("订阅消息结果:" + result);
|
}
|
|
|
|
|
public static String getAccessToken() {
|
|
|
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
|
url = url.replace("APPID", appId).replace("APPSECRET", appSecret);
|
try {
|
|
String result = HttpUtil.get(url);
|
JSONObject jsonObject = JSON.parseObject(result);
|
return jsonObject.getString("access_token");
|
} catch (Exception e) {
|
System.err.println("请求失败:" + e.getMessage());
|
e.printStackTrace();
|
return null;
|
}
|
|
}
|
|
|
}
|
|