Skip to content

Commit 0a37aa4

Browse files
committed
docs: add API docs
1 parent 054b00b commit 0a37aa4

File tree

4 files changed

+101
-23
lines changed

4 files changed

+101
-23
lines changed

controller/user.go

+56-21
Original file line numberDiff line numberDiff line change
@@ -180,27 +180,27 @@ func Register(c *gin.Context) {
180180
}
181181

182182
func GetAllUsers(c *gin.Context) {
183-
p, _ := strconv.Atoi(c.Query("p"))
184-
if p < 0 {
185-
p = 0
186-
}
187-
188-
order := c.DefaultQuery("order", "")
189-
users, err := model.GetAllUsers(p*config.ItemsPerPage, config.ItemsPerPage, order)
190-
191-
if err != nil {
192-
c.JSON(http.StatusOK, gin.H{
193-
"success": false,
194-
"message": err.Error(),
195-
})
196-
return
197-
}
198-
199-
c.JSON(http.StatusOK, gin.H{
200-
"success": true,
201-
"message": "",
202-
"data": users,
203-
})
183+
p, _ := strconv.Atoi(c.Query("p"))
184+
if p < 0 {
185+
p = 0
186+
}
187+
188+
order := c.DefaultQuery("order", "")
189+
users, err := model.GetAllUsers(p*config.ItemsPerPage, config.ItemsPerPage, order)
190+
191+
if err != nil {
192+
c.JSON(http.StatusOK, gin.H{
193+
"success": false,
194+
"message": err.Error(),
195+
})
196+
return
197+
}
198+
199+
c.JSON(http.StatusOK, gin.H{
200+
"success": true,
201+
"message": "",
202+
"data": users,
203+
})
204204
}
205205

206206
func SearchUsers(c *gin.Context) {
@@ -770,3 +770,38 @@ func TopUp(c *gin.Context) {
770770
})
771771
return
772772
}
773+
774+
type adminTopUpRequest struct {
775+
UserId int `json:"user_id"`
776+
Quota int `json:"quota"`
777+
Remark string `json:"remark"`
778+
}
779+
780+
func AdminTopUp(c *gin.Context) {
781+
req := adminTopUpRequest{}
782+
err := c.ShouldBindJSON(&req)
783+
if err != nil {
784+
c.JSON(http.StatusOK, gin.H{
785+
"success": false,
786+
"message": err.Error(),
787+
})
788+
return
789+
}
790+
err = model.IncreaseUserQuota(req.UserId, int64(req.Quota))
791+
if err != nil {
792+
c.JSON(http.StatusOK, gin.H{
793+
"success": false,
794+
"message": err.Error(),
795+
})
796+
return
797+
}
798+
if req.Remark == "" {
799+
req.Remark = fmt.Sprintf("通过 API 充值 %s", common.LogQuota(int64(req.Quota)))
800+
}
801+
model.RecordTopupLog(req.UserId, req.Remark, req.Quota)
802+
c.JSON(http.StatusOK, gin.H{
803+
"success": true,
804+
"message": "",
805+
})
806+
return
807+
}

docs/API.md

+29-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# 使用 API 操控 & 扩展 One API
22
> 欢迎提交 PR 在此放上你的拓展项目。
33
4+
例如,虽然 One API 本身没有直接支持支付,但是你可以通过系统扩展的 API 来实现支付功能。
5+
6+
又或者你想自定义渠道管理策略,也可以通过 API 来实现渠道的禁用与启用。
7+
48
## 鉴权
59
One API 支持两种鉴权方式:Cookie 和 Token,对于 Token,参照下图获取:
610

@@ -9,9 +13,32 @@ One API 支持两种鉴权方式:Cookie 和 Token,对于 Token,参照下
913
之后,将 Token 作为请求头的 Authorization 字段的值即可,例如下面使用 Token 调用测试渠道的 API:
1014
![image](https://github.com/songquanpeng/songquanpeng.github.io/assets/39998050/1273b7ae-cb60-4c0d-93a6-b1cbc039c4f8)
1115

16+
## 请求格式与响应格式
17+
One API 使用 JSON 格式进行请求和响应。
18+
19+
对于响应体,一般格式如下:
20+
```json
21+
{
22+
"message": "请求信息",
23+
"success": true,
24+
"data": {}
25+
}
26+
```
27+
1228
## API 列表
1329
> 当前 API 列表不全,请自行通过浏览器抓取前端请求
1430
15-
欢迎此处 PR 补充。
31+
如果现有的 API 没有办法满足你的需求,欢迎提交 issue 讨论。
32+
33+
### 获取当前登录用户信息
34+
**GET** `/api/user/self`
1635

17-
如果现有的 API 没有办法满足你的需求,欢迎提交 issue 讨论。
36+
### 为给定用户充值额度
37+
**POST** `/api/topup`
38+
```json
39+
{
40+
"user_id": 1,
41+
"quota": 100000,
42+
"remark": "充值 100000 额度"
43+
}
44+
```

model/log.go

+15
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,21 @@ func RecordLog(userId int, logType int, content string) {
5151
}
5252
}
5353

54+
func RecordTopupLog(userId int, content string, quota int) {
55+
log := &Log{
56+
UserId: userId,
57+
Username: GetUsernameById(userId),
58+
CreatedAt: helper.GetTimestamp(),
59+
Type: LogTypeTopup,
60+
Content: content,
61+
Quota: quota,
62+
}
63+
err := LOG_DB.Create(log).Error
64+
if err != nil {
65+
logger.SysError("failed to record log: " + err.Error())
66+
}
67+
}
68+
5469
func RecordConsumeLog(ctx context.Context, userId int, channelId int, promptTokens int, completionTokens int, modelName string, tokenName string, quota int64, content string) {
5570
logger.Info(ctx, fmt.Sprintf("record consume log: userId=%d, channelId=%d, promptTokens=%d, completionTokens=%d, modelName=%s, tokenName=%s, quota=%d, content=%s", userId, channelId, promptTokens, completionTokens, modelName, tokenName, quota, content))
5671
if !config.LogConsumeEnabled {

router/api-router.go

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ func SetApiRouter(router *gin.Engine) {
2626
apiRouter.GET("/oauth/wechat", middleware.CriticalRateLimit(), controller.WeChatAuth)
2727
apiRouter.GET("/oauth/wechat/bind", middleware.CriticalRateLimit(), middleware.UserAuth(), controller.WeChatBind)
2828
apiRouter.GET("/oauth/email/bind", middleware.CriticalRateLimit(), middleware.UserAuth(), controller.EmailBind)
29+
apiRouter.POST("/topup", middleware.AdminAuth(), controller.AdminTopUp)
2930

3031
userRoute := apiRouter.Group("/user")
3132
{

0 commit comments

Comments
 (0)