This commit is contained in:
孟帅
2022-02-25 17:11:17 +08:00
parent 9bd05abb2c
commit 8f3d679a57
897 changed files with 95731 additions and 0 deletions

View File

@@ -0,0 +1,65 @@
//
// @Link https://github.com/bufanyun/hotgo
// @Copyright Copyright (c) 2022 HotGo CLI
// @Author  Ms <133814250@qq.com>
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
//
package utils
import (
"context"
"github.com/gogf/gf/v2/frame/g"
)
// 权限认证类
var Auth = new(auth)
type auth struct{}
//
//  @Title  是否是不需要验证权限的路由地址
//  @Description
//  @Author  Ms <133814250@qq.com>
//  @Param   ctx
//  @Param   path
//  @Return  bool
//
func (util *auth) IsExceptAuth(ctx context.Context, path string) bool {
var pathList []string
except, _ := g.Cfg().Get(ctx, "router.admin.exceptAuth")
pathList = except.Strings()
for i := 0; i < len(pathList); i++ {
if Charset.IsExists(pathList[i], path) {
return true
}
}
return false
}
//
//  @Title  是否是不需要登录的路由地址
//  @Description
//  @Author  Ms <133814250@qq.com>
//  @Param   ctx
//  @Param   path
//  @Return  bool
//
func (util *auth) IsExceptLogin(ctx context.Context, path string) bool {
var pathList []string
except, _ := g.Cfg().Get(ctx, "router.admin.exceptLogin")
pathList = except.Strings()
for i := 0; i < len(pathList); i++ {
if Charset.IsExists(pathList[i], path) {
return true
}
}
return false
}

View File

@@ -0,0 +1,121 @@
//
// @Link https://github.com/bufanyun/hotgo
// @Copyright Copyright (c) 2022 HotGo CLI
// @Author  Ms <133814250@qq.com>
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
//
package utils
import (
"crypto/md5"
"crypto/rand"
"fmt"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/text/gstr"
"github.com/gogf/gf/v2/util/gconv"
r "math/rand"
"time"
)
// 字符类
var Charset = new(charset)
type charset struct{}
//
//  @Title  获取map的所有key字串符类型
//  @Description
//  @Author  Ms <133814250@qq.com>
//  @Param   m
//  @Return  []string
//
func (util *charset) GetMapKeysByString(m map[string]string) []string {
// 数组默认长度为map长度,后面append时,不需要重新申请内存和拷贝,效率很高
j := 0
keys := make([]string, len(m))
for k := range m {
keys[j] = k
j++
}
return keys
}
//
//  @Title  生成md5
//  @Description
//  @Author  Ms <133814250@qq.com>
//  @Param   str
//  @Return  string
//
func (util *charset) Md5ToString(str string) string {
md5str := fmt.Sprintf("%x", md5.Sum([]byte(str)))
return md5str
}
//
//  @Title  生成随机字串符
//  @Description
//  @Author  Ms <133814250@qq.com>
//  @Param   n
//  @Param   alphabets
//  @Return  []byte
//
func (util *charset) RandomCreateBytes(n int, alphabets ...byte) []byte {
if len(alphabets) == 0 {
alphabets = []byte(`0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz`)
}
var bytes = make([]byte, n)
var randBy bool
r.Seed(time.Now().UnixNano())
if num, err := rand.Read(bytes); num != n || err != nil {
randBy = true
}
for i, b := range bytes {
if randBy {
bytes[i] = alphabets[r.Intn(len(alphabets))]
} else {
bytes[i] = alphabets[b%byte(len(alphabets))]
}
}
return bytes
}
//
//  @Title  格式化错误的堆栈信息
//  @Description
//  @Author  Ms <133814250@qq.com>
//  @Param   err
//  @Return  []string
//
func (util *charset) GetStack(err error) []string {
stackList := gstr.Split(gerror.Stack(err), "\n")
for i := 0; i < len(stackList); i++ {
stackList[i] = gstr.Replace(stackList[i], "\t", "--> ")
}
return stackList
}
//
//  @Title  判断字符或切片字符是否存在指定字符
//  @Description
//  @Author  Ms <133814250@qq.com>
//  @Param   elems
//  @Param   search
//  @Return  bool
//
func (util *charset) IsExists(elems interface{}, search string) bool {
switch elems.(type) {
case []string:
elem := gconv.Strings(elems)
for i := 0; i < len(elem); i++ {
if gconv.String(elem[i]) == search {
return true
}
}
default:
return gconv.String(elems) == search
}
return false
}

View File

@@ -0,0 +1,87 @@
//
// @Link https://github.com/bufanyun/hotgo
// @Copyright Copyright (c) 2022 HotGo CLI
// @Author  Ms <133814250@qq.com>
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
//
package utils
import (
"fmt"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/util/gconv"
"github.com/xuri/excelize/v2"
"net/url"
"reflect"
)
// 字符类
var Excel = new(excel)
type excel struct{}
func (util *excel) ExportByStruct(w *ghttp.ResponseWriter, titleList []string, data []interface{}, fileName string, sheetName string) error {
f := excelize.NewFile()
f.SetSheetName("Sheet1", sheetName)
header := make([]string, 0)
for _, v := range titleList {
header = append(header, v)
}
rowStyleID, _ := f.NewStyle(`{"font":{"color":"#666666","size":13,"family":"arial"},"alignment":{"vertical":"center","horizontal":"center"}}`)
_ = f.SetSheetRow(sheetName, "A1", &header)
_ = f.SetRowHeight("Sheet1", 1, 30)
length := len(titleList)
headStyle := util.letter(length)
var lastRow string
var widthRow string
for k, v := range headStyle {
if k == length-1 {
lastRow = fmt.Sprintf("%s1", v)
widthRow = v
}
}
if err := f.SetColWidth(sheetName, "A", widthRow, 30); err != nil {
return err
}
rowNum := 1
for _, v := range data {
t := reflect.TypeOf(v)
value := reflect.ValueOf(v)
row := make([]interface{}, 0)
for l := 0; l < t.NumField(); l++ {
val := value.Field(l).Interface()
row = append(row, val)
}
rowNum++
err := f.SetSheetRow(sheetName, "A"+gconv.String(rowNum), &row)
_ = f.SetCellStyle(sheetName, fmt.Sprintf("A%d", rowNum), fmt.Sprintf("%s", lastRow), rowStyleID)
if err != nil {
return err
}
}
disposition := fmt.Sprintf("attachment; filename=%s.xlsx", url.QueryEscape(fileName))
w.Header().Set("Content-Type", "application/octet-stream")
w.Header().Set("Content-Disposition", disposition)
w.Header().Set("Content-Transfer-Encoding", "binary")
w.Header().Set("Access-Control-Expose-Headers", "Content-Disposition")
err := f.Write(w)
if err != nil {
return err
}
return nil
}
// Letter 遍历a-z
func (util *excel) letter(length int) []string {
var str []string
for i := 0; i < length; i++ {
str = append(str, string(rune('A'+i)))
}
return str
}

View File

@@ -0,0 +1,17 @@
package utils
// 过滤类
var Filter = new(filter)
type filter struct{}
//func (util *filter) RangeDate(startTime string, endTime string) map[string]interface{} {
// g.Map{
// "uid <=" : 1000,
// "age >=" : 18,
// }
// return g.Map{
// "uid <=" : 1000,
// "age >=" : 18,
// }
//}

View File

@@ -0,0 +1,76 @@
//
// @Link https://github.com/bufanyun/hotgo
// @Copyright Copyright (c) 2022 HotGo CLI
// @Author  Ms <133814250@qq.com>
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
//
package utils
import (
"sync"
)
// 信号类
var Signal = new(signal)
type signal struct{}
type StopSignal int32
type exitWait struct {
mutex sync.Mutex
wg *sync.WaitGroup
deferFuns []func()
stopSignList []chan StopSignal
}
var exitWaitHandler *exitWait
func init() {
exitWaitHandler = &exitWait{
wg: &sync.WaitGroup{},
}
}
//
//  @Title  退出后等待处理完成
//  @Description 
//  @Author  Ms <133814250@qq.com>
//  @Param   doFun
//
func (util *signal) ExitWaitFunDo(doFun func()) {
exitWaitHandler.wg.Add(1)
defer exitWaitHandler.wg.Done()
if doFun != nil {
doFun()
}
}
//
//  @Title  应用退出后置操作
//  @Description 
//  @Author  Ms <133814250@qq.com>
//  @Param   deferFun
//
func (util *signal) AppDefer(deferFun ...func()) {
exitWaitHandler.mutex.Lock()
defer exitWaitHandler.mutex.Unlock()
for _, funcItem := range deferFun {
if funcItem != nil {
exitWaitHandler.deferFuns = append(exitWaitHandler.deferFuns, funcItem)
}
}
}
//
//  @Title  订阅app退出信号
//  @Description 
//  @Author  Ms <133814250@qq.com>
//  @Param   stopSig
//
func (util *signal) ListenStop(stopSig chan StopSignal) {
exitWaitHandler.mutex.Lock()
defer exitWaitHandler.mutex.Unlock()
exitWaitHandler.stopSignList = append(exitWaitHandler.stopSignList, stopSig)
}

View File

@@ -0,0 +1,30 @@
//
// @Link https://github.com/bufanyun/hotgo
// @Copyright Copyright (c) 2022 HotGo CLI
// @Author  Ms <133814250@qq.com>
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
//
package utils
import (
"net"
)
// 验证类
var Validate = new(validate)
type validate struct{}
//
//  @Title  是否为ipv4
//  @Description 
//  @Author  Ms <133814250@qq.com>
//  @Param   ip
//  @Return  bool
//
func (util *validate) IsIp(ip string) bool {
if net.ParseIP(ip) != nil {
return true
}
return false
}