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());
|
}
|
}
|
}
|