|
|
|
@ -87,7 +87,7 @@ func checkBlance(key string, model string) (ServerInfo, error) {
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return serverInfo, errors.New("余额计算失败")
|
|
|
|
return serverInfo, errors.New("余额计算失败")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.Printf("用户余额 %f key: %v", balance, key)
|
|
|
|
log.Printf("用户余额 %f key: %v 预扣了:%f", balance, key, (float64(modelInfo.ModelPrepayment) * modelInfo.ModelPrice))
|
|
|
|
if balance < 0 {
|
|
|
|
if balance < 0 {
|
|
|
|
Redis.IncrByFloat(context.Background(), "user:"+userInfo.UID+":balance", float64(modelInfo.ModelPrepayment)*modelInfo.ModelPrice).Result()
|
|
|
|
Redis.IncrByFloat(context.Background(), "user:"+userInfo.UID+":balance", float64(modelInfo.ModelPrepayment)*modelInfo.ModelPrice).Result()
|
|
|
|
return serverInfo, errors.New("用户余额不足")
|
|
|
|
return serverInfo, errors.New("用户余额不足")
|
|
|
|
@ -96,6 +96,36 @@ func checkBlance(key string, model string) (ServerInfo, error) {
|
|
|
|
return serverInfo, nil
|
|
|
|
return serverInfo, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func checkBlanceReturn(key string, model string) error {
|
|
|
|
|
|
|
|
var serverInfo ServerInfo
|
|
|
|
|
|
|
|
//获取用户信息
|
|
|
|
|
|
|
|
userInfoStr, err := Redis.Get(context.Background(), "user:"+key).Result()
|
|
|
|
|
|
|
|
var userInfo UserInfo
|
|
|
|
|
|
|
|
err = json.Unmarshal([]byte(userInfoStr), &userInfo)
|
|
|
|
|
|
|
|
//获取服务器信息
|
|
|
|
|
|
|
|
serverInfoStr, err := Redis.Get(context.Background(), "server:"+userInfo.SID).Result()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return errors.New("服务器信息不存在")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal([]byte(serverInfoStr), &serverInfo)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return errors.New("服务器信息解析失败")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//获取模型价格
|
|
|
|
|
|
|
|
modelPriceStr, err := Redis.Get(context.Background(), "model:"+model).Result()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return errors.New("模型信息不存在")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
var modelInfo ModelInfo
|
|
|
|
|
|
|
|
err = json.Unmarshal([]byte(modelPriceStr), &modelInfo)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return errors.New("模型信息解析失败")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
balance, err := Redis.IncrByFloat(context.Background(), "user:"+userInfo.UID+":balance", (float64(modelInfo.ModelPrepayment) * modelInfo.ModelPrice)).Result()
|
|
|
|
|
|
|
|
log.Printf("用户余额 %f key: %v 返还预扣:%f", balance, key, (float64(modelInfo.ModelPrepayment) * modelInfo.ModelPrice))
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 余额消费
|
|
|
|
// 余额消费
|
|
|
|
func consumption(key string, model string, prompt_tokens int, completion_tokens int, total_tokens int, msg_id string) (string, error) {
|
|
|
|
func consumption(key string, model string, prompt_tokens int, completion_tokens int, total_tokens int, msg_id string) (string, error) {
|
|
|
|
//获取用户信息
|
|
|
|
//获取用户信息
|
|
|
|
|