集成 One-Api
This commit is contained in:
@@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
<groupId>com.rnb</groupId>
|
<groupId>com.rnb</groupId>
|
||||||
<artifactId>rnb</artifactId>
|
<artifactId>rnb</artifactId>
|
||||||
<version>1.12</version>
|
<version>1.13</version>
|
||||||
<description>基于 Java 17 + SpringBoot 3 + Spring Security 构建的后台管理系统。</description>
|
<description>基于 Java 17 + SpringBoot 3 + Spring Security 构建的后台管理系统。</description>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
|
|||||||
@@ -30,4 +30,8 @@ public interface SystemConstants {
|
|||||||
String SYSTEM_CONFIG_IP_QPS_LIMIT_KEY = "IP_QPS_THRESHOLD_LIMIT";
|
String SYSTEM_CONFIG_IP_QPS_LIMIT_KEY = "IP_QPS_THRESHOLD_LIMIT";
|
||||||
String SYSTEM_CONFIG_CLIENT_RESP_KEY = "CLIENT_RESP_KEY";
|
String SYSTEM_CONFIG_CLIENT_RESP_KEY = "CLIENT_RESP_KEY";
|
||||||
int DEFAULT_CLIENT_KEY = 5;
|
int DEFAULT_CLIENT_KEY = 5;
|
||||||
|
|
||||||
|
// one-api 配置键(与 sys_config 表 configKey 字段对应)
|
||||||
|
String ONE_API_URL = "ONE_API_URL";
|
||||||
|
String ONE_API_TOKEN = "ONE_API_TOKEN";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,4 +28,5 @@ public class Account {
|
|||||||
|
|
||||||
@TableField(updateStrategy = FieldStrategy.ALWAYS)
|
@TableField(updateStrategy = FieldStrategy.ALWAYS)
|
||||||
private Long deviceId; //设备ID
|
private Long deviceId; //设备ID
|
||||||
|
private String apiKey; //one-api token
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,13 +10,21 @@ import com.rnb.rnb.model.entity.Account;
|
|||||||
import com.rnb.rnb.model.query.AccountPageQuery;
|
import com.rnb.rnb.model.query.AccountPageQuery;
|
||||||
import com.rnb.rnb.model.vo.AccountBaseVo;
|
import com.rnb.rnb.model.vo.AccountBaseVo;
|
||||||
import com.rnb.rnb.util.ActCodeGen;
|
import com.rnb.rnb.util.ActCodeGen;
|
||||||
|
import com.rnb.rnb.util.OneApiClient;
|
||||||
import jakarta.validation.Valid;
|
import jakarta.validation.Valid;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
@Service
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
public class AccountService extends ServiceImpl<AccountMapper, Account> {
|
public class AccountService extends ServiceImpl<AccountMapper, Account> {
|
||||||
|
|
||||||
|
private final OneApiClient oneApiClient;
|
||||||
|
|
||||||
public IPage<AccountBaseVo> pageBase(AccountPageQuery queryParams) {
|
public IPage<AccountBaseVo> pageBase(AccountPageQuery queryParams) {
|
||||||
Page<AccountBaseVo> page = new Page<>(queryParams.getPageNum(),
|
Page<AccountBaseVo> page = new Page<>(queryParams.getPageNum(),
|
||||||
queryParams.getPageSize());
|
queryParams.getPageSize());
|
||||||
@@ -53,8 +61,10 @@ public class AccountService extends ServiceImpl<AccountMapper, Account> {
|
|||||||
account.setBacAccount(ActCodeGen.GenerateCode());
|
account.setBacAccount(ActCodeGen.GenerateCode());
|
||||||
account.setBacPlatform("FS");
|
account.setBacPlatform("FS");
|
||||||
|
|
||||||
if(save(account))
|
if(save(account)) {
|
||||||
|
syncOneApiToken(account);
|
||||||
return account.getId();
|
return account.getId();
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -89,12 +99,31 @@ public class AccountService extends ServiceImpl<AccountMapper, Account> {
|
|||||||
account.setLinkAccount(acc.getLinkAccount());
|
account.setLinkAccount(acc.getLinkAccount());
|
||||||
}
|
}
|
||||||
|
|
||||||
if(saveOrUpdate(account))
|
if(saveOrUpdate(account)) {
|
||||||
|
syncOneApiToken(account);
|
||||||
return account.getId();
|
return account.getId();
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在 one-api 创建令牌并存入 account.apiKey。
|
||||||
|
* 失败仅记日志,不阻塞账户创建。
|
||||||
|
*/
|
||||||
|
private void syncOneApiToken(Account account) {
|
||||||
|
try {
|
||||||
|
String name = "rbm-" + String.format("%05d", account.getId());
|
||||||
|
String key = oneApiClient.createToken(name);
|
||||||
|
if (key != null && !key.isEmpty()) {
|
||||||
|
account.setApiKey(key);
|
||||||
|
updateById(account);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.warn("syncOneApiToken failed for accountId={}", account.getId(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public boolean setStatus(Integer id, Integer status) {
|
public boolean setStatus(Integer id, Integer status) {
|
||||||
String username = SecurityUtils.getUsername();
|
String username = SecurityUtils.getUsername();
|
||||||
int tmNow = (int) (System.currentTimeMillis()/1000);
|
int tmNow = (int) (System.currentTimeMillis()/1000);
|
||||||
|
|||||||
@@ -7,7 +7,9 @@ import com.rnb.rnb.model.entity.Account;
|
|||||||
import com.rnb.rnb.model.entity.Amount;
|
import com.rnb.rnb.model.entity.Amount;
|
||||||
import com.rnb.rnb.model.entity.AmountSum;
|
import com.rnb.rnb.model.entity.AmountSum;
|
||||||
import com.rnb.rnb.model.entity.Software;
|
import com.rnb.rnb.model.entity.Software;
|
||||||
|
import com.rnb.rnb.util.OneApiClient;
|
||||||
import com.rnb.system.service.ConfigService;
|
import com.rnb.system.service.ConfigService;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
@@ -17,6 +19,7 @@ import java.time.ZoneOffset;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
@Service
|
@Service
|
||||||
public class ApiService {
|
public class ApiService {
|
||||||
@Autowired
|
@Autowired
|
||||||
@@ -29,6 +32,8 @@ public class ApiService {
|
|||||||
private AmountSumService amoSumService;
|
private AmountSumService amoSumService;
|
||||||
@Autowired
|
@Autowired
|
||||||
private SoftwareService softwareService;
|
private SoftwareService softwareService;
|
||||||
|
@Autowired
|
||||||
|
private OneApiClient oneApiClient;
|
||||||
|
|
||||||
public String getUpgradePath(int sid){
|
public String getUpgradePath(int sid){
|
||||||
LambdaQueryWrapper<Software> swWrapper = new LambdaQueryWrapper<>();
|
LambdaQueryWrapper<Software> swWrapper = new LambdaQueryWrapper<>();
|
||||||
@@ -109,10 +114,35 @@ public class ApiService {
|
|||||||
accountService.updateById(account);
|
accountService.updateById(account);
|
||||||
|
|
||||||
int exp = 0;
|
int exp = 0;
|
||||||
if(account.getStatus()==0)
|
String apiKey = "";
|
||||||
|
if(account.getStatus()==0) {
|
||||||
exp = account.getExpireTime();
|
exp = account.getExpireTime();
|
||||||
|
apiKey = account.getApiKey();
|
||||||
|
// 如果没有 apiKey,立即在 one-api 创建令牌
|
||||||
|
if (apiKey == null || apiKey.isEmpty()) {
|
||||||
|
syncOneApiToken(account);
|
||||||
|
apiKey = account.getApiKey();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return "uid:" + account.getId() + "|exp:" + exp;
|
return "uid:" + account.getId() + "|exp:" + exp + "|key:" + apiKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在 one-api 创建令牌并存入 account.apiKey。
|
||||||
|
* 失败仅记日志,不阻塞登录。
|
||||||
|
*/
|
||||||
|
private void syncOneApiToken(Account account) {
|
||||||
|
try {
|
||||||
|
String name = "rbm-" + String.format("%05d", account.getId());
|
||||||
|
String key = oneApiClient.createToken(name);
|
||||||
|
if (key != null && !key.isEmpty()) {
|
||||||
|
account.setApiKey(key);
|
||||||
|
accountService.updateById(account);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.warn("syncOneApiToken failed for accountId={}", account.getId(), e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public String rbReport(Integer uid, Integer amo) {
|
public String rbReport(Integer uid, Integer amo) {
|
||||||
|
|||||||
@@ -0,0 +1,86 @@
|
|||||||
|
package com.rnb.rnb.util;
|
||||||
|
|
||||||
|
import cn.hutool.http.HttpRequest;
|
||||||
|
import cn.hutool.json.JSONObject;
|
||||||
|
import cn.hutool.json.JSONUtil;
|
||||||
|
import com.rnb.common.constant.SystemConstants;
|
||||||
|
import com.rnb.system.service.ConfigService;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* one-api 客户端,封装对 one-api 的接口调用。
|
||||||
|
*/
|
||||||
|
@Slf4j
|
||||||
|
@Component
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class OneApiClient {
|
||||||
|
|
||||||
|
private final ConfigService configService;
|
||||||
|
|
||||||
|
public String getBaseUrl() {
|
||||||
|
return (String) configService.getSystemConfig(SystemConstants.ONE_API_URL);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAuthToken() {
|
||||||
|
return (String) configService.getSystemConfig(SystemConstants.ONE_API_TOKEN);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 在 one-api 创建令牌,返回生成的 key。
|
||||||
|
*
|
||||||
|
* @param name 令牌名称,例如 "rbm-00007"
|
||||||
|
* @return one-api token key,失败时返回 null
|
||||||
|
*/
|
||||||
|
public String createToken(String name) {
|
||||||
|
String baseUrl = getBaseUrl();
|
||||||
|
String authToken = getAuthToken();
|
||||||
|
if (baseUrl == null || baseUrl.isEmpty() || authToken == null || authToken.isEmpty()) {
|
||||||
|
log.warn("OneApiClient.createToken: ONE_API_URL or ONE_API_TOKEN not configured, skip token creation for name={}", name);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
JSONObject body = new JSONObject();
|
||||||
|
body.set("name", name);
|
||||||
|
body.set("remain_quota", 1000000);
|
||||||
|
body.set("expired_time", -1); // 永不过期
|
||||||
|
|
||||||
|
String respStr = HttpRequest.post(baseUrl + "/api/token")
|
||||||
|
.header("Authorization", "Bearer " + authToken)
|
||||||
|
.header("Content-Type", "application/json")
|
||||||
|
.body(body.toString())
|
||||||
|
.timeout(10000)
|
||||||
|
.execute().body();
|
||||||
|
|
||||||
|
if (respStr == null || respStr.isEmpty()) {
|
||||||
|
log.warn("OneApiClient.createToken: empty response for name={}", name);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
JSONObject resp = JSONUtil.parseObj(respStr);
|
||||||
|
if (!resp.getBool("success", false)) {
|
||||||
|
log.warn("OneApiClient.createToken: success=false, name={}, message={}", name, resp.getStr("message"));
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
String key = resp.getJSONObject("data").getStr("key");
|
||||||
|
if (key == null || key.isEmpty()) {
|
||||||
|
log.warn("OneApiClient.createToken: missing data.key, name={}, resp={}", name, respStr);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
log.info("OneApiClient.createToken: token created, name={}, key={}", name, maskKey(key));
|
||||||
|
return key;
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.warn("OneApiClient.createToken: exception, name={}", name, e);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String maskKey(String key) {
|
||||||
|
if (key == null || key.length() <= 8) {
|
||||||
|
return "***";
|
||||||
|
}
|
||||||
|
return key.substring(0, 6) + "..." + key.substring(key.length() - 2);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,7 +5,7 @@ spring:
|
|||||||
datasource:
|
datasource:
|
||||||
type: com.alibaba.druid.pool.DruidDataSource
|
type: com.alibaba.druid.pool.DruidDataSource
|
||||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||||
url: jdbc:mysql://localhost:3308/rnb?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&autoReconnect=true&allowMultiQueries=true
|
url: jdbc:mysql://localhost:3306/rnb?zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&autoReconnect=true&allowMultiQueries=true
|
||||||
username: root
|
username: root
|
||||||
password: 168soft.xyz
|
password: 168soft.xyz
|
||||||
data:
|
data:
|
||||||
@@ -13,7 +13,7 @@ spring:
|
|||||||
database: 2
|
database: 2
|
||||||
host: localhost
|
host: localhost
|
||||||
port: 6379
|
port: 6379
|
||||||
#password: ""
|
password: CZwy168
|
||||||
timeout: 10s
|
timeout: 10s
|
||||||
lettuce:
|
lettuce:
|
||||||
pool:
|
pool:
|
||||||
@@ -89,11 +89,11 @@ security:
|
|||||||
- /ws/** # WebSocket接口
|
- /ws/** # WebSocket接口
|
||||||
# 非安全端点路径,完全绕过 Spring Security 的过滤器
|
# 非安全端点路径,完全绕过 Spring Security 的过滤器
|
||||||
unsecured-urls:
|
unsecured-urls:
|
||||||
- ${springdoc.swagger-ui.path}
|
# - ${springdoc.swagger-ui.path}
|
||||||
- /doc.html
|
# - /doc.html
|
||||||
- /swagger-ui/**
|
# - /swagger-ui/**
|
||||||
- /v3/api-docs/**
|
# - /v3/api-docs/**
|
||||||
- /webjars/**
|
# - /webjars/**
|
||||||
- /favicon.ico
|
- /favicon.ico
|
||||||
|
|
||||||
# 文件存储配置
|
# 文件存储配置
|
||||||
@@ -125,7 +125,7 @@ oss:
|
|||||||
# 本地存储
|
# 本地存储
|
||||||
local:
|
local:
|
||||||
# 文件存储路径 请注意下,mac用户请使用 /Users/your-username/your-path/,否则会有权限问题,windows用户请使用 D:/your-path/
|
# 文件存储路径 请注意下,mac用户请使用 /Users/your-username/your-path/,否则会有权限问题,windows用户请使用 D:/your-path/
|
||||||
storage-path: C:\application\rnb\files\ #/opt/datao/rnb/files/
|
storage-path: /opt/datao/rnb/files/ # C:\application\rnb\files\ #
|
||||||
# 短信配置
|
# 短信配置
|
||||||
sms:
|
sms:
|
||||||
# 阿里云短信
|
# 阿里云短信
|
||||||
@@ -144,30 +144,30 @@ sms:
|
|||||||
change-mobile: SMS_22xxx773
|
change-mobile: SMS_22xxx773
|
||||||
|
|
||||||
# springdoc配置: https://springdoc.org/properties.html
|
# springdoc配置: https://springdoc.org/properties.html
|
||||||
springdoc:
|
#springdoc:
|
||||||
swagger-ui:
|
# swagger-ui:
|
||||||
path: /swagger-ui.html
|
# path: /swagger-ui.html
|
||||||
operationsSorter: alpha
|
# operationsSorter: alpha
|
||||||
tags-sorter: alpha
|
# tags-sorter: alpha
|
||||||
api-docs:
|
# api-docs:
|
||||||
path: /v3/api-docs
|
# path: /v3/api-docs
|
||||||
group-configs:
|
# group-configs:
|
||||||
- group: '系统管理'
|
# - group: '系统管理'
|
||||||
paths-to-match: "/**"
|
# paths-to-match: "/**"
|
||||||
packages-to-scan:
|
# packages-to-scan:
|
||||||
- com.rnb.system.controller
|
# - com.rnb.system.controller
|
||||||
- com.rnb.shared.auth.controller
|
# - com.rnb.shared.auth.controller
|
||||||
- com.rnb.shared.file.controller
|
# - com.rnb.shared.file.controller
|
||||||
- com.rnb.shared.codegen.controller
|
# - com.rnb.shared.codegen.controller
|
||||||
- com.rnb.maoyan.controller
|
# - com.rnb.maoyan.controller
|
||||||
default-flat-param-object: true
|
# default-flat-param-object: true
|
||||||
|
|
||||||
# knife4j 接口文档配置
|
# knife4j 接口文档配置
|
||||||
knife4j:
|
knife4j:
|
||||||
# 是否开启 Knife4j 增强功能
|
# 是否开启 Knife4j 增强功能
|
||||||
enable: true # 设置为 true 表示开启增强功能
|
enable: true # 设置为 true 表示开启增强功能
|
||||||
# 生产环境配置
|
# 生产环境配置
|
||||||
production: false # 设置为 true 表示在生产环境中不显示文档,为 false 表示显示文档(通常在开发环境中使用)
|
production: true # 设置为 true 表示在生产环境中不显示文档,为 false 表示显示文档(通常在开发环境中使用)
|
||||||
setting:
|
setting:
|
||||||
language: zh_cn
|
language: zh_cn
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user