zss
2024-10-18 1f15333b0a97a327865f7aab8f1e3f9ba8fc16f8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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());
        }
    }
}