1
This commit is contained in:
@@ -0,0 +1,486 @@
|
||||
package com.ruoyi.web.controller.jarvis;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.ruoyi.common.annotation.Anonymous;
|
||||
import com.ruoyi.common.core.controller.BaseController;
|
||||
import com.ruoyi.common.core.domain.AjaxResult;
|
||||
import com.ruoyi.common.core.redis.RedisCache;
|
||||
import com.ruoyi.jarvis.domain.dto.WPS365TokenInfo;
|
||||
import com.ruoyi.jarvis.service.IWPS365ApiService;
|
||||
import com.ruoyi.jarvis.service.IWPS365OAuthService;
|
||||
import com.ruoyi.jarvis.service.impl.WPS365OAuthServiceImpl;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* WPS365控制器
|
||||
*
|
||||
* @author system
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/jarvis/wps365")
|
||||
public class WPS365Controller extends BaseController {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(WPS365Controller.class);
|
||||
|
||||
@Autowired
|
||||
private IWPS365OAuthService wps365OAuthService;
|
||||
|
||||
@Autowired
|
||||
private IWPS365ApiService wps365ApiService;
|
||||
|
||||
@Autowired
|
||||
private WPS365OAuthServiceImpl wps365OAuthServiceImpl;
|
||||
|
||||
@Autowired
|
||||
private RedisCache redisCache;
|
||||
|
||||
/**
|
||||
* 获取授权URL
|
||||
*/
|
||||
@GetMapping("/authUrl")
|
||||
public AjaxResult getAuthUrl(@RequestParam(required = false) String state) {
|
||||
try {
|
||||
String authUrl = wps365OAuthService.getAuthUrl(state);
|
||||
return AjaxResult.success("获取授权URL成功", authUrl);
|
||||
} catch (Exception e) {
|
||||
log.error("获取授权URL失败", e);
|
||||
return AjaxResult.error("获取授权URL失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* OAuth回调处理
|
||||
*/
|
||||
@Anonymous
|
||||
@GetMapping("/oauth/callback")
|
||||
public AjaxResult oauthCallback(@RequestParam String code,
|
||||
@RequestParam(required = false) String state) {
|
||||
try {
|
||||
log.info("收到OAuth回调 - code: {}, state: {}", code, state);
|
||||
|
||||
// 通过授权码获取访问令牌
|
||||
WPS365TokenInfo tokenInfo = wps365OAuthService.getAccessTokenByCode(code);
|
||||
|
||||
// 保存Token到Redis(使用userId作为key)
|
||||
if (tokenInfo.getUserId() != null) {
|
||||
wps365OAuthService.saveToken(tokenInfo.getUserId(), tokenInfo);
|
||||
}
|
||||
|
||||
return AjaxResult.success("授权成功", tokenInfo);
|
||||
} catch (Exception e) {
|
||||
log.error("OAuth回调处理失败", e);
|
||||
return AjaxResult.error("授权失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 刷新访问令牌
|
||||
*/
|
||||
@PostMapping("/refreshToken")
|
||||
public AjaxResult refreshToken(@RequestBody Map<String, Object> params) {
|
||||
try {
|
||||
String refreshToken = (String) params.get("refreshToken");
|
||||
if (refreshToken == null || refreshToken.trim().isEmpty()) {
|
||||
return AjaxResult.error("refreshToken不能为空");
|
||||
}
|
||||
|
||||
WPS365TokenInfo tokenInfo = wps365OAuthService.refreshAccessToken(refreshToken);
|
||||
|
||||
// 更新Token到Redis
|
||||
if (tokenInfo.getUserId() != null) {
|
||||
wps365OAuthService.saveToken(tokenInfo.getUserId(), tokenInfo);
|
||||
}
|
||||
|
||||
return AjaxResult.success("刷新令牌成功", tokenInfo);
|
||||
} catch (Exception e) {
|
||||
log.error("刷新访问令牌失败", e);
|
||||
return AjaxResult.error("刷新令牌失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前用户的Token状态
|
||||
*/
|
||||
@GetMapping("/tokenStatus")
|
||||
public AjaxResult getTokenStatus(@RequestParam(required = false) String userId) {
|
||||
try {
|
||||
// 如果没有提供userId,可以尝试从当前登录用户获取
|
||||
// 这里暂时需要前端传入userId,后续可以集成到认证系统中
|
||||
if (userId == null || userId.trim().isEmpty()) {
|
||||
return AjaxResult.error("userId不能为空");
|
||||
}
|
||||
|
||||
WPS365TokenInfo tokenInfo = wps365OAuthServiceImpl.getTokenByUserId(userId);
|
||||
if (tokenInfo == null) {
|
||||
return AjaxResult.success("未授权", false);
|
||||
}
|
||||
|
||||
boolean isValid = wps365OAuthService.isTokenValid(tokenInfo);
|
||||
JSONObject result = new JSONObject();
|
||||
result.put("hasToken", true);
|
||||
result.put("isValid", isValid);
|
||||
result.put("userId", tokenInfo.getUserId());
|
||||
result.put("expired", tokenInfo.isExpired());
|
||||
|
||||
return AjaxResult.success("获取Token状态成功", result);
|
||||
} catch (Exception e) {
|
||||
log.error("获取Token状态失败", e);
|
||||
return AjaxResult.error("获取Token状态失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 手动设置Token(用于测试或手动授权)
|
||||
*/
|
||||
@PostMapping("/setToken")
|
||||
public AjaxResult setToken(@RequestBody Map<String, Object> params) {
|
||||
try {
|
||||
String accessToken = (String) params.get("accessToken");
|
||||
String refreshToken = (String) params.get("refreshToken");
|
||||
String userId = (String) params.get("userId");
|
||||
Integer expiresIn = params.get("expiresIn") != null ?
|
||||
Integer.valueOf(params.get("expiresIn").toString()) : 7200;
|
||||
|
||||
if (accessToken == null || accessToken.trim().isEmpty()) {
|
||||
return AjaxResult.error("accessToken不能为空");
|
||||
}
|
||||
if (userId == null || userId.trim().isEmpty()) {
|
||||
return AjaxResult.error("userId不能为空");
|
||||
}
|
||||
|
||||
WPS365TokenInfo tokenInfo = new WPS365TokenInfo();
|
||||
tokenInfo.setAccessToken(accessToken);
|
||||
tokenInfo.setRefreshToken(refreshToken);
|
||||
tokenInfo.setExpiresIn(expiresIn);
|
||||
tokenInfo.setUserId(userId);
|
||||
|
||||
wps365OAuthService.saveToken(userId, tokenInfo);
|
||||
|
||||
return AjaxResult.success("设置Token成功");
|
||||
} catch (Exception e) {
|
||||
log.error("设置Token失败", e);
|
||||
return AjaxResult.error("设置Token失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户信息
|
||||
*/
|
||||
@GetMapping("/userInfo")
|
||||
public AjaxResult getUserInfo(@RequestParam String userId) {
|
||||
try {
|
||||
WPS365TokenInfo tokenInfo = wps365OAuthServiceImpl.getTokenByUserId(userId);
|
||||
if (tokenInfo == null) {
|
||||
return AjaxResult.error("用户未授权,请先完成授权");
|
||||
}
|
||||
|
||||
// 检查Token是否有效,如果过期则尝试刷新
|
||||
if (tokenInfo.isExpired() && tokenInfo.getRefreshToken() != null) {
|
||||
try {
|
||||
tokenInfo = wps365OAuthService.refreshAccessToken(tokenInfo.getRefreshToken());
|
||||
wps365OAuthService.saveToken(userId, tokenInfo);
|
||||
} catch (Exception e) {
|
||||
log.error("刷新Token失败", e);
|
||||
return AjaxResult.error("Token已过期且刷新失败,请重新授权");
|
||||
}
|
||||
}
|
||||
|
||||
JSONObject userInfo = wps365ApiService.getUserInfo(tokenInfo.getAccessToken());
|
||||
return AjaxResult.success("获取用户信息成功", userInfo);
|
||||
} catch (Exception e) {
|
||||
log.error("获取用户信息失败", e);
|
||||
return AjaxResult.error("获取用户信息失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件列表
|
||||
*/
|
||||
@GetMapping("/files")
|
||||
public AjaxResult getFileList(@RequestParam String userId,
|
||||
@RequestParam(required = false, defaultValue = "1") Integer page,
|
||||
@RequestParam(required = false, defaultValue = "20") Integer pageSize) {
|
||||
try {
|
||||
WPS365TokenInfo tokenInfo = wps365OAuthServiceImpl.getTokenByUserId(userId);
|
||||
if (tokenInfo == null) {
|
||||
return AjaxResult.error("用户未授权,请先完成授权");
|
||||
}
|
||||
|
||||
// 检查Token是否有效,如果过期则尝试刷新
|
||||
if (tokenInfo.isExpired() && tokenInfo.getRefreshToken() != null) {
|
||||
try {
|
||||
tokenInfo = wps365OAuthService.refreshAccessToken(tokenInfo.getRefreshToken());
|
||||
wps365OAuthService.saveToken(userId, tokenInfo);
|
||||
} catch (Exception e) {
|
||||
log.error("刷新Token失败", e);
|
||||
return AjaxResult.error("Token已过期且刷新失败,请重新授权");
|
||||
}
|
||||
}
|
||||
|
||||
Map<String, Object> params = new java.util.HashMap<>();
|
||||
params.put("page", page);
|
||||
params.put("page_size", pageSize);
|
||||
|
||||
JSONObject fileList = wps365ApiService.getFileList(tokenInfo.getAccessToken(), params);
|
||||
return AjaxResult.success("获取文件列表成功", fileList);
|
||||
} catch (Exception e) {
|
||||
log.error("获取文件列表失败", e);
|
||||
return AjaxResult.error("获取文件列表失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件信息
|
||||
*/
|
||||
@GetMapping("/fileInfo")
|
||||
public AjaxResult getFileInfo(@RequestParam String userId,
|
||||
@RequestParam String fileToken) {
|
||||
try {
|
||||
WPS365TokenInfo tokenInfo = wps365OAuthServiceImpl.getTokenByUserId(userId);
|
||||
if (tokenInfo == null) {
|
||||
return AjaxResult.error("用户未授权,请先完成授权");
|
||||
}
|
||||
|
||||
// 检查Token是否有效
|
||||
if (tokenInfo.isExpired() && tokenInfo.getRefreshToken() != null) {
|
||||
try {
|
||||
tokenInfo = wps365OAuthService.refreshAccessToken(tokenInfo.getRefreshToken());
|
||||
wps365OAuthService.saveToken(userId, tokenInfo);
|
||||
} catch (Exception e) {
|
||||
log.error("刷新Token失败", e);
|
||||
return AjaxResult.error("Token已过期且刷新失败,请重新授权");
|
||||
}
|
||||
}
|
||||
|
||||
JSONObject fileInfo = wps365ApiService.getFileInfo(tokenInfo.getAccessToken(), fileToken);
|
||||
return AjaxResult.success("获取文件信息成功", fileInfo);
|
||||
} catch (Exception e) {
|
||||
log.error("获取文件信息失败 - fileToken: {}", fileToken, e);
|
||||
return AjaxResult.error("获取文件信息失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取工作表列表
|
||||
*/
|
||||
@GetMapping("/sheets")
|
||||
public AjaxResult getSheetList(@RequestParam String userId,
|
||||
@RequestParam String fileToken) {
|
||||
try {
|
||||
WPS365TokenInfo tokenInfo = wps365OAuthServiceImpl.getTokenByUserId(userId);
|
||||
if (tokenInfo == null) {
|
||||
return AjaxResult.error("用户未授权,请先完成授权");
|
||||
}
|
||||
|
||||
// 检查Token是否有效
|
||||
if (tokenInfo.isExpired() && tokenInfo.getRefreshToken() != null) {
|
||||
try {
|
||||
tokenInfo = wps365OAuthService.refreshAccessToken(tokenInfo.getRefreshToken());
|
||||
wps365OAuthService.saveToken(userId, tokenInfo);
|
||||
} catch (Exception e) {
|
||||
log.error("刷新Token失败", e);
|
||||
return AjaxResult.error("Token已过期且刷新失败,请重新授权");
|
||||
}
|
||||
}
|
||||
|
||||
JSONObject sheetList = wps365ApiService.getSheetList(tokenInfo.getAccessToken(), fileToken);
|
||||
return AjaxResult.success("获取工作表列表成功", sheetList);
|
||||
} catch (Exception e) {
|
||||
log.error("获取工作表列表失败 - fileToken: {}", fileToken, e);
|
||||
return AjaxResult.error("获取工作表列表失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取单元格数据
|
||||
*/
|
||||
@GetMapping("/readCells")
|
||||
public AjaxResult readCells(@RequestParam String userId,
|
||||
@RequestParam String fileToken,
|
||||
@RequestParam(defaultValue = "0") int sheetIdx,
|
||||
@RequestParam(required = false) String range) {
|
||||
try {
|
||||
WPS365TokenInfo tokenInfo = wps365OAuthServiceImpl.getTokenByUserId(userId);
|
||||
if (tokenInfo == null) {
|
||||
return AjaxResult.error("用户未授权,请先完成授权");
|
||||
}
|
||||
|
||||
// 检查Token是否有效
|
||||
if (tokenInfo.isExpired() && tokenInfo.getRefreshToken() != null) {
|
||||
try {
|
||||
tokenInfo = wps365OAuthService.refreshAccessToken(tokenInfo.getRefreshToken());
|
||||
wps365OAuthService.saveToken(userId, tokenInfo);
|
||||
} catch (Exception e) {
|
||||
log.error("刷新Token失败", e);
|
||||
return AjaxResult.error("Token已过期且刷新失败,请重新授权");
|
||||
}
|
||||
}
|
||||
|
||||
JSONObject result = wps365ApiService.readCells(tokenInfo.getAccessToken(), fileToken, sheetIdx, range);
|
||||
return AjaxResult.success("读取单元格数据成功", result);
|
||||
} catch (Exception e) {
|
||||
log.error("读取单元格数据失败 - fileToken: {}, sheetIdx: {}, range: {}", fileToken, sheetIdx, range, e);
|
||||
return AjaxResult.error("读取单元格数据失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新单元格数据
|
||||
*/
|
||||
@PostMapping("/updateCells")
|
||||
public AjaxResult updateCells(@RequestBody Map<String, Object> params) {
|
||||
try {
|
||||
String userId = (String) params.get("userId");
|
||||
String fileToken = (String) params.get("fileToken");
|
||||
Integer sheetIdx = params.get("sheetIdx") != null ?
|
||||
Integer.valueOf(params.get("sheetIdx").toString()) : 0;
|
||||
String range = (String) params.get("range");
|
||||
@SuppressWarnings("unchecked")
|
||||
List<List<Object>> values = (List<List<Object>>) params.get("values");
|
||||
|
||||
if (userId == null || userId.trim().isEmpty()) {
|
||||
return AjaxResult.error("userId不能为空");
|
||||
}
|
||||
if (fileToken == null || fileToken.trim().isEmpty()) {
|
||||
return AjaxResult.error("fileToken不能为空");
|
||||
}
|
||||
if (range == null || range.trim().isEmpty()) {
|
||||
return AjaxResult.error("range不能为空");
|
||||
}
|
||||
if (values == null || values.isEmpty()) {
|
||||
return AjaxResult.error("values不能为空");
|
||||
}
|
||||
|
||||
WPS365TokenInfo tokenInfo = wps365OAuthServiceImpl.getTokenByUserId(userId);
|
||||
if (tokenInfo == null) {
|
||||
return AjaxResult.error("用户未授权,请先完成授权");
|
||||
}
|
||||
|
||||
// 检查Token是否有效
|
||||
if (tokenInfo.isExpired() && tokenInfo.getRefreshToken() != null) {
|
||||
try {
|
||||
tokenInfo = wps365OAuthService.refreshAccessToken(tokenInfo.getRefreshToken());
|
||||
wps365OAuthService.saveToken(userId, tokenInfo);
|
||||
} catch (Exception e) {
|
||||
log.error("刷新Token失败", e);
|
||||
return AjaxResult.error("Token已过期且刷新失败,请重新授权");
|
||||
}
|
||||
}
|
||||
|
||||
JSONObject result = wps365ApiService.updateCells(
|
||||
tokenInfo.getAccessToken(),
|
||||
fileToken,
|
||||
sheetIdx,
|
||||
range,
|
||||
values
|
||||
);
|
||||
return AjaxResult.success("更新单元格数据成功", result);
|
||||
} catch (Exception e) {
|
||||
log.error("更新单元格数据失败", e);
|
||||
return AjaxResult.error("更新单元格数据失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建数据表
|
||||
*/
|
||||
@PostMapping("/createSheet")
|
||||
public AjaxResult createSheet(@RequestBody Map<String, Object> params) {
|
||||
try {
|
||||
String userId = (String) params.get("userId");
|
||||
String fileToken = (String) params.get("fileToken");
|
||||
String sheetName = (String) params.get("sheetName");
|
||||
|
||||
if (userId == null || userId.trim().isEmpty()) {
|
||||
return AjaxResult.error("userId不能为空");
|
||||
}
|
||||
if (fileToken == null || fileToken.trim().isEmpty()) {
|
||||
return AjaxResult.error("fileToken不能为空");
|
||||
}
|
||||
if (sheetName == null || sheetName.trim().isEmpty()) {
|
||||
return AjaxResult.error("sheetName不能为空");
|
||||
}
|
||||
|
||||
WPS365TokenInfo tokenInfo = wps365OAuthServiceImpl.getTokenByUserId(userId);
|
||||
if (tokenInfo == null) {
|
||||
return AjaxResult.error("用户未授权,请先完成授权");
|
||||
}
|
||||
|
||||
// 检查Token是否有效
|
||||
if (tokenInfo.isExpired() && tokenInfo.getRefreshToken() != null) {
|
||||
try {
|
||||
tokenInfo = wps365OAuthService.refreshAccessToken(tokenInfo.getRefreshToken());
|
||||
wps365OAuthService.saveToken(userId, tokenInfo);
|
||||
} catch (Exception e) {
|
||||
log.error("刷新Token失败", e);
|
||||
return AjaxResult.error("Token已过期且刷新失败,请重新授权");
|
||||
}
|
||||
}
|
||||
|
||||
JSONObject result = wps365ApiService.createSheet(tokenInfo.getAccessToken(), fileToken, sheetName);
|
||||
return AjaxResult.success("创建数据表成功", result);
|
||||
} catch (Exception e) {
|
||||
log.error("创建数据表失败", e);
|
||||
return AjaxResult.error("创建数据表失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量更新单元格数据
|
||||
*/
|
||||
@PostMapping("/batchUpdateCells")
|
||||
public AjaxResult batchUpdateCells(@RequestBody Map<String, Object> params) {
|
||||
try {
|
||||
String userId = (String) params.get("userId");
|
||||
String fileToken = (String) params.get("fileToken");
|
||||
Integer sheetIdx = params.get("sheetIdx") != null ?
|
||||
Integer.valueOf(params.get("sheetIdx").toString()) : 0;
|
||||
@SuppressWarnings("unchecked")
|
||||
List<Map<String, Object>> updates = (List<Map<String, Object>>) params.get("updates");
|
||||
|
||||
if (userId == null || userId.trim().isEmpty()) {
|
||||
return AjaxResult.error("userId不能为空");
|
||||
}
|
||||
if (fileToken == null || fileToken.trim().isEmpty()) {
|
||||
return AjaxResult.error("fileToken不能为空");
|
||||
}
|
||||
if (updates == null || updates.isEmpty()) {
|
||||
return AjaxResult.error("updates不能为空");
|
||||
}
|
||||
|
||||
WPS365TokenInfo tokenInfo = wps365OAuthServiceImpl.getTokenByUserId(userId);
|
||||
if (tokenInfo == null) {
|
||||
return AjaxResult.error("用户未授权,请先完成授权");
|
||||
}
|
||||
|
||||
// 检查Token是否有效
|
||||
if (tokenInfo.isExpired() && tokenInfo.getRefreshToken() != null) {
|
||||
try {
|
||||
tokenInfo = wps365OAuthService.refreshAccessToken(tokenInfo.getRefreshToken());
|
||||
wps365OAuthService.saveToken(userId, tokenInfo);
|
||||
} catch (Exception e) {
|
||||
log.error("刷新Token失败", e);
|
||||
return AjaxResult.error("Token已过期且刷新失败,请重新授权");
|
||||
}
|
||||
}
|
||||
|
||||
JSONObject result = wps365ApiService.batchUpdateCells(
|
||||
tokenInfo.getAccessToken(),
|
||||
fileToken,
|
||||
sheetIdx,
|
||||
updates
|
||||
);
|
||||
return AjaxResult.success("批量更新单元格数据成功", result);
|
||||
} catch (Exception e) {
|
||||
log.error("批量更新单元格数据失败", e);
|
||||
return AjaxResult.error("批量更新单元格数据失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -222,3 +222,21 @@ tencent:
|
||||
# 刷新Token地址(用于通过refresh_token刷新access_token)
|
||||
refresh-token-url: https://docs.qq.com/oauth/v2/token
|
||||
|
||||
# WPS365开放平台配置
|
||||
# 文档地址:https://open.wps.cn/
|
||||
wps365:
|
||||
# 应用ID(AppId)- 需要在WPS365开放平台申请
|
||||
app-id: YOUR_APP_ID
|
||||
# 应用密钥(AppKey)- 需要在WPS365开放平台申请,注意保密
|
||||
app-key: YOUR_APP_KEY
|
||||
# 授权回调地址(需要在WPS365开放平台配置授权域名)
|
||||
redirect-uri: https://jarvis.van333.cn/jarvis/wps365/oauth/callback
|
||||
# API基础地址
|
||||
api-base-url: https://open.wps.cn/api/v1
|
||||
# OAuth授权地址
|
||||
oauth-url: https://open.wps.cn/oauth2/v1/authorize
|
||||
# 获取Token地址
|
||||
token-url: https://open.wps.cn/oauth2/v1/token
|
||||
# 刷新Token地址
|
||||
refresh-token-url: https://open.wps.cn/oauth2/v1/token
|
||||
|
||||
|
||||
Reference in New Issue
Block a user