Ver código fonte

功能完善

yxh 4 anos atrás
pai
commit
325c34ae17
2 arquivos alterados com 51 adições e 31 exclusões
  1. 40 0
      app/common/service/form_token.go
  2. 11 31
      library/response.go

+ 40 - 0
app/common/service/form_token.go

@@ -0,0 +1,40 @@
+/**
+ * 表单令牌
+ * @Company: 云南奇讯科技有限公司
+ * @Author: yxf
+ * @Description:
+ * @Version: 1.0.0
+ * @Date: 2021/9/15 17:47
+ */
+
+package service
+
+import (
+	"github.com/gogf/gf/util/guid"
+	"time"
+)
+
+type formTokenService struct{}
+
+var FormToken = new(formTokenService)
+
+func (s *formTokenService) New(action string) (key string) {
+	key = guid.S()
+	cache := Cache.New()
+	cache.Set(key, action, time.Hour)
+	return key
+}
+
+func (s *formTokenService) Verify(key string, action string) bool {
+	cache := Cache.New()
+	value := cache.Get(key)
+	if value != nil && value == action {
+		return true
+	}
+	return false
+}
+
+func (s *formTokenService) Remove(key string) interface{} {
+	cache := Cache.New()
+	return cache.Remove(key)
+}

+ 11 - 31
library/response.go

@@ -1,11 +1,10 @@
 package library
 
 import (
-	"github.com/gogf/gf/frame/g"
+	"fmt"
+	"github.com/gogf/gf/encoding/gurl"
 	"github.com/gogf/gf/net/ghttp"
 	"github.com/gogf/gf/os/gview"
-	"github.com/gogf/gf/text/gstr"
-	"github.com/gogf/gf/util/gconv"
 )
 
 const (
@@ -87,35 +86,16 @@ func (res *Response) FailJson(isExit bool, r *ghttp.Request, msg string, data ..
 
 //模板输出
 func (res *Response) WriteTpl(r *ghttp.Request, tpl string, view *gview.View, params ...gview.Params) error {
-	//绑定模板中需要用到的方法
-	view.BindFuncMap(g.Map{
-		// 根据长度i来切割字符串
-		"subStr": func(str interface{}, i int) (s string) {
-			s1 := gconv.String(str)
-			if gstr.LenRune(s1) > i {
-				s = gstr.SubStrRune(s1, 0, i) + "..."
-				return s
-			}
-			return s1
-		},
-		// 格式化时间戳 年月日
-		"timeFormatYear": func(time interface{}) string {
-			return TimeStampToDate(gconv.Int64(time))
-		},
-		// 格式化时间戳 年月日时分秒
-		"timeFormatDateTime": func(time interface{}) string {
-			return TimeStampToDateTime(gconv.Int64(time))
-		},
-		"add": func(a, b interface{}) int {
-			return gconv.Int(a) + gconv.Int(b)
-		},
-	})
 	//设置全局变量
-	domain, _ := GetDomain(r)
-	view.Assigns(g.Map{
-		"domain": domain,
-	})
-	return r.Response.WriteTpl(tpl, params...)
+	urlInfo, _ := gurl.ParseURL(r.GetUrl(), -1)
+	view.Assign("urlInfo", urlInfo)
+	r.SetView(view)
+	err := r.Response.WriteTpl(tpl, params...)
+	if err != nil {
+		fmt.Println(err.Error())
+		r.Response.WriteExit(err.Error())
+	}
+	return nil
 }
 
 func (res *Response) Redirect(r *ghttp.Request, location string, code ...int) {