-
Notifications
You must be signed in to change notification settings - Fork 8
/
common.go
114 lines (100 loc) · 2.54 KB
/
common.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package global
import (
"bytes"
"fmt"
"github.com/dchest/captcha"
"github.com/gin-gonic/gin"
"github.com/go-playground/validator/v10"
"github.com/sonhineboy/gsadmin/service/config"
"github.com/sonhineboy/gsadmin/service/src"
"golang.org/x/time/rate"
"gorm.io/gorm"
"net/http"
"reflect"
"time"
)
var (
GAD_R *gin.Engine
GAD_APP_PATH string
Config *config.Config
Db *gorm.DB
SuperAdmin string
EventDispatcher src.EventDispatcher
Limiter *rate.Limiter
)
// GetError 获取错误信息
func GetError(errs validator.ValidationErrors, r interface{}) string {
s := reflect.TypeOf(r)
for _, fieldError := range errs {
filed, _ := s.FieldByName(fieldError.Field())
errTag := fieldError.Tag() + "_msg"
// 获取对应binding得错误消息
errTagText := filed.Tag.Get(errTag)
// 获取统一错误消息
errText := filed.Tag.Get("msg")
if errTagText != "" {
return errTagText
}
if errText != "" {
return errText
}
return fieldError.Field() + ":" + fieldError.Tag()
}
return ""
}
// Pages 通用分页
func Pages(page int, pageSize int, total int, rows interface{}) map[string]interface{} {
var data = make(map[string]interface{})
data["page"] = page
data["pageSize"] = pageSize
data["rows"] = rows
data["total"] = total
return data
}
// IsSuperAdmin 即将废弃,请勿使用
func IsSuperAdmin(roles []string, role string) bool {
for _, v := range roles {
if v == role {
return true
}
}
return false
}
// CaptchaServe 验证码
func CaptchaServe(w http.ResponseWriter, r *http.Request, id, ext, lang string, download bool, width, height int) error {
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
w.Header().Set("Pragma", "no-cache")
w.Header().Set("Expires", "0")
var content bytes.Buffer
switch ext {
case ".png":
w.Header().Set("Content-Type", "image/png")
err := captcha.WriteImage(&content, id, width, height)
if err != nil {
println(err.Error())
}
case ".wav":
w.Header().Set("Content-Type", "audio/x-wav")
_ = captcha.WriteAudio(&content, id, lang)
default:
return captcha.ErrNotFound
}
if download {
w.Header().Set("Content-Type", "application/octet-stream")
}
http.ServeContent(w, r, id+ext, time.Time{}, bytes.NewReader(content.Bytes()))
return nil
}
func GetEventDispatcher(c *gin.Context) *src.EventDispatcher {
v, ok := c.Get("e")
if ok == false {
fmt.Print("无法获取对象")
return nil
}
e, ok := v.(src.EventDispatcher)
if ok == false {
fmt.Print("类型不正确")
return nil
}
return &e
}