fuwencai vor 4 Jahren
Ursprung
Commit
51e99b4a35

+ 0 - 60
api/v1/userRecharge.go

@@ -1,60 +0,0 @@
-package v1
-
-import (
-	"encoding/json"
-	"sfbase/global"
-	"sfis/model/response"
-	"sfis/service"
-	"strconv"
-
-	"github.com/gin-gonic/gin"
-	"go.uber.org/zap"
-)
-
-//充值相关接口服务
-func RechargeApiRegister(router *gin.Engine) {
-	routerGroup := router.Group("/sfis/api/v1/user/")
-	routerGroup.Use()
-	{
-		routerGroup.POST("/moneyRecharge", moneyRecharge)
-		routerGroup.POST("/productRecharge", productRecharge)
-	}
-}
-
-//余额充值接口
-func moneyRecharge(c *gin.Context) {
-	appid := c.PostForm("appid")
-	money, err := strconv.Atoi(c.PostForm("money"))
-	if err != nil {
-		response.FailWithDetailed(response.ParamError, nil, "参数错误", c)
-	}
-	p := gin.H{
-		"appid": appid,
-		"money": money,
-	}
-	bs, _ := json.Marshal(p)
-	param := string(bs)
-	global.Logger.Info("api moneyRecharge:", zap.Any("param:", param))
-	service.MoneyRecharge(appid, money, c)
-}
-
-//产品剩余量充值接口
-func productRecharge(c *gin.Context) {
-	appid := c.PostForm("appid")
-	productId, err := strconv.Atoi(c.PostForm("productId"))
-	rechargeNum, errs := strconv.Atoi(c.PostForm("rechargeNum"))
-	endTime := c.PostForm("endTime")
-	if err != nil || errs != nil {
-		response.FailWithDetailed(response.ParamError, nil, "参数错误", c)
-	}
-	p := gin.H{
-		"appid":       appid,
-		"productId":   productId,
-		"rechargeNum": rechargeNum,
-		"endTime":     endTime,
-	}
-	bs, _ := json.Marshal(p)
-	param := string(bs)
-	global.Logger.Info("api productRecharge:", zap.Any("param:", param))
-	service.ProductRecharge(appid, productId, rechargeNum, endTime, c)
-}

+ 5 - 2
manage/user/user.go

@@ -2,11 +2,12 @@ package user
 
 import (
 	"encoding/json"
-	"github.com/gin-gonic/gin"
-	"go.uber.org/zap"
 	"log"
 	"sfbase/global"
 
+	"github.com/gin-gonic/gin"
+	"go.uber.org/zap"
+
 	sutils "sfbase/utils"
 	"sfis/model"
 	"sfis/model/response"
@@ -23,6 +24,8 @@ func DevUserManageRegister(router *gin.Engine) {
 		userGroup.POST("/create", userCreate)
 		userGroup.POST("/userProductChoose", userProductChoose)
 		userGroup.POST("/userProductList", userProductList)
+		userGroup.POST("/moneyRecharge", moneyRecharge)
+		userGroup.POST("/productRecharge", productRecharge)
 	}
 }
 

+ 0 - 26
middleware/auth.go

@@ -3,10 +3,7 @@ package middleware
 import (
 	"fmt"
 	"sfbase/global"
-	"sfbase/redis"
 	sutils "sfbase/utils"
-	"sfis/db"
-	"sfis/model"
 	"sfis/model/response"
 	"sfis/utils"
 	"strconv"
@@ -95,29 +92,6 @@ func TokenAuth() gin.HandlerFunc {
 			context.Abort()
 			return
 		}
-
-		userProduct := &model.UserProduct{}
-		db.GetSFISDB().First(userProduct, &model.UserProduct{AppID: appID, ProductID: productID})
-		//校验是否过期
-		end := userProduct.EndAt
-		if now > end.Unix() {
-			response.FailWithDetailed(response.InterfaceExpired, nil, "剩余量已过期", context)
-			context.Abort()
-			return
-		}
-		//校验每日调用上限
-		limittodaykey := fmt.Sprintf("limittoday_%d_%d_%s", time.Now().Day(), productID, appID)
-		limittoday := redis.GetInt("limit", limittodaykey)
-		if limittoday >= userProduct.CallTimesLimitDay { //当天调用超过次数
-			response.FailWithDetailed(response.MoreThanEveryDayDataNumberLimit, nil, "请求超过每日调用总量限制", context)
-			context.Abort()
-			return
-		} else {
-			if limittoday == 0 {
-				_, max := sutils.GetDayMinMax(time.Now())
-				redis.Put("limit", limittodaykey, 0, int(max-now))
-			}
-		}
 		context.Set("appID", appID)
 		context.Set("productID", productID)
 		context.Set("requestIP", requestIP)

+ 5 - 6
model/response/response.go

@@ -20,11 +20,11 @@ const (
 	QueryError                   int = 299 //系统查询异常,请联系客服
 
 	//系统级错误码
-	InValidKey    = 101 //当前KEY无效
-	RemainingLack = 102 //当前KEY余额|余量不足
-	DeleteKey     = 103 //当前Key被暂停使用,请联系管理员
-	TokenInvalid  = 104 //身份验证错误
-	TokenExpired  = 107 //身份验证已过期
+	InValidKey    = 101  //当前KEY无效
+	RemainingLack = 102  //当前KEY余额|余量不足
+	DeleteKey     = 103  //当前Key被暂停使用,请联系管理员
+	TokenInvalid  = 4000 //身份验证错误
+	TokenExpired  = 4002 //身份验证已过期
 
 	//105非法请求过多,请联系管理员
 	IpInvalid                       = 108 //被禁止的IP
@@ -35,7 +35,6 @@ const (
 	InterfaceDeleted                = 112 //接口已停用,请联系管理员
 	MoreThanEveryDayDataNumberLimit = 113 //请求超过每日调用总量限制
 	OtherError                      = 199 //系统未知错误,请联系技术客服
-	//
 )
 
 type Response struct {

+ 0 - 1
router/route.go

@@ -17,7 +17,6 @@ func InitRouter(middleware ...gin.HandlerFunc) *gin.Engine {
 		})
 	})
 	v1.ProjectApiRegister(router)
-	v1.RechargeApiRegister(router)
 	user.DevUserManageRegister(router)
 	product.ProductManageRegister(router)
 	return router

+ 5 - 18
service/userRecharge.go

@@ -1,11 +1,9 @@
 package service
 
 import (
-	"sfbase/global"
 	"sfis/db"
 	"sfis/lock"
 	"sfis/model"
-	"sfis/model/response"
 
 	"github.com/gin-gonic/gin"
 
@@ -13,11 +11,10 @@ import (
 	"sfis/utils"
 	"time"
 
-	"go.uber.org/zap"
 	"gorm.io/gorm"
 )
 
-func MoneyRecharge(appid string, money int, context *gin.Context) {
+func MoneyRecharge(appid string, money int, context *gin.Context) error {
 	//取出用户锁
 	lock.MainLock.Lock()
 	userLock := lock.UserLockMap[appid]
@@ -46,15 +43,10 @@ func MoneyRecharge(appid string, money int, context *gin.Context) {
 		}
 		return nil
 	})
-	if errs == nil {
-		response.Ok(context)
-	} else {
-		global.Logger.Error("数据库操作失败", zap.Any("error:", errs))
-		response.FailWithMessage("充值失败", context)
-	}
+	return errs
 }
 
-func ProductRecharge(appid string, productId, rechargeNum int, endTime string, context *gin.Context) {
+func ProductRecharge(appid string, productId, rechargeNum int, endTime string, context *gin.Context) error {
 	//取出用户锁
 	lock.MainLock.Lock()
 	userLock := lock.UserLockMap[appid]
@@ -96,7 +88,7 @@ func ProductRecharge(appid string, productId, rechargeNum int, endTime string, c
 				return err
 			}
 		}
-		//生充值记录
+		//生购买记录
 		err = tx.Exec("insert into user_buy_record (app_id,product_id,user_product_id,`before`,`after`,trade_money,buy_type,history_unit_price) values (?,?,?,?,?,?,?,?)", appid, productId, userProduct.ID, before, after, product.UnitPrice*rechargeNum, 1, product.UnitPrice).Error
 		if err != nil {
 			log.Printf("appID:[%s],product_id:[%d],user_product_id:[%d],after:[%d],trade_money:[%d] execute insert into user_buy_record error:[%v]", appid, productId, userProduct.ID, after, product.UnitPrice*rechargeNum, err)
@@ -105,10 +97,5 @@ func ProductRecharge(appid string, productId, rechargeNum int, endTime string, c
 		}
 		return nil
 	})
-	if errs == nil {
-		response.Ok(context)
-	} else {
-		global.Logger.Error("数据库操作失败", zap.Any("error:", errs))
-		response.FailWithMessage("充值失败", context)
-	}
+	return errs
 }

+ 5 - 1
sword_base/elastic/elasticSim.go

@@ -2,6 +2,7 @@ package elastic
 
 import (
 	"encoding/json"
+	"errors"
 	"fmt"
 	"log"
 	"runtime"
@@ -104,7 +105,7 @@ func (e *Elastic) Get(index, itype, query string) (*[]map[string]interface{}, er
 		searchResult, err := client.Search().Index(index).Type(itype).Source(query).Do()
 		if err != nil {
 			log.Println("从ES查询出错", err.Error())
-			return nil, err
+			return nil, errors.New("Es查询出错")
 		}
 		if searchResult.Hits != nil {
 			resNum := len(searchResult.Hits.Hits)
@@ -117,9 +118,12 @@ func (e *Elastic) Get(index, itype, query string) (*[]map[string]interface{}, er
 					}
 				}
 			} else {
+				return nil, errors.New("查询数量超过限制")
 				log.Println("查询结果太多,查询到:", resNum, "条")
 			}
 		}
+	} else {
+		return nil, errors.New("Es查询出错")
 	}
 	return &res, nil
 }

+ 20 - 0
utils/api_util.go

@@ -6,6 +6,7 @@ import (
 	"log"
 	"sfbase/global"
 	"sfbase/redis"
+	"sfbase/utils"
 	"sfis/db"
 	"sfis/lock"
 	"sfis/model"
@@ -40,6 +41,25 @@ func Check(appID string, productID int, c *gin.Context, getData func() ([]map[st
 		response.FailWithDetailed(response.InterfaceDeleted, nil, "该用户接口暂不提供服务", c)
 		return
 	}
+	//校验是否过期
+	now := time.Now().Unix()
+	end := userProduct.EndAt
+	if now > end.Unix() {
+		response.FailWithDetailed(response.InterfaceExpired, nil, "剩余量已过期", c)
+		return
+	}
+	//校验每日调用上限
+	limittodaykey := fmt.Sprintf("limittoday_%d_%d_%s", time.Now().Day(), productID, appID)
+	limittoday := redis.GetInt("limit", limittodaykey)
+	if limittoday >= userProduct.CallTimesLimitDay { //当天调用超过次数
+		response.FailWithDetailed(response.MoreThanEveryDayDataNumberLimit, nil, "请求超过每日调用总量限制", c)
+		return
+	} else {
+		if limittoday == 0 {
+			_, max := utils.GetDayMinMax(time.Now())
+			redis.Put("limit", limittodaykey, 0, int(max-now))
+		}
+	}
 	//2.2 取用户(产品余量|钱包账户余额)校验-必须加锁
 	costModel := userProduct.CostModel //扣费模式 0扣余量,1-扣余额
 	product := GetProductByID(productID)