xyxie пре 11 месеци
родитељ
комит
9a90a01d1f
6 измењених фајлова са 145 додато и 5 уклоњено
  1. 1 1
      controllers/admin.go
  2. 1 2
      controllers/auth.go
  3. 129 0
      controllers/base_auth.go
  4. 1 1
      controllers/chart_info.go
  5. 1 1
      controllers/chart_theme.go
  6. 12 0
      utils/config.go

+ 1 - 1
controllers/admin.go

@@ -9,7 +9,7 @@ import (
 )
 
 type AdminController struct {
-	BaseCommonController
+	BaseAuthController
 }
 
 // Save

+ 1 - 2
controllers/auth.go

@@ -7,11 +7,10 @@ import (
 	"eta/eta_forum_hub/models/system"
 	"eta/eta_forum_hub/utils"
 	"fmt"
-	"github.com/beego/beego/v2/server/web"
 )
 
 type AuthController struct {
-	web.Controller
+	BaseAuthController
 }
 
 // CreateAuthCode

+ 129 - 0
controllers/base_auth.go

@@ -0,0 +1,129 @@
+package controllers
+
+import (
+	"encoding/json"
+	"eta/eta_forum_hub/models"
+	"eta/eta_forum_hub/services/alarm_msg"
+	"eta/eta_forum_hub/utils"
+	"fmt"
+	"net/http"
+	"net/url"
+
+	"github.com/beego/beego/v2/server/web"
+)
+
+func init() {
+}
+
+type BaseAuthController struct {
+	web.Controller
+}
+
+func (this *BaseAuthController) Prepare() {
+	fmt.Println("enter prepare")
+	method := this.Ctx.Input.Method()
+	uri := this.Ctx.Input.URI()
+	fmt.Println("Url:", uri)
+	if method != "HEAD" {
+		if method == "POST" {
+			authorization := this.Ctx.Input.Header("authorization")
+			if authorization == "" {
+				this.JSON(models.BaseResponse{Ret: 408, Msg: "请重新授权!", ErrMsg: "请重新授权:authorization is empty "}, false, false)
+				this.StopRun()
+				return
+			}
+			checkAuthorization := utils.MD5(utils.APP_NAME_EN + utils.Md5Key)
+			fmt.Println(checkAuthorization)
+			if authorization != checkAuthorization {
+				this.JSON(models.BaseResponse{Ret: 408, Msg: "签名错误!", ErrMsg: "签名错误:authorization is err "}, false, false)
+				this.StopRun()
+				return
+			}
+		} else {
+			this.JSON(models.BaseResponse{Ret: 408, Msg: "请求异常,请联系客服!", ErrMsg: "POST之外的请求,暂不支持"}, false, false)
+			this.StopRun()
+			return
+		}
+	} else {
+		this.JSON(models.BaseResponse{Ret: 408, Msg: "请求异常,请联系客服!", ErrMsg: "method:" + method}, false, false)
+		this.StopRun()
+		return
+	}
+}
+
+func (c *BaseAuthController) ServeJSON(encoding ...bool) {
+	// 方法处理完后,需要后置处理的业务逻辑
+	//if handlerList, ok := AfterHandlerUrlMap[c.Ctx.Request.URL.Path]; ok {
+	//	for _, handler := range handlerList {
+	//		handler(c.Ctx.Input.RequestBody)
+	//	}
+	//}
+
+	var (
+		hasIndent   = false
+		hasEncoding = false
+	)
+	if web.BConfig.RunMode == web.PROD {
+		hasIndent = false
+	}
+	if len(encoding) > 0 && encoding[0] == true {
+		hasEncoding = true
+	}
+	if c.Data["json"] == nil {
+		//go utils.SendEmail("异常提醒:", "接口:"+"URI:"+c.Ctx.Input.URI()+";无返回值", utils.EmailSendToUsers)
+		body := "接口:" + "URI:" + c.Ctx.Input.URI() + ";无返回值"
+		go alarm_msg.SendAlarmMsg(body, 1)
+		return
+	}
+
+	baseRes := c.Data["json"].(*models.BaseResponse)
+	if baseRes != nil && baseRes.Ret != 408 {
+		body, _ := json.Marshal(baseRes)
+		var requestBody string
+		method := c.Ctx.Input.Method()
+		if method == "GET" {
+			requestBody = c.Ctx.Request.RequestURI
+		} else {
+			requestBody, _ = url.QueryUnescape(string(c.Ctx.Input.RequestBody))
+		}
+		if baseRes.Ret != 200 && baseRes.IsSendEmail {
+			//go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "URI:"+c.Ctx.Input.URI()+"<br/> "+"Params"+requestBody+" <br/>"+"ErrMsg:"+baseRes.ErrMsg+";<br/>Msg:"+baseRes.Msg+";<br/> Body:"+string(body)+"<br/>"+c.SysUser.RealName, utils.EmailSendToUsers)
+			tip := "URI:" + c.Ctx.Input.URI() + "<br/> " + "Params" + requestBody + " <br/>" + "ErrMsg:" + baseRes.ErrMsg + ";<br/>Msg:" + baseRes.Msg + ";<br/> Body:" + string(body)
+			go alarm_msg.SendAlarmMsg(tip, 1)
+		}
+	}
+	c.JSON(c.Data["json"], hasIndent, hasEncoding)
+}
+
+func (c *BaseAuthController) JSON(data interface{}, hasIndent bool, coding bool) error {
+	c.Ctx.Output.Header("Content-Type", "application/json; charset=utf-8")
+	var content []byte
+	var err error
+	if hasIndent {
+		content, err = json.MarshalIndent(data, "", "  ")
+	} else {
+		content, err = json.Marshal(data)
+	}
+	if err != nil {
+		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))
+	if err != nil {
+		requestBody = string(c.Ctx.Input.RequestBody)
+	}
+	if requestBody == "" {
+		requestBody = c.Ctx.Input.URI()
+	}
+	authorization := c.Ctx.Input.Header("authorization")
+	if authorization == "" {
+		authorization = c.Ctx.Input.Header("Authorization")
+	}
+
+	utils.ApiLog.Info("uri:%s, authorization:%s, requestBody:%s, responseBody:%s, ip:%s", c.Ctx.Input.URI(), authorization, requestBody, content, ip)
+	if coding {
+		content = []byte(utils.StringsToJSON(string(content)))
+	}
+	return c.Ctx.Output.Body(content)
+}

+ 1 - 1
controllers/chart_info.go

@@ -10,7 +10,7 @@ import (
 )
 
 type ChartInfoController struct {
-	BaseCommonController
+	BaseAuthController
 }
 
 // Update

+ 1 - 1
controllers/chart_theme.go

@@ -9,7 +9,7 @@ import (
 // ChartThemeController
 // @Description: 图表主题配置模块
 type ChartThemeController struct {
-	BaseCommonController
+	BaseAuthController
 }
 
 // Save

+ 12 - 0
utils/config.go

@@ -22,6 +22,12 @@ var (
 	MgoDataCli    *mongo.Client // mongodb客户端连接
 	MgoDataDbName string        // mongodb指标数据的库名
 )
+var (
+	// APP_NAME_EN 项目英文名称
+	APP_NAME_EN string
+	// Md5Key 签名秘钥
+	Md5Key string
+)
 
 // ES配置
 var (
@@ -95,6 +101,11 @@ func init() {
 		panic(any("配置文件读取app_name_cn错误 " + err.Error()))
 	}
 	APP_NAME_CN = appNameCn
+	// 项目英文文名称
+	APP_NAME_EN = config["app_name_en"]
+	// 签名秘钥
+	Md5Key = config["md5_key"]
+
 	MYSQL_URL = config["mysql_url"]
 
 	// mongodb数据库连接配置
@@ -157,6 +168,7 @@ func init() {
 		DATA_INDEX_NAME = config["data_index_name"]
 		CHART_INDEX_NAME = config["chart_index_name"]
 	}
+
 }
 
 //修改接口文档