init repo

This commit is contained in:
liuyuanchuang
2025-12-10 18:33:37 +08:00
commit 48e63894eb
2408 changed files with 1053045 additions and 0 deletions

21
pkg/utils/arr.go Normal file
View File

@@ -0,0 +1,21 @@
package utils
import "math/rand"
func InArray[T comparable](needle T, haystack []T) bool {
for _, item := range haystack {
if item == needle {
return true
}
}
return false
}
func NewRandNumber(length int) (string, error) {
letters := []byte{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}
b := make([]byte, length)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b), nil
}

29
pkg/utils/context.go Normal file
View File

@@ -0,0 +1,29 @@
package utils
import (
"context"
"github.com/google/uuid"
)
type contextKey string
const RequestIDKey contextKey = "request_id"
const RequestIDHeaderKey = "X-Request-ID"
func NewContextWithRequestID(ctx context.Context, requestID string) context.Context {
newCtx := context.Background()
newCtx = context.WithValue(newCtx, RequestIDKey, requestID)
return newCtx
}
func NewUUID() string {
return uuid.New().String()
}
func GetRequestIDFromContext(ctx context.Context) string {
if requestID, ok := ctx.Value(RequestIDKey).(string); ok {
return requestID
}
return ""
}

115
pkg/utils/katex.go Normal file
View File

@@ -0,0 +1,115 @@
package utils
import (
"regexp"
"strings"
)
// Helper function to change patterns
func changeAll(text, oldIns, newIns, leftDelim, rightDelim, newLeftDelim, newRightDelim string) string {
pattern := regexp.MustCompile(regexp.QuoteMeta(oldIns) + `\s*` + regexp.QuoteMeta(leftDelim) + `(.*?)` + regexp.QuoteMeta(rightDelim))
return pattern.ReplaceAllString(text, newIns+newLeftDelim+"$1"+newRightDelim)
}
// Helper function to remove dollar surroundings
func rmDollarSurr(text string) string {
if strings.HasPrefix(text, "$") && strings.HasSuffix(text, "$") {
return text[1 : len(text)-1]
}
return text
}
// ToKatex converts LaTeX formula to KaTeX compatible format
func ToKatex(formula string) string {
res := formula
// Remove mbox surrounding
res = changeAll(res, `\mbox `, " ", "{", "}", "", "")
res = changeAll(res, `\mbox`, " ", "{", "}", "", "")
// Remove hbox surrounding
hboxPattern := regexp.MustCompile(`\\hbox to ?-? ?\d+\.\d+(pt)?\{`)
res = hboxPattern.ReplaceAllString(res, `\hbox{`)
res = changeAll(res, `\hbox`, " ", "{", "}", "", " ")
// Remove raise surrounding
raisePattern := regexp.MustCompile(`\\raise ?-? ?\d+\.\d+(pt)?`)
res = raisePattern.ReplaceAllString(res, " ")
// Remove makebox
makeboxPattern := regexp.MustCompile(`\\makebox ?\[\d+\.\d+(pt)?\]\{`)
res = makeboxPattern.ReplaceAllString(res, `\makebox{`)
res = changeAll(res, `\makebox`, " ", "{", "}", "", " ")
// Remove vbox, scalebox, raisebox surrounding
raisebox := regexp.MustCompile(`\\raisebox\{-? ?\d+\.\d+(pt)?\}\{`)
scalebox := regexp.MustCompile(`\\scalebox\{-? ?\d+\.\d+(pt)?\}\{`)
res = raisebox.ReplaceAllString(res, `\raisebox{`)
res = scalebox.ReplaceAllString(res, `\scalebox{`)
res = changeAll(res, `\scalebox`, " ", "{", "}", "", " ")
res = changeAll(res, `\raisebox`, " ", "{", "}", "", " ")
res = changeAll(res, `\vbox`, " ", "{", "}", "", " ")
// Handle size instructions
sizeInstructions := []string{
`\Huge`, `\huge`, `\LARGE`, `\Large`, `\large`,
`\normalsize`, `\small`, `\footnotesize`, `\tiny`,
}
for _, ins := range sizeInstructions {
res = changeAll(res, ins, ins, "$", "$", "{", "}")
}
// Handle boldmath
res = changeAll(res, `\boldmath `, `\bm`, "{", "}", "{", "}")
res = changeAll(res, `\boldmath`, `\bm`, "{", "}", "{", "}")
res = changeAll(res, `\boldmath `, `\bm`, "$", "$", "{", "}")
res = changeAll(res, `\boldmath`, `\bm`, "$", "$", "{", "}")
// Handle other instructions
res = changeAll(res, `\scriptsize`, `\scriptsize`, "$", "$", "{", "}")
res = changeAll(res, `\emph`, `\textit`, "{", "}", "{", "}")
res = changeAll(res, `\emph `, `\textit`, "{", "}", "{", "}")
// Handle math delimiters
delimiters := []string{
`\left`, `\middle`, `\right`, `\big`, `\Big`, `\bigg`, `\Bigg`,
`\bigl`, `\Bigl`, `\biggl`, `\Biggl`, `\bigm`, `\Bigm`, `\biggm`,
`\Biggm`, `\bigr`, `\Bigr`, `\biggr`, `\Biggr`,
}
for _, delim := range delimiters {
res = changeAll(res, delim, delim, "{", "}", "", "")
}
// Handle display math
displayMath := regexp.MustCompile(`\\\[(.*?)\\\]`)
res = displayMath.ReplaceAllString(res, "$1\\newline")
res = strings.TrimSuffix(res, `\newline`)
// Remove multiple spaces
spaces := regexp.MustCompile(`(\\,){1,}`)
res = spaces.ReplaceAllString(res, " ")
res = regexp.MustCompile(`(\\!){1,}`).ReplaceAllString(res, " ")
res = regexp.MustCompile(`(\\;){1,}`).ReplaceAllString(res, " ")
res = regexp.MustCompile(`(\\:){1,}`).ReplaceAllString(res, " ")
res = regexp.MustCompile(`\\vspace\{.*?}`).ReplaceAllString(res, "")
// Merge consecutive text
textPattern := regexp.MustCompile(`(\\text\{[^}]*\}\s*){2,}`)
res = textPattern.ReplaceAllStringFunc(res, func(match string) string {
texts := regexp.MustCompile(`\\text\{([^}]*)\}`).FindAllStringSubmatch(match, -1)
var merged strings.Builder
for _, t := range texts {
merged.WriteString(t[1])
}
return `\text{` + merged.String() + "}"
})
res = strings.ReplaceAll(res, `\bf `, "")
res = rmDollarSurr(res)
// Remove extra spaces
res = regexp.MustCompile(` +`).ReplaceAllString(res, " ")
return strings.TrimSpace(res)
}

18
pkg/utils/routine.go Normal file
View File

@@ -0,0 +1,18 @@
package utils
import (
"context"
"gitea.com/bitwsd/core/common/log"
)
func SafeGo(fn func()) {
go func() {
defer func() {
if err := recover(); err != nil {
log.Error(context.Background(), "panic recover", "err", err)
}
}()
fn()
}()
}

30
pkg/utils/sms.go Normal file
View File

@@ -0,0 +1,30 @@
package utils
import "strings"
// 校验手机号
// 规则:
// 1. 长度必须为11位
// 2. 必须以1开头
// 3. 第二位必须是3,4,5,6,7,8,9
// 4. 其余必须都是数字
func ValidatePhone(phone string) bool {
if len(phone) != 11 || !strings.HasPrefix(phone, "1") {
return false
}
// 检查第二位
secondDigit := phone[1]
if secondDigit < '3' || secondDigit > '9' {
return false
}
// 检查剩余数字
for i := 2; i < len(phone); i++ {
if phone[i] < '0' || phone[i] > '9' {
return false
}
}
return true
}

5
pkg/utils/token.go Normal file
View File

@@ -0,0 +1,5 @@
package utils
const (
SiliconFlowToken = "Bearer sk-akbroznlbxikkbiouzasspbbzwgxubnjjtqlujxmxsnvpmhn"
)