Compare commits
2 Commits
16967381d8
...
62db070f61
Author | SHA1 | Date | |
---|---|---|---|
62db070f61 | |||
de7cd23deb |
|
@ -1,6 +1,8 @@
|
|||
package model
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
|
@ -9,3 +11,140 @@ type KillaraCatalogLanguageModel struct {
|
|||
db *gorm.DB
|
||||
TableName string // 表名
|
||||
}
|
||||
|
||||
func (m *KillaraCatalogLanguageModel) InsertLanguage(language *KillaraCatalogLanguage) (uint64, error) {
|
||||
if err := m.db.Model(&KillaraCatalogLanguage{}).Create(language).Error; err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return *language.LanguageId, nil
|
||||
}
|
||||
|
||||
func (m *KillaraCatalogLanguageModel) UpdateLanguage(language *KillaraCatalogLanguage) error {
|
||||
return m.db.Model(&KillaraCatalogLanguage{}).Where("language_id = ?", *language.LanguageId).Updates(language).Error
|
||||
}
|
||||
|
||||
func (m *KillaraCatalogLanguageModel) DeleteLanguage(language *KillaraCatalogLanguage) error {
|
||||
return m.db.Where("language_id = ?", *language.LanguageId).Delete(&KillaraCatalogLanguage{}).Error
|
||||
}
|
||||
|
||||
func (m *KillaraCatalogLanguageModel) GetLanguage(languageID uint64) (*KillaraCatalogLanguage, error) {
|
||||
var language KillaraCatalogLanguage
|
||||
err := m.db.Where("language_id = ?", languageID).First(&language).Error
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return nil, nil
|
||||
}
|
||||
return &language, err
|
||||
}
|
||||
|
||||
func (m *KillaraCatalogLanguageModel) GetLanguagesForBackEnd(data map[string]interface{}) ([]*KillaraCatalogLanguage, error) {
|
||||
var languages []*KillaraCatalogLanguage
|
||||
query := m.db.Model(&KillaraCatalogLanguage{})
|
||||
|
||||
var conditions []interface{}
|
||||
|
||||
if filterName, ok := data["filter_name"].(string); ok && filterName != "" {
|
||||
conditions = append(conditions, m.db.Where("name LIKE ?", filterName+"%"))
|
||||
}
|
||||
|
||||
if filterCode, ok := data["filter_code"].(string); ok && filterCode != "" {
|
||||
conditions = append(conditions, m.db.Where("code LIKE ?", filterCode+"%"))
|
||||
}
|
||||
|
||||
if filterStatus, ok := data["filter_status"]; ok {
|
||||
conditions = append(conditions, m.db.Where("status = ?", filterStatus))
|
||||
}
|
||||
|
||||
if len(conditions) > 0 {
|
||||
query = query.Where(strings.Join(make([]string, len(conditions)), " AND "), conditions...)
|
||||
}
|
||||
|
||||
if sort, ok := data["sort"].(string); ok && sort != "" {
|
||||
order := "DESC"
|
||||
if sortOrder, ok := data["order"].(string); ok {
|
||||
order = sortOrder
|
||||
}
|
||||
query = query.Order(sort + " " + order)
|
||||
}
|
||||
|
||||
if start, ok := data["start"].(int); ok && start > 0 {
|
||||
query = query.Offset(start)
|
||||
}
|
||||
|
||||
if limit, ok := data["limit"].(int); ok && limit > 0 {
|
||||
query = query.Limit(limit)
|
||||
} else {
|
||||
query = query.Limit(10)
|
||||
}
|
||||
|
||||
err := query.Find(&languages).Error
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return nil, nil
|
||||
}
|
||||
return languages, err
|
||||
}
|
||||
|
||||
func (m *KillaraCatalogLanguageModel) GetTotalLanguagesForBackEnd(data map[string]interface{}) (int64, error) {
|
||||
var count int64
|
||||
query := m.db.Model(&KillaraCatalogLanguage{})
|
||||
|
||||
var conditions []interface{}
|
||||
|
||||
if filterName, ok := data["filter_name"].(string); ok && filterName != "" {
|
||||
conditions = append(conditions, m.db.Where("name LIKE ?", filterName+"%"))
|
||||
}
|
||||
|
||||
if filterCode, ok := data["filter_code"].(string); ok && filterCode != "" {
|
||||
conditions = append(conditions, m.db.Where("code LIKE ?", filterCode+"%"))
|
||||
}
|
||||
|
||||
if filterStatus, ok := data["filter_status"]; ok {
|
||||
conditions = append(conditions, m.db.Where("status = ?", filterStatus))
|
||||
}
|
||||
|
||||
if len(conditions) > 0 {
|
||||
query = query.Where(strings.Join(make([]string, len(conditions)), " AND "), conditions...)
|
||||
}
|
||||
|
||||
err := query.Count(&count).Error
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return 0, nil
|
||||
}
|
||||
return count, err
|
||||
}
|
||||
|
||||
func (m *KillaraCatalogLanguageModel) GetLanguageForBackEnd(languageID uint64) (*KillaraCatalogLanguage, error) {
|
||||
var language KillaraCatalogLanguage
|
||||
err := m.db.Where("language_id = ?", languageID).First(&language).Error
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return nil, nil
|
||||
}
|
||||
return &language, err
|
||||
}
|
||||
|
||||
func (m *KillaraCatalogLanguageModel) GetLanguageByCodeForBackEnd(code string) (*KillaraCatalogLanguage, error) {
|
||||
var language KillaraCatalogLanguage
|
||||
err := m.db.Where("code = ?", code).First(&language).Error
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return nil, nil
|
||||
}
|
||||
return &language, err
|
||||
}
|
||||
|
||||
// 前台使用的方法
|
||||
func (m *KillaraCatalogLanguageModel) GetLanguagesForFrontEnd() ([]*KillaraCatalogLanguage, error) {
|
||||
var languages []*KillaraCatalogLanguage
|
||||
err := m.db.Where("status = 1").Order("sort_order").Find(&languages).Error
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return nil, nil
|
||||
}
|
||||
return languages, err
|
||||
}
|
||||
|
||||
func (m *KillaraCatalogLanguageModel) GetLanguageForFrontEnd(languageID uint64) (*KillaraCatalogLanguage, error) {
|
||||
var language KillaraCatalogLanguage
|
||||
err := m.db.Where("status = 1 AND language_id = ?", languageID).First(&language).Error
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return nil, nil
|
||||
}
|
||||
return &language, err
|
||||
}
|
||||
|
|
|
@ -32,6 +32,9 @@ func (m *KillaraCustomerModel) DeleteCustomer(customer *KillaraCustomer) error {
|
|||
func (m *KillaraCustomerModel) GetCustomer(customerID uint64) (*KillaraCustomer, error) {
|
||||
var customer KillaraCustomer
|
||||
err := m.db.Where("customer_id = ? AND status = 1", customerID).First(&customer).Error
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return nil, nil
|
||||
}
|
||||
return &customer, err
|
||||
}
|
||||
|
||||
|
|
|
@ -34,6 +34,7 @@ func (m *KillaraCustomerTokenModel) InsertToken(data *KillaraCustomerToken) erro
|
|||
return m.db.Model(&KillaraCustomerToken{}).Transaction(func(tx *gorm.DB) error {
|
||||
// 查找是否存在相同客户ID、平台和Token的记录
|
||||
var existingToken KillaraCustomerToken
|
||||
|
||||
err := tx.Where("customer_id = ? AND platform = ? AND token = ?", data.CustomerId, data.Platform, *data.Token).First(&existingToken).Error
|
||||
|
||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
|
@ -57,7 +58,7 @@ func (m *KillaraCustomerTokenModel) InsertToken(data *KillaraCustomerToken) erro
|
|||
return err
|
||||
}
|
||||
|
||||
return tx.Commit().Error // 提交事务
|
||||
return err // 提交事务
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -113,12 +114,12 @@ func (m *KillaraCustomerTokenModel) GetToken(token string) (*KillaraCustomerToke
|
|||
// return nil, nil
|
||||
// }
|
||||
|
||||
func (m *KillaraCustomerTokenModel) CheckToken(token string) (*KillaraCustomerToken, error) {
|
||||
func (m *KillaraCustomerTokenModel) CheckToken(tokenstr string) (*KillaraCustomerToken, error) {
|
||||
var resultToken *KillaraCustomerToken
|
||||
err := m.db.Model(&KillaraCustomerToken{}).Transaction(func(tx *gorm.DB) error {
|
||||
// 查找 Token 记录
|
||||
var token KillaraCustomerToken
|
||||
err := tx.Where("token = ?", token).First(&token).Error
|
||||
err := tx.Where("token = ?", tokenstr).First(&token).Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil
|
||||
|
@ -138,7 +139,7 @@ func (m *KillaraCustomerTokenModel) CheckToken(token string) (*KillaraCustomerTo
|
|||
}
|
||||
|
||||
resultToken = &token
|
||||
return tx.Commit().Error // 提交事务
|
||||
return err // 提交事务
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
|
@ -150,7 +151,7 @@ func (m *KillaraCustomerTokenModel) CheckToken(token string) (*KillaraCustomerTo
|
|||
func (m *KillaraCustomerTokenModel) ClearDuplicateToken(customerID uint64, currentToken string, platform int) error {
|
||||
|
||||
return m.db.Model(&KillaraCustomerToken{}).Transaction(func(tx *gorm.DB) error {
|
||||
var tokens []KillaraCustomerToken
|
||||
var tokens []*KillaraCustomerToken
|
||||
err := tx.Where("customer_id = ? AND platform = ?", customerID, platform).Find(&tokens).Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
|
@ -164,7 +165,7 @@ func (m *KillaraCustomerTokenModel) ClearDuplicateToken(customerID uint64, curre
|
|||
for _, token := range tokens {
|
||||
if *token.Token != currentToken {
|
||||
// err := tx.Delete(token).Error todo: 不太明白php为什么不删除token, 难道用来链路跟踪? 但是客户端id也被更新了, 没有存在的意义了
|
||||
err := tx.Where("token = ?", token).Update("customer_id", 0).Error
|
||||
err := tx.Where("token = ?", *token.Token).Update("customer_id", 0).Error
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return fmt.Errorf("tokens 查询出来, 自身都不存在, 疑似出了什么错误2")
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package actions
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
|
@ -17,16 +18,99 @@ var CompanyKey = "vestmore-bjwl"
|
|||
|
||||
// @Action base/getToken
|
||||
// Base_GetToken
|
||||
// action: string;
|
||||
// app_market: string;
|
||||
// lang: string;
|
||||
// token: string;
|
||||
// randstr: string;
|
||||
// sign: string;
|
||||
// action: string;
|
||||
// lang: string;
|
||||
// app_market: int64;
|
||||
// timestamp: int64;
|
||||
// token: string;
|
||||
func BaseGetToken(ctx *ActionContext[BaseGetTokenParam]) (resp *basic.Response) {
|
||||
|
||||
// model.Models.KillaraCustomerModel.Find()
|
||||
log.Println()
|
||||
const (
|
||||
TokenExpiry = 864000 // Token过期时间,单位:秒
|
||||
)
|
||||
|
||||
return resp.Success()
|
||||
type TokenData struct {
|
||||
LanguageID uint64 `json:"language_id"`
|
||||
LanguageCode string `json:"language_code"`
|
||||
}
|
||||
|
||||
modelToken := model.Models.KillaraCustomerTokenModel
|
||||
modelLanguage := model.Models.KillaraCatalogLanguageModel
|
||||
|
||||
lang := ctx.Lang
|
||||
|
||||
language, err := modelLanguage.GetLanguageByCodeForBackEnd(lang)
|
||||
if err != nil || language == nil {
|
||||
language, _ = modelLanguage.GetLanguageByCodeForBackEnd("en")
|
||||
}
|
||||
|
||||
tokenData := TokenData{
|
||||
LanguageID: *language.LanguageId,
|
||||
LanguageCode: *language.Code,
|
||||
}
|
||||
|
||||
isLogin := false
|
||||
var token string
|
||||
|
||||
if ctx.Param.Token != "" {
|
||||
result, err := modelToken.CheckToken(ctx.Param.Token)
|
||||
if err == nil && result != nil {
|
||||
token = ctx.Param.Token
|
||||
|
||||
// Token存在并且有效
|
||||
modelToken.UpdateTokenExpiry(token, time.Now().Unix()+TokenExpiry)
|
||||
|
||||
tokenDataJSON, _ := json.Marshal(tokenData)
|
||||
modelToken.UpdateTokenData(token, map[string]interface{}{
|
||||
"data": string(tokenDataJSON),
|
||||
})
|
||||
|
||||
if result.CustomerId != nil && *result.CustomerId != 0 {
|
||||
modelCustomer := model.Models.KillaraCustomerModel
|
||||
customer, err := modelCustomer.GetCustomer(*result.CustomerId)
|
||||
if err == nil && customer != nil {
|
||||
isLogin = true
|
||||
} else {
|
||||
modelToken.UpdateTokenCustomerID(token, 0)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if token == "" {
|
||||
// Token不存在或失效,重新创建一个
|
||||
token = auth.GenerateMD5()
|
||||
|
||||
tokenDataJSON, err := json.Marshal(tokenData)
|
||||
if err != nil {
|
||||
return resp.Error(basic.ErrJSONUnMarshal)
|
||||
}
|
||||
|
||||
tokenItem := &model.KillaraCustomerToken{
|
||||
Token: &token,
|
||||
CustomerId: basic.Uint64Ptr(0),
|
||||
Data: basic.StringPtr(string(tokenDataJSON)),
|
||||
Expiry: basic.Int64Ptr(time.Now().Unix() + TokenExpiry),
|
||||
Platform: basic.Int64Ptr(1),
|
||||
}
|
||||
|
||||
err = modelToken.InsertToken(tokenItem)
|
||||
log.Println(*tokenItem.Token)
|
||||
if err != nil {
|
||||
return resp.ErrorErr(1, err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return resp.Success(map[string]interface{}{
|
||||
"token": token,
|
||||
"is_login": isLogin,
|
||||
"lang_data": map[string]interface{}{
|
||||
"language_id": tokenData.LanguageID,
|
||||
"language_code": tokenData.LanguageCode,
|
||||
}})
|
||||
}
|
||||
|
||||
// @Action account/loginWithTelephonePassword
|
||||
|
@ -258,7 +342,6 @@ func AccountLoginWithEmailPassword(ctx *ActionContext[AccountLoginWithEmailPassw
|
|||
return resp.ErrorErr(1, err)
|
||||
}
|
||||
|
||||
ctx.Localize(translator.AccountNotRegistered)
|
||||
if customer == nil {
|
||||
return resp.ErrorTrCode(ctx, translator.AccountNotRegistered)
|
||||
}
|
||||
|
|
|
@ -233,9 +233,12 @@ func AccountRegisterSmsCodeHandler(ctx *gin.Context) {
|
|||
}
|
||||
|
||||
type BaseGetTokenParam struct {
|
||||
Randstr string `json:"randstr" form:"randstr" binding:"-"`
|
||||
Sign string `json:"sign" form:"sign" binding:"-"`
|
||||
Action string `json:"action" form:"action" binding:"-"`
|
||||
AppMarket string `json:"app_market" form:"app_market" binding:"-"`
|
||||
Lang string `json:"lang" form:"lang" binding:"-"`
|
||||
AppMarket int64 `json:"app_market" form:"app_market" binding:"-"`
|
||||
Timestamp int64 `json:"timestamp" form:"timestamp" binding:"-"`
|
||||
Token string `json:"token" form:"token" binding:"-"`
|
||||
}
|
||||
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"testing"
|
||||
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
"github.com/iapologizewhenimwrong/Vestmore_GO/translator"
|
||||
"github.com/iapologizewhenimwrong/Vestmore_GO/utils/log"
|
||||
"github.com/nicksnyder/go-i18n/v2/i18n"
|
||||
"github.com/pelletier/go-toml/v2"
|
||||
"golang.org/x/text/language"
|
||||
|
|
|
@ -21,7 +21,27 @@ func init() {
|
|||
globpath := filepath.Join(currentDir, "/*.toml")
|
||||
|
||||
Bundle = i18n.NewBundle(language.Chinese)
|
||||
Bundle.RegisterUnmarshalFunc("toml", toml.Unmarshal)
|
||||
Bundle.RegisterUnmarshalFunc("toml", func(data []byte, v interface{}) error {
|
||||
var m map[string]any
|
||||
err := toml.Unmarshal(data, &m)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, v := range m {
|
||||
vm := v.(map[string]any)
|
||||
desc := vm["description"].(string)
|
||||
vm["one"] = desc
|
||||
vm["other"] = desc
|
||||
}
|
||||
|
||||
fixdata, err := toml.Marshal(m)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return toml.Unmarshal(fixdata, v)
|
||||
})
|
||||
matches, err := filepath.Glob(globpath)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
|
@ -30,14 +50,6 @@ func init() {
|
|||
Bundle.MustLoadMessageFile(m)
|
||||
}
|
||||
|
||||
// localizer := i18n.NewLocalizer(Bundle, "zh_cn")
|
||||
// log.Println(localizer.Localize(&i18n.LocalizeConfig{
|
||||
// MessageID: "format_account_is_insufficient",
|
||||
// TemplateData: map[string]any{
|
||||
// "CatalogCurrency": "USDT",
|
||||
// },
|
||||
// }))
|
||||
|
||||
}
|
||||
|
||||
func Localize(MessageID TrCode, Lang string, MessageTemplateParam any) (string, error) {
|
||||
|
|
|
@ -13,6 +13,7 @@ import (
|
|||
"text/template"
|
||||
"unicode"
|
||||
|
||||
"github.com/nicksnyder/go-i18n/v2/i18n"
|
||||
"github.com/pelletier/go-toml/v2"
|
||||
)
|
||||
|
||||
|
@ -118,7 +119,10 @@ func createTrCode(filePath string) {
|
|||
}
|
||||
|
||||
func TestTrOnline(t *testing.T) {
|
||||
|
||||
var localizer = i18n.NewLocalizer(Bundle, "zh_cn")
|
||||
log.Println(localizer.Localize(&i18n.LocalizeConfig{
|
||||
MessageID: string("account_not_registered"),
|
||||
}))
|
||||
}
|
||||
|
||||
func toCamelCase(s string) string {
|
||||
|
|
|
@ -280,5 +280,6 @@ description = "默认交易时间段"
|
|||
description = "非默认交易时间段"
|
||||
[all_day_transaction]
|
||||
description = "全天交易"
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -1,21 +1,40 @@
|
|||
package auth
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
var myRand = rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
|
||||
func GenerateVerificationCode() string {
|
||||
var code string
|
||||
|
||||
for i := 0; i < 3; i++ {
|
||||
// 生成两个 10-99 之间的随机数
|
||||
a := rand.Intn(90-10+1) + 10
|
||||
b := rand.Intn(90-10+1) + 10
|
||||
c := rand.Intn(90-10+1) + 10
|
||||
a := myRand.Intn(90-10+1) + 10
|
||||
b := myRand.Intn(90-10+1) + 10
|
||||
c := myRand.Intn(90-10+1) + 10
|
||||
// 将随机数拼接到验证码字符串
|
||||
code += fmt.Sprintf("%d%d%d", a, b, c)
|
||||
}
|
||||
|
||||
return code
|
||||
}
|
||||
|
||||
func GenerateMD5() string {
|
||||
|
||||
// 将时间戳转换为字节slice
|
||||
timestampBytes := []byte(strconv.FormatInt(time.Now().UnixNano(), 10))
|
||||
|
||||
// 计算MD5哈希
|
||||
hashBytes := md5.Sum(timestampBytes)
|
||||
|
||||
// 将哈希值转换为十六进制字符串
|
||||
hashHex := fmt.Sprintf("%x", hashBytes)
|
||||
|
||||
return hashHex
|
||||
}
|
||||
|
|
|
@ -8,7 +8,8 @@ type ErrorCode struct {
|
|||
var (
|
||||
ErrRespNotNil = &ErrorCode{Code: 10000, Message: "resp must not nil"}
|
||||
|
||||
ErrEncGcm = &ErrorCode{Code: 10001, Message: "gmc加密错误"}
|
||||
ErrEncGcm = &ErrorCode{Code: 10001, Message: "gmc加密错误"}
|
||||
ErrJSONUnMarshal = &ErrorCode{Code: 10002, Message: "json 解析错误"}
|
||||
|
||||
ErrParamParse = &ErrorCode{Code: 10100, Message: "参数解析错误"}
|
||||
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
package basic
|
||||
|
||||
import "reflect"
|
||||
import (
|
||||
"reflect"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func GetLangString(param any) string {
|
||||
// 获取参数的反射值
|
||||
|
@ -18,10 +21,11 @@ func GetLangString(param any) string {
|
|||
|
||||
// 如果字段存在并且是字符串类型
|
||||
if langField.IsValid() && langField.Kind() == reflect.String {
|
||||
return langField.String()
|
||||
return strings.TrimSpace(langField.String())
|
||||
}
|
||||
}
|
||||
|
||||
// 如果无法获取有效的字符串值,则返回zh_cn
|
||||
|
||||
return "zh_cn"
|
||||
}
|
||||
|
|
|
@ -5,6 +5,8 @@ import (
|
|||
"github.com/iapologizewhenimwrong/Vestmore_GO/utils/log"
|
||||
)
|
||||
|
||||
var respLog = log.New(0)
|
||||
|
||||
// 全局返回的结构体
|
||||
type Response struct {
|
||||
Data interface{} `json:"data"`
|
||||
|
@ -25,7 +27,7 @@ func (resp *Response) Error(errcode *ErrorCode, Data ...interface{}) *Response {
|
|||
resp.ErrorText = errcode.Message
|
||||
resp.IsSuccess = false
|
||||
resp.setData(Data)
|
||||
log.Error(resp.ErrorText)
|
||||
respLog.Error(resp.ErrorText)
|
||||
return resp
|
||||
}
|
||||
|
||||
|
@ -37,7 +39,8 @@ func (resp *Response) ErrorErr(Code int, err error, Data ...interface{}) *Respon
|
|||
resp.ErrorText = err.Error()
|
||||
resp.IsSuccess = false
|
||||
resp.setData(Data)
|
||||
log.Error(resp.ErrorText)
|
||||
|
||||
respLog.Error(resp.ErrorText)
|
||||
return resp
|
||||
}
|
||||
|
||||
|
@ -49,7 +52,7 @@ func (resp *Response) ErrorMsg(Code int, Message string, Data ...interface{}) *R
|
|||
resp.ErrorText = Message
|
||||
resp.IsSuccess = false
|
||||
resp.setData(Data)
|
||||
log.Error(resp.ErrorText)
|
||||
respLog.Error(resp.ErrorText)
|
||||
return resp
|
||||
}
|
||||
|
||||
|
|
|
@ -19,9 +19,9 @@ type levelSkip struct {
|
|||
|
||||
// JSONFormatter formats logs into parsable json
|
||||
type JSONFormatter struct {
|
||||
skip []*levelSkip
|
||||
|
||||
once sync.Once
|
||||
diffSkip int
|
||||
skip []*levelSkip
|
||||
once sync.Once
|
||||
}
|
||||
|
||||
// Format renders a single log entry
|
||||
|
@ -49,12 +49,11 @@ func (h *JSONFormatter) Format(e *logrus.Entry) ([]byte, error) {
|
|||
})
|
||||
|
||||
var fileinfo string
|
||||
if _, file, line, ok := runtime.Caller(skipOnce.Skip); ok {
|
||||
if _, file, line, ok := runtime.Caller(skipOnce.Skip + h.diffSkip); ok {
|
||||
if e.Level == logrus.InfoLevel {
|
||||
fileinfo = fmt.Sprintf("%s:%d", file, line)
|
||||
} else {
|
||||
ps := strings.Split(file, "/")
|
||||
ps = ps[len(ps)-4:]
|
||||
fileinfo = fmt.Sprintf("%s:%d", strings.Join(ps, "/"), line)
|
||||
}
|
||||
|
||||
|
|
|
@ -12,28 +12,34 @@ import (
|
|||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
var l *logrus.Logger
|
||||
var dlog *logrus.Logger
|
||||
|
||||
func init() {
|
||||
l = logrus.New()
|
||||
dlog = New(0)
|
||||
}
|
||||
|
||||
func New(skip int) *logrus.Logger {
|
||||
myl := logrus.New()
|
||||
|
||||
// 配置 Logstash 作为输出
|
||||
|
||||
l.AddHook(NewUTCTimeHook())
|
||||
myl.AddHook(NewUTCTimeHook())
|
||||
jf := &JSONFormatter{
|
||||
skip: make([]*levelSkip, len(logrus.AllLevels)),
|
||||
skip: make([]*levelSkip, len(logrus.AllLevels)),
|
||||
diffSkip: skip,
|
||||
}
|
||||
|
||||
for i := range jf.skip {
|
||||
jf.skip[i] = &levelSkip{}
|
||||
}
|
||||
|
||||
l.Formatter = jf
|
||||
myl.Formatter = jf
|
||||
myl.SetReportCaller(true)
|
||||
|
||||
// l.AddHook(&SkipHook{})
|
||||
|
||||
l.SetReportCaller(true)
|
||||
if dlog != nil {
|
||||
myl.SetOutput(dlog.Out)
|
||||
}
|
||||
|
||||
return myl
|
||||
}
|
||||
|
||||
type SkipHook struct {
|
||||
|
@ -99,154 +105,154 @@ func (hook *UTCTimeHook) Fire(entry *logrus.Entry) error {
|
|||
// this new returned entry.
|
||||
// If you want multiple fields, use `WithFields`.
|
||||
func WithField(key string, value interface{}) *logrus.Entry {
|
||||
return l.WithField(key, value)
|
||||
return dlog.WithField(key, value)
|
||||
}
|
||||
|
||||
// Adds a struct of fields to the log entry. All it does is call `WithField` for
|
||||
// each `Field`.
|
||||
func WithFields(fields logrus.Fields) *logrus.Entry {
|
||||
return l.WithFields(fields)
|
||||
return dlog.WithFields(fields)
|
||||
}
|
||||
|
||||
// Add an error as single field to the log entry. All it does is call
|
||||
// `WithError` for the given `error`.
|
||||
func WithError(err error) *logrus.Entry {
|
||||
|
||||
return l.WithError(err)
|
||||
return dlog.WithError(err)
|
||||
}
|
||||
|
||||
// Add a context to the log entry.
|
||||
func WithContext(ctx context.Context) *logrus.Entry {
|
||||
|
||||
return l.WithContext(ctx)
|
||||
return dlog.WithContext(ctx)
|
||||
}
|
||||
|
||||
// Overrides the time of the log entry.
|
||||
func WithTime(t time.Time) *logrus.Entry {
|
||||
|
||||
return l.WithTime(t)
|
||||
return dlog.WithTime(t)
|
||||
}
|
||||
|
||||
func Logf(level logrus.Level, format string, args ...interface{}) {
|
||||
l.Logf(level, format, args...)
|
||||
dlog.Logf(level, format, args...)
|
||||
}
|
||||
|
||||
func Tracef(format string, args ...interface{}) {
|
||||
l.Tracef(format, args...)
|
||||
dlog.Tracef(format, args...)
|
||||
}
|
||||
|
||||
func Debugf(format string, args ...interface{}) {
|
||||
l.Debugf(format, args...)
|
||||
dlog.Debugf(format, args...)
|
||||
}
|
||||
|
||||
func Infof(format string, args ...interface{}) {
|
||||
l.Infof(format, args...)
|
||||
dlog.Infof(format, args...)
|
||||
}
|
||||
|
||||
func Printf(format string, args ...interface{}) {
|
||||
l.Printf(format, args...)
|
||||
dlog.Printf(format, args...)
|
||||
}
|
||||
|
||||
func Warnf(format string, args ...interface{}) {
|
||||
l.Warnf(format, args...)
|
||||
dlog.Warnf(format, args...)
|
||||
}
|
||||
|
||||
func Warningf(format string, args ...interface{}) {
|
||||
l.Warningf(format, args...)
|
||||
dlog.Warningf(format, args...)
|
||||
}
|
||||
|
||||
func Errorf(format string, args ...interface{}) {
|
||||
l.Errorf(format, args...)
|
||||
dlog.Errorf(format, args...)
|
||||
}
|
||||
|
||||
func Fatalf(format string, args ...interface{}) {
|
||||
l.Fatalf(format, args...)
|
||||
dlog.Fatalf(format, args...)
|
||||
}
|
||||
|
||||
func Panicf(format string, args ...interface{}) {
|
||||
l.Panicf(format, args...)
|
||||
dlog.Panicf(format, args...)
|
||||
}
|
||||
|
||||
func Log(level logrus.Level, args ...interface{}) {
|
||||
l.Log(level, args...)
|
||||
dlog.Log(level, args...)
|
||||
}
|
||||
|
||||
func Trace(args ...interface{}) {
|
||||
l.Trace(args...)
|
||||
dlog.Trace(args...)
|
||||
}
|
||||
|
||||
func Debug(args ...interface{}) {
|
||||
l.Debug(args...)
|
||||
dlog.Debug(args...)
|
||||
}
|
||||
|
||||
func Info(args ...interface{}) {
|
||||
l.Info(args...)
|
||||
dlog.Info(args...)
|
||||
}
|
||||
|
||||
func Print(args ...interface{}) {
|
||||
l.Print(args...)
|
||||
dlog.Print(args...)
|
||||
}
|
||||
|
||||
func Warn(args ...interface{}) {
|
||||
l.Warn(args...)
|
||||
dlog.Warn(args...)
|
||||
}
|
||||
|
||||
func Warning(args ...interface{}) {
|
||||
l.Warning(args...)
|
||||
dlog.Warning(args...)
|
||||
}
|
||||
|
||||
func Error(args ...interface{}) {
|
||||
l.Error(args...)
|
||||
dlog.Error(args...)
|
||||
}
|
||||
|
||||
func Fatal(args ...interface{}) {
|
||||
l.Fatal(args...)
|
||||
dlog.Fatal(args...)
|
||||
}
|
||||
|
||||
func Panic(args ...interface{}) {
|
||||
l.Panic(args...)
|
||||
dlog.Panic(args...)
|
||||
}
|
||||
|
||||
func Logln(level logrus.Level, args ...interface{}) {
|
||||
l.Logln(level, args...)
|
||||
dlog.Logln(level, args...)
|
||||
}
|
||||
|
||||
func Traceln(args ...interface{}) {
|
||||
l.Traceln(args...)
|
||||
dlog.Traceln(args...)
|
||||
}
|
||||
|
||||
func Debugln(args ...interface{}) {
|
||||
l.Debugln(args...)
|
||||
dlog.Debugln(args...)
|
||||
}
|
||||
|
||||
func Infoln(args ...interface{}) {
|
||||
l.Infoln(args...)
|
||||
dlog.Infoln(args...)
|
||||
}
|
||||
|
||||
func Println(args ...interface{}) {
|
||||
l.Println(args...)
|
||||
dlog.Println(args...)
|
||||
}
|
||||
|
||||
func Warnln(args ...interface{}) {
|
||||
l.Warnln(args...)
|
||||
dlog.Warnln(args...)
|
||||
}
|
||||
|
||||
func Warningln(args ...interface{}) {
|
||||
l.Warningln(args...)
|
||||
dlog.Warningln(args...)
|
||||
}
|
||||
|
||||
func Errorln(args ...interface{}) {
|
||||
l.Errorln(args...)
|
||||
dlog.Errorln(args...)
|
||||
}
|
||||
|
||||
func Fatalln(args ...interface{}) {
|
||||
l.Fatalln(args...)
|
||||
dlog.Fatalln(args...)
|
||||
}
|
||||
|
||||
func Panicln(args ...interface{}) {
|
||||
l.Panicln(args...)
|
||||
dlog.Panicln(args...)
|
||||
}
|
||||
|
||||
func Exit(code int) {
|
||||
l.Exit(code)
|
||||
dlog.Exit(code)
|
||||
}
|
||||
|
|
|
@ -14,7 +14,7 @@ func DebuglnTrackTime(do func(), fargs ...interface{}) {
|
|||
var out []interface{}
|
||||
out = append(out, t)
|
||||
out = append(out, fargs...)
|
||||
l.Debugln(out...)
|
||||
dlog.Debugln(out...)
|
||||
}
|
||||
|
||||
func InfolnTrackTime(do func(), fargs ...interface{}) {
|
||||
|
@ -24,5 +24,5 @@ func InfolnTrackTime(do func(), fargs ...interface{}) {
|
|||
var out []interface{}
|
||||
out = append(out, t)
|
||||
out = append(out, fargs...)
|
||||
l.Infoln(out...)
|
||||
dlog.Infoln(out...)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user