xuzhiheng 4 năm trước cách đây
mục cha
commit
ed66034560
3 tập tin đã thay đổi với 20 bổ sung20 xóa
  1. 2 7
      middleware/auth.go
  2. 15 11
      model/product.go
  3. 3 2
      service/user.go

+ 2 - 7
middleware/auth.go

@@ -100,12 +100,7 @@ func TokenAuth() gin.HandlerFunc {
 		db.GetSFISDB().First(userProduct, &model.UserProduct{AppID: appID, ProductID: productID})
 		//校验是否过期
 		end := userProduct.EndAt
-		loc, _ := time.LoadLocation("Local")
-		endTimes := int64(0)
-		if endTime, err := time.ParseInLocation("2006-01-02 15:04:05", end, loc); err == nil {
-			endTimes = endTime.Unix()
-		}
-		if now > endTimes {
+		if now > end.Unix() {
 			response.FailWithDetailed(response.InterfaceExpired, nil, "剩余量已过期", context)
 			context.Abort()
 			return
@@ -113,7 +108,7 @@ func TokenAuth() gin.HandlerFunc {
 		//校验每日调用上限
 		limittodaykey := fmt.Sprintf("limittoday_%d_%d_%s", time.Now().Day(), productID, appID)
 		limittoday := redis.GetInt("limit", limittodaykey)
-		if limittoday > userProduct.CallTimesLimitDay { //当天调用超过次数
+		if limittoday >= userProduct.CallTimesLimitDay { //当天调用超过次数
 			response.FailWithDetailed(response.MoreThanEveryDayDataNumberLimit, nil, "请求超过每日调用总量限制", context)
 			context.Abort()
 			return

+ 15 - 11
model/product.go

@@ -1,5 +1,9 @@
 package model
 
+import (
+	"time"
+)
+
 type Product struct {
 	BaseModel
 	ID          int    `json:"id" form:"id" gorm:"primaryKey" binding:"required"`
@@ -16,17 +20,17 @@ func (p *Product) TableName() string {
 }
 
 type UserProduct struct {
-	ID                   int    `json:"id" gorm:"primaryKey"`
-	AppID                string `json:"app_id"`
-	ProductID            int    `json:"product_id"`
-	CreateAt             string `json:"-" gorm:"autoCreateTime"` //标签autoCreateTime设置如果字段名字不为CreatAt时候自动插入当前时间
-	StartAt              string `json:"start_at" grom:"start_at"`
-	EndAt                string `json:"end_at" grom:"end_at"`
-	LeftNum              int    `json:"left_num"`                 //剩余量  加锁处理
-	CostModel            int    `json:"cost_model"`               //扣费模式(0-按剩余量扣,1-按账户余额扣,2-优先扣剩余量,量为0扣余额)
-	InterfaceStatus      int    `json:"interface_status"`         //接口状态(0开启|-1停用|-2异常|-3维护)
-	CallTimesLimitDay    int    `json:"call_times_limit_day"`     //接口调用次数每日上限
-	DataNumLimitOneTimes int    `json:"data_num_limit_one_times"` //接口每次返回数据量上限
+	ID                   int       `json:"id" gorm:"primaryKey"`
+	AppID                string    `json:"app_id"`
+	ProductID            int       `json:"product_id"`
+	CreateAt             time.Time `json:"-" gorm:"autoCreateTime"` //标签autoCreateTime设置如果字段名字不为CreatAt时候自动插入当前时间
+	StartAt              time.Time `json:"start_at" grom:"start_at"`
+	EndAt                time.Time `json:"end_at" grom:"end_at"`
+	LeftNum              int       `json:"left_num"`                 //剩余量  加锁处理
+	CostModel            int       `json:"cost_model"`               //扣费模式(0-按剩余量扣,1-按账户余额扣,2-优先扣剩余量,量为0扣余额)
+	InterfaceStatus      int       `json:"interface_status"`         //接口状态(0开启|-1停用|-2异常|-3维护)
+	CallTimesLimitDay    int       `json:"call_times_limit_day"`     //接口调用次数每日上限
+	DataNumLimitOneTimes int       `json:"data_num_limit_one_times"` //接口每次返回数据量上限
 }
 
 func (p *UserProduct) TableName() string {

+ 3 - 2
service/user.go

@@ -7,6 +7,7 @@ import (
 	"sfis/model/response"
 	"strconv"
 	"strings"
+	"time"
 
 	"github.com/gin-gonic/gin"
 	"gorm.io/gorm"
@@ -19,8 +20,8 @@ func UserProject(projectIds, appId, startTime, endTime string, leftNum, costMode
 		for _, v := range projectIdsArr {
 			userProject := &model.UserProduct{}
 			userProject.AppID = appId
-			userProject.StartAt = startTime
-			userProject.EndAt = endTime
+			userProject.StartAt, _ = time.ParseInLocation("2006-01-02 15:04:05", startTime, time.Local)
+			userProject.EndAt, _ = time.ParseInLocation("2006-01-02 15:04:05", endTime, time.Local)
 			userProject.LeftNum = leftNum
 			userProject.CostModel = costModel
 			userProject.InterfaceStatus = interfaceStatus