package middleware

import (
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
)

func generateDuplicateKey(r *ghttp.Request) string {
	// 用请求的uri和
	return r.RequestURI + r.GetRemoteIp()
}

func DuplicateVerify(r *ghttp.Request) {
	key := generateDuplicateKey(r)
	v, _ := g.Redis().DoVar("GET", key)
	if v.Int() != 0 {
		r.Response.WriteJson(g.Map{
			"code": 500,
			"msg": "操作过于频繁",
		})

		return
	}

	g.Redis().Do("SETEX", key, 1, 1)

	r.Middleware.Next()
}