|
@@ -2,6 +2,7 @@ package com.xjrsoft.common.utils;
|
|
|
|
|
|
|
|
|
import cn.hutool.http.HttpUtil;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.xjrsoft.common.exception.MyException;
|
|
|
import com.xjrsoft.module.organization.dto.WeChatDepartDto;
|
|
@@ -52,13 +53,17 @@ public class WeChatUtil {
|
|
|
//获取通讯录或发消息token
|
|
|
public String getToken(Integer type) {
|
|
|
HashMap<String, Object> paramMap = new HashMap<>();
|
|
|
- paramMap.put("corpid", appKey);
|
|
|
- if(type==1) {
|
|
|
+ String url = "https://api.weixin.qq.com/cgi-bin/token";
|
|
|
+ if (type == 1) {
|
|
|
+ url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken";
|
|
|
paramMap.put("corpsecret", appSecret);
|
|
|
- }else if(type==2){
|
|
|
- paramMap.put("corpsecret", appSecret1);
|
|
|
+ paramMap.put("corpid", appKey);
|
|
|
+ } else if (type == 2) {
|
|
|
+ paramMap.put("grant_type", "client_credential");
|
|
|
+ paramMap.put("appid", appKey);
|
|
|
+ paramMap.put("secret", appSecret1);
|
|
|
}
|
|
|
- String result = HttpUtil.get("https://qyapi.weixin.qq.com/cgi-bin/gettoken", paramMap);
|
|
|
+ String result = HttpUtil.get(url, paramMap);
|
|
|
JSONObject jsonObject = JSONObject.parseObject(result);
|
|
|
String token = jsonObject.get("access_token").toString();
|
|
|
return token;
|
|
@@ -93,18 +98,19 @@ public class WeChatUtil {
|
|
|
return weChatUserDtos;
|
|
|
}
|
|
|
|
|
|
-// //发送消息给企业微信用户
|
|
|
-// public Boolean sendMessage(XjrOaMessage xjrOaMessage, String userId) {
|
|
|
-// String token = this.getToken(2);
|
|
|
-// JSONObject object = new JSONObject();
|
|
|
-// object.put("touser", userId);
|
|
|
-// object.put("msgtype", "text");
|
|
|
-// object.put("agentid", agentid);
|
|
|
-// JSONObject text = new JSONObject();
|
|
|
-// text.put("content", xjrOaMessage.getContent());
|
|
|
-// object.put("text",text);
|
|
|
-// String result = HttpUtil.post("https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token="+token, JSONObject.toJSONString(object));
|
|
|
-// System.out.printf(result);
|
|
|
-// return true;
|
|
|
-// }
|
|
|
+ //公众号发送文本消息给用户
|
|
|
+ public Boolean sendMessage(String content, String userId) {
|
|
|
+ String token = this.getToken(2);
|
|
|
+ JSONObject object = new JSONObject();
|
|
|
+ JSONArray touser=new JSONArray();
|
|
|
+ touser.add(userId);
|
|
|
+ object.put("touser", touser);
|
|
|
+ object.put("msgtype", "text");
|
|
|
+ JSONObject text = new JSONObject();
|
|
|
+ text.put("content", content);
|
|
|
+ object.put("text",text);
|
|
|
+ String result = HttpUtil.post("https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token="+token, JSONObject.toJSONString(object));
|
|
|
+ System.out.printf(result);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
}
|