package com.yuanchu.mom.utils;
|
|
import cn.hutool.http.HttpRequest;
|
import com.alibaba.fastjson.JSONArray;
|
import com.alibaba.fastjson.JSONObject;
|
import com.yuanchu.mom.exception.ErrorException;
|
import com.yuanchu.mom.vo.Result;
|
import lombok.Data;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.stereotype.Component;
|
import java.util.HashMap;
|
import java.util.Map;
|
|
@Data
|
@Slf4j
|
@Component
|
//企业微信对接接口
|
public class QYWXApi {
|
|
|
public static final String WWA_423654_B_975441_AC = "wwa423654b975441ac";
|
final String ip = "https://qyapi.weixin.qq.com/";
|
final String corpid = WWA_423654_B_975441_AC;
|
final String corpsecret = "rQ1_ddKBVW5qVSMNK_p0EnR3Z2OHrAMxGahBKTKWYxE";
|
|
|
/**
|
* 企业微信获取token
|
*
|
* @return
|
*/
|
public String getToken() {
|
try {
|
Map<String, Object> map = new HashMap<>();
|
map.put("corpid", corpid);
|
map.put("corpsecret", corpsecret);
|
String result = HttpRequest.get(ip + "cgi-bin/gettoken")
|
.contentType("application/x-www-form-urlencoded")
|
.form(map).execute().body();
|
JSONObject jsonObject = JSONObject.parseObject(result);
|
return jsonObject.getString("access_token");
|
} catch (Exception e) {
|
throw new RuntimeException("token获取异常");
|
}
|
}
|
|
/**
|
* 发送应用卡片消息
|
*
|
* @param map
|
* @return
|
*/
|
public void send(Map map) {
|
try {
|
String result = HttpRequest.post(ip + "cgi-bin/message/send?access_token=" + getToken())
|
.contentType("application/json")
|
.body(JSONArray.toJSONString(map)).execute().body();
|
JSONObject jsonObject = JSONObject.parseObject(result);
|
if (jsonObject.getInteger("errcode") == 0) {
|
} else {
|
throw new ErrorException("企业微信推送消息失败" + jsonObject.getString("errmsg"));
|
}
|
} catch (Exception e) {
|
throw new ErrorException("企业微信推送消息失败,获取异常:" + e.getMessage());
|
}
|
}
|
}
|