|
|
|
|
@ -1,7 +1,7 @@
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"api2gpt-mid/common"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"errors"
|
|
|
|
|
"log"
|
|
|
|
|
@ -13,7 +13,7 @@ import (
|
|
|
|
|
func checkKeyAndTimeCount(key string) (int, error) {
|
|
|
|
|
var timeOut = 60 * time.Second
|
|
|
|
|
var timeCount = 30
|
|
|
|
|
userInfoStr, err := Redis.Get(context.Background(), "user:"+key).Result()
|
|
|
|
|
userInfoStr, err := common.RedisGet("user:" + key)
|
|
|
|
|
log.Printf("用户信息 %s", userInfoStr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
//用户不存在
|
|
|
|
|
@ -25,14 +25,14 @@ func checkKeyAndTimeCount(key string) (int, error) {
|
|
|
|
|
//用户状态异常
|
|
|
|
|
return 401, errors.New("40004")
|
|
|
|
|
}
|
|
|
|
|
count, err := Redis.Incr(context.Background(), "user:count:"+key).Result()
|
|
|
|
|
count, err := common.RedisIncr("user:count:" + key)
|
|
|
|
|
log.Printf("用户请求次数 %d", count)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Printf("系统计数器设置异常 %s", err.Error())
|
|
|
|
|
return 500, errors.New("系统计数器设置异常")
|
|
|
|
|
}
|
|
|
|
|
if count == 1 {
|
|
|
|
|
_, err := Redis.Expire(context.Background(), "user:count:"+key, timeOut).Result()
|
|
|
|
|
_, err := common.RedisExpire("user:count:"+key, timeOut)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Printf("系统计数器异常 %s", err.Error())
|
|
|
|
|
return 500, errors.New("系统计数器异常")
|
|
|
|
|
@ -47,10 +47,10 @@ func checkKeyAndTimeCount(key string) (int, error) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func queryBlance(key string) (float64, error) {
|
|
|
|
|
userInfoStr, err := Redis.Get(context.Background(), "user:"+key).Result()
|
|
|
|
|
userInfoStr, err := common.RedisGet("user:" + key)
|
|
|
|
|
var userInfo UserInfo
|
|
|
|
|
err = json.Unmarshal([]byte(userInfoStr), &userInfo)
|
|
|
|
|
balance, err := Redis.IncrByFloat(context.Background(), "user:"+userInfo.UID+":balance", 0).Result()
|
|
|
|
|
balance, err := common.RedisIncrByFloat("user:"+userInfo.UID+":balance", 0)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, errors.New("余额计算失败")
|
|
|
|
|
}
|
|
|
|
|
@ -62,7 +62,7 @@ func checkBlance(key string, model string) (ServerInfo, error) {
|
|
|
|
|
var serverInfo ServerInfo
|
|
|
|
|
|
|
|
|
|
//获取模型价格
|
|
|
|
|
modelPriceStr, err := Redis.Get(context.Background(), "model:"+model).Result()
|
|
|
|
|
modelPriceStr, err := common.RedisGet("model:" + model)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return serverInfo, errors.New("模型信息不存在")
|
|
|
|
|
}
|
|
|
|
|
@ -73,11 +73,11 @@ func checkBlance(key string, model string) (ServerInfo, error) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//获取用户信息
|
|
|
|
|
userInfoStr, err := Redis.Get(context.Background(), "user:"+key).Result()
|
|
|
|
|
userInfoStr, err := common.RedisGet("user:" + key)
|
|
|
|
|
var userInfo UserInfo
|
|
|
|
|
err = json.Unmarshal([]byte(userInfoStr), &userInfo)
|
|
|
|
|
//获取服务器信息
|
|
|
|
|
serverInfoStr, err := Redis.Get(context.Background(), "server:"+strconv.Itoa(modelInfo.ServerId)).Result()
|
|
|
|
|
serverInfoStr, err := common.RedisGet("server:" + strconv.Itoa(modelInfo.ServerId))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return serverInfo, errors.New("服务器信息不存在")
|
|
|
|
|
}
|
|
|
|
|
@ -87,13 +87,13 @@ func checkBlance(key string, model string) (ServerInfo, error) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//计算余额-先扣除指定金额
|
|
|
|
|
balance, err := Redis.IncrByFloat(context.Background(), "user:"+userInfo.UID+":balance", -(float64(modelInfo.ModelPrepayment) * modelInfo.ModelPrice)).Result()
|
|
|
|
|
balance, err := common.RedisIncrByFloat("user:"+userInfo.UID+":balance", -(float64(modelInfo.ModelPrepayment) * modelInfo.ModelPrice))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return serverInfo, errors.New("余额计算失败")
|
|
|
|
|
}
|
|
|
|
|
log.Printf("用户余额 %f key: %v 预扣了:%f", balance, key, (float64(modelInfo.ModelPrepayment) * modelInfo.ModelPrice))
|
|
|
|
|
if balance < 0 {
|
|
|
|
|
Redis.IncrByFloat(context.Background(), "user:"+userInfo.UID+":balance", float64(modelInfo.ModelPrepayment)*modelInfo.ModelPrice).Result()
|
|
|
|
|
common.RedisIncrByFloat("user:"+userInfo.UID+":balance", float64(modelInfo.ModelPrepayment)*modelInfo.ModelPrice)
|
|
|
|
|
return serverInfo, errors.New("用户余额不足")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -105,7 +105,7 @@ func checkBlanceForImages(key string, model string, n int) (ServerInfo, error) {
|
|
|
|
|
var serverInfo ServerInfo
|
|
|
|
|
|
|
|
|
|
//获取模型价格
|
|
|
|
|
modelPriceStr, err := Redis.Get(context.Background(), "model:"+model).Result()
|
|
|
|
|
modelPriceStr, err := common.RedisGet("model:" + model)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return serverInfo, errors.New("模型信息不存在")
|
|
|
|
|
}
|
|
|
|
|
@ -116,11 +116,11 @@ func checkBlanceForImages(key string, model string, n int) (ServerInfo, error) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//获取用户信息
|
|
|
|
|
userInfoStr, err := Redis.Get(context.Background(), "user:"+key).Result()
|
|
|
|
|
userInfoStr, err := common.RedisGet("user:" + key)
|
|
|
|
|
var userInfo UserInfo
|
|
|
|
|
err = json.Unmarshal([]byte(userInfoStr), &userInfo)
|
|
|
|
|
//获取服务器信息
|
|
|
|
|
serverInfoStr, err := Redis.Get(context.Background(), "server:"+strconv.Itoa(modelInfo.ServerId)).Result()
|
|
|
|
|
serverInfoStr, err := common.RedisGet("server:" + strconv.Itoa(modelInfo.ServerId))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return serverInfo, errors.New("服务器信息不存在")
|
|
|
|
|
}
|
|
|
|
|
@ -130,13 +130,13 @@ func checkBlanceForImages(key string, model string, n int) (ServerInfo, error) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//计算余额-先扣除指定金额
|
|
|
|
|
balance, err := Redis.IncrByFloat(context.Background(), "user:"+userInfo.UID+":balance", -(float64(modelInfo.ModelPrepayment*n) * modelInfo.ModelPrice)).Result()
|
|
|
|
|
balance, err := common.RedisIncrByFloat("user:"+userInfo.UID+":balance", -(float64(modelInfo.ModelPrepayment*n) * modelInfo.ModelPrice))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return serverInfo, errors.New("余额计算失败")
|
|
|
|
|
}
|
|
|
|
|
log.Printf("用户余额 %f key: %v 预扣了:%f", balance, key, (float64(modelInfo.ModelPrepayment*n) * modelInfo.ModelPrice))
|
|
|
|
|
if balance < 0 {
|
|
|
|
|
Redis.IncrByFloat(context.Background(), "user:"+userInfo.UID+":balance", float64(modelInfo.ModelPrepayment*n)*modelInfo.ModelPrice).Result()
|
|
|
|
|
common.RedisIncrByFloat("user:"+userInfo.UID+":balance", float64(modelInfo.ModelPrepayment*n)*modelInfo.ModelPrice)
|
|
|
|
|
return serverInfo, errors.New("用户余额不足")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -146,12 +146,12 @@ func checkBlanceForImages(key string, model string, n int) (ServerInfo, error) {
|
|
|
|
|
// 预扣返还
|
|
|
|
|
func checkBlanceReturn(key string, model string) error {
|
|
|
|
|
//获取用户信息
|
|
|
|
|
userInfoStr, err := Redis.Get(context.Background(), "user:"+key).Result()
|
|
|
|
|
userInfoStr, err := common.RedisGet("user:" + key)
|
|
|
|
|
var userInfo UserInfo
|
|
|
|
|
err = json.Unmarshal([]byte(userInfoStr), &userInfo)
|
|
|
|
|
|
|
|
|
|
//获取模型价格
|
|
|
|
|
modelPriceStr, err := Redis.Get(context.Background(), "model:"+model).Result()
|
|
|
|
|
modelPriceStr, err := common.RedisGet("model:" + model)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return errors.New("模型信息不存在")
|
|
|
|
|
}
|
|
|
|
|
@ -160,7 +160,7 @@ func checkBlanceReturn(key string, model string) error {
|
|
|
|
|
if err != nil {
|
|
|
|
|
return errors.New("模型信息解析失败")
|
|
|
|
|
}
|
|
|
|
|
balance, err := Redis.IncrByFloat(context.Background(), "user:"+userInfo.UID+":balance", (float64(modelInfo.ModelPrepayment) * modelInfo.ModelPrice)).Result()
|
|
|
|
|
balance, err := common.RedisIncrByFloat("user:"+userInfo.UID+":balance", (float64(modelInfo.ModelPrepayment) * modelInfo.ModelPrice))
|
|
|
|
|
log.Printf("用户余额 %f key: %v 返还预扣:%f", balance, key, (float64(modelInfo.ModelPrepayment) * modelInfo.ModelPrice))
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
@ -168,11 +168,11 @@ func checkBlanceReturn(key string, model string) error {
|
|
|
|
|
// 预扣返还 for images
|
|
|
|
|
func checkBlanceReturnForImages(key string, model string, n int) error {
|
|
|
|
|
//获取用户信息
|
|
|
|
|
userInfoStr, err := Redis.Get(context.Background(), "user:"+key).Result()
|
|
|
|
|
userInfoStr, err := common.RedisGet("user:" + key)
|
|
|
|
|
var userInfo UserInfo
|
|
|
|
|
err = json.Unmarshal([]byte(userInfoStr), &userInfo)
|
|
|
|
|
//获取模型价格
|
|
|
|
|
modelPriceStr, err := Redis.Get(context.Background(), "model:"+model).Result()
|
|
|
|
|
modelPriceStr, err := common.RedisGet("model:" + model)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return errors.New("模型信息不存在")
|
|
|
|
|
}
|
|
|
|
|
@ -181,7 +181,7 @@ func checkBlanceReturnForImages(key string, model string, n int) error {
|
|
|
|
|
if err != nil {
|
|
|
|
|
return errors.New("模型信息解析失败")
|
|
|
|
|
}
|
|
|
|
|
balance, err := Redis.IncrByFloat(context.Background(), "user:"+userInfo.UID+":balance", (float64(modelInfo.ModelPrepayment*n) * modelInfo.ModelPrice)).Result()
|
|
|
|
|
balance, err := common.RedisIncrByFloat("user:"+userInfo.UID+":balance", (float64(modelInfo.ModelPrepayment*n) * modelInfo.ModelPrice))
|
|
|
|
|
log.Printf("用户余额 %f key: %v 返还预扣:%f", balance, key, (float64(modelInfo.ModelPrepayment*n) * modelInfo.ModelPrice))
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
@ -189,7 +189,7 @@ func checkBlanceReturnForImages(key string, model string, n int) error {
|
|
|
|
|
// 余额消费
|
|
|
|
|
func consumption(key string, model string, prompt_tokens int, completion_tokens int, total_tokens int, msg_id string) (string, error) {
|
|
|
|
|
//获取用户信息
|
|
|
|
|
userInfoStr, err := Redis.Get(context.Background(), "user:"+key).Result()
|
|
|
|
|
userInfoStr, err := common.RedisGet("user:" + key)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", errors.New("用户信息不存在")
|
|
|
|
|
}
|
|
|
|
|
@ -199,7 +199,7 @@ func consumption(key string, model string, prompt_tokens int, completion_tokens
|
|
|
|
|
return "", errors.New("用户信息解析失败")
|
|
|
|
|
}
|
|
|
|
|
//获取模型价格
|
|
|
|
|
modelPriceStr, err := Redis.Get(context.Background(), "model:"+model).Result()
|
|
|
|
|
modelPriceStr, err := common.RedisGet("model:" + model)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", errors.New("模型信息不存在")
|
|
|
|
|
}
|
|
|
|
|
@ -208,7 +208,7 @@ func consumption(key string, model string, prompt_tokens int, completion_tokens
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", errors.New("模型信息解析失败")
|
|
|
|
|
}
|
|
|
|
|
balance, err := Redis.IncrByFloat(context.Background(), "user:"+userInfo.UID+":balance", float64(modelInfo.ModelPrepayment)*modelInfo.ModelPrice-(float64(prompt_tokens)*modelInfo.ModelPrice+float64(completion_tokens)*modelInfo.ModelPrice2)).Result()
|
|
|
|
|
balance, err := common.RedisIncrByFloat("user:"+userInfo.UID+":balance", float64(modelInfo.ModelPrepayment)*modelInfo.ModelPrice-(float64(prompt_tokens)*modelInfo.ModelPrice+float64(completion_tokens)*modelInfo.ModelPrice2))
|
|
|
|
|
|
|
|
|
|
// 余额消费日志请求
|
|
|
|
|
result, err := balanceConsumption(key, model, prompt_tokens, completion_tokens, total_tokens, msg_id)
|
|
|
|
|
@ -223,7 +223,7 @@ func consumption(key string, model string, prompt_tokens int, completion_tokens
|
|
|
|
|
// 余额消费 for images
|
|
|
|
|
func consumptionForImages(key string, model string, n int, dataNum int, msg_id string) (string, error) {
|
|
|
|
|
//获取用户信息
|
|
|
|
|
userInfoStr, err := Redis.Get(context.Background(), "user:"+key).Result()
|
|
|
|
|
userInfoStr, err := common.RedisGet("user:" + key)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", errors.New("用户信息不存在")
|
|
|
|
|
}
|
|
|
|
|
@ -233,7 +233,7 @@ func consumptionForImages(key string, model string, n int, dataNum int, msg_id s
|
|
|
|
|
return "", errors.New("用户信息解析失败")
|
|
|
|
|
}
|
|
|
|
|
//获取模型价格
|
|
|
|
|
modelPriceStr, err := Redis.Get(context.Background(), "model:"+model).Result()
|
|
|
|
|
modelPriceStr, err := common.RedisGet("model:" + model)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", errors.New("模型信息不存在")
|
|
|
|
|
}
|
|
|
|
|
@ -242,7 +242,7 @@ func consumptionForImages(key string, model string, n int, dataNum int, msg_id s
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", errors.New("模型信息解析失败")
|
|
|
|
|
}
|
|
|
|
|
balance, err := Redis.IncrByFloat(context.Background(), "user:"+userInfo.UID+":balance", float64(modelInfo.ModelPrepayment*n)*modelInfo.ModelPrice-(float64(1000*dataNum)*modelInfo.ModelPrice)).Result()
|
|
|
|
|
balance, err := common.RedisIncrByFloat("user:"+userInfo.UID+":balance", float64(modelInfo.ModelPrepayment*n)*modelInfo.ModelPrice-(float64(1000*dataNum)*modelInfo.ModelPrice))
|
|
|
|
|
|
|
|
|
|
// 余额消费日志请求
|
|
|
|
|
result, err := balanceConsumption(key, model, 0, 1000*dataNum, 1000*dataNum, msg_id)
|
|
|
|
|
|