Pārlūkot izejas kodu

返回上传附件地址

xyxie 4 dienas atpakaļ
vecāks
revīzija
ec69b3b761
1 mainītis faili ar 41 papildinājumiem un 1 dzēšanām
  1. 41 1
      controllers/base_auth.go

+ 41 - 1
controllers/base_auth.go

@@ -471,5 +471,45 @@ func (c *BaseAuthController) ServeJSONOther(encoding ...bool) {
 		return
 	}
 	
-	c.JSON(c.Data["json"], hasIndent, hasEncoding)
+	c.JSONOther(c.Data["json"], hasIndent, hasEncoding)
+}
+
+func (c *BaseAuthController) JSONOther(data interface{}, hasIndent bool, coding bool) error {
+	c.Ctx.Output.Header("Content-Type", "application/json; charset=utf-8")
+	desEncrypt := utils.DesBase64Encrypt([]byte(utils.DesKey), utils.DesKeySalt)
+	c.Ctx.Output.Header("Dk", string(desEncrypt)) // des3加解密key
+	// 设置Cookie为HTTPOnly
+	c.Ctx.SetCookie("", "", -1, "/", "", false, true, "")
+	//fmt.Println(c.Ctx.ResponseWriter.Header().Get("Set-Cookie"))
+
+	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()
+	}
+	c.logUri(content, requestBody, ip)
+	// 如果不是debug分支的话,那么需要加密返回
+	// if utils.RunMode != "debug" {
+	// 	content = utils.DesBase64Encrypt(content, utils.DesKey)
+	// 	// get请求时,不加双引号就获取不到数据,不知道什么原因,所以还是在前后加上双引号吧
+	// 	content = []byte(`"` + string(content) + `"`)
+	// }
+	if coding {
+		content = []byte(utils.StringsToJSON(string(content)))
+	}
+	return c.Ctx.Output.Body(content)
 }