|
|
|
@ -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,6 +22,10 @@ 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() {
|
|
|
|
|
String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken";
|
|
|
|
@ -28,18 +33,21 @@ public class HttpUtil {
|
|
|
|
|
String str = httpRequest(requestUrl, "GET", null);
|
|
|
|
|
return JSONObject.parseObject(str).getString("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) {
|
|
|
|
|
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 str = httpRequest(requestUrl, "GET", null);
|
|
|
|
|
return JSONObject.parseObject(str);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
@ -57,6 +65,32 @@ public class HttpUtil {
|
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|