初始化提交
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"github.com/mojocn/base64Captcha"
|
||||
"hospital-admin-api/api/requests"
|
||||
"image/color"
|
||||
"time"
|
||||
)
|
||||
|
||||
// GenerateCaptcha 生成验证码-base64
|
||||
func GenerateCaptcha() (id, b64s string, err error) {
|
||||
var driver *base64Captcha.DriverString
|
||||
// 配置验证码的参数
|
||||
driverString := &base64Captcha.DriverString{
|
||||
Height: 40,
|
||||
Width: 100,
|
||||
NoiseCount: 0,
|
||||
ShowLineOptions: 0,
|
||||
Length: 4,
|
||||
Source: "1234567890",
|
||||
BgColor: &color.RGBA{R: 3, G: 102, B: 214, A: 125},
|
||||
Fonts: []string{"wqy-microhei.ttc"},
|
||||
}
|
||||
// ConvertFonts 按名称加载字体
|
||||
driver = driverString.ConvertFonts()
|
||||
|
||||
base64Captcha.Expiration = 30 * time.Minute
|
||||
store := base64Captcha.DefaultMemStore
|
||||
|
||||
captcha := base64Captcha.NewCaptcha(driver, store)
|
||||
id, b64s, err = captcha.Generate()
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
return id, b64s, nil
|
||||
}
|
||||
|
||||
// VerifyCaptcha 验证验证码
|
||||
func VerifyCaptcha(login requests.Login) bool {
|
||||
// 创建验证码实例
|
||||
base64Captcha.Expiration = 30 * time.Minute
|
||||
store := base64Captcha.DefaultMemStore
|
||||
captcha := base64Captcha.NewCaptcha(nil, store)
|
||||
|
||||
// 验证验证码
|
||||
isValid := captcha.Verify(login.CaptchaId, login.Captcha, true)
|
||||
if !isValid {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
)
|
||||
|
||||
// PathExists 文件是否存在
|
||||
func PathExists(path string) (bool, error) {
|
||||
fi, err := os.Stat(path)
|
||||
if err == nil {
|
||||
if fi.IsDir() {
|
||||
return true, nil
|
||||
}
|
||||
return false, errors.New("存在同名文件")
|
||||
}
|
||||
if os.IsNotExist(err) {
|
||||
return false, nil
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
"hospital-admin-api/config"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Token struct {
|
||||
UserId int64 `json:"user_id"` // 用户id
|
||||
RoleId int64 `json:"role_id"` // 角色id
|
||||
DeptId int64 `json:"dept_id"` // 部门id
|
||||
PostId int64 `json:"post_id"` // 岗位id
|
||||
jwt.RegisteredClaims // v5版本新加的方法
|
||||
}
|
||||
|
||||
// NewJWT GenerateJWT 生成JWT
|
||||
func (t Token) NewJWT() (string, error) {
|
||||
ttl := time.Duration(config.C.Jwt.Ttl)
|
||||
|
||||
t.RegisteredClaims.ExpiresAt = jwt.NewNumericDate(time.Now().Add(ttl * time.Hour)) // 过期时间24小时
|
||||
t.RegisteredClaims.IssuedAt = jwt.NewNumericDate(time.Now()) // 签发时间
|
||||
t.RegisteredClaims.NotBefore = jwt.NewNumericDate(time.Now()) // 生效时间
|
||||
|
||||
// 使用HS256签名算法
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, t)
|
||||
s, err := token.SignedString([]byte(config.C.Jwt.SignKey))
|
||||
|
||||
return s, err
|
||||
}
|
||||
|
||||
// ParseJwt 解析JWT
|
||||
func ParseJwt(authorization string) (*Token, error) {
|
||||
t, err := jwt.ParseWithClaims(authorization, &Token{}, func(token *jwt.Token) (interface{}, error) {
|
||||
return []byte(config.C.Jwt.SignKey), nil
|
||||
})
|
||||
|
||||
if claims, ok := t.Claims.(*Token); ok && t.Valid {
|
||||
return claims, nil
|
||||
} else {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/sirupsen/logrus"
|
||||
"hospital-admin-api/global"
|
||||
)
|
||||
|
||||
func LogJsonInfo(v interface{}) {
|
||||
jsonData, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
fmt.Println("Error marshaling struct to JSON:", err)
|
||||
return
|
||||
}
|
||||
|
||||
jsonString := string(jsonData)
|
||||
fmt.Println(jsonString)
|
||||
|
||||
global.Logger.WithFields(logrus.Fields{
|
||||
"data": jsonString,
|
||||
}).Info("info")
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package utils
|
||||
|
||||
import "regexp"
|
||||
|
||||
// RegexpMobile 手机号匹配
|
||||
func RegexpMobile(mobile string) bool {
|
||||
ok, err := regexp.MatchString(`^1[3-9][0-9]{9}$`, mobile)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return ok
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"github.com/go-playground/validator/v10"
|
||||
"hospital-admin-api/global"
|
||||
)
|
||||
|
||||
// Translate 检验并返回检验错误信息
|
||||
func Translate(err error) (errMsg string) {
|
||||
errs := err.(validator.ValidationErrors)
|
||||
for _, err := range errs {
|
||||
errMsg = err.Translate(global.Trans)
|
||||
}
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user