|
|
|
|
@ -49,7 +49,7 @@ func queryBlance(key string) (float64, error) {
|
|
|
|
|
userInfoStr, err := Redis.Get(context.Background(), "user:"+key).Result()
|
|
|
|
|
var userInfo UserInfo
|
|
|
|
|
err = json.Unmarshal([]byte(userInfoStr), &userInfo)
|
|
|
|
|
balance, err := Redis.IncrByFloat(context.Background(), "user:"+userInfo.SID+":balance", 0).Result()
|
|
|
|
|
balance, err := Redis.IncrByFloat(context.Background(), "user:"+userInfo.UID+":balance", 0).Result()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, errors.New("余额计算失败")
|
|
|
|
|
}
|
|
|
|
|
@ -83,12 +83,13 @@ func checkBlance(key string, model string) (ServerInfo, error) {
|
|
|
|
|
return serverInfo, errors.New("模型信息解析失败")
|
|
|
|
|
}
|
|
|
|
|
//计算余额-先扣除指定金额
|
|
|
|
|
balance, err := Redis.IncrByFloat(context.Background(), "user:"+userInfo.SID+":balance", -(float64(modelInfo.ModelPrepayment) * modelInfo.ModelPrice)).Result()
|
|
|
|
|
balance, err := Redis.IncrByFloat(context.Background(), "user:"+userInfo.UID+":balance", -(float64(modelInfo.ModelPrepayment) * modelInfo.ModelPrice)).Result()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return serverInfo, errors.New("余额计算失败")
|
|
|
|
|
}
|
|
|
|
|
log.Printf("用户余额 %f key: %v", balance, key)
|
|
|
|
|
if balance < 0 {
|
|
|
|
|
Redis.IncrByFloat(context.Background(), "user:"+userInfo.SID+":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("用户余额不足")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -117,7 +118,7 @@ func consumption(key string, model string, prompt_tokens int, completion_tokens
|
|
|
|
|
if err != nil {
|
|
|
|
|
return "", errors.New("模型信息解析失败")
|
|
|
|
|
}
|
|
|
|
|
Redis.IncrByFloat(context.Background(), "user:"+userInfo.SID+":balance", float64(modelInfo.ModelPrepayment)*modelInfo.ModelPrice-(float64(total_tokens)*modelInfo.ModelPrice)).Result()
|
|
|
|
|
Redis.IncrByFloat(context.Background(), "user:"+userInfo.UID+":balance", float64(modelInfo.ModelPrepayment)*modelInfo.ModelPrice-(float64(total_tokens)*modelInfo.ModelPrice)).Result()
|
|
|
|
|
|
|
|
|
|
// 余额消费日志请求
|
|
|
|
|
result, err := balanceConsumption(key, model, prompt_tokens, completion_tokens, total_tokens, msg_id)
|
|
|
|
|
|