|
|
|
|
package app
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"reflect"
|
|
|
|
|
"server/call"
|
|
|
|
|
"server/common"
|
|
|
|
|
"server/config"
|
|
|
|
|
"server/util"
|
|
|
|
|
|
|
|
|
|
"github.com/liangdas/mqant/log"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func ParseUser(name string) (string, string) {
|
|
|
|
|
first := name[0]
|
|
|
|
|
// uid或者token(token全是大写)判断是哪个服
|
|
|
|
|
if (first >= 48 && first <= 57) || (first >= 65 && first <= 90) {
|
|
|
|
|
return "a", name
|
|
|
|
|
}
|
|
|
|
|
return name[0:1], name[1:]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ShouldRoute 是否需要转发到其他服
|
|
|
|
|
func (g *Gin) ShouldRoute(req interface{}, fieldName string, apiType int) bool {
|
|
|
|
|
ref := reflect.ValueOf(req).Elem().FieldByName(fieldName)
|
|
|
|
|
name := ref.String()
|
|
|
|
|
flag, userName := ParseUser(name)
|
|
|
|
|
log.Debug("flag:%v,userName:%v", flag, userName)
|
|
|
|
|
if config.GetBase().ServerFlag == flag {
|
|
|
|
|
if name != userName {
|
|
|
|
|
ref.SetString(userName)
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
server := call.GetConfigServerFlag(flag)
|
|
|
|
|
if server == nil {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
apiURL := fmt.Sprintf("%s%s", server.APIURL, g.Context.Request.URL.Path)
|
|
|
|
|
if apiType == common.ProviderAPITypeJson {
|
|
|
|
|
util.HttpPost(apiURL, req, g.RetData, nil)
|
|
|
|
|
} else {
|
|
|
|
|
util.HttpPostForm(apiURL, req, g.RetData, nil)
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ShouldRouteByAccount 是否需要转发到其他服
|
|
|
|
|
func (g *Gin) ShouldRouteByAccount(account *string, apiType int, req interface{}, headers map[string]string) bool {
|
|
|
|
|
flag, userName := ParseUser(*account)
|
|
|
|
|
log.Debug("flag:%v,userName:%v", flag, userName)
|
|
|
|
|
if config.GetBase().ServerFlag == flag {
|
|
|
|
|
if *account != userName {
|
|
|
|
|
*account = userName
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
server := call.GetConfigServerFlag(flag)
|
|
|
|
|
if server == nil {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
apiURL := fmt.Sprintf("%s%s", server.APIURL, g.Context.Request.URL.Path)
|
|
|
|
|
if apiType == common.ProviderAPITypeJson {
|
|
|
|
|
util.HttpPost(apiURL, req, g.RetData, headers)
|
|
|
|
|
} else {
|
|
|
|
|
util.HttpPostForm(apiURL, req, g.RetData, headers)
|
|
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|