Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
a8077cd6
authored
Jun 02, 2026
by
renyizhao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
创建new api账号到获取令牌
parent
682c7f39
Show whitespace changes
Inline
Side-by-side
Showing
15 changed files
with
791 additions
and
5 deletions
+791
-5
computility-module-apihub/computility-module-apihub-biz/src/main/java/com/luhu/computility/module/apihub/config/ApiOrderConfig.java
+2
-5
computility-module-apihub/computility-module-apihub-biz/src/main/java/com/luhu/computility/module/apihub/config/ApihubConfig.java
+10
-0
computility-module-apihub/computility-module-apihub-biz/src/main/java/com/luhu/computility/module/apihub/config/NewApiProperties.java
+28
-0
computility-module-apihub/computility-module-apihub-biz/src/main/java/com/luhu/computility/module/apihub/controller/admin/newapi/AiTokenRespVO.java
+18
-0
computility-module-apihub/computility-module-apihub-biz/src/main/java/com/luhu/computility/module/apihub/controller/admin/newapi/NewApiResponse.java
+19
-0
computility-module-apihub/computility-module-apihub-biz/src/main/java/com/luhu/computility/module/apihub/controller/admin/newapi/NewApiTokenItemVO.java
+32
-0
computility-module-apihub/computility-module-apihub-biz/src/main/java/com/luhu/computility/module/apihub/controller/admin/newapi/NewApiTokenPageVO.java
+18
-0
computility-module-apihub/computility-module-apihub-biz/src/main/java/com/luhu/computility/module/apihub/controller/admin/newapi/NewApiUserVO.java
+20
-0
computility-module-apihub/computility-module-apihub-biz/src/main/java/com/luhu/computility/module/apihub/controller/app/AppAiTokenController.java
+101
-0
computility-module-apihub/computility-module-apihub-biz/src/main/java/com/luhu/computility/module/apihub/service/newapi/AiTokenService.java
+217
-0
computility-module-apihub/computility-module-apihub-biz/src/main/java/com/luhu/computility/module/apihub/service/newapi/NewApiClient.java
+276
-0
computility-module-member/src/main/java/com/luhu/computility/module/member/dal/dataobject/user/MemberUserDO.java
+22
-0
computility-module-member/src/main/java/com/luhu/computility/module/member/service/user/MemberUserService.java
+13
-0
computility-module-member/src/main/java/com/luhu/computility/module/member/service/user/MemberUserServiceImpl.java
+13
-0
computility-server/src/main/resources/application-local.yaml
+2
-0
No files found.
computility-module-apihub/computility-module-apihub-biz/src/main/java/com/luhu/computility/module/apihub/config/ApiOrderConfig.java
View file @
a8077cd6
...
...
@@ -3,11 +3,7 @@ package com.luhu.computility.module.apihub.config;
import
org.springframework.boot.context.properties.EnableConfigurationProperties
;
import
org.springframework.context.annotation.Configuration
;
/**
* @author LeeYan9
* @since 2022-09-15
*/
@Configuration
@EnableConfigurationProperties
(
ApiOrderProperties
.
class
)
@EnableConfigurationProperties
(
{
ApiOrderProperties
.
class
,
NewApiProperties
.
class
}
)
public
class
ApiOrderConfig
{
}
\ No newline at end of file
computility-module-apihub/computility-module-apihub-biz/src/main/java/com/luhu/computility/module/apihub/config/ApihubConfig.java
0 → 100644
View file @
a8077cd6
package
com
.
luhu
.
computility
.
module
.
apihub
.
config
;
import
org.springframework.boot.context.properties.EnableConfigurationProperties
;
import
org.springframework.context.annotation.Configuration
;
@Configuration
@EnableConfigurationProperties
({
ApiOrderProperties
.
class
})
public
class
ApihubConfig
{
}
\ No newline at end of file
computility-module-apihub/computility-module-apihub-biz/src/main/java/com/luhu/computility/module/apihub/config/NewApiProperties.java
0 → 100644
View file @
a8077cd6
package
com
.
luhu
.
computility
.
module
.
apihub
.
config
;
import
org.springframework.boot.context.properties.ConfigurationProperties
;
@ConfigurationProperties
(
prefix
=
"computility.new-api"
)
public
class
NewApiProperties
{
private
String
baseUrl
=
"http://localhost:3000"
;
private
String
adminToken
;
public
String
getBaseUrl
()
{
return
baseUrl
;
}
public
void
setBaseUrl
(
String
baseUrl
)
{
this
.
baseUrl
=
baseUrl
;
}
public
String
getAdminToken
()
{
return
adminToken
;
}
public
void
setAdminToken
(
String
adminToken
)
{
this
.
adminToken
=
adminToken
;
}
}
\ No newline at end of file
computility-module-apihub/computility-module-apihub-biz/src/main/java/com/luhu/computility/module/apihub/controller/admin/newapi/AiTokenRespVO.java
0 → 100644
View file @
a8077cd6
package
com
.
luhu
.
computility
.
module
.
apihub
.
controller
.
admin
.
newapi
;
import
lombok.AllArgsConstructor
;
import
lombok.Builder
;
import
lombok.Data
;
import
lombok.NoArgsConstructor
;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public
class
AiTokenRespVO
{
private
Boolean
success
;
private
Boolean
hasToken
;
private
String
apiKey
;
private
String
message
;
}
\ No newline at end of file
computility-module-apihub/computility-module-apihub-biz/src/main/java/com/luhu/computility/module/apihub/controller/admin/newapi/NewApiResponse.java
0 → 100644
View file @
a8077cd6
package
com
.
luhu
.
computility
.
module
.
apihub
.
controller
.
admin
.
newapi
;
import
cn.hutool.http.HttpResponse
;
import
lombok.AllArgsConstructor
;
import
lombok.Builder
;
import
lombok.Data
;
import
lombok.NoArgsConstructor
;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public
class
NewApiResponse
<
T
>
{
private
Boolean
success
;
private
String
message
;
private
T
data
;
private
HttpResponse
httpResponse
;
}
\ No newline at end of file
computility-module-apihub/computility-module-apihub-biz/src/main/java/com/luhu/computility/module/apihub/controller/admin/newapi/NewApiTokenItemVO.java
0 → 100644
View file @
a8077cd6
package
com
.
luhu
.
computility
.
module
.
apihub
.
controller
.
admin
.
newapi
;
import
lombok.AllArgsConstructor
;
import
lombok.Builder
;
import
lombok.Data
;
import
lombok.NoArgsConstructor
;
import
lombok.experimental.SuperBuilder
;
@Data
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
public
class
NewApiTokenItemVO
{
private
Long
id
;
private
Long
userId
;
private
String
key
;
private
Integer
status
;
private
String
name
;
private
Long
createdTime
;
private
Long
accessedTime
;
private
Integer
expiredTime
;
private
Integer
remainQuota
;
private
Boolean
unlimitedQuota
;
private
Boolean
modelLimitsEnabled
;
private
String
modelLimits
;
private
String
allowIps
;
private
Double
usedQuota
;
private
String
group
;
private
Boolean
crossGroupRetry
;
private
Object
deletedAt
;
}
\ No newline at end of file
computility-module-apihub/computility-module-apihub-biz/src/main/java/com/luhu/computility/module/apihub/controller/admin/newapi/NewApiTokenPageVO.java
0 → 100644
View file @
a8077cd6
package
com
.
luhu
.
computility
.
module
.
apihub
.
controller
.
admin
.
newapi
;
import
lombok.AllArgsConstructor
;
import
lombok.Builder
;
import
lombok.Data
;
import
lombok.NoArgsConstructor
;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public
class
NewApiTokenPageVO
{
private
Integer
page
;
private
Integer
pageSize
;
private
Integer
total
;
private
java
.
util
.
List
<
NewApiTokenItemVO
>
items
;
}
\ No newline at end of file
computility-module-apihub/computility-module-apihub-biz/src/main/java/com/luhu/computility/module/apihub/controller/admin/newapi/NewApiUserVO.java
0 → 100644
View file @
a8077cd6
package
com
.
luhu
.
computility
.
module
.
apihub
.
controller
.
admin
.
newapi
;
import
lombok.AllArgsConstructor
;
import
lombok.Builder
;
import
lombok.Data
;
import
lombok.NoArgsConstructor
;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public
class
NewApiUserVO
{
private
Integer
id
;
private
String
username
;
private
String
displayName
;
private
Integer
status
;
private
String
group
;
private
Integer
role
;
}
\ No newline at end of file
computility-module-apihub/computility-module-apihub-biz/src/main/java/com/luhu/computility/module/apihub/controller/app/AppAiTokenController.java
0 → 100644
View file @
a8077cd6
package
com
.
luhu
.
computility
.
module
.
apihub
.
controller
.
app
;
import
com.luhu.computility.framework.security.core.util.SecurityFrameworkUtils
;
import
com.luhu.computility.module.apihub.controller.admin.newapi.AiTokenRespVO
;
import
com.luhu.computility.module.apihub.service.newapi.AiTokenService
;
import
com.luhu.computility.module.member.dal.dataobject.user.MemberUserDO
;
import
com.luhu.computility.module.member.service.user.MemberUserService
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.web.bind.annotation.*
;
import
javax.annotation.Resource
;
@Slf4j
@RestController
@RequestMapping
(
"/app/ai-token"
)
public
class
AppAiTokenController
{
@Resource
private
AiTokenService
aiTokenService
;
@Resource
private
MemberUserService
memberUserService
;
@PostMapping
(
"/get"
)
public
AiTokenRespVO
getToken
()
{
Long
userId
=
SecurityFrameworkUtils
.
getLoginUserId
();
log
.
info
(
"[获取令牌] userId={}"
,
userId
);
MemberUserDO
user
=
memberUserService
.
getUser
(
userId
);
if
(
user
==
null
)
{
log
.
warn
(
"[获取令牌] 用户不存在, userId={}"
,
userId
);
return
AiTokenRespVO
.
builder
()
.
success
(
false
)
.
message
(
"用户不存在"
)
.
build
();
}
log
.
info
(
"[获取令牌] 用户信息: newapiUsername={}, newapiPassword={}, newapiUserId={}, newapiAccessToken={}, newapiKey={}"
,
user
.
getNewapiUsername
(),
user
.
getNewapiPassword
()
!=
null
?
"***"
:
null
,
user
.
getNewapiUserId
(),
user
.
getNewapiAccessToken
()
!=
null
?
"***"
:
null
,
user
.
getNewapiKey
()
!=
null
?
"***"
:
null
);
AiTokenService
.
AiTokenResult
result
=
aiTokenService
.
getOrCreateToken
(
user
.
getId
(),
user
.
getNewapiUsername
(),
user
.
getNewapiPassword
(),
user
.
getNewapiUserId
(),
user
.
getNewapiAccessToken
(),
user
.
getNewapiKey
()
);
log
.
info
(
"[获取令牌] 结果: success={}, hasToken={}, apiKey={}, message={}"
,
result
.
getSuccess
(),
result
.
getHasToken
(),
result
.
getApiKey
()
!=
null
?
"***"
:
null
,
result
.
getMessage
());
if
(
Boolean
.
TRUE
.
equals
(
result
.
getSuccess
())
||
result
.
getNewApiUserId
()
!=
null
||
result
.
getNewApiUsername
()
!=
null
)
{
log
.
info
(
"[获取令牌] 更新用户NewAPI信息"
);
memberUserService
.
updateNewApiInfo
(
userId
,
result
.
getNewApiUserId
()
!=
null
?
result
.
getNewApiUserId
()
:
user
.
getNewapiUserId
(),
result
.
getNewApiAccessToken
()
!=
null
?
result
.
getNewApiAccessToken
()
:
user
.
getNewapiAccessToken
(),
result
.
getApiKey
()
!=
null
?
result
.
getApiKey
()
:
user
.
getNewapiKey
(),
result
.
getNewApiUsername
()
!=
null
?
result
.
getNewApiUsername
()
:
user
.
getNewapiUsername
(),
result
.
getNewApiPassword
()
!=
null
?
result
.
getNewApiPassword
()
:
user
.
getNewapiPassword
()
);
}
return
AiTokenRespVO
.
builder
()
.
success
(
result
.
getSuccess
())
.
hasToken
(
result
.
getHasToken
())
.
apiKey
(
result
.
getApiKey
())
.
message
(
result
.
getMessage
())
.
build
();
}
@GetMapping
(
"/info"
)
public
AiTokenRespVO
getTokenInfo
()
{
Long
userId
=
SecurityFrameworkUtils
.
getLoginUserId
();
log
.
info
(
"[查询令牌] userId={}"
,
userId
);
MemberUserDO
user
=
memberUserService
.
getUser
(
userId
);
if
(
user
==
null
)
{
log
.
warn
(
"[查询令牌] 用户不存在, userId={}"
,
userId
);
return
AiTokenRespVO
.
builder
()
.
success
(
false
)
.
message
(
"用户不存在"
)
.
build
();
}
boolean
hasToken
=
user
.
getNewapiKey
()
!=
null
&&
!
user
.
getNewapiKey
().
isEmpty
();
log
.
info
(
"[查询令牌] hasToken={}, apiKey={}"
,
hasToken
,
hasToken
?
"***"
:
null
);
return
AiTokenRespVO
.
builder
()
.
success
(
true
)
.
hasToken
(
hasToken
)
.
apiKey
(
user
.
getNewapiKey
())
.
build
();
}
}
\ No newline at end of file
computility-module-apihub/computility-module-apihub-biz/src/main/java/com/luhu/computility/module/apihub/service/newapi/AiTokenService.java
0 → 100644
View file @
a8077cd6
package
com
.
luhu
.
computility
.
module
.
apihub
.
service
.
newapi
;
import
cn.hutool.core.util.RandomUtil
;
import
com.luhu.computility.module.apihub.controller.admin.newapi.*
;
import
lombok.AllArgsConstructor
;
import
lombok.Builder
;
import
lombok.Data
;
import
lombok.NoArgsConstructor
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.stereotype.Service
;
import
javax.annotation.Resource
;
import
java.util.Map
;
@Slf4j
@Service
public
class
AiTokenService
{
@Resource
private
NewApiClient
newApiClient
;
public
AiTokenResult
getOrCreateToken
(
Long
userId
,
String
newApiUsername
,
String
newApiPassword
,
Long
newApiUserId
,
String
newApiAccessToken
,
String
newApiKey
)
{
log
.
info
(
"[AiTokenService] userId={}, hasApiKey={}"
,
userId
,
newApiKey
!=
null
&&
!
newApiKey
.
isEmpty
());
if
(
newApiKey
!=
null
&&
!
newApiKey
.
isEmpty
())
{
log
.
info
(
"[AiTokenService] 用户已有apiKey,直接返回"
);
return
AiTokenResult
.
builder
()
.
success
(
true
)
.
hasToken
(
true
)
.
apiKey
(
newApiKey
)
.
build
();
}
if
(
newApiUsername
!=
null
&&
!
newApiUsername
.
isEmpty
()
&&
newApiAccessToken
!=
null
&&
!
newApiAccessToken
.
isEmpty
())
{
log
.
info
(
"[AiTokenService] 有账号密码和AccessToken,从第4步开始"
);
return
createTokenStep4
(
newApiUserId
.
intValue
(),
newApiAccessToken
,
userId
,
newApiUsername
,
newApiPassword
);
}
if
(
newApiUsername
!=
null
&&
!
newApiUsername
.
isEmpty
())
{
log
.
info
(
"[AiTokenService] 有账号密码,从第2步开始"
);
return
createTokenStep2
(
newApiUsername
,
newApiPassword
,
userId
);
}
log
.
info
(
"[AiTokenService] 无账号密码,从第1步开始"
);
return
createTokenFull
(
userId
);
}
private
AiTokenResult
createTokenFull
(
Long
userId
)
{
String
username
=
"puhui_"
+
userId
;
String
password
=
RandomUtil
.
randomString
(
12
);
log
.
info
(
"[AiTokenService.createTokenFull] 准备注册: username={}"
,
username
);
try
{
NewApiResponse
<
NewApiUserVO
>
registerResp
=
newApiClient
.
register
(
username
,
password
);
log
.
info
(
"[AiTokenService.createTokenFull] 注册结果: success={}, message={}"
,
registerResp
.
getSuccess
(),
registerResp
.
getMessage
());
if
(!
Boolean
.
TRUE
.
equals
(
registerResp
.
getSuccess
()))
{
return
AiTokenResult
.
builder
()
.
success
(
false
)
.
message
(
"注册NewAPI账号失败: "
+
registerResp
.
getMessage
())
.
build
();
}
return
createTokenStep2
(
username
,
password
,
userId
);
}
catch
(
Exception
e
)
{
log
.
error
(
"[AiTokenService.createTokenFull] 注册异常"
,
e
);
return
AiTokenResult
.
builder
()
.
success
(
false
)
.
message
(
"注册NewAPI账号异常: "
+
e
.
getMessage
())
.
build
();
}
}
private
AiTokenResult
createTokenStep2
(
String
username
,
String
password
,
Long
userId
)
{
log
.
info
(
"[AiTokenService.createTokenStep2] 准备登录: username={}"
,
username
);
try
{
NewApiResponse
<
NewApiUserVO
>
loginResp
=
newApiClient
.
login
(
username
,
password
);
log
.
info
(
"[AiTokenService.createTokenStep2] 登录结果: success={}, message={}"
,
loginResp
.
getSuccess
(),
loginResp
.
getMessage
());
if
(!
Boolean
.
TRUE
.
equals
(
loginResp
.
getSuccess
())
||
loginResp
.
getData
()
==
null
)
{
return
AiTokenResult
.
builder
()
.
success
(
false
)
.
message
(
"登录NewAPI失败: "
+
loginResp
.
getMessage
())
.
build
();
}
Integer
newApiUserId
=
loginResp
.
getData
().
getId
();
log
.
info
(
"[AiTokenService.createTokenStep2] 登录成功, newApiUserId={}"
,
newApiUserId
);
String
session
=
newApiClient
.
getSessionCookie
(
loginResp
.
getHttpResponse
());
log
.
info
(
"[AiTokenService.createTokenStep2] 获取session: {}"
,
session
!=
null
?
"成功"
:
"失败"
);
NewApiResponse
<
String
>
tokenResp
=
newApiClient
.
generateAccessToken
(
session
,
newApiUserId
);
log
.
info
(
"[AiTokenService.createTokenStep2] 生成AccessToken结果: success={}, message={}"
,
tokenResp
.
getSuccess
(),
tokenResp
.
getMessage
());
if
(!
Boolean
.
TRUE
.
equals
(
tokenResp
.
getSuccess
())
||
tokenResp
.
getData
()
==
null
)
{
return
AiTokenResult
.
builder
()
.
success
(
false
)
.
message
(
"生成访问令牌失败: "
+
tokenResp
.
getMessage
())
.
newApiUsername
(
username
)
.
newApiPassword
(
password
)
.
newApiUserId
(
newApiUserId
.
longValue
())
.
build
();
}
String
accessToken
=
tokenResp
.
getData
();
log
.
info
(
"[AiTokenService.createTokenStep2] AccessToken生成成功: {}***"
,
accessToken
.
substring
(
0
,
Math
.
min
(
5
,
accessToken
.
length
())));
return
createTokenStep4
(
newApiUserId
,
accessToken
,
userId
,
username
,
password
);
}
catch
(
Exception
e
)
{
log
.
error
(
"[AiTokenService.createTokenStep2] 登录异常"
,
e
);
return
AiTokenResult
.
builder
()
.
success
(
false
)
.
message
(
"登录NewAPI异常: "
+
e
.
getMessage
())
.
newApiUsername
(
username
)
.
newApiPassword
(
password
)
.
build
();
}
}
private
AiTokenResult
createTokenStep4
(
Integer
newApiUserId
,
String
accessToken
,
Long
userId
,
String
username
,
String
password
)
{
String
tokenName
=
"puhui_"
+
userId
+
"_token"
;
log
.
info
(
"[AiTokenService.createTokenStep4] 准备创建令牌: newApiUserId={}, tokenName={}"
,
newApiUserId
,
tokenName
);
try
{
NewApiResponse
<
Void
>
createResp
=
newApiClient
.
createToken
(
accessToken
,
newApiUserId
,
tokenName
);
log
.
info
(
"[AiTokenService.createTokenStep4] 创建令牌结果: success={}, message={}"
,
createResp
.
getSuccess
(),
createResp
.
getMessage
());
if
(!
Boolean
.
TRUE
.
equals
(
createResp
.
getSuccess
()))
{
return
AiTokenResult
.
builder
()
.
success
(
false
)
.
message
(
"创建令牌失败: "
+
createResp
.
getMessage
())
.
newApiUserId
(
newApiUserId
.
longValue
())
.
newApiAccessToken
(
accessToken
)
.
newApiUsername
(
username
)
.
newApiPassword
(
password
)
.
build
();
}
NewApiResponse
<
NewApiTokenPageVO
>
listResp
=
newApiClient
.
getTokenList
(
accessToken
,
newApiUserId
);
log
.
info
(
"[AiTokenService.createTokenStep4] 获取令牌列表结果: success={}, message={}"
,
listResp
.
getSuccess
(),
listResp
.
getMessage
());
if
(!
Boolean
.
TRUE
.
equals
(
listResp
.
getSuccess
())
||
listResp
.
getData
()
==
null
||
listResp
.
getData
().
getItems
()
==
null
||
listResp
.
getData
().
getItems
().
isEmpty
())
{
return
AiTokenResult
.
builder
()
.
success
(
false
)
.
message
(
"获取令牌列表失败: "
+
listResp
.
getMessage
())
.
newApiUserId
(
newApiUserId
.
longValue
())
.
newApiAccessToken
(
accessToken
)
.
newApiUsername
(
username
)
.
newApiPassword
(
password
)
.
build
();
}
Long
tokenId
=
listResp
.
getData
().
getItems
().
get
(
0
).
getId
();
log
.
info
(
"[AiTokenService.createTokenStep4] 获取到tokenId={}"
,
tokenId
);
NewApiResponse
<
Map
<
String
,
Object
>>
keyResp
=
newApiClient
.
getTokenKey
(
accessToken
,
newApiUserId
,
tokenId
);
log
.
info
(
"[AiTokenService.createTokenStep4] 获取apiKey结果: success={}, message={}"
,
keyResp
.
getSuccess
(),
keyResp
.
getMessage
());
if
(!
Boolean
.
TRUE
.
equals
(
keyResp
.
getSuccess
())
||
keyResp
.
getData
()
==
null
)
{
return
AiTokenResult
.
builder
()
.
success
(
false
)
.
message
(
"获取apiKey失败: "
+
keyResp
.
getMessage
())
.
newApiUserId
(
newApiUserId
.
longValue
())
.
newApiAccessToken
(
accessToken
)
.
newApiUsername
(
username
)
.
newApiPassword
(
password
)
.
build
();
}
Map
<
String
,
Object
>
keyData
=
keyResp
.
getData
();
String
apiKey
=
(
String
)
keyData
.
get
(
"key"
);
log
.
info
(
"[AiTokenService.createTokenStep4] apiKey获取成功: {}***"
,
apiKey
.
substring
(
0
,
Math
.
min
(
5
,
apiKey
.
length
())));
return
AiTokenResult
.
builder
()
.
success
(
true
)
.
hasToken
(
true
)
.
apiKey
(
apiKey
)
.
newApiUserId
(
newApiUserId
.
longValue
())
.
newApiAccessToken
(
accessToken
)
.
newApiUsername
(
username
)
.
newApiPassword
(
password
)
.
build
();
}
catch
(
Exception
e
)
{
log
.
error
(
"[AiTokenService.createTokenStep4] 创建令牌异常"
,
e
);
return
AiTokenResult
.
builder
()
.
success
(
false
)
.
message
(
"创建令牌异常: "
+
e
.
getMessage
())
.
newApiUserId
(
newApiUserId
.
longValue
())
.
newApiAccessToken
(
accessToken
)
.
newApiUsername
(
username
)
.
newApiPassword
(
password
)
.
build
();
}
}
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public
static
class
AiTokenResult
{
private
Boolean
success
;
private
Boolean
hasToken
;
private
String
apiKey
;
private
String
message
;
private
Long
newApiUserId
;
private
String
newApiAccessToken
;
private
String
newApiUsername
;
private
String
newApiPassword
;
}
}
\ No newline at end of file
computility-module-apihub/computility-module-apihub-biz/src/main/java/com/luhu/computility/module/apihub/service/newapi/NewApiClient.java
0 → 100644
View file @
a8077cd6
package
com
.
luhu
.
computility
.
module
.
apihub
.
service
.
newapi
;
import
cn.hutool.core.util.StrUtil
;
import
cn.hutool.http.HttpRequest
;
import
cn.hutool.http.HttpResponse
;
import
cn.hutool.json.JSONObject
;
import
cn.hutool.json.JSONUtil
;
import
com.luhu.computility.module.apihub.config.NewApiProperties
;
import
com.luhu.computility.module.apihub.controller.admin.newapi.*
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.stereotype.Service
;
import
javax.annotation.Resource
;
import
java.util.HashMap
;
import
java.util.Map
;
@Slf4j
@Service
public
class
NewApiClient
{
@Resource
private
NewApiProperties
newApiProperties
;
private
static
final
String
HEADER_USER
=
"New-Api-User"
;
private
static
final
String
HEADER_AUTH
=
"Authorization"
;
private
static
final
String
USER_ANONYMOUS
=
"-1"
;
public
NewApiResponse
<
NewApiUserVO
>
register
(
String
username
,
String
password
)
{
String
url
=
newApiProperties
.
getBaseUrl
()
+
"/api/user/register"
;
log
.
info
(
"[NewApiClient.register] 请求URL: {}"
,
url
);
log
.
info
(
"[NewApiClient.register] 请求参数: username={}, password={}"
,
username
,
"***"
);
Map
<
String
,
Object
>
body
=
new
HashMap
<>();
body
.
put
(
"username"
,
username
);
body
.
put
(
"password"
,
password
);
try
{
HttpResponse
response
=
HttpRequest
.
post
(
url
)
.
header
(
HEADER_USER
,
USER_ANONYMOUS
)
.
body
(
JSONUtil
.
toJsonStr
(
body
))
.
contentType
(
"application/json"
)
.
timeout
(
30000
)
.
execute
();
log
.
info
(
"[NewApiClient.register] 响应状态: {}"
,
response
.
getStatus
());
log
.
info
(
"[NewApiClient.register] 响应内容: {}"
,
response
.
body
());
return
parseResponse
(
response
,
NewApiUserVO
.
class
);
}
catch
(
Exception
e
)
{
log
.
error
(
"[NewApiClient.register] 请求异常"
,
e
);
return
NewApiResponse
.<
NewApiUserVO
>
builder
()
.
success
(
false
)
.
message
(
"请求异常: "
+
e
.
getMessage
())
.
build
();
}
}
public
NewApiResponse
<
NewApiUserVO
>
login
(
String
username
,
String
password
)
{
String
url
=
newApiProperties
.
getBaseUrl
()
+
"/api/user/login"
;
log
.
info
(
"[NewApiClient.login] 请求URL: {}"
,
url
);
Map
<
String
,
Object
>
body
=
new
HashMap
<>();
body
.
put
(
"username"
,
username
);
body
.
put
(
"password"
,
password
);
try
{
HttpResponse
response
=
HttpRequest
.
post
(
url
)
.
header
(
HEADER_USER
,
USER_ANONYMOUS
)
.
body
(
JSONUtil
.
toJsonStr
(
body
))
.
contentType
(
"application/json"
)
.
timeout
(
30000
)
.
execute
();
log
.
info
(
"[NewApiClient.login] 响应状态: {}"
,
response
.
getStatus
());
log
.
info
(
"[NewApiClient.login] 响应Cookie: {}"
,
response
.
getCookies
());
return
parseResponse
(
response
,
NewApiUserVO
.
class
);
}
catch
(
Exception
e
)
{
log
.
error
(
"[NewApiClient.login] 请求异常"
,
e
);
return
NewApiResponse
.<
NewApiUserVO
>
builder
()
.
success
(
false
)
.
message
(
"请求异常: "
+
e
.
getMessage
())
.
build
();
}
}
public
String
getSessionCookie
(
HttpResponse
loginResponse
)
{
String
session
=
loginResponse
.
getCookieValue
(
"session"
);
log
.
info
(
"[NewApiClient.getSessionCookie] session={}"
,
session
!=
null
?
session
:
"null"
);
return
session
;
}
public
NewApiResponse
<
String
>
generateAccessToken
(
String
session
,
Integer
userId
)
{
String
url
=
newApiProperties
.
getBaseUrl
()
+
"/api/user/token"
;
log
.
info
(
"[NewApiClient.generateAccessToken] 请求URL: {}"
,
url
);
log
.
info
(
"[NewApiClient.generateAccessToken] session={}"
,
session
!=
null
?
session
:
"null"
);
try
{
HttpResponse
response
=
HttpRequest
.
get
(
url
)
.
header
(
HEADER_USER
,
String
.
valueOf
(
userId
))
.
header
(
"Cookie"
,
"session="
+
session
)
.
timeout
(
30000
)
.
execute
();
log
.
info
(
"[NewApiClient.generateAccessToken] 响应状态: {}"
,
response
.
getStatus
());
log
.
info
(
"[NewApiClient.generateAccessToken] 响应内容: {}"
,
response
.
body
());
return
parseResponse
(
response
,
String
.
class
);
}
catch
(
Exception
e
)
{
log
.
error
(
"[NewApiClient.generateAccessToken] 请求异常"
,
e
);
return
NewApiResponse
.<
String
>
builder
()
.
success
(
false
)
.
message
(
"请求异常: "
+
e
.
getMessage
())
.
build
();
}
}
public
NewApiResponse
<
String
>
generateAccessTokenByToken
(
String
accessToken
,
Integer
userId
)
{
String
url
=
newApiProperties
.
getBaseUrl
()
+
"/api/user/token"
;
log
.
info
(
"[NewApiClient.generateAccessTokenByToken] 请求URL: {}"
,
url
);
try
{
HttpResponse
response
=
HttpRequest
.
get
(
url
)
.
header
(
HEADER_USER
,
String
.
valueOf
(
userId
))
.
header
(
HEADER_AUTH
,
"Bearer "
+
accessToken
)
.
timeout
(
30000
)
.
execute
();
return
parseResponse
(
response
,
String
.
class
);
}
catch
(
Exception
e
)
{
log
.
error
(
"[NewApiClient.generateAccessTokenByToken] 请求异常"
,
e
);
return
NewApiResponse
.<
String
>
builder
()
.
success
(
false
)
.
message
(
"请求异常: "
+
e
.
getMessage
())
.
build
();
}
}
public
NewApiResponse
<
Void
>
createToken
(
String
accessToken
,
Integer
userId
,
String
name
)
{
String
url
=
newApiProperties
.
getBaseUrl
()
+
"/api/token/"
;
log
.
info
(
"[NewApiClient.createToken] 请求URL: {}"
,
url
);
Map
<
String
,
Object
>
body
=
new
HashMap
<>();
body
.
put
(
"name"
,
name
);
body
.
put
(
"remain_quota"
,
0
);
body
.
put
(
"expired_time"
,
-
1
);
body
.
put
(
"unlimited_quota"
,
true
);
body
.
put
(
"model_limits_enabled"
,
false
);
body
.
put
(
"model_limits"
,
""
);
body
.
put
(
"cross_group_retry"
,
false
);
body
.
put
(
"group"
,
""
);
body
.
put
(
"allow_ips"
,
""
);
body
.
put
(
"remain_amount"
,
0
);
try
{
HttpResponse
response
=
HttpRequest
.
post
(
url
)
.
header
(
HEADER_USER
,
String
.
valueOf
(
userId
))
.
header
(
HEADER_AUTH
,
"Bearer "
+
accessToken
)
.
body
(
JSONUtil
.
toJsonStr
(
body
))
.
contentType
(
"application/json"
)
.
timeout
(
30000
)
.
execute
();
return
parseResponse
(
response
,
Void
.
class
);
}
catch
(
Exception
e
)
{
log
.
error
(
"[NewApiClient.createToken] 请求异常"
,
e
);
return
NewApiResponse
.<
Void
>
builder
()
.
success
(
false
)
.
message
(
"请求异常: "
+
e
.
getMessage
())
.
build
();
}
}
public
NewApiResponse
<
NewApiTokenPageVO
>
getTokenList
(
String
accessToken
,
Integer
userId
)
{
String
url
=
newApiProperties
.
getBaseUrl
()
+
"/api/token/?p=1&size=10"
;
log
.
info
(
"[NewApiClient.getTokenList] 请求URL: {}"
,
url
);
try
{
HttpResponse
response
=
HttpRequest
.
get
(
url
)
.
header
(
HEADER_USER
,
String
.
valueOf
(
userId
))
.
header
(
HEADER_AUTH
,
"Bearer "
+
accessToken
)
.
timeout
(
30000
)
.
execute
();
return
parseResponse
(
response
,
NewApiTokenPageVO
.
class
);
}
catch
(
Exception
e
)
{
log
.
error
(
"[NewApiClient.getTokenList] 请求异常"
,
e
);
return
NewApiResponse
.<
NewApiTokenPageVO
>
builder
()
.
success
(
false
)
.
message
(
"请求异常: "
+
e
.
getMessage
())
.
build
();
}
}
@SuppressWarnings
(
"unchecked"
)
public
NewApiResponse
<
Map
<
String
,
Object
>>
getTokenKey
(
String
accessToken
,
Integer
userId
,
Long
tokenId
)
{
String
url
=
newApiProperties
.
getBaseUrl
()
+
"/api/token/"
+
tokenId
+
"/key"
;
log
.
info
(
"[NewApiClient.getTokenKey] 请求URL: {}"
,
url
);
try
{
HttpResponse
response
=
HttpRequest
.
post
(
url
)
.
header
(
HEADER_USER
,
String
.
valueOf
(
userId
))
.
header
(
HEADER_AUTH
,
"Bearer "
+
accessToken
)
.
timeout
(
30000
)
.
execute
();
log
.
info
(
"[NewApiClient.getTokenKey] 响应状态: {}"
,
response
.
getStatus
());
log
.
info
(
"[NewApiClient.getTokenKey] 响应内容: {}"
,
response
.
body
());
String
body
=
response
.
body
();
JSONObject
json
=
JSONUtil
.
parseObj
(
body
);
NewApiResponse
<
Map
<
String
,
Object
>>
result
=
new
NewApiResponse
<>();
result
.
setSuccess
(
json
.
getBool
(
"success"
));
result
.
setMessage
(
json
.
getStr
(
"message"
));
result
.
setHttpResponse
(
response
);
if
(
json
.
containsKey
(
"data"
)
&&
!
json
.
isNull
(
"data"
))
{
Object
data
=
json
.
get
(
"data"
);
if
(
data
instanceof
Map
)
{
result
.
setData
((
Map
<
String
,
Object
>)
data
);
}
else
{
result
.
setData
((
Map
<
String
,
Object
>)
JSONUtil
.
parseObj
(
data
.
toString
()));
}
}
return
result
;
}
catch
(
Exception
e
)
{
log
.
error
(
"[NewApiClient.getTokenKey] 请求异常"
,
e
);
return
NewApiResponse
.<
Map
<
String
,
Object
>>
builder
()
.
success
(
false
)
.
message
(
"请求异常: "
+
e
.
getMessage
())
.
build
();
}
}
@SuppressWarnings
(
"unchecked"
)
private
<
T
>
NewApiResponse
<
T
>
parseResponse
(
HttpResponse
response
,
Class
<
T
>
dataClass
)
{
try
{
String
body
=
response
.
body
();
if
(
StrUtil
.
isBlank
(
body
))
{
return
NewApiResponse
.<
T
>
builder
()
.
success
(
false
)
.
message
(
"Empty response"
)
.
httpResponse
(
response
)
.
build
();
}
JSONObject
json
=
JSONUtil
.
parseObj
(
body
);
NewApiResponse
<
T
>
result
=
new
NewApiResponse
<>();
result
.
setSuccess
(
json
.
getBool
(
"success"
));
result
.
setMessage
(
json
.
getStr
(
"message"
));
result
.
setHttpResponse
(
response
);
if
(
json
.
containsKey
(
"data"
)
&&
!
json
.
isNull
(
"data"
)
&&
dataClass
!=
null
)
{
Object
data
=
json
.
get
(
"data"
);
if
(
dataClass
==
String
.
class
)
{
result
.
setData
((
T
)
data
.
toString
());
}
else
if
(
dataClass
==
Void
.
class
)
{
result
.
setData
(
null
);
}
else
if
(
data
instanceof
JSONObject
)
{
result
.
setData
((
T
)
JSONUtil
.
toBean
((
JSONObject
)
data
,
dataClass
));
}
else
{
result
.
setData
((
T
)
data
);
}
}
return
result
;
}
catch
(
Exception
e
)
{
log
.
error
(
"[NewApiClient.parseResponse] 解析响应异常"
,
e
);
return
NewApiResponse
.<
T
>
builder
()
.
success
(
false
)
.
message
(
"解析响应失败: "
+
e
.
getMessage
())
.
httpResponse
(
response
)
.
build
();
}
}
}
\ No newline at end of file
computility-module-member/src/main/java/com/luhu/computility/module/member/dal/dataobject/user/MemberUserDO.java
View file @
a8077cd6
...
...
@@ -142,5 +142,27 @@ public class MemberUserDO extends TenantBaseDO {
*/
private
Long
groupId
;
// ========== NewAPI 相关信息 ==========
/**
* NewAPI 用户ID
*/
private
Long
newapiUserId
;
/**
* NewAPI 访问令牌
*/
private
String
newapiAccessToken
;
/**
* NewAPI API Key
*/
private
String
newapiKey
;
/**
* NewAPI 用户名
*/
private
String
newapiUsername
;
/**
* NewAPI 密码
*/
private
String
newapiPassword
;
}
computility-module-member/src/main/java/com/luhu/computility/module/member/service/user/MemberUserService.java
View file @
a8077cd6
...
...
@@ -194,4 +194,17 @@ public interface MemberUserService {
*/
boolean
updateUserPoint
(
Long
userId
,
Integer
point
);
/**
* 更新用户的NewAPI相关信息
*
* @param userId 用户编号
* @param newapiUserId NewAPI用户ID
* @param newapiAccessToken NewAPI访问令牌
* @param newapiKey NewAPI密钥
* @param newapiUsername NewAPI用户名
* @param newapiPassword NewAPI密码
*/
void
updateNewApiInfo
(
Long
userId
,
Long
newapiUserId
,
String
newapiAccessToken
,
String
newapiKey
,
String
newapiUsername
,
String
newapiPassword
);
}
computility-module-member/src/main/java/com/luhu/computility/module/member/service/user/MemberUserServiceImpl.java
View file @
a8077cd6
...
...
@@ -319,4 +319,17 @@ public class MemberUserServiceImpl implements MemberUserService {
return
true
;
}
@Override
public
void
updateNewApiInfo
(
Long
userId
,
Long
newapiUserId
,
String
newapiAccessToken
,
String
newapiKey
,
String
newapiUsername
,
String
newapiPassword
)
{
memberUserMapper
.
updateById
(
MemberUserDO
.
builder
()
.
id
(
userId
)
.
newapiUserId
(
newapiUserId
)
.
newapiAccessToken
(
newapiAccessToken
)
.
newapiKey
(
newapiKey
)
.
newapiUsername
(
newapiUsername
)
.
newapiPassword
(
newapiPassword
)
.
build
());
}
}
computility-server/src/main/resources/application-local.yaml
View file @
a8077cd6
...
...
@@ -211,6 +211,8 @@ wx:
# 芋道配置项,设置当前项目所有自定义的配置
computility
:
new-api
:
base-url
:
http://172.25.164.0:3000
captcha
:
enable
:
false
# 本地环境,暂时关闭图片验证码,方便登录等接口的测试;
security
:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment