| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- package controller
- import (
- "github.com/gin-gonic/gin"
- "net/http"
- "one-api/common"
- "one-api/model"
- "strconv"
- )
- func GetAllTokens(c *gin.Context) {
- userId := c.GetInt("id")
- p, _ := strconv.Atoi(c.Query("p"))
- if p < 0 {
- p = 0
- }
- tokens, err := model.GetAllUserTokens(userId, p*common.ItemsPerPage, common.ItemsPerPage)
- if err != nil {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": err.Error(),
- })
- return
- }
- c.JSON(http.StatusOK, gin.H{
- "success": true,
- "message": "",
- "data": tokens,
- })
- return
- }
- func SearchTokens(c *gin.Context) {
- userId := c.GetInt("id")
- keyword := c.Query("keyword")
- tokens, err := model.SearchUserTokens(userId, keyword)
- if err != nil {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": err.Error(),
- })
- return
- }
- c.JSON(http.StatusOK, gin.H{
- "success": true,
- "message": "",
- "data": tokens,
- })
- return
- }
- func GetToken(c *gin.Context) {
- id, err := strconv.Atoi(c.Param("id"))
- userId := c.GetInt("id")
- if err != nil {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": err.Error(),
- })
- return
- }
- token, err := model.GetTokenByIds(id, userId)
- if err != nil {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": err.Error(),
- })
- return
- }
- c.JSON(http.StatusOK, gin.H{
- "success": true,
- "message": "",
- "data": token,
- })
- return
- }
- func AddToken(c *gin.Context) {
- token := model.Token{}
- err := c.ShouldBindJSON(&token)
- if err != nil {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": err.Error(),
- })
- return
- }
- if len(token.Name) == 0 || len(token.Name) > 20 {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": "令牌名称长度必须在1-20之间",
- })
- return
- }
- cleanToken := model.Token{
- UserId: c.GetInt("id"),
- Name: token.Name,
- Key: common.GetUUID(),
- CreatedTime: common.GetTimestamp(),
- AccessedTime: common.GetTimestamp(),
- }
- err = cleanToken.Insert()
- if err != nil {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": err.Error(),
- })
- return
- }
- c.JSON(http.StatusOK, gin.H{
- "success": true,
- "message": "",
- })
- return
- }
- func DeleteToken(c *gin.Context) {
- id, _ := strconv.Atoi(c.Param("id"))
- userId := c.GetInt("id")
- err := model.DeleteTokenById(id, userId)
- if err != nil {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": err.Error(),
- })
- return
- }
- c.JSON(http.StatusOK, gin.H{
- "success": true,
- "message": "",
- })
- return
- }
- func UpdateToken(c *gin.Context) {
- userId := c.GetInt("id")
- token := model.Token{}
- err := c.ShouldBindJSON(&token)
- if err != nil {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": err.Error(),
- })
- return
- }
- cleanToken, err := model.GetTokenByIds(token.Id, userId)
- if err != nil {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": err.Error(),
- })
- return
- }
- cleanToken.Name = token.Name
- cleanToken.Status = token.Status
- err = cleanToken.Update()
- if err != nil {
- c.JSON(http.StatusOK, gin.H{
- "success": false,
- "message": err.Error(),
- })
- return
- }
- c.JSON(http.StatusOK, gin.H{
- "success": true,
- "message": "",
- "data": cleanToken,
- })
- return
- }
|