|
|
|
@ -1,6 +1,7 @@
|
|
|
|
|
package com.zh.project0512.utils;
|
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
import com.zh.project0512.model.AppMessage;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
@ -21,42 +22,75 @@ public class HttpUtil {
|
|
|
|
|
private String corpid;
|
|
|
|
|
@Value("${qywx.corpsecret}")
|
|
|
|
|
private String corpsecret;
|
|
|
|
|
@Value("${qywx.agentid}")
|
|
|
|
|
private String agentid;
|
|
|
|
|
@Value("${wx.appid}")
|
|
|
|
|
private String appid;
|
|
|
|
|
|
|
|
|
|
public String qywxGetToken(){
|
|
|
|
|
public String qywxGetToken() {
|
|
|
|
|
String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken";
|
|
|
|
|
String requestUrl = url + "?corpid=" + corpid + "&corpsecret=" + corpsecret;
|
|
|
|
|
String str = httpRequest(requestUrl, "GET", null);
|
|
|
|
|
return JSONObject.parseObject(str).getString("access_token");
|
|
|
|
|
}
|
|
|
|
|
public JSONObject qywxDepartmentList(String access_token){
|
|
|
|
|
|
|
|
|
|
public JSONObject qywxDepartmentList(String access_token) {
|
|
|
|
|
String url = "https://qyapi.weixin.qq.com/cgi-bin/department/list";
|
|
|
|
|
String requestUrl = url + "?access_token=" + access_token;
|
|
|
|
|
String str = httpRequest(requestUrl, "GET", null);
|
|
|
|
|
return JSONObject.parseObject(str);
|
|
|
|
|
}
|
|
|
|
|
public JSONObject qywxUserSession(String access_token,String code){
|
|
|
|
|
|
|
|
|
|
public JSONObject qywxUserSession(String access_token, String code) {
|
|
|
|
|
String url = "https://qyapi.weixin.qq.com/cgi-bin/miniprogram/jscode2session";
|
|
|
|
|
String requestUrl = url + "?access_token=" + access_token +"&js_code="+ code +"&grant_type=authorization_code";
|
|
|
|
|
String requestUrl = url + "?access_token=" + access_token + "&js_code=" + code + "&grant_type=authorization_code";
|
|
|
|
|
String str = httpRequest(requestUrl, "GET", null);
|
|
|
|
|
return JSONObject.parseObject(str);
|
|
|
|
|
}
|
|
|
|
|
public JSONObject qywxUser(String access_token,String userid){
|
|
|
|
|
|
|
|
|
|
public JSONObject qywxUser(String access_token, String userid) {
|
|
|
|
|
String url = "https://qyapi.weixin.qq.com/cgi-bin/user/get";
|
|
|
|
|
String requestUrl = url + "?access_token=" + access_token +"&userid="+ userid ;
|
|
|
|
|
String requestUrl = url + "?access_token=" + access_token + "&userid=" + userid;
|
|
|
|
|
String str = httpRequest(requestUrl, "GET", null);
|
|
|
|
|
return JSONObject.parseObject(str);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Integer qywxDepartmentParentid(String access_token,int id,int loop){
|
|
|
|
|
public Integer qywxDepartmentParentid(String access_token, int id, int loop) {
|
|
|
|
|
String url = " https://qyapi.weixin.qq.com/cgi-bin/department/get";
|
|
|
|
|
String requestUrl = url + "?access_token=" + access_token +"&id="+ id ;
|
|
|
|
|
String requestUrl = url + "?access_token=" + access_token + "&id=" + id;
|
|
|
|
|
String str = httpRequest(requestUrl, "GET", null);
|
|
|
|
|
Integer parentid = JSONObject.parseObject(str).getJSONObject("department").getInteger("parentid");
|
|
|
|
|
if( parentid != 1 && loop< 8 ){
|
|
|
|
|
qywxDepartmentParentid(access_token,parentid,++loop);
|
|
|
|
|
if (parentid != 1 && loop < 8) {
|
|
|
|
|
qywxDepartmentParentid(access_token, parentid, ++loop);
|
|
|
|
|
}
|
|
|
|
|
return parentid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 企业微信消息生成
|
|
|
|
|
* @param access_token
|
|
|
|
|
* @param touser:成员ID列表(消息接收者,多个接收者用‘|’分隔,最多支持1000个)eg:"zhangsan|lisi"
|
|
|
|
|
* @param title:必填,消息标题,长度限制4-12个汉字
|
|
|
|
|
* @param description:消息标题,长度限制4-12个汉字
|
|
|
|
|
* @param wxAppUrl:点击消息卡片后的小程序页面,最长1024个字节,仅限本小程序内的页面。该字段不填则消息点击后不跳转。
|
|
|
|
|
* @param content_item:消息内容键值对,最多允许10个item
|
|
|
|
|
*/
|
|
|
|
|
public void qywxMessage(String access_token,String touser, String title,String description,String wxAppUrl,JSONObject content_item) {
|
|
|
|
|
JSONObject obj = new JSONObject();
|
|
|
|
|
obj.fluentPut("msgtype", "miniprogram_notice").fluentPut("agentid", agentid).fluentPut("touser", touser)
|
|
|
|
|
.fluentPut("miniprogram_notice",
|
|
|
|
|
new JSONObject().fluentPut("appid",appid)
|
|
|
|
|
.fluentPut("description",description)
|
|
|
|
|
.fluentPut("page",wxAppUrl)
|
|
|
|
|
.fluentPut("title",title)
|
|
|
|
|
.fluentPut("content_item",content_item));
|
|
|
|
|
String url = " https://qyapi.weixin.qq.com/cgi-bin/message/send";
|
|
|
|
|
String requestUrl = url + "?access_token=" + access_token;
|
|
|
|
|
String str = httpPost(requestUrl, obj);
|
|
|
|
|
System.out.println(obj);
|
|
|
|
|
System.out.println(str);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String httpRequest(String requestUrl, String requestMethod, String output) {
|
|
|
|
|
try {
|
|
|
|
|
URL url = new URL(requestUrl);
|
|
|
|
@ -125,7 +159,7 @@ public class HttpUtil {
|
|
|
|
|
new InputStreamReader(conn.getInputStream()));
|
|
|
|
|
String line;
|
|
|
|
|
while ((line = in.readLine()) != null) {
|
|
|
|
|
result=result.concat(line);
|
|
|
|
|
result = result.concat(line);
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
System.out.println("发送 POST 请求出现异常!" + e);
|
|
|
|
|