335 lines
9.1 KiB
Go
335 lines
9.1 KiB
Go
|
package utils
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"crypto/md5"
|
||
|
"encoding/json"
|
||
|
"errors"
|
||
|
"finclip-app-manager/domain/entity"
|
||
|
"finclip-app-manager/domain/entity/proto"
|
||
|
"fmt"
|
||
|
"github.com/gin-gonic/gin"
|
||
|
"gitlab.finogeeks.club/finclip-backend-v2/finclip-mgo/bson"
|
||
|
"strconv"
|
||
|
"strings"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
func GetNowMs() int64 {
|
||
|
return time.Now().UnixNano() / 1e6
|
||
|
}
|
||
|
|
||
|
func GetUserId(c *gin.Context) string {
|
||
|
return c.GetHeader(entity.ACCOUNT_ID_HEADER_KEY)
|
||
|
}
|
||
|
|
||
|
func GenMd5(s string) string {
|
||
|
return fmt.Sprintf("%x", md5.Sum([]byte(s)))
|
||
|
}
|
||
|
|
||
|
func Gen16Md5(s string) string {
|
||
|
return fmt.Sprintf("%x", md5.Sum([]byte(s)))[8:24]
|
||
|
}
|
||
|
|
||
|
func GetCommonParam(c *gin.Context) (bson.M, []string, bson.M, int, int, error) {
|
||
|
query := c.DefaultQuery("query", "{}")
|
||
|
sort := c.Query("sort")
|
||
|
searchFields := c.Query("searchFields")
|
||
|
searchText := c.Query("searchText")
|
||
|
pageSize := c.DefaultQuery("pageSize", "10")
|
||
|
pageNo := c.DefaultQuery("pageNo", "0")
|
||
|
pageSizeInt, err := strconv.Atoi(pageSize)
|
||
|
if err != nil {
|
||
|
return nil, nil, nil, 0, 0, err
|
||
|
}
|
||
|
pageNoInt, err := strconv.Atoi(pageNo)
|
||
|
if err != nil {
|
||
|
return nil, nil, nil, 0, 0, err
|
||
|
}
|
||
|
sortFilter, err := ParseSortString(sort)
|
||
|
if err != nil {
|
||
|
return nil, nil, nil, 0, 0, err
|
||
|
}
|
||
|
queryFilter, err := ParseQueryString(query)
|
||
|
if err != nil {
|
||
|
return nil, nil, nil, 0, 0, err
|
||
|
}
|
||
|
if searchFields != "" && searchText != "" {
|
||
|
searchFilter, err := ParseSearchString(searchText, searchFields)
|
||
|
if err != nil {
|
||
|
return nil, nil, nil, 0, 0, err
|
||
|
}
|
||
|
return queryFilter, sortFilter, searchFilter, pageSizeInt, pageNoInt, nil
|
||
|
}
|
||
|
|
||
|
return queryFilter, sortFilter, bson.M{}, pageSizeInt, pageNoInt, nil
|
||
|
}
|
||
|
|
||
|
func ParseSearchString(searchText string, searchFields string) (bson.M, error) {
|
||
|
fieldsArray := strings.Split(searchFields, ",")
|
||
|
result := []bson.M{}
|
||
|
for _, v := range fieldsArray {
|
||
|
regex := bson.RegEx{
|
||
|
Pattern: searchText,
|
||
|
Options: "i",
|
||
|
}
|
||
|
filter := bson.M{v: regex}
|
||
|
result = append(result, filter)
|
||
|
}
|
||
|
return bson.M{"$or": result}, nil
|
||
|
}
|
||
|
|
||
|
func ParseSortString(sortStr string) ([]string, error) {
|
||
|
if sortStr == "" {
|
||
|
return []string{}, nil
|
||
|
}
|
||
|
sortMap := bson.M{}
|
||
|
err := json.Unmarshal([]byte(sortStr), &sortMap)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
result := []string{}
|
||
|
for k, v := range sortMap {
|
||
|
value, ok := v.(float64)
|
||
|
if !ok {
|
||
|
return nil, errors.New(fmt.Sprint("Invalid sort value type: ", v))
|
||
|
}
|
||
|
if value == 1 {
|
||
|
result = append(result, k)
|
||
|
} else if value == -1 {
|
||
|
result = append(result, "-"+k)
|
||
|
} else {
|
||
|
return nil, errors.New(fmt.Sprint("Invalid sort value: ", v))
|
||
|
}
|
||
|
}
|
||
|
return result, nil
|
||
|
}
|
||
|
|
||
|
func ParseQueryString(queryStr string) (bson.M, error) {
|
||
|
result := bson.M{}
|
||
|
err := json.Unmarshal([]byte(queryStr), &result)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return result, nil
|
||
|
}
|
||
|
|
||
|
func InArry(s string, arry []string) bool {
|
||
|
for _, v := range arry {
|
||
|
if v == s {
|
||
|
return true
|
||
|
}
|
||
|
}
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
type MenuInfoFetcher interface {
|
||
|
GetMenuRspInfo(ctx context.Context) (*proto.MenuInfoRspData, error)
|
||
|
}
|
||
|
|
||
|
//func ConvertModelCustomDataToPb(
|
||
|
// ctx context.Context,
|
||
|
// rs MenuInfoFetcher,
|
||
|
// groupId string,
|
||
|
// data entity.CustomDataInfo,
|
||
|
// domainInfo *httpcall.DomainResponseData,
|
||
|
// sdkVer string,
|
||
|
//) (error, string, *pb.CustomData) {
|
||
|
// var err error
|
||
|
// hCaller := httpcall.NewClient()
|
||
|
// if domainInfo == nil {
|
||
|
// domainInfo, err = hCaller.GetDomainInfoByOrganId(ctx, groupId)
|
||
|
// if err != nil {
|
||
|
// logger.GetLogger().Errorf("InternalGetAppVerInfo ger domain info err:%s", err.Error())
|
||
|
// return err, utility.FS_GET_DOMAIN_INFO_ERROR, nil
|
||
|
// }
|
||
|
// }
|
||
|
//
|
||
|
// //menuRsp, err := GetMenuRspInfo(ctx)
|
||
|
// //if err != nil {
|
||
|
// // log.Errorf("GetDevInfo GetMenuRspInfo err:%+v", err)
|
||
|
// // return err, utility.FS_SERVER_ERR, nil
|
||
|
// //}
|
||
|
//
|
||
|
// apiData, err := hCaller.GetApiList(ctx, groupId)
|
||
|
// if err != nil {
|
||
|
// logger.GetLogger().Errorf("get api list err:%s", err.Error())
|
||
|
// return err, utility.FS_SERVER_ERR, nil
|
||
|
// }
|
||
|
// res := new(pb.CustomData)
|
||
|
// res.ApiInfo = nil
|
||
|
// if apiData != nil {
|
||
|
// res.ApiInfo = make([]*pb.ApiInfoRspData, 0)
|
||
|
// for _, v := range apiData {
|
||
|
// res.ApiInfo = append(res.ApiInfo, &pb.ApiInfoRspData{Url: v.Url, ApiName: v.ApiName})
|
||
|
// }
|
||
|
// }
|
||
|
// //if menuRsp != nil {
|
||
|
// // res.MenuInfo = new(pb.MenuInfoRspData)
|
||
|
// // res.MenuInfo.Total = int32(menuRsp.Total)
|
||
|
// // res.MenuInfo.List = make([]*pb.MenuInfoRspDataItem, 0)
|
||
|
// // for _, v := range menuRsp.List {
|
||
|
// // res.MenuInfo.List = append(res.MenuInfo.List, &pb.MenuInfoRspDataItem{Name: v.Name, Id: v.ID, Image: v.Image})
|
||
|
// // }
|
||
|
// //}
|
||
|
//
|
||
|
// res.DetailDescription = data.DetailDescription
|
||
|
// res.VersionDescription = data.VersionDescription
|
||
|
// res.SourceFile = make([]*pb.SourceFileData, 0)
|
||
|
// for _, v := range data.SourceFile {
|
||
|
// item := &pb.SourceFileData{
|
||
|
// FileMd5: v.FileMd5,
|
||
|
// Name: v.Name,
|
||
|
// UploadDate: v.UploadDate,
|
||
|
// Url: v.Url,
|
||
|
// SourceFileUrl: "",
|
||
|
// BasicPackVer: v.BasicPackVer,
|
||
|
// }
|
||
|
// if sdkVer != "" {
|
||
|
// isEncry := false
|
||
|
// if sdkVer == "cache" {
|
||
|
// isEncry = true
|
||
|
// } else {
|
||
|
// isEncry, err = GetSdkVerEncryResult(ctx, sdkVer)
|
||
|
// if err != nil {
|
||
|
// return err, utility.FS_SERVER_ERR, nil
|
||
|
// }
|
||
|
// }
|
||
|
// if isEncry && v.EncryptedUrl != "" && v.EncryptedFileMd5 != "" {
|
||
|
// item.Url = v.EncryptedUrl
|
||
|
// item.FileMd5 = v.EncryptedFileMd5
|
||
|
// }
|
||
|
// }
|
||
|
// for _, val := range v.EncryptPackages {
|
||
|
// item.Packages = append(item.Packages, &pb.Package{
|
||
|
// Root: val.Root,
|
||
|
// Name: val.Name,
|
||
|
// Pages: val.Pages,
|
||
|
// Independent: val.Independent,
|
||
|
// Filename: val.Filename,
|
||
|
// FileUrl: val.FileUrl,
|
||
|
// FileMd5: val.FileMd5,
|
||
|
// })
|
||
|
// }
|
||
|
// res.SourceFile = append(res.SourceFile, item)
|
||
|
// }
|
||
|
// res.AppRuntimeDomain = GetPbRuntimeDomains(domainInfo)
|
||
|
// return nil, "", res
|
||
|
//}
|
||
|
|
||
|
//func GetSdkVerEncryResult(ctx context.Context, sdkVer string) (bool, error) {
|
||
|
// var isEncry bool
|
||
|
// var err error
|
||
|
// /*isEncry, ok := cache.GetSdkVerResult(ctx, sdkVer)
|
||
|
// if ok {
|
||
|
// return isEncry, nil
|
||
|
// }*/
|
||
|
// switch config.Cfg.ReqType {
|
||
|
// case "grpc":
|
||
|
// isEncry, err = grpc.GetSdkVerEncrypted(ctx, sdkVer)
|
||
|
// default:
|
||
|
// isEncry, err = httpcall.NewClient().GetSdkVerIsEncrypted(ctx, sdkVer)
|
||
|
// }
|
||
|
// if err != nil {
|
||
|
// return false, err
|
||
|
// }
|
||
|
// //cache.SetSdkVerResult(ctx, sdkVer, isEncry)
|
||
|
// return isEncry, nil
|
||
|
//}
|
||
|
|
||
|
//func GetPbRuntimeDomains(domainInfo *httpcall.DomainResponseData) *pb.AppRuntimeDomain {
|
||
|
// result := &pb.AppRuntimeDomain{}
|
||
|
// result.Business = &pb.AppRuntimeDomain_Business{Domains: make([]string, 0)}
|
||
|
// if domainInfo.Business.Domains != nil {
|
||
|
// result.Business.Domains = domainInfo.Business.Domains
|
||
|
// }
|
||
|
// result.Service = &pb.AppRuntimeDomain_Service{
|
||
|
// Download: make([]string, 0),
|
||
|
// Request: make([]string, 0),
|
||
|
// Socket: make([]string, 0),
|
||
|
// Upload: make([]string, 0),
|
||
|
// }
|
||
|
// if domainInfo.Service.Download != nil {
|
||
|
// result.Service.Download = domainInfo.Service.Download
|
||
|
// }
|
||
|
// if domainInfo.Service.Request != nil {
|
||
|
// result.Service.Request = domainInfo.Service.Request
|
||
|
// }
|
||
|
// if domainInfo.Service.Socket != nil {
|
||
|
// result.Service.Socket = domainInfo.Service.Socket
|
||
|
// }
|
||
|
// if domainInfo.Service.Download != nil {
|
||
|
// result.Service.Upload = domainInfo.Service.Upload
|
||
|
// }
|
||
|
//
|
||
|
// result.Whitelist = &pb.AppRuntimeDomain_Whitelist{Domains: make([]string, 0)}
|
||
|
// if domainInfo.Whitelist.Domains != nil {
|
||
|
// result.Whitelist.Domains = domainInfo.Whitelist.Domains
|
||
|
// }
|
||
|
// result.Blacklist = &pb.AppRuntimeDomain_Blacklist{Domains: make([]string, 0)}
|
||
|
// if domainInfo.Blacklist.Domains != nil {
|
||
|
// result.Blacklist.Domains = domainInfo.Blacklist.Domains
|
||
|
// }
|
||
|
// return result
|
||
|
//}
|
||
|
|
||
|
//func GetMenuRspInfo(ctx context.Context) (*proto.MenuInfoRspData, error) {
|
||
|
// //非私有化环境置为nil
|
||
|
// if !config.Cfg.IsPrivateEnv() {
|
||
|
// return nil, nil
|
||
|
// }
|
||
|
// /*menuCreated, err := rs.menuCache.MenuHasCreated(ctx)
|
||
|
// if err != nil {
|
||
|
// return nil, err
|
||
|
// }
|
||
|
// if !menuCreated {
|
||
|
// return nil, nil
|
||
|
// }*/
|
||
|
// result := new(proto.MenuInfoRspData)
|
||
|
// result.List = make([]proto.MenuInfoRspDataItem, 0)
|
||
|
// /*cacheRsp, err := rs.menuCache.Get(ctx)
|
||
|
// if err == nil {
|
||
|
// result.Total = cacheRsp.Total
|
||
|
// for _, m := range cacheRsp.List {
|
||
|
// item := proto.MenuInfoRspDataItem{}
|
||
|
// item.ID = m.ID
|
||
|
// item.Name = m.Name
|
||
|
// item.Image = m.Image
|
||
|
// result.List = append(result.List, item)
|
||
|
// }
|
||
|
// return result, nil
|
||
|
// }*/
|
||
|
//
|
||
|
// menuCacheInfo := apiproto.GetAllMenuRsp{}
|
||
|
//
|
||
|
// repoRspList, total, err := impl.InitMenuInfoRepo().GetAllMenuList(ctx, []string{"sortNum"})
|
||
|
// if err != nil {
|
||
|
// return nil, err
|
||
|
// }
|
||
|
// result.Total = total
|
||
|
// menuCacheInfo.Total = total
|
||
|
// for _, m := range repoRspList {
|
||
|
// item := proto.MenuInfoRspDataItem{}
|
||
|
// item.ID = m.InfoId
|
||
|
// item.Name = m.Name
|
||
|
// item.Image = m.ImageUrl
|
||
|
// result.List = append(result.List, item)
|
||
|
//
|
||
|
// cacheItem := apiproto.GetAllMenuRspItem{
|
||
|
// TraceId: m.TraceId,
|
||
|
// Name: m.Name,
|
||
|
// ID: m.InfoId,
|
||
|
// Image: m.ImageUrl,
|
||
|
// SortNum: m.SortNum,
|
||
|
// CreateTime: m.CreateTime,
|
||
|
// UpdateTime: m.UpdateTime,
|
||
|
// UpdateOperator: m.UpdateOperator,
|
||
|
// }
|
||
|
// menuCacheInfo.List = append(menuCacheInfo.List, cacheItem)
|
||
|
// }
|
||
|
//
|
||
|
// //rs.menuCache.Set(ctx, menuCacheInfo)
|
||
|
//
|
||
|
// return result, nil
|
||
|
//}
|