Skip to content

Commit

Permalink
doc: add README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
nianhua99 committed May 12, 2024
1 parent fd51295 commit fb06547
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@
* **账号管理**:在`账号管理`中可以查看所有账号的`Refresh Token``Access Token``Email`
* **刷新Token**:在`账号管理`中点击`刷新`可以刷新`Access Token`**只有你填入了`Refresh Token`才能使用此功能**。程序会在每日凌晨自动刷新。
* **添加账号**:在`账号管理`中点击`新建`,输入`Refresh Token``Access Token`,以及`Email`点击`保存`。请注意,这里的`密码`没有实际作用。
* **用量统计**:统计本账号下各个`Share Token`的用量情况。当前版本暂不可用。
* **用量统计**:统计本账号下各个`Share Token`的用量情况。
* ![1.png](imgs/1.png)
* ![img_3.png](imgs/img_3.png)
### 生成共享账号
`账号管理`中可以生成`Share Token`。点击`共享`列的 + 号,输入`Email``限额`等信息。点击`保存`即可生成`Share Token`
- Unique Name / 密码: 你的伙伴将在本系统的 /login 页面使用Unique Name和这个密码登录。
Expand Down
Binary file added imgs/img_3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions internal/handler/share.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,19 @@ func (h *ShareHandler) SearchShare(ctx *gin.Context) {
}
v1.HandleSuccess(ctx, shareList)
}

func (h *ShareHandler) ShareStatistic(ctx *gin.Context) {
var req struct {
AccountId int `json:"accountId"`
}
if err := ctx.ShouldBindJSON(&req); err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"error": "请求参数错误"})
return
}
shareStatistic, err := h.shareService.ShareStatistic(ctx, req.AccountId)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": "服务器错误"})
return
}
v1.HandleSuccess(ctx, shareStatistic)
}
2 changes: 1 addition & 1 deletion internal/repository/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (r *accountRepository) DeleteAccount(ctx context.Context, id int64) error {

func (r *accountRepository) GetAccount(ctx context.Context, id int64) (*model.Account, error) {
var account model.Account
if err := r.DB(ctx).Where("id = ?", id).First(&account).Error; err != nil {
if err := r.DB(ctx).Preload("Shares").Where("id = ?", id).First(&account).Error; err != nil {
return nil, err
}
return &account, nil
Expand Down
1 change: 1 addition & 0 deletions internal/server/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func NewHTTPServer(
shareAuthRouter.POST("/update", shareHandler.UpdateShare)
shareAuthRouter.POST("/delete", shareHandler.DeleteShare)
shareAuthRouter.POST("/search", shareHandler.SearchShare)
shareAuthRouter.POST("/statistic", shareHandler.ShareStatistic)
}

accountAuthRouter := v1.Group("/account").Use(middleware.StrictAuth(jwt, logger))
Expand Down
78 changes: 78 additions & 0 deletions internal/service/share.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import (
"PandoraHelper/internal/repository"
"context"
"fmt"
"github.com/gin-gonic/gin"
"github.com/go-resty/resty/v2"
"github.com/spf13/viper"
"go.uber.org/zap"
"strconv"
)

type ShareService interface {
Expand All @@ -20,6 +22,7 @@ type ShareService interface {
SearchShare(ctx context.Context, email string, uniqueName string) ([]*model.Share, error)
DeleteShare(ctx context.Context, id int64) error
LoginShareByPassword(ctx context.Context, username string, password string) (string, error)
ShareStatistic(ctx *gin.Context, accountId int) (interface{}, interface{})
}

func NewShareService(service *Service, shareRepository repository.ShareRepository, viper *viper.Viper, coordinator *Coordinator) ShareService {
Expand All @@ -38,6 +41,46 @@ type shareService struct {
accountService AccountService
}

// ShareStatistic 转换为Go语言
func (s *shareService) ShareStatistic(ctx *gin.Context, accountId int) (interface{}, interface{}) {
account, err := s.accountService.GetAccount(ctx, int64(accountId))
if err != nil {
return nil, err
}
shares := account.Shares

uniqueNames := make([]string, 0)
gpt35Counts := make([]int, 0)
gpt4Counts := make([]int, 0)

for _, share := range shares {
uniqueNames = append(uniqueNames, share.UniqueName)
gpt35count, gpt4Count, err := s.GetShareTokenInfo(share.ShareToken, account.AccessToken)
if err != nil {
return nil, nil
}
gpt35Counts = append(gpt35Counts, gpt35count)
gpt4Counts = append(gpt4Counts, gpt4Count)
}

series := []map[string]interface{}{
{
"name": "GPT-3.5",
"data": gpt35Counts,
},
{
"name": "GPT-4",
"data": gpt4Counts,
},
}

return map[string]interface{}{
"categories": uniqueNames,
"series": series,
}, nil

}

func (s *shareService) LoginShareByPassword(ctx context.Context, username string, password string) (string, error) {
share, err := s.shareRepository.GetShareByUniqueName(ctx, username)
if err != nil {
Expand Down Expand Up @@ -164,3 +207,38 @@ func (s *shareService) ResetShareLimit(ctx context.Context, id int64) error {
}
return nil
}

func (s *shareService) GetShareTokenInfo(shareToken string, accessToken string) (int, int, error) {
host := fmt.Sprintf("%s/token/info/%s", s.viper.GetString("pandora.domain.chat"), shareToken)
headers := map[string]string{}
if accessToken != "" {
headers["Authorization"] = fmt.Sprintf("Bearer %s", accessToken)
}
var result struct {
Gpt35Limit string `json:"gpt35_limit"`
Gpt4Limit string `json:"gpt4_limit"`
}
client := resty.New()
resp, err := client.R().
SetHeader("Content-Type", "application/json").
SetHeaders(headers).
SetResult(&result).
Get(host)

if err != nil {
return 0, 0, err
}

// 将字符串转换为整数
gpt35Limit, err := strconv.Atoi(result.Gpt35Limit)
if err != nil {
gpt35Limit = 0
}

gpt4Limit, err := strconv.Atoi(result.Gpt4Limit)
if err != nil {
gpt4Limit = 0
}
s.logger.Info("GetShareTokenInfo resp", zap.Any("resp", resp))
return gpt35Limit, gpt4Limit, nil
}

0 comments on commit fb06547

Please sign in to comment.