|
@@ -6,24 +6,40 @@ import (
|
|
|
"eta/eta_pub/utils"
|
|
|
"fmt"
|
|
|
"github.com/beego/beego/v2/server/web"
|
|
|
+ "github.com/sirupsen/logrus"
|
|
|
"net/http"
|
|
|
"net/url"
|
|
|
+ "strings"
|
|
|
)
|
|
|
|
|
|
type BaseCommonController struct {
|
|
|
web.Controller
|
|
|
}
|
|
|
|
|
|
-func (this *BaseCommonController) Prepare() {
|
|
|
+func (c *BaseCommonController) Prepare() {
|
|
|
var requestBody string
|
|
|
- method := this.Ctx.Input.Method()
|
|
|
+ method := c.Ctx.Input.Method()
|
|
|
if method == "GET" {
|
|
|
- requestBody = this.Ctx.Request.RequestURI
|
|
|
+ requestBody = c.Ctx.Request.RequestURI
|
|
|
} else {
|
|
|
- requestBody, _ = url.QueryUnescape(string(this.Ctx.Input.RequestBody))
|
|
|
+ requestBody, _ = url.QueryUnescape(string(c.Ctx.Input.RequestBody))
|
|
|
+ }
|
|
|
+ ip := c.Ctx.Input.IP()
|
|
|
+ var reqData interface{}
|
|
|
+ err := json.Unmarshal([]byte(requestBody), &reqData)
|
|
|
+ if err != nil {
|
|
|
+ utils.ApiLog.WithFields(logrus.Fields{
|
|
|
+ "uri": c.Ctx.Input.URI(),
|
|
|
+ "requestBody": requestBody,
|
|
|
+ "ip": ip,
|
|
|
+ }).Info("Prepare 请求详情")
|
|
|
+ } else {
|
|
|
+ utils.ApiLog.WithFields(logrus.Fields{
|
|
|
+ "uri": c.Ctx.Input.URI(),
|
|
|
+ "requestBody": reqData,
|
|
|
+ "ip": ip,
|
|
|
+ }).Info("Prepare 请求详情")
|
|
|
}
|
|
|
- ip := this.Ctx.Input.IP()
|
|
|
- apiLog.Println("请求地址:", this.Ctx.Input.URI(), "RequestBody:", requestBody, "IP:", ip)
|
|
|
}
|
|
|
|
|
|
func (c *BaseCommonController) ServeJSON(encoding ...bool) {
|
|
@@ -63,10 +79,69 @@ func (c *BaseCommonController) JSON(data interface{}, hasIndent bool, coding boo
|
|
|
fmt.Println("params")
|
|
|
fmt.Println(params)
|
|
|
requestBody, _ := url.QueryUnescape(string(c.Ctx.Input.RequestBody))
|
|
|
- apiLog.Println("请求地址:", c.Ctx.Input.URI(), "Authorization:", c.Ctx.Input.Header("Authorization"), "RequestBody:", requestBody, "ResponseBody", string(content), "IP:", ip)
|
|
|
-
|
|
|
+ c.logUri(data, requestBody, ip)
|
|
|
if coding {
|
|
|
content = []byte(utils.StringsToJSON(string(content)))
|
|
|
}
|
|
|
return c.Ctx.Output.Body(content)
|
|
|
}
|
|
|
+
|
|
|
+func (c *BaseCommonController) logUri(data interface{}, requestBody, ip string) {
|
|
|
+ authorization := ""
|
|
|
+ method := c.Ctx.Input.Method()
|
|
|
+ uri := c.Ctx.Input.URI()
|
|
|
+ fmt.Println("Url:", uri)
|
|
|
+ if method != "HEAD" {
|
|
|
+ if method == "POST" || method == "GET" {
|
|
|
+ authorization = c.Ctx.Input.Header("authorization")
|
|
|
+ if authorization == "" {
|
|
|
+ authorization = c.Ctx.Input.Header("Authorization")
|
|
|
+ }
|
|
|
+ if authorization == "" {
|
|
|
+ newAuthorization := c.GetString("authorization")
|
|
|
+ if newAuthorization != `` {
|
|
|
+ authorization = "authorization=" + newAuthorization
|
|
|
+ } else {
|
|
|
+ newAuthorization = c.GetString("Authorization")
|
|
|
+ authorization = "authorization=" + newAuthorization
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if strings.Contains(authorization, ";") {
|
|
|
+ authorization = strings.Replace(authorization, ";", "$", 1)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if authorization == "" {
|
|
|
+ strArr := strings.Split(uri, "?")
|
|
|
+ for k, v := range strArr {
|
|
|
+ fmt.Println(k, v)
|
|
|
+ }
|
|
|
+ if len(strArr) > 1 {
|
|
|
+ authorization = strArr[1]
|
|
|
+ authorization = strings.Replace(authorization, "Authorization", "authorization", -1)
|
|
|
+ fmt.Println(authorization)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ var reqData interface{}
|
|
|
+ err := json.Unmarshal([]byte(requestBody), &reqData)
|
|
|
+ if err != nil {
|
|
|
+ utils.ApiLog.WithFields(logrus.Fields{
|
|
|
+ "uri": c.Ctx.Input.URI(),
|
|
|
+ "authorization": authorization,
|
|
|
+ "requestBody": requestBody,
|
|
|
+ "responseBody": data,
|
|
|
+ "ip": ip,
|
|
|
+ }).Info("请求详情")
|
|
|
+ } else {
|
|
|
+ utils.ApiLog.WithFields(logrus.Fields{
|
|
|
+ "uri": c.Ctx.Input.URI(),
|
|
|
+ "authorization": authorization,
|
|
|
+ "requestBody": reqData,
|
|
|
+ "responseBody": data,
|
|
|
+ "ip": ip,
|
|
|
+ }).Info("请求详情")
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|