wlzboy
5 天以前 fe33646ee6e2d1e57f2b51812e94983a0e9efb04
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package com.ruoyi.web.controller.system;
 
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.service.IQyWechatAccessTokenService;
import com.ruoyi.system.service.IQyWechatService;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
/**
 * 企业微信测试控制器
 * 用于测试企业微信AccessToken获取和消息发送功能
 * 
 * @author ruoyi
 * @date 2025-12-11
 */
@Anonymous()
@RestController
@RequestMapping("/system/qywechat/test")
public class QyWechatTestController extends BaseController {
 
    @Autowired
    private IQyWechatAccessTokenService qyWechatAccessTokenService;
 
    @Autowired
    private IQyWechatService qyWechatService;
 
    @Autowired
    private ISysConfigService configService;
 
    /**
     * 测试获取企业微信AccessToken
     */
    @Anonymous()
    @GetMapping("/token")
    public AjaxResult testGetToken() {
        try {
            String corpId = configService.selectConfigByKey("qy_wechat.corp_id");
            String corpSecret = configService.selectConfigByKey("qy_wechat.corp_secret");
 
            if (corpId == null || corpSecret == null) {
                return AjaxResult.error("企业微信配置不完整,请检查corp_id和corp_secret配置");
            }
 
            String accessToken = qyWechatAccessTokenService.getAppAccessToken(corpId, corpSecret);
            
            if (accessToken != null) {
                return AjaxResult.success("获取AccessToken成功", accessToken);
            } else {
                return AjaxResult.error("获取AccessToken失败");
            }
        } catch (Exception e) {
            return AjaxResult.error("获取AccessToken异常:" + e.getMessage());
        }
    }
 
    /**
     * 测试刷新企业微信AccessToken
     */
    @Anonymous()
    @PostMapping("/refreshToken")
    public AjaxResult testRefreshToken() {
        try {
            String corpId = configService.selectConfigByKey("qy_wechat.corp_id");
            String corpSecret = configService.selectConfigByKey("qy_wechat.corp_secret");
 
            if (corpId == null || corpSecret == null) {
                return AjaxResult.error("企业微信配置不完整,请检查corp_id和corp_secret配置");
            }
 
            String accessToken = qyWechatAccessTokenService.refreshAppAccessToken(corpId, corpSecret);
            
            if (accessToken != null) {
                return AjaxResult.success("刷新AccessToken成功", accessToken);
            } else {
                return AjaxResult.error("刷新AccessToken失败");
            }
        } catch (Exception e) {
            return AjaxResult.error("刷新AccessToken异常:" + e.getMessage());
        }
    }
 
    /**
     * 测试发送企业微信消息
     */
    @Anonymous()
    @PostMapping("/sendMessage")
    public AjaxResult testSendMessage(@RequestParam Long userId, 
                                     @RequestParam String title, 
                                     @RequestParam String content,@RequestParam String notifyUrl) {
        try {
            boolean result = qyWechatService.sendNotifyMessage(userId, title, content,notifyUrl);
            
            if (result) {
                return AjaxResult.success("发送企业微信消息成功");
            } else {
                return AjaxResult.error("发送企业微信消息失败");
            }
        } catch (Exception e) {
            return AjaxResult.error("发送企业微信消息异常:" + e.getMessage());
        }
    }
 
    /**
     * 测试发送企业微信文本消息
     */
    @Anonymous()
    @PostMapping("/sendTextMessage")
    public AjaxResult testSendTextMessage(@RequestParam String qyUserId, 
                                         @RequestParam String title, 
                                         @RequestParam String content, 
                                         @RequestParam String notifyUrl) {
        try {
            boolean result = qyWechatService.sendTextMessage(qyUserId, title, content, notifyUrl);
            
            if (result) {
                return AjaxResult.success("发送企业微信文本消息成功");
            } else {
                return AjaxResult.error("发送企业微信文本消息失败");
            }
        } catch (Exception e) {
            return AjaxResult.error("发送企业微信文本消息异常:" + e.getMessage());
        }
    }
 
    /**
     * 检查企业微信服务是否启用
     */
    @Anonymous()
    @GetMapping("/enabled")
    public AjaxResult checkEnabled() {
        try {
            boolean enabled = qyWechatService.isEnabled();
            return AjaxResult.success("企业微信服务状态:" + (enabled ? "启用" : "禁用"), enabled);
        } catch (Exception e) {
            return AjaxResult.error("检查企业微信服务状态异常:" + e.getMessage());
        }
    }
}