mirror of
https://gitee.com/XM-GO/PandaX.git
synced 2026-04-23 02:48:34 +08:00
170 lines
3.8 KiB
Go
170 lines
3.8 KiB
Go
package utils
|
||
|
||
import (
|
||
"bytes"
|
||
"encoding/base64"
|
||
"github.com/kakuilan/kgo"
|
||
"math/rand"
|
||
"strings"
|
||
"text/template"
|
||
"time"
|
||
)
|
||
|
||
func UnicodeIndex(str, substr string) int {
|
||
// 子串在字符串的字节位置
|
||
result := strings.Index(str, substr)
|
||
if result >= 0 {
|
||
// 获得子串之前的字符串并转换成[]byte
|
||
prefix := []byte(str)[0:result]
|
||
// 将子串之前的字符串转换成[]rune
|
||
rs := []rune(string(prefix))
|
||
// 获得子串之前的字符串的长度,便是子串在字符串的字符位置
|
||
result = len(rs)
|
||
}
|
||
|
||
return result
|
||
}
|
||
|
||
func ReplaceString(pattern, replace, src string) (string, error) {
|
||
if r, err := GetRegexp(pattern); err == nil {
|
||
return r.ReplaceAllString(src, replace), nil
|
||
} else {
|
||
return "", err
|
||
}
|
||
}
|
||
|
||
func Contains(haystack, needle string, startOffset ...int) int {
|
||
length := len(haystack)
|
||
offset := 0
|
||
if len(startOffset) > 0 {
|
||
offset = startOffset[0]
|
||
}
|
||
if length == 0 || offset > length || -offset > length {
|
||
return -1
|
||
}
|
||
|
||
if offset < 0 {
|
||
offset += length
|
||
}
|
||
pos := strings.Index(strings.ToLower(haystack[offset:]), strings.ToLower(needle))
|
||
if pos == -1 {
|
||
return -1
|
||
}
|
||
return pos + offset
|
||
}
|
||
|
||
// 字符串模板解析
|
||
func TemplateResolve(temp string, data any) string {
|
||
t, _ := template.New("string-temp").Parse(temp)
|
||
var tmplBytes bytes.Buffer
|
||
|
||
err := t.Execute(&tmplBytes, data)
|
||
if err != nil {
|
||
panic(err)
|
||
}
|
||
return tmplBytes.String()
|
||
}
|
||
|
||
func ReverStrTemplate(temp, str string, res map[string]any) {
|
||
index := UnicodeIndex(temp, "{")
|
||
ei := UnicodeIndex(temp, "}") + 1
|
||
next := kgo.KStr.Trim(temp[ei:], " ")
|
||
nextContain := UnicodeIndex(next, "{")
|
||
nextIndexValue := next
|
||
if nextContain != -1 {
|
||
nextIndexValue = kgo.KStr.Substr(next, 0, nextContain)
|
||
}
|
||
key := temp[index+1 : ei-1]
|
||
// 如果后面没有内容了,则取字符串的长度即可
|
||
var valueLastIndex int
|
||
if nextIndexValue == "" {
|
||
valueLastIndex = kgo.KStr.MbStrlen(str)
|
||
} else {
|
||
valueLastIndex = UnicodeIndex(str, nextIndexValue)
|
||
}
|
||
value := kgo.KStr.Trim(kgo.KStr.Substr(str, index, valueLastIndex), " ")
|
||
res[key] = value
|
||
// 如果后面的还有需要解析的,则递归调用解析
|
||
if nextContain != -1 {
|
||
ReverStrTemplate(next, kgo.KStr.Trim(kgo.KStr.Substr(str, UnicodeIndex(str, value)+kgo.KStr.MbStrlen(value), kgo.KStr.MbStrlen(str))), res)
|
||
}
|
||
}
|
||
|
||
func B2S(bs []uint8) string {
|
||
ba := []byte{}
|
||
for _, b := range bs {
|
||
ba = append(ba, byte(b))
|
||
}
|
||
return string(ba)
|
||
}
|
||
|
||
func IdsStrToIdsIntGroup(keys string) []int64 {
|
||
IDS := make([]int64, 0)
|
||
ids := strings.Split(keys, ",")
|
||
for i := 0; i < len(ids); i++ {
|
||
ID := kgo.KConv.Str2Int(ids[i])
|
||
IDS = append(IDS, int64(ID))
|
||
}
|
||
return IDS
|
||
}
|
||
|
||
// 获取部门
|
||
// isP 是父ID 否则子ID
|
||
func DeptPCIds(deptIds []string, id int64, isP bool) []int64 {
|
||
pRes := make([]int64, 0)
|
||
cRes := make([]int64, 0)
|
||
is := true
|
||
for _, deptId := range deptIds {
|
||
did := kgo.KConv.Str2Int64(deptId)
|
||
if is {
|
||
pRes = append(pRes, did)
|
||
}
|
||
if kgo.KConv.Str2Int64(deptId) == id {
|
||
is = false
|
||
}
|
||
if !is {
|
||
cRes = append(cRes, did)
|
||
}
|
||
}
|
||
if isP {
|
||
return pRes
|
||
} else {
|
||
return cRes
|
||
}
|
||
}
|
||
|
||
// 获取组织
|
||
// isP 是父ID 否则子ID
|
||
func OrganizationPCIds(orgIds []string, id int64, isP bool) []int64 {
|
||
pRes := make([]int64, 0)
|
||
cRes := make([]int64, 0)
|
||
is := true
|
||
for _, orgId := range orgIds {
|
||
did := kgo.KConv.Str2Int64(orgId)
|
||
if is {
|
||
pRes = append(pRes, did)
|
||
}
|
||
if kgo.KConv.Str2Int64(orgId) == id {
|
||
is = false
|
||
}
|
||
if !is {
|
||
cRes = append(cRes, did)
|
||
}
|
||
}
|
||
if isP {
|
||
return pRes
|
||
} else {
|
||
return cRes
|
||
}
|
||
}
|
||
|
||
func GenerateID() string {
|
||
rand.Seed(time.Now().UnixNano())
|
||
id := make([]byte, 7) // 由于base64编码会增加字符数,这里使用7个字节生成10位ID
|
||
_, err := rand.Read(id)
|
||
if err != nil {
|
||
panic(err) // 错误处理,根据实际情况进行处理
|
||
}
|
||
return base64.URLEncoding.EncodeToString(id)[:10]
|
||
}
|