Jelajahi Sumber

新增日志

hongze 8 bulan lalu
induk
melakukan
3ccb372625
3 mengubah file dengan 95 tambahan dan 1 penghapusan
  1. 47 0
      controllers/base_auth.go
  2. 45 1
      controllers/base_common.go
  3. 3 0
      services/sms.go

+ 47 - 0
controllers/base_auth.go

@@ -4,7 +4,10 @@ import (
 	"encoding/json"
 	"eta/eta_mini_api/models"
 	"eta/eta_mini_api/utils"
+	"fmt"
 	"net/http"
+	"net/url"
+	"strings"
 	"time"
 
 	"github.com/beego/beego/v2/server/web"
@@ -123,6 +126,9 @@ func (c *BaseAuthController) JSON(data interface{}, hasIndent bool, coding bool)
 		http.Error(c.Ctx.Output.Context.ResponseWriter, err.Error(), http.StatusInternalServerError)
 		return err
 	}
+	ip := c.Ctx.Input.IP()
+	requestBody, err := url.QueryUnescape(string(c.Ctx.Input.RequestBody))
+	c.logUri(content, requestBody, ip)
 	if utils.RunMode != "debug" {
 		content = utils.DesBase64Encrypt(content, utils.DesKey)
 		content = []byte(`"` + string(content) + `"`)
@@ -132,3 +138,44 @@ func (c *BaseAuthController) JSON(data interface{}, hasIndent bool, coding bool)
 	}
 	return c.Ctx.Output.Body(content)
 }
+
+func (c *BaseAuthController) logUri(respContent []byte, requestBody, ip string) {
+	authorization := ""
+	method := c.Ctx.Input.Method()
+	uri := c.Ctx.Input.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)
+				}
+			}
+		}
+	}
+
+	utils.ApiLog.Info("uri:%s, authorization:%s, requestBody:%s, responseBody:%s, ip:%s", c.Ctx.Input.URI(), authorization, requestBody, respContent, ip)
+	return
+}

+ 45 - 1
controllers/base_common.go

@@ -5,8 +5,10 @@ import (
 	"eta/eta_mini_api/models"
 	"eta/eta_mini_api/services"
 	"eta/eta_mini_api/utils"
+	"fmt"
 	"net/http"
 	"net/url"
+	"strings"
 
 	"github.com/beego/beego/v2/server/web"
 )
@@ -65,7 +67,8 @@ func (c *BaseCommonController) JSON(data interface{}, hasIndent bool, coding boo
 	}
 	ip := c.Ctx.Input.IP()
 	requestBody, _ := url.QueryUnescape(string(c.Ctx.Input.RequestBody))
-	utils.ApiLog.Info("请求地址:", c.Ctx.Input.URI(), "Authorization:", c.Ctx.Input.Header("Authorization"), "RequestBody:", requestBody, "ResponseBody", string(content), "IP:", ip)
+
+	c.logUri(content, requestBody, ip)
 
 	if coding {
 		content = []byte(utils.StringsToJSON(string(content)))
@@ -80,3 +83,44 @@ func (c *BaseCommonController) JSON(data interface{}, hasIndent bool, coding boo
 
 	return c.Ctx.Output.Body(content)
 }
+
+func (c *BaseCommonController) logUri(respContent []byte, requestBody, ip string) {
+	authorization := ""
+	method := c.Ctx.Input.Method()
+	uri := c.Ctx.Input.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)
+				}
+			}
+		}
+	}
+
+	utils.ApiLog.Info("uri:%s, authorization:%s, requestBody:%s, responseBody:%s, ip:%s", c.Ctx.Input.URI(), authorization, requestBody, respContent, ip)
+	return
+}

+ 3 - 0
services/sms.go

@@ -12,6 +12,9 @@ import (
 func SendSmsCode(mobile, vcode string) bool {
 	flag := false
 	tplId := utils.SMS_TPLID
+	if tplId == "" {
+		tplId = "262642"
+	}
 	expiresTime := "15"
 	result, err := sendSms(mobile, tplId, vcode, expiresTime)
 	if err != nil {