Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
new-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
70370e40
authored
Jun 09, 2023
by
JustSong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: able to check topup history & consumption history (#78, #95)
parent
3cc22de8
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
136 additions
and
0 deletions
+136
-0
common/constants.go
+1
-0
controller/log.go
+86
-0
model/log.go
+44
-0
router/api-router.go
+5
-0
No files found.
common/constants.go
View file @
70370e40
...
@@ -25,6 +25,7 @@ var OptionMap map[string]string
...
@@ -25,6 +25,7 @@ var OptionMap map[string]string
var
OptionMapRWMutex
sync
.
RWMutex
var
OptionMapRWMutex
sync
.
RWMutex
var
ItemsPerPage
=
10
var
ItemsPerPage
=
10
var
MaxRecentItems
=
100
var
PasswordLoginEnabled
=
true
var
PasswordLoginEnabled
=
true
var
PasswordRegisterEnabled
=
true
var
PasswordRegisterEnabled
=
true
...
...
controller/log.go
0 → 100644
View file @
70370e40
package
controller
import
(
"github.com/gin-gonic/gin"
"one-api/common"
"one-api/model"
"strconv"
)
func
GetAllLogs
(
c
*
gin
.
Context
)
{
p
,
_
:=
strconv
.
Atoi
(
c
.
Query
(
"p"
))
if
p
<
0
{
p
=
0
}
logType
,
_
:=
strconv
.
Atoi
(
c
.
Query
(
"type"
))
logs
,
err
:=
model
.
GetAllLogs
(
logType
,
p
*
common
.
ItemsPerPage
,
common
.
ItemsPerPage
)
if
err
!=
nil
{
c
.
JSON
(
200
,
gin
.
H
{
"success"
:
false
,
"message"
:
err
.
Error
(),
})
return
}
c
.
JSON
(
200
,
gin
.
H
{
"success"
:
true
,
"message"
:
""
,
"data"
:
logs
,
})
}
func
GetUserLogs
(
c
*
gin
.
Context
)
{
p
,
_
:=
strconv
.
Atoi
(
c
.
Query
(
"p"
))
if
p
<
0
{
p
=
0
}
userId
:=
c
.
GetInt
(
"id"
)
logType
,
_
:=
strconv
.
Atoi
(
c
.
Query
(
"type"
))
logs
,
err
:=
model
.
GetUserLogs
(
userId
,
logType
,
p
*
common
.
ItemsPerPage
,
common
.
ItemsPerPage
)
if
err
!=
nil
{
c
.
JSON
(
200
,
gin
.
H
{
"success"
:
false
,
"message"
:
err
.
Error
(),
})
return
}
c
.
JSON
(
200
,
gin
.
H
{
"success"
:
true
,
"message"
:
""
,
"data"
:
logs
,
})
}
func
SearchAllLogs
(
c
*
gin
.
Context
)
{
keyword
:=
c
.
Query
(
"keyword"
)
logs
,
err
:=
model
.
SearchAllLogs
(
keyword
)
if
err
!=
nil
{
c
.
JSON
(
200
,
gin
.
H
{
"success"
:
false
,
"message"
:
err
.
Error
(),
})
return
}
c
.
JSON
(
200
,
gin
.
H
{
"success"
:
true
,
"message"
:
""
,
"data"
:
logs
,
})
}
func
SearchUserLogs
(
c
*
gin
.
Context
)
{
keyword
:=
c
.
Query
(
"keyword"
)
userId
:=
c
.
GetInt
(
"id"
)
logs
,
err
:=
model
.
SearchUserLogs
(
userId
,
keyword
)
if
err
!=
nil
{
c
.
JSON
(
200
,
gin
.
H
{
"success"
:
false
,
"message"
:
err
.
Error
(),
})
return
}
c
.
JSON
(
200
,
gin
.
H
{
"success"
:
true
,
"message"
:
""
,
"data"
:
logs
,
})
}
model/log.go
0 → 100644
View file @
70370e40
package
model
import
"one-api/common"
type
Log
struct
{
Id
int
`json:"id"`
UserId
int
`json:"user_id" gorm:"index"`
CreatedAt
int64
`json:"created_at" gorm:"bigint"`
Type
int
`json:"type" gorm:"index"`
Content
string
`json:"content"`
}
func
RecordLog
(
userId
int
,
logType
int
,
content
string
)
{
log
:=
&
Log
{
UserId
:
userId
,
CreatedAt
:
common
.
GetTimestamp
(),
Type
:
logType
,
Content
:
content
,
}
err
:=
DB
.
Create
(
log
)
.
Error
if
err
!=
nil
{
common
.
SysError
(
"failed to record log: "
+
err
.
Error
())
}
}
func
GetAllLogs
(
logType
int
,
startIdx
int
,
num
int
)
(
logs
[]
*
Log
,
err
error
)
{
err
=
DB
.
Where
(
"type = ?"
,
logType
)
.
Order
(
"id desc"
)
.
Limit
(
num
)
.
Offset
(
startIdx
)
.
Find
(
&
logs
)
.
Error
return
logs
,
err
}
func
GetUserLogs
(
userId
int
,
logType
int
,
startIdx
int
,
num
int
)
(
logs
[]
*
Log
,
err
error
)
{
err
=
DB
.
Where
(
"user_id = ? and type = ?"
,
userId
,
logType
)
.
Order
(
"id desc"
)
.
Limit
(
num
)
.
Offset
(
startIdx
)
.
Find
(
&
logs
)
.
Error
return
logs
,
err
}
func
SearchAllLogs
(
keyword
string
)
(
logs
[]
*
Log
,
err
error
)
{
err
=
DB
.
Where
(
"type = ? or content LIKE ?"
,
keyword
,
keyword
+
"%"
)
.
Order
(
"id desc"
)
.
Limit
(
common
.
MaxRecentItems
)
.
Find
(
&
logs
)
.
Error
return
logs
,
err
}
func
SearchUserLogs
(
userId
int
,
keyword
string
)
(
logs
[]
*
Log
,
err
error
)
{
err
=
DB
.
Where
(
"user_id = ? and type = ?"
,
userId
,
keyword
)
.
Order
(
"id desc"
)
.
Limit
(
common
.
MaxRecentItems
)
.
Find
(
&
logs
)
.
Error
return
logs
,
err
}
router/api-router.go
View file @
70370e40
...
@@ -93,5 +93,10 @@ func SetApiRouter(router *gin.Engine) {
...
@@ -93,5 +93,10 @@ func SetApiRouter(router *gin.Engine) {
redemptionRoute
.
PUT
(
"/"
,
controller
.
UpdateRedemption
)
redemptionRoute
.
PUT
(
"/"
,
controller
.
UpdateRedemption
)
redemptionRoute
.
DELETE
(
"/:id"
,
controller
.
DeleteRedemption
)
redemptionRoute
.
DELETE
(
"/:id"
,
controller
.
DeleteRedemption
)
}
}
logRoute
:=
apiRouter
.
Group
(
"/log"
)
logRoute
.
GET
(
"/"
,
middleware
.
AdminAuth
(),
controller
.
GetAllLogs
)
logRoute
.
GET
(
"/search"
,
middleware
.
AdminAuth
(),
controller
.
SearchAllLogs
)
logRoute
.
GET
(
"/self"
,
middleware
.
UserAuth
(),
controller
.
GetUserLogs
)
logRoute
.
GET
(
"/self/search"
,
middleware
.
UserAuth
(),
controller
.
SearchUserLogs
)
}
}
}
}
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