2023-09-28 09:56:09 +08:00
|
|
|
|
package com.ghy.common.utils;
|
|
|
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
|
import com.ghy.common.config.WxConfig;
|
|
|
|
|
|
import com.ghy.common.enums.WxMsgEnum;
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
|
import java.io.OutputStreamWriter;
|
|
|
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
|
|
import java.net.URL;
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @Author 但星霖
|
|
|
|
|
|
* @Date 2023-09-23 10:35:41
|
|
|
|
|
|
* 说明:微信消息工具类
|
|
|
|
|
|
*/
|
|
|
|
|
|
@Slf4j
|
|
|
|
|
|
@Component
|
|
|
|
|
|
public class WechatMsgUtils {
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
|
private static WxConfig wxConfig;
|
|
|
|
|
|
|
2023-10-06 00:33:50 +08:00
|
|
|
|
static final String appid = "wx404f2439a8c24e15";
|
|
|
|
|
|
static final String appsecret = "49ade04a817067fe2d65ab2f17afce75";
|
2023-10-05 21:42:10 +08:00
|
|
|
|
|
2023-09-28 09:56:09 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取token
|
|
|
|
|
|
* token有效期暂定
|
|
|
|
|
|
* 方便后面存储为redis数据使用。
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static String getToken() {
|
|
|
|
|
|
// 接口地址拼接参数
|
2023-10-05 21:42:10 +08:00
|
|
|
|
String getTokenApi = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + appsecret;
|
2023-09-28 09:56:09 +08:00
|
|
|
|
String tokenJsonStr = doGetPost(getTokenApi, "GET", null);
|
|
|
|
|
|
JSONObject tokenJson = JSONObject.parseObject(tokenJsonStr);
|
2023-10-06 00:33:50 +08:00
|
|
|
|
log.info("获取到的token返回json内容: {} ", tokenJson);
|
2023-09-28 09:56:09 +08:00
|
|
|
|
String token = tokenJson.get("access_token").toString();
|
|
|
|
|
|
log.info("获取到用户token:{}", token);
|
|
|
|
|
|
return token;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-06 00:33:50 +08:00
|
|
|
|
private void setConfig(String token){
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-09-28 09:56:09 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 推送消息
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param dataMap 消息内容
|
|
|
|
|
|
* @param mesType 消息类型-- 后面补充为枚举数据
|
|
|
|
|
|
* @param userOpenId 用户openId
|
|
|
|
|
|
* @param token 鉴权token信息
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static void sendWeChatMsg(String token, String userOpenId, WxMsgEnum mesType, Map<String, Object> dataMap) {
|
|
|
|
|
|
// 接口地址
|
2023-10-05 21:42:10 +08:00
|
|
|
|
String sendMsgApi = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + token;
|
2023-09-28 09:56:09 +08:00
|
|
|
|
//整体参数map
|
|
|
|
|
|
Map<String, Object> paramMap = new HashMap<String, Object>();
|
2023-10-09 00:03:50 +08:00
|
|
|
|
paramMap.put("touser", userOpenId);
|
2023-10-06 00:33:50 +08:00
|
|
|
|
paramMap.put("page", "index");
|
2024-02-18 17:59:52 +08:00
|
|
|
|
paramMap.put("template_id", mesType.getTempCode());
|
2025-04-28 15:51:08 +08:00
|
|
|
|
paramMap.put("url","");
|
|
|
|
|
|
Map<String, Object> miniprogram=new HashMap<>();
|
|
|
|
|
|
miniprogram.put("appid","");
|
|
|
|
|
|
miniprogram.put("pagepath","");
|
|
|
|
|
|
paramMap.put("miniprogram",miniprogram);
|
2023-10-06 00:33:50 +08:00
|
|
|
|
JSONObject jsonObject = new JSONObject();
|
|
|
|
|
|
for (Map.Entry<String, Object> objectEntry : dataMap.entrySet()) {
|
|
|
|
|
|
JSONObject model = new JSONObject();
|
|
|
|
|
|
model.put("value", objectEntry.getValue());
|
|
|
|
|
|
jsonObject.put(objectEntry.getKey(), model);
|
|
|
|
|
|
}
|
|
|
|
|
|
paramMap.put("data", jsonObject);
|
2023-09-28 09:56:09 +08:00
|
|
|
|
String reposont = doGetPost(sendMsgApi, "POST", paramMap);
|
2023-10-05 21:42:10 +08:00
|
|
|
|
log.info("reposont:{}", reposont);
|
2023-10-06 00:33:50 +08:00
|
|
|
|
log.info("用户:{}消息:{}推送返回谁:{}", userOpenId, paramMap.toString(),reposont);
|
2023-09-28 09:56:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 调用接口 post
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param apiPath
|
|
|
|
|
|
*/
|
|
|
|
|
|
public static String doGetPost(String apiPath, String type, Map<String, Object> paramMap) {
|
|
|
|
|
|
OutputStreamWriter out = null;
|
|
|
|
|
|
InputStream is = null;
|
|
|
|
|
|
String result = null;
|
|
|
|
|
|
try {
|
|
|
|
|
|
// 创建连接
|
|
|
|
|
|
URL url = new URL(apiPath);
|
|
|
|
|
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
|
|
|
|
|
connection.setDoOutput(true);
|
|
|
|
|
|
connection.setDoInput(true);
|
|
|
|
|
|
connection.setUseCaches(false);
|
|
|
|
|
|
connection.setInstanceFollowRedirects(true);
|
|
|
|
|
|
// 设置请求方式
|
|
|
|
|
|
connection.setRequestMethod(type);
|
|
|
|
|
|
// 设置接收数据的格式
|
|
|
|
|
|
connection.setRequestProperty("Accept", "application/json");
|
|
|
|
|
|
// 设置发送数据的格式
|
|
|
|
|
|
connection.setRequestProperty("Content-Type", "application/json");
|
|
|
|
|
|
connection.connect();
|
|
|
|
|
|
if (type.equals("POST")) {
|
|
|
|
|
|
// utf-8编码
|
|
|
|
|
|
out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
|
|
|
|
|
|
out.append(JSON.toJSONString(paramMap));
|
|
|
|
|
|
out.flush();
|
|
|
|
|
|
out.close();
|
|
|
|
|
|
}
|
|
|
|
|
|
// 读取响应
|
|
|
|
|
|
is = connection.getInputStream();
|
|
|
|
|
|
// 获取长度
|
|
|
|
|
|
int length = (int) connection.getContentLength();
|
|
|
|
|
|
if (length != -1) {
|
|
|
|
|
|
byte[] data = new byte[length];
|
|
|
|
|
|
byte[] temp = new byte[512];
|
|
|
|
|
|
int readLen = 0;
|
|
|
|
|
|
int destPos = 0;
|
|
|
|
|
|
while ((readLen = is.read(temp)) > 0) {
|
|
|
|
|
|
System.arraycopy(temp, 0, data, destPos, readLen);
|
|
|
|
|
|
destPos += readLen;
|
|
|
|
|
|
}
|
|
|
|
|
|
// utf-8编码
|
|
|
|
|
|
result = new String(data, "UTF-8");
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
try {
|
|
|
|
|
|
is.close();
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|