æ·feat: commplete init process
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
func TimeStampToYmdMHD(stamp int) string {
|
||||
timestamp := int64(stamp)
|
||||
timeObj := time.Unix(timestamp, 0)
|
||||
formattedTime := timeObj.Format("2006-01-02 15:04:05")
|
||||
return formattedTime
|
||||
}
|
||||
|
||||
func GetNowTimeStampToYmdMHD() string {
|
||||
now := time.Now().Unix()
|
||||
timeObj := time.Unix(now, 0)
|
||||
formattedTime := timeObj.Format("2006-01-02 15:04:05")
|
||||
return formattedTime
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/dgrijalva/jwt-go"
|
||||
)
|
||||
|
||||
var jwtSecret []byte
|
||||
|
||||
type Claims struct {
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
jwt.StandardClaims
|
||||
}
|
||||
|
||||
// GenerateToken generate tokens used for auth
|
||||
func GenerateToken(username, password string) (string, error) {
|
||||
nowTime := time.Now()
|
||||
expireTime := nowTime.Add(7 * 24 * time.Hour)
|
||||
|
||||
claims := Claims{
|
||||
username,
|
||||
EncodeMD5(password),
|
||||
jwt.StandardClaims{
|
||||
ExpiresAt: expireTime.Unix(),
|
||||
Issuer: "message-nest",
|
||||
},
|
||||
}
|
||||
|
||||
tokenClaims := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||||
token, err := tokenClaims.SignedString(jwtSecret)
|
||||
|
||||
return token, err
|
||||
}
|
||||
|
||||
// ParseToken parsing token
|
||||
func ParseToken(token string) (*Claims, error) {
|
||||
tokenClaims, err := jwt.ParseWithClaims(token, &Claims{}, func(token *jwt.Token) (interface{}, error) {
|
||||
return jwtSecret, nil
|
||||
})
|
||||
|
||||
if tokenClaims != nil {
|
||||
if claims, ok := tokenClaims.Claims.(*Claims); ok && tokenClaims.Valid {
|
||||
return claims, nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil, err
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
)
|
||||
|
||||
// EncodeMD5 md5 encryption
|
||||
func EncodeMD5(value string) string {
|
||||
m := md5.New()
|
||||
m.Write([]byte(value))
|
||||
|
||||
return hex.EncodeToString(m.Sum(nil))
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/unknwon/com"
|
||||
|
||||
"message-nest/pkg/setting"
|
||||
)
|
||||
|
||||
// GetPage get page parameters
|
||||
func GetPage(c *gin.Context) int {
|
||||
result := 0
|
||||
page := com.StrTo(c.Query("page")).MustInt()
|
||||
if page > 0 {
|
||||
result = (page - 1) * setting.AppSetting.PageSize
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func GetPageSize(c *gin.Context) (int, int) {
|
||||
result := 0
|
||||
page := com.StrTo(c.Query("page")).MustInt()
|
||||
size := com.StrTo(c.Query("size")).MustInt()
|
||||
if page > 0 {
|
||||
result = (page - 1) * size
|
||||
}
|
||||
return result, size
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
const timeFormat = "2006-01-02 15:04:05"
|
||||
const timezone = "Asia/Shanghai"
|
||||
|
||||
// 全局定义
|
||||
type Time time.Time
|
||||
|
||||
func (t Time) MarshalJSON() ([]byte, error) {
|
||||
b := make([]byte, 0, len(timeFormat)+2)
|
||||
b = append(b, '"')
|
||||
b = time.Time(t).AppendFormat(b, timeFormat)
|
||||
b = append(b, '"')
|
||||
|
||||
return b, nil
|
||||
}
|
||||
|
||||
func (t *Time) UnmarshalJSON(data []byte) (err error) {
|
||||
now, err := time.ParseInLocation(`"`+timeFormat+`"`, string(data), time.Local)
|
||||
*t = Time(now)
|
||||
return
|
||||
}
|
||||
|
||||
func (t Time) String() string {
|
||||
return time.Time(t).Format(timeFormat)
|
||||
}
|
||||
|
||||
func (t Time) local() time.Time {
|
||||
loc, _ := time.LoadLocation(timezone)
|
||||
return time.Time(t).In(loc)
|
||||
}
|
||||
|
||||
func (t Time) Value() (driver.Value, error) {
|
||||
var zeroTime time.Time
|
||||
var ti = time.Time(t)
|
||||
if ti.UnixNano() == zeroTime.UnixNano() {
|
||||
return nil, nil
|
||||
}
|
||||
return ti, nil
|
||||
}
|
||||
|
||||
func (t *Time) Scan(v interface{}) error {
|
||||
value, ok := v.(time.Time)
|
||||
if ok {
|
||||
*t = Time(value)
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("can not convert %v to timestamp", v)
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"github.com/go-playground/locales/en"
|
||||
"github.com/go-playground/locales/zh"
|
||||
"github.com/go-playground/universal-translator"
|
||||
"github.com/go-playground/validator/v10"
|
||||
zh_trans "github.com/go-playground/validator/v10/translations/zh"
|
||||
"reflect"
|
||||
"strings"
|
||||
)
|
||||
|
||||
//var trans ut.Translator
|
||||
|
||||
func TransInit() (trans ut.Translator, validate *validator.Validate) {
|
||||
// 创建翻译器
|
||||
zhTrans := zh.New() // 中文转换器
|
||||
enTrans := en.New() // 因为转换器
|
||||
|
||||
uni := ut.New(zhTrans, zhTrans, enTrans) // 创建一个通用转换器
|
||||
|
||||
curLocales := "zh" // 设置当前语言类型
|
||||
trans, _ = uni.GetTranslator(curLocales) // 获取对应语言的转换器
|
||||
|
||||
validate = validator.New() // 创建验证器
|
||||
_ = zh_trans.RegisterDefaultTranslations(validate, trans)
|
||||
|
||||
//switch curLocales {
|
||||
//case "zh":
|
||||
// // 内置tag注册 中文翻译器
|
||||
// _ = zh_trans.RegisterDefaultTranslations(validate, trans)
|
||||
//case "en":
|
||||
// // 内置tag注册 英文翻译器
|
||||
// _ = en_trans.RegisterDefaultTranslations(validate, trans)
|
||||
//}
|
||||
|
||||
// 注册 RegisterTagNameFunc
|
||||
validate.RegisterTagNameFunc(func(field reflect.StructField) string {
|
||||
name := strings.SplitN(field.Tag.Get("label"), ",", 2)[0]
|
||||
if name == "-" {
|
||||
return ""
|
||||
}
|
||||
|
||||
return name
|
||||
})
|
||||
return trans, validate
|
||||
}
|
||||
|
||||
var Trans, CustomerValidate = TransInit()
|
||||
@@ -0,0 +1,10 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"message-nest/pkg/setting"
|
||||
)
|
||||
|
||||
// Setup Initialize the util
|
||||
func Setup() {
|
||||
jwtSecret = []byte(setting.AppSetting.JwtSecret)
|
||||
}
|
||||
Reference in New Issue
Block a user