base_common.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "hongze/hongze_robot/models"
  6. "net/http"
  7. "net/url"
  8. "github.com/astaxie/beego"
  9. "hongze/hongze_robot/utils"
  10. "rdluck_tools/log"
  11. )
  12. type BaseCommonController struct {
  13. beego.Controller
  14. }
  15. var apiLog *log.Log
  16. func init() {
  17. if utils.RunMode == "release" {
  18. logDir := `/data/rdlucklog/hongze_robot`
  19. apiLog = log.Init("20060102.api", logDir)
  20. } else {
  21. apiLog = log.Init("20060102.api")
  22. }
  23. }
  24. func (this *BaseCommonController) Prepare() {
  25. var requestBody string
  26. method := this.Ctx.Input.Method()
  27. if method == "GET" {
  28. requestBody = this.Ctx.Request.RequestURI
  29. } else {
  30. requestBody, _ = url.QueryUnescape(string(this.Ctx.Input.RequestBody))
  31. }
  32. ip := this.Ctx.Input.IP()
  33. apiLog.Println("请求地址:", this.Ctx.Input.URI(), "RequestBody:", requestBody, "IP:", ip)
  34. }
  35. func (c *BaseCommonController) ServeJSON(encoding ...bool) {
  36. var (
  37. hasIndent = false
  38. hasEncoding = false
  39. )
  40. if beego.BConfig.RunMode == beego.PROD {
  41. hasIndent = false
  42. }
  43. if len(encoding) > 0 && encoding[0] == true {
  44. hasEncoding = true
  45. }
  46. if c.Data["json"] == nil {
  47. go utils.SendEmail("异常提醒:"+utils.RunMode, "接口:"+"URI:"+c.Ctx.Input.URI()+";无返回值", utils.EmailSendToUsers)
  48. return
  49. }
  50. baseRes := c.Data["json"].(*models.BaseResponse)
  51. if baseRes != nil && !baseRes.Success && baseRes.IsSendEmail {
  52. go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "URI:"+c.Ctx.Input.URI()+" ErrMsg:"+baseRes.ErrMsg+";Msg"+baseRes.Msg, utils.EmailSendToUsers)
  53. }
  54. c.JSON(c.Data["json"], hasIndent, hasEncoding)
  55. }
  56. func (c *BaseCommonController) JSON(data interface{}, hasIndent bool, coding bool) error {
  57. c.Ctx.Output.Header("Content-Type", "application/json; charset=utf-8")
  58. var content []byte
  59. var err error
  60. if hasIndent {
  61. content, err = json.MarshalIndent(data, "", " ")
  62. } else {
  63. content, err = json.Marshal(data)
  64. }
  65. if err != nil {
  66. http.Error(c.Ctx.Output.Context.ResponseWriter, err.Error(), http.StatusInternalServerError)
  67. return err
  68. }
  69. ip := c.Ctx.Input.IP()
  70. params := c.Ctx.Input.Params()
  71. fmt.Println("params")
  72. fmt.Println(params)
  73. requestBody, _ := url.QueryUnescape(string(c.Ctx.Input.RequestBody))
  74. apiLog.Println("请求地址:", c.Ctx.Input.URI(), "Authorization:", c.Ctx.Input.Header("Authorization"), "RequestBody:", requestBody, "ResponseBody", string(content), "IP:", ip)
  75. if coding {
  76. content = []byte(utils.StringsToJSON(string(content)))
  77. }
  78. return c.Ctx.Output.Body(content)
  79. }