base_auth_xy.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package xy
  2. import (
  3. "encoding/json"
  4. "eta/eta_hub/models/system"
  5. "eta/eta_hub/models/xy"
  6. "eta/eta_hub/utils"
  7. "fmt"
  8. "github.com/beego/beego/v2/server/web"
  9. "net/http"
  10. "net/url"
  11. "strings"
  12. )
  13. type BaseAuthXyController struct {
  14. web.Controller
  15. SysUser *system.Admin
  16. }
  17. func (this *BaseAuthXyController) Prepare() {
  18. fmt.Println("enter prepare")
  19. method := this.Ctx.Input.Method()
  20. uri := this.Ctx.Input.URI()
  21. fmt.Println("Url:", uri)
  22. if method != "HEAD" {
  23. var nonce, timestamp, appid, signature string
  24. headers := this.Ctx.Request.Header
  25. for name, values := range headers {
  26. fmt.Println(fmt.Sprintf("%s: %v\n", name, values))
  27. switch strings.ToLower(name) {
  28. case `nonce`:
  29. nonce = values[0]
  30. case `timestamp`:
  31. timestamp = values[0]
  32. case `appid`:
  33. appid = values[0]
  34. case `signature`:
  35. signature = values[0]
  36. }
  37. }
  38. //校验签名
  39. //nonce := this.Ctx.Input.Header("nonce")
  40. //timestamp := this.Ctx.Input.Header("timestamp")
  41. //appid := this.Ctx.Input.Header("appid")
  42. //signature := this.Ctx.Input.Header("signature")
  43. if nonce == "" {
  44. errMsg := "随机字符串不能为空"
  45. this.JSON(xy.BaseResponse{ReturnCode: "E", Status: "E", Msg: errMsg, ErrMsg: errMsg}, false, false)
  46. this.StopRun()
  47. return
  48. }
  49. if timestamp == "" {
  50. errMsg := "时间戳不能为空"
  51. this.JSON(xy.BaseResponse{ReturnCode: "E", Status: "E", Msg: errMsg, ErrMsg: errMsg}, false, false)
  52. this.StopRun()
  53. return
  54. }
  55. if appid != utils.AppId {
  56. errMsg := "商家AppId错误,请核查"
  57. this.JSON(xy.BaseResponse{ReturnCode: "E", Status: "E", Msg: errMsg, ErrMsg: errMsg}, false, false)
  58. this.StopRun()
  59. return
  60. }
  61. checkSign := utils.GetSign(nonce, timestamp)
  62. if signature != checkSign {
  63. utils.FileLog.Debug("用户提交签名:%s;\n系统生成签名:%s\n", signature, checkSign)
  64. errMsg := "签名错误"
  65. this.JSON(xy.BaseResponse{ReturnCode: "E", Status: "E", Msg: errMsg, ErrMsg: errMsg}, false, false)
  66. this.StopRun()
  67. return
  68. }
  69. if method != "GET" && method != "POST" {
  70. errMsg := "无效的请求方式"
  71. this.JSON(xy.BaseResponse{ReturnCode: "E", Status: "E", Msg: "无效的请求方式", ErrMsg: errMsg}, false, false)
  72. this.StopRun()
  73. return
  74. }
  75. } else {
  76. this.JSON(xy.BaseResponse{ReturnCode: "E", Status: "E", Msg: "系统异常,请联系客服!", ErrMsg: "method:" + method}, false, false)
  77. this.StopRun()
  78. return
  79. }
  80. }
  81. func (c *BaseAuthXyController) ServeJSON(encoding ...bool) {
  82. var (
  83. hasIndent = false
  84. hasEncoding = false
  85. )
  86. if web.BConfig.RunMode == web.PROD {
  87. hasIndent = false
  88. }
  89. if len(encoding) > 0 && encoding[0] == true {
  90. hasEncoding = true
  91. }
  92. if c.Data["json"] == nil {
  93. //go utils.SendEmail("异常提醒:", "接口:"+"URI:"+c.Ctx.Input.URI()+";无返回值", utils.EmailSendToUsers)
  94. //body := "接口:" + "URI:" + c.Ctx.Input.URI() + ";无返回值"
  95. //go alarm_msg.SendAlarmMsg(body, 1)
  96. return
  97. }
  98. c.JSON(c.Data["json"], hasIndent, hasEncoding)
  99. }
  100. func (c *BaseAuthXyController) JSON(data interface{}, hasIndent bool, coding bool) error {
  101. c.Ctx.Output.Header("Content-Type", "application/json; charset=utf-8")
  102. var content []byte
  103. var err error
  104. if hasIndent {
  105. content, err = json.MarshalIndent(data, "", " ")
  106. } else {
  107. content, err = json.Marshal(data)
  108. }
  109. if err != nil {
  110. http.Error(c.Ctx.Output.Context.ResponseWriter, err.Error(), http.StatusInternalServerError)
  111. return err
  112. }
  113. ip := c.Ctx.Input.IP()
  114. requestBody, err := url.QueryUnescape(string(c.Ctx.Input.RequestBody))
  115. if err != nil {
  116. requestBody = string(c.Ctx.Input.RequestBody)
  117. }
  118. if requestBody == "" {
  119. requestBody = c.Ctx.Input.URI()
  120. }
  121. c.logUri(content, requestBody, ip)
  122. if coding {
  123. content = []byte(utils.StringsToJSON(string(content)))
  124. }
  125. return c.Ctx.Output.Body(content)
  126. }
  127. func (c *BaseAuthXyController) logUri(respContent []byte, requestBody, ip string) {
  128. var reqData interface{}
  129. err := json.Unmarshal([]byte(requestBody), &reqData)
  130. if err != nil {
  131. utils.ApiLog.Info("uri:%s, requestBody:%s, ip:%s", c.Ctx.Input.URI(), requestBody, ip)
  132. } else {
  133. utils.ApiLog.Info("uri:%s, requestBody:%s, ip:%s", c.Ctx.Input.URI(), requestBody, ip)
  134. }
  135. return
  136. }