change layout
This commit is contained in:
+2
-2
@@ -1,6 +1,6 @@
|
||||
package utils
|
||||
|
||||
import "encoding/json"
|
||||
import jsoniter "github.com/json-iterator/go"
|
||||
|
||||
var _ Error = (*err)(nil)
|
||||
|
||||
@@ -53,7 +53,7 @@ func (e *err) ToString() string {
|
||||
Data: e.Data,
|
||||
}
|
||||
|
||||
raw, _ := json.Marshal(err)
|
||||
raw, _ := jsoniter.Marshal(err)
|
||||
return string(raw)
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
lonelog "lone-services/rpc/lonelog/pb"
|
||||
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
const (
|
||||
LogActionTypeAdd = 1 //增加
|
||||
LogActionTypeEdit = 2 //编辑
|
||||
LogActionTypeDel = 3 //删除
|
||||
LogActionTypeOut = 4 //强退
|
||||
LogActionTypeDown = 5 //下载
|
||||
LogActionTypeStatus = 6 //状态
|
||||
LogActionTypePassword = 7 //修改密码
|
||||
|
||||
//增加、删除、修改时需要改 services.log中的返回值
|
||||
|
||||
LogActionModuleAdmin = "管理员管理"
|
||||
LogActionModuleRole = "角色管理"
|
||||
LogActionModuleService = "服务管理"
|
||||
LogActionModuleAuthority = "权限管理"
|
||||
LogActionModuleProduct = "产品管理"
|
||||
LogActionModuleUser = "用户管理"
|
||||
LogActionModuleOrder = "订单管理"
|
||||
LogActionModuleSale = "销售管理"
|
||||
LogActionModuleTask = "提成与任务管理"
|
||||
LogActionModuleAccount = "结算管理"
|
||||
LogActionModulePay = "支付管理"
|
||||
LogActionModuleStore = "服务商管理"
|
||||
LogActionModuleExpress = "快递管理"
|
||||
LogActionModuleEquipment = "设备管理"
|
||||
LogActionModuleAd = "广告管理"
|
||||
LogActionModuleCustomService = "客服管理"
|
||||
LogActionModuleWorkWeChat = "企业微信管理"
|
||||
LogActionModuleOther = "其他管理"
|
||||
LogActionModulePrison = "监狱管理"
|
||||
LogActionModuleThirdParty = "第三方管理"
|
||||
LogActionModuleBill = "票据管理"
|
||||
LogActionModuleWarehouse = "仓库管理"
|
||||
LogActionModuleBuy = "采购管理"
|
||||
LogActionModuleLog = "日志管理"
|
||||
LogActionModuleFactory = "工厂管理"
|
||||
)
|
||||
|
||||
type ActionAdd struct {
|
||||
NewContent interface{} `json:"new_content"`
|
||||
OldContent interface{} `json:"old_content"`
|
||||
Reason string `json:"reason"`
|
||||
Type int32 `json:"type"`
|
||||
ModuleName string `json:"module_name"`
|
||||
}
|
||||
|
||||
const LoginReasonOK = "OK"
|
||||
|
||||
func GetActionStruct(adminInfo UserInfo, info ActionAdd) *lonelog.ActionLog {
|
||||
content := map[string]interface{}{
|
||||
"old_content": info.OldContent,
|
||||
"new_content": info.NewContent,
|
||||
}
|
||||
jsonData, _ := jsoniter.Marshal(content)
|
||||
return &lonelog.ActionLog{
|
||||
Content: string(jsonData),
|
||||
AdminId: adminInfo.ID,
|
||||
AdminName: adminInfo.Name,
|
||||
Type: info.Type,
|
||||
ModuleName: info.ModuleName,
|
||||
Ip: adminInfo.ClientIP,
|
||||
BrowserInfo: adminInfo.UserAgent,
|
||||
BrowserName: adminInfo.BrowserName,
|
||||
BrowserVersion: adminInfo.BrowserVer,
|
||||
Reason: info.Reason,
|
||||
CreateTime: Now().GoString(),
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func GetLogStruct(adminInfo UserInfo, status bool, reason string) *lonelog.LoginLog {
|
||||
st := 2
|
||||
if status {
|
||||
st = 1
|
||||
}
|
||||
|
||||
return &lonelog.LoginLog{
|
||||
AdminId: adminInfo.ID,
|
||||
Ip: adminInfo.ClientIP,
|
||||
Name: adminInfo.Name,
|
||||
BrowserInfo: adminInfo.UserAgent,
|
||||
BrowserName: adminInfo.BrowserName,
|
||||
BrowserVersion: adminInfo.BrowserVer,
|
||||
Status: int32(st),
|
||||
CreateTime: Now().GoString(),
|
||||
Reason: reason,
|
||||
}
|
||||
}
|
||||
+35
-30
@@ -5,15 +5,20 @@ import (
|
||||
"net/url"
|
||||
"strconv"
|
||||
|
||||
"github.com/mssola/user_agent"
|
||||
"google.golang.org/grpc/metadata"
|
||||
)
|
||||
|
||||
type UserInfo struct {
|
||||
ID int64
|
||||
Name string
|
||||
RawUID string
|
||||
Refresh string
|
||||
Valid bool
|
||||
ID int64
|
||||
Name string
|
||||
RawUID string
|
||||
Refresh string
|
||||
ClientIP string // 客户端ip
|
||||
UserAgent string // 完整原始ua
|
||||
BrowserName string // 浏览器名称
|
||||
BrowserVer string // 浏览器版本
|
||||
Valid bool
|
||||
}
|
||||
|
||||
func GetUserFromCtx(ctx context.Context) UserInfo {
|
||||
@@ -24,45 +29,45 @@ func GetUserFromCtx(ctx context.Context) UserInfo {
|
||||
uidList := md.Get("x-user-id")
|
||||
nameList := md.Get("x-user-name")
|
||||
refreshList := md.Get("x-refresh")
|
||||
ipList := md.Get("x-client-ip")
|
||||
userAgentList := md.Get("x-user-agent")
|
||||
|
||||
if len(refreshList) == 0 {
|
||||
if len(refreshList) == NumberZero {
|
||||
refreshList = md.Get("X-Refresh")
|
||||
}
|
||||
|
||||
rawUID := ""
|
||||
encodeName := ""
|
||||
refresh := ""
|
||||
var userInfo UserInfo
|
||||
if len(uidList) > 0 {
|
||||
rawUID = uidList[0]
|
||||
userInfo.RawUID = uidList[0]
|
||||
}
|
||||
if len(nameList) > 0 {
|
||||
encodeName = nameList[0]
|
||||
userInfo.Name = nameList[0]
|
||||
}
|
||||
if len(refreshList) > 0 {
|
||||
refresh = refreshList[0]
|
||||
userInfo.Refresh = refreshList[0]
|
||||
}
|
||||
|
||||
if rawUID == "" {
|
||||
return UserInfo{Valid: false, Refresh: refresh}
|
||||
if len(ipList) > 0 {
|
||||
userInfo.ClientIP = ipList[0]
|
||||
}
|
||||
if len(userAgentList) > 0 {
|
||||
userInfo.UserAgent = userAgentList[0]
|
||||
ua := user_agent.New(userInfo.UserAgent)
|
||||
userInfo.BrowserName, userInfo.BrowserVer = ua.Browser()
|
||||
}
|
||||
userInfo.Valid = false
|
||||
if userInfo.RawUID == StringEmpty {
|
||||
return userInfo
|
||||
}
|
||||
|
||||
uid, err := strconv.ParseInt(rawUID, 10, 64)
|
||||
uid, err := strconv.ParseInt(userInfo.RawUID, NumberTen, NumberSixtyFourth)
|
||||
if err != nil {
|
||||
return UserInfo{
|
||||
RawUID: rawUID,
|
||||
Name: encodeName,
|
||||
Refresh: refresh,
|
||||
Valid: false,
|
||||
}
|
||||
return userInfo
|
||||
}
|
||||
|
||||
userName, _ := url.QueryUnescape(encodeName)
|
||||
userName, _ := url.QueryUnescape(userInfo.Name)
|
||||
userInfo.ID = uid
|
||||
userInfo.Name = userName
|
||||
userInfo.Valid = true
|
||||
|
||||
return UserInfo{
|
||||
ID: uid,
|
||||
Name: userName,
|
||||
RawUID: rawUID,
|
||||
Refresh: refresh,
|
||||
Valid: true,
|
||||
}
|
||||
return userInfo
|
||||
}
|
||||
|
||||
+6
-5
@@ -1,10 +1,11 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
const YMDHIS = "2006-01-02 15:04:05"
|
||||
@@ -35,7 +36,7 @@ func GetStructKeys(obj any) []string {
|
||||
fileName, ok := objT.Field(i).Tag.Lookup("json")
|
||||
if ok {
|
||||
keys = append(keys, fileName)
|
||||
}else{
|
||||
} else {
|
||||
keys = append(keys, objT.Field(i).Name)
|
||||
}
|
||||
}
|
||||
@@ -55,7 +56,7 @@ func Struct2map(obj any) (data map[string]any, err error) {
|
||||
}
|
||||
if ok {
|
||||
data[fileName] = objV.Field(i).Interface()
|
||||
}else{
|
||||
} else {
|
||||
data[objT.Field(i).Name] = objV.Field(i).Interface()
|
||||
}
|
||||
}
|
||||
@@ -69,7 +70,7 @@ func IsString(obj interface{}) bool {
|
||||
return kind == reflect.String
|
||||
}
|
||||
|
||||
func CheckInArrInt(list []int, val int) {
|
||||
func CheckInArrInt(list []int, val int) {
|
||||
|
||||
}
|
||||
|
||||
@@ -85,7 +86,7 @@ func CheckInStr(list []string, val string) bool {
|
||||
|
||||
func JsonToStrArr(jsonStr string) ([]string, error) {
|
||||
var arr []string
|
||||
err := json.Unmarshal([]byte(jsonStr), &arr)
|
||||
err := jsoniter.Unmarshal([]byte(jsonStr), &arr)
|
||||
|
||||
return arr, err
|
||||
}
|
||||
|
||||
+17
-16
@@ -14,22 +14,23 @@ type LoginRedis struct {
|
||||
}
|
||||
|
||||
const (
|
||||
NumberMinusOne = -1
|
||||
NumberZero = 0
|
||||
NumberOne = 1
|
||||
NumberTwo = 2
|
||||
NumberThree = 3
|
||||
NumberFour = 4
|
||||
NumberFive = 5
|
||||
NumberSix = 6
|
||||
NumberSeven = 7
|
||||
NumberEight = 8
|
||||
NumberNine = 9
|
||||
NumberTen = 10
|
||||
NumberEleven = 11
|
||||
NumberTwelve = 12
|
||||
NumberSixteen = 16
|
||||
NumberThirtyTwo = 32
|
||||
NumberMinusOne = -1
|
||||
NumberZero = 0
|
||||
NumberOne = 1
|
||||
NumberTwo = 2
|
||||
NumberThree = 3
|
||||
NumberFour = 4
|
||||
NumberFive = 5
|
||||
NumberSix = 6
|
||||
NumberSeven = 7
|
||||
NumberEight = 8
|
||||
NumberNine = 9
|
||||
NumberTen = 10
|
||||
NumberEleven = 11
|
||||
NumberTwelve = 12
|
||||
NumberSixteen = 16
|
||||
NumberThirtyTwo = 32
|
||||
NumberSixtyFourth = 64
|
||||
|
||||
MaxSort uint16 = 999 //全局基础最大排序
|
||||
MinSort uint8 = 1 //全局基础最小排序
|
||||
|
||||
+4
-5
@@ -3,7 +3,6 @@ package utils
|
||||
import (
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
@@ -539,12 +538,12 @@ func Unique(s []string) []string {
|
||||
// data: 源 map(key 为字符串,value 为任意类型)
|
||||
// target: 目标 struct 指针(必须传指针,否则无法赋值
|
||||
func MapToStruct(data map[string]interface{}, target interface{}) error {
|
||||
jsonData, err := json.Marshal(data) // 将 map 转换为 JSON 字节切片
|
||||
jsonData, err := jsoniter.Marshal(data) // 将 map 转换为 JSON 字节切片
|
||||
if err != nil {
|
||||
Logger.Error("Error marshalling map to JSON:", err)
|
||||
return err
|
||||
}
|
||||
err = json.Unmarshal(jsonData, &target) // 将 JSON 字节切片解析到结构体中
|
||||
err = jsoniter.Unmarshal(jsonData, &target) // 将 JSON 字节切片解析到结构体中
|
||||
if err != nil {
|
||||
Logger.Error("Error unmarshalling JSON:", err)
|
||||
return err
|
||||
@@ -637,7 +636,7 @@ func ParseIntArray(value string) ([]int, error) {
|
||||
return result, nil
|
||||
}
|
||||
|
||||
err := json.Unmarshal(
|
||||
err := jsoniter.Unmarshal(
|
||||
[]byte(value),
|
||||
&result,
|
||||
)
|
||||
@@ -655,7 +654,7 @@ func ParseStringArray(value string) ([]string, error) {
|
||||
return result, nil
|
||||
}
|
||||
|
||||
err := json.Unmarshal(
|
||||
err := jsoniter.Unmarshal(
|
||||
[]byte(value),
|
||||
&result,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user