Merge branch 'alipay'

This commit is contained in:
RockYang
2023-11-10 16:51:00 +08:00
61 changed files with 4119 additions and 304 deletions

View File

@@ -242,8 +242,7 @@ func (h *ChatHandler) sendAzureMessage(
}
// 更新用户信息
h.db.Model(&model.User{}).Where("id = ?", userVo.Id).
UpdateColumn("total_tokens", gorm.Expr("total_tokens + ?", totalTokens))
h.incUserTokenFee(userVo.Id, totalTokens)
}
// 保存当前会话

View File

@@ -9,7 +9,6 @@ import (
"context"
"encoding/json"
"fmt"
"gorm.io/gorm"
"io"
"net/http"
"strings"
@@ -184,8 +183,7 @@ func (h *ChatHandler) sendBaiduMessage(
logger.Error("failed to save reply history message: ", res.Error)
}
// 更新用户信息
h.db.Model(&model.User{}).Where("id = ?", userVo.Id).
UpdateColumn("total_tokens", gorm.Expr("total_tokens + ?", totalTokens))
h.incUserTokenFee(userVo.Id, totalTokens)
}
// 保存当前会话

View File

@@ -187,8 +187,14 @@ func (h *ChatHandler) sendMessage(ctx context.Context, session *types.ChatSessio
return nil
}
if userVo.Calls < session.Model.Weight {
utils.ReplyMessage(ws, fmt.Sprintf("您当前剩余对话次数(%d已不足以支付当前模型的单次对话需要消耗的对话额度%d", userVo.Calls, session.Model.Weight))
utils.ReplyMessage(ws, ErrImg)
return nil
}
if userVo.Calls <= 0 && userVo.ChatConfig.ApiKeys[session.Model.Platform] == "" {
utils.ReplyMessage(ws, "您的对话次数已经用尽,请联系管理员或者点击左下角菜单加入众筹获得100次对话!")
utils.ReplyMessage(ws, "您的对话次数已经用尽,请联系管理员或者充值点卡继续对话!")
utils.ReplyMessage(ws, ErrImg)
return nil
}
@@ -479,3 +485,10 @@ func (h *ChatHandler) subUserCalls(userVo vo.User, session *types.ChatSession) {
h.db.Model(&model.User{}).Where("id = ?", userVo.Id).UpdateColumn("calls", gorm.Expr("calls - ?", num))
}
}
func (h *ChatHandler) incUserTokenFee(userId uint, tokens int) {
h.db.Model(&model.User{}).Where("id = ?", userId).
UpdateColumn("total_tokens", gorm.Expr("total_tokens + ?", tokens))
h.db.Model(&model.User{}).Where("id = ?", userId).
UpdateColumn("tokens", gorm.Expr("tokens + ?", tokens))
}

View File

@@ -10,7 +10,6 @@ import (
"encoding/json"
"fmt"
"github.com/golang-jwt/jwt/v5"
"gorm.io/gorm"
"io"
"strings"
"time"
@@ -164,8 +163,7 @@ func (h *ChatHandler) sendChatGLMMessage(
logger.Error("failed to save reply history message: ", res.Error)
}
// 更新用户信息
h.db.Model(&model.User{}).Where("id = ?", userVo.Id).
UpdateColumn("total_tokens", gorm.Expr("total_tokens + ?", totalTokens))
h.incUserTokenFee(userVo.Id, totalTokens)
}
// 保存当前会话

View File

@@ -241,8 +241,7 @@ func (h *ChatHandler) sendOpenAiMessage(
}
// 更新用户信息
h.db.Model(&model.User{}).Where("id = ?", userVo.Id).
UpdateColumn("total_tokens", gorm.Expr("total_tokens + ?", totalTokens))
h.incUserTokenFee(userVo.Id, totalTokens)
}
// 保存当前会话

View File

@@ -12,7 +12,6 @@ import (
"encoding/json"
"fmt"
"github.com/gorilla/websocket"
"gorm.io/gorm"
"io"
"net/http"
"net/url"
@@ -227,8 +226,7 @@ func (h *ChatHandler) sendXunFeiMessage(
logger.Error("failed to save reply history message: ", res.Error)
}
// 更新用户信息
h.db.Model(&model.User{}).Where("id = ?", userVo.Id).
UpdateColumn("total_tokens", gorm.Expr("total_tokens + ?", totalTokens))
h.incUserTokenFee(userVo.Id, totalTokens)
}
// 保存当前会话