微信消息通知更新。
This commit is contained in:
parent
b5a09dfd71
commit
8625d959da
|
|
@ -0,0 +1,136 @@
|
||||||
|
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;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取token
|
||||||
|
* token有效期暂定
|
||||||
|
* 方便后面存储为redis数据使用。
|
||||||
|
*/
|
||||||
|
public static String getToken() {
|
||||||
|
// 接口地址拼接参数
|
||||||
|
String getTokenApi = wxConfig.getGerTokenUrl() + wxConfig.getGrantTypeInToken() + "&appid=" + "wx404f2439a8c24e15" + "&secret=" + "49ade04a817067fe2d65ab2f17afce75";
|
||||||
|
String tokenJsonStr = doGetPost(getTokenApi, "GET", null);
|
||||||
|
JSONObject tokenJson = JSONObject.parseObject(tokenJsonStr);
|
||||||
|
String token = tokenJson.get("access_token").toString();
|
||||||
|
log.info("获取到用户token:{}", token);
|
||||||
|
return token;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 推送消息
|
||||||
|
*
|
||||||
|
* @param dataMap 消息内容
|
||||||
|
* @param mesType 消息类型-- 后面补充为枚举数据
|
||||||
|
* @param userOpenId 用户openId
|
||||||
|
* @param token 鉴权token信息
|
||||||
|
*/
|
||||||
|
public static void sendWeChatMsg(String token, String userOpenId, WxMsgEnum mesType, Map<String, Object> dataMap) {
|
||||||
|
// 接口地址
|
||||||
|
String sendMsgApi = wxConfig.getSendMsgUrl() + token;
|
||||||
|
//整体参数map
|
||||||
|
Map<String, Object> paramMap = new HashMap<String, Object>();
|
||||||
|
//根据自己的模板定义内容和颜色
|
||||||
|
// dataMap.put("first", new DataEntity("详细内容XXXXXXX", "#173177"));
|
||||||
|
// dataMap.put("keyword1", new DataEntity("私有化部署XXX", "#173177"));
|
||||||
|
// dataMap.put("keyword2", new DataEntity("2020-08-18XXX", "#173177"));
|
||||||
|
// dataMap.put("remark", new DataEntity("申请成功XXX", "#173177"));
|
||||||
|
paramMap.put("touser", userOpenId);
|
||||||
|
switch (mesType) {
|
||||||
|
case TEXT:
|
||||||
|
paramMap.put("template_id", wxConfig.getTextTemplateId());
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
paramMap.put("data", dataMap);
|
||||||
|
String reposont = doGetPost(sendMsgApi, "POST", paramMap);
|
||||||
|
log.info("用户:{}消息:{}推送成功。{}", userOpenId, dataMap.toString(), System.currentTimeMillis());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 调用接口 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue