代码规范过滤,移除冗余代码,替换掉不推荐的包

This commit is contained in:
孟帅
2023-05-30 12:09:40 +08:00
parent c8a808fcfd
commit b2ef3487d3
74 changed files with 564 additions and 741 deletions

View File

@@ -3,7 +3,6 @@
// @Copyright Copyright (c) 2023 HotGo CLI
// @Author Ms <133814250@qq.com>
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
//
package charset
import (
@@ -30,7 +29,6 @@ func SplitMemberIds(str, pos string) (memberIds []int64) {
for _, memberId := range receiver {
memberIds = append(memberIds, gconv.Int64(strings.TrimSpace(memberId)))
}
return convert.UniqueSliceInt64(memberIds)
}

View File

@@ -3,14 +3,12 @@
// @Copyright Copyright (c) 2023 HotGo CLI
// @Author Ms <133814250@qq.com>
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
//
package convert
import (
"github.com/gogf/gf/v2/errors/gerror"
"hotgo/utility/validate"
"reflect"
"strings"
"unicode"
)
@@ -45,20 +43,7 @@ func UniqueSliceString(languages []string) []string {
return result
}
// UnderlineToUpperCamelCase 下划线单词转为大写驼峰单词
func UnderlineToUpperCamelCase(s string) string {
s = strings.Replace(s, "_", " ", -1)
s = strings.Title(s)
return strings.Replace(s, " ", "", -1)
}
// UnderlineToLowerCamelCase 下划线单词转为小写驼峰单词
func UnderlineToLowerCamelCase(s string) string {
s = UnderlineToUpperCamelCase(s)
return string(unicode.ToLower(rune(s[0]))) + s[1:]
}
//CamelCaseToUnderline 驼峰单词转下划线单词
// CamelCaseToUnderline 驼峰单词转下划线单词
func CamelCaseToUnderline(s string) string {
var output []rune
for i, r := range s {

View File

@@ -27,7 +27,6 @@ func IpFilterStrategy(originIp string) (list map[string]struct{}) {
list[ip] = struct{}{}
}
}
return
}
@@ -51,7 +50,6 @@ func IpFilterStrategy(originIp string) (list map[string]struct{}) {
for i := index; i <= 254; i++ {
list[prefix+gconv.String(i)] = struct{}{}
}
return
}
@@ -97,6 +95,5 @@ func IpFilterStrategy(originIp string) (list map[string]struct{}) {
list[originIp] = struct{}{}
return
}
return list
}

View File

@@ -3,7 +3,6 @@
// @Copyright Copyright (c) 2023 HotGo CLI
// @Author Ms <133814250@qq.com>
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
//
package excel
import (
@@ -34,16 +33,12 @@ func ExportByStructs(ctx context.Context, tags []string, list interface{}, fileN
f := excelize.NewFile()
f.SetSheetName("Sheet1", sheetName)
_ = f.SetRowHeight("Sheet1", 1, 30)
header := make([]string, 0)
for _, v := range tags {
header = append(header, v)
}
rowStyleID, _ := f.NewStyle(defaultRowStyle)
if err != nil {
return
}
_ = f.SetSheetRow(sheetName, "A1", &header)
_ = f.SetSheetRow(sheetName, "A1", &tags)
var (
length = len(tags)
@@ -73,10 +68,10 @@ func ExportByStructs(ctx context.Context, tags []string, list interface{}, fileN
}
rowNum++
if err = f.SetSheetRow(sheetName, "A"+gconv.String(rowNum), &row); err != nil {
return err
return
}
if err = f.SetCellStyle(sheetName, fmt.Sprintf("A%d", rowNum), fmt.Sprintf("%s", lastRow), rowStyleID); err != nil {
return err
if err = f.SetCellStyle(sheetName, fmt.Sprintf("A%d", rowNum), lastRow, rowStyleID); err != nil {
return
}
}
@@ -91,8 +86,8 @@ func ExportByStructs(ctx context.Context, tags []string, list interface{}, fileN
w.Header().Set("Content-Transfer-Encoding", "binary")
w.Header().Set("Access-Control-Expose-Headers", "Content-Disposition")
if err := f.Write(w); err != nil {
return err
if err = f.Write(w); err != nil {
return
}
// 加入到上下文
@@ -102,8 +97,7 @@ func ExportByStructs(ctx context.Context, tags []string, list interface{}, fileN
Timestamp: time.Now().Unix(),
TraceID: gctx.CtxId(ctx),
})
return nil
return
}
// letter 生成完整的表头

View File

@@ -3,13 +3,12 @@
// @Copyright Copyright (c) 2023 HotGo CLI
// @Author Ms <133814250@qq.com>
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
//
package file
import (
"github.com/gogf/gf/v2/os/gfile"
"hotgo/utility/format"
"io/ioutil"
"os"
"path/filepath"
)
@@ -30,7 +29,7 @@ func WalkDir(dirname string) (error, []fileInfo) {
if nil != err {
return err, nil
}
files, err := ioutil.ReadDir(op) //获取目录下所有文件的信息,包括文件和文件夹
files, err := os.ReadDir(op) //获取目录下所有文件的信息,包括文件和文件夹
if nil != err {
return err, nil
}
@@ -44,7 +43,11 @@ func WalkDir(dirname string) (error, []fileInfo) {
}
fileInfos = append(fileInfos, fs...) //将 slice 添加到 slice
} else {
fi := fileInfo{op + `/` + f.Name(), f.Size()}
info, err := f.Info()
if nil != err {
return err, nil
}
fi := fileInfo{op + `/` + f.Name(), info.Size()}
fileInfos = append(fileInfos, fi) //slice 中添加成员
}
}

View File

@@ -8,15 +8,9 @@ package format
import (
"fmt"
"github.com/gogf/gf/v2/util/gconv"
"math"
"strconv"
)
// RoundInt64 四舍五入
func RoundInt64(x float64) int64 {
return int64(math.Floor(x + 0/5))
}
// Round2String 四舍五入保留小数默认2位
func Round2String(value float64, args ...interface{}) (v string) {
var places = 2

View File

@@ -20,7 +20,7 @@ import (
// FilterMaskDemo 过滤演示环境下的配置隐藏字段
func FilterMaskDemo(ctx context.Context, src g.Map) g.Map {
if src == nil || len(src) == 0 {
if src == nil {
return nil
}
@@ -28,7 +28,7 @@ func FilterMaskDemo(ctx context.Context, src g.Map) g.Map {
return src
}
for k, _ := range src {
for k := range src {
if _, ok := consts.ConfigMaskDemoField[k]; ok {
src[k] = consts.DemoTips
}
@@ -88,45 +88,22 @@ func SafeGo(ctx context.Context, f func(ctx context.Context), level ...interface
func Logf(level int, ctx context.Context, format string, v ...interface{}) {
switch level {
case glog.LEVEL_DEBU:
g.Log().Debugf(ctx, format, v)
g.Log().Debugf(ctx, format, v...)
case glog.LEVEL_INFO:
g.Log().Infof(ctx, format, v)
g.Log().Infof(ctx, format, v...)
case glog.LEVEL_NOTI:
g.Log().Noticef(ctx, format, v)
g.Log().Noticef(ctx, format, v...)
case glog.LEVEL_WARN:
g.Log().Warningf(ctx, format, v)
g.Log().Warningf(ctx, format, v...)
case glog.LEVEL_ERRO:
g.Log().Errorf(ctx, format, v)
g.Log().Errorf(ctx, format, v...)
case glog.LEVEL_CRIT:
g.Log().Critical(ctx, format, v)
g.Log().Criticalf(ctx, format, v...)
case glog.LEVEL_PANI:
g.Log().Panicf(ctx, format, v)
g.Log().Panicf(ctx, format, v...)
case glog.LEVEL_FATA:
g.Log().Fatalf(ctx, format, v)
g.Log().Fatalf(ctx, format, v...)
default:
g.Log().Error(ctx, "Logf level not find")
}
}
func Log(level int, ctx context.Context, v ...interface{}) {
switch level {
case glog.LEVEL_DEBU:
g.Log().Debug(ctx, v)
case glog.LEVEL_INFO:
g.Log().Info(ctx, v)
case glog.LEVEL_NOTI:
g.Log().Notice(ctx, v)
case glog.LEVEL_WARN:
g.Log().Warning(ctx, v)
case glog.LEVEL_ERRO:
g.Log().Error(ctx, v)
case glog.LEVEL_CRIT:
g.Log().Critical(ctx, v)
case glog.LEVEL_PANI:
g.Log().Panic(ctx, v)
case glog.LEVEL_FATA:
g.Log().Fatal(ctx, v)
default:
g.Log().Error(ctx, "Logf level not find")
g.Log().Errorf(ctx, format, v...)
}
}

View File

@@ -3,7 +3,6 @@
// @Copyright Copyright (c) 2023 HotGo CLI
// @Author Ms <133814250@qq.com>
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
//
package tree
import (
@@ -54,8 +53,6 @@ func GetIds(tree string) (ids []int64) {
return
}
/////////////////////////// 转换类
// GenTree 生成关系树
func GenTree(menus []map[string]interface{}) (realMenu []map[string]interface{}) {
return GenTreeWithField(menus, GenOption{

View File

@@ -18,7 +18,7 @@ func GetOs(userAgent string) string {
return osName
}
strRe, _ := regexp.Compile("(?i:\\((.*?)\\))")
strRe, _ := regexp.Compile(`(?i:((.*?)))`)
userAgent = strRe.FindString(userAgent)
levelNames := ":micromessenger:dart:Windows NT:Windows Mobile:Windows Phone:Windows Phone OS:Macintosh|Macintosh:Mac OS:CrOS|CrOS:iPhone OS:iPad|iPad:OS:Android:Linux:blackberry:hpwOS:Series:Symbian:PalmOS:SymbianOS:J2ME:Sailfish:Bada:MeeGo:webOS|hpwOS:Maemo:"
@@ -83,7 +83,7 @@ func GetBrowser(userAgent string) string {
level := 0
for _, name := range names {
replaceRe, _ := regexp.Compile("(?i:[\\s?\\/0-9.]+)")
replaceRe, _ := regexp.Compile(`(?i:[\s?\/0-9.]+)`)
n := replaceRe.ReplaceAllString(name, "")
l := strings.Index(levelNames, fmt.Sprintf(":%s:", n))
if level == 0 {

View File

@@ -33,16 +33,12 @@ func IsHTTPS(ctx context.Context) bool {
g.Log().Info(ctx, "IsHTTPS ctx not request")
return false
}
return r.TLS != nil || gstr.Equal(r.Header.Get("X-Forwarded-Proto"), "https")
}
// IsIp 是否为ipv4
func IsIp(ip string) bool {
if net.ParseIP(ip) != nil {
return true
}
return false
return net.ParseIP(ip) != nil
}
// IsPublicIp 是否是公网IP
@@ -56,9 +52,7 @@ func IsPublicIp(Ip string) bool {
if ip4 := ip.To4(); ip4 != nil {
return !ip.Equal(net.IPv4bcast)
}
return true
}
// IsLocalIPAddr 检测 IP 地址字符串是否是内网地址
@@ -150,16 +144,15 @@ func IsMobileVisit(userAgent string) bool {
return false
}
isMobile := false
is := false
mobileKeywords := []string{"Mobile", "Android", "Silk/", "Kindle", "BlackBerry", "Opera Mini", "Opera Mobi"}
for i := 0; i < len(mobileKeywords); i++ {
if strings.Contains(userAgent, mobileKeywords[i]) {
isMobile = true
is = true
break
}
}
return isMobile
return is
}
// IsWxBrowserVisit 是否为微信访问
@@ -177,7 +170,6 @@ func IsWxBrowserVisit(userAgent string) bool {
break
}
}
return is
}
@@ -196,6 +188,5 @@ func IsWxMiniProgramVisit(userAgent string) bool {
break
}
}
return is
}