base_auth_xy.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package xy
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_hub/models"
  6. "eta/eta_hub/models/system"
  7. "eta/eta_hub/models/xy"
  8. "eta/eta_hub/utils"
  9. "fmt"
  10. "github.com/beego/beego/v2/server/web"
  11. "net/http"
  12. "net/url"
  13. "strings"
  14. )
  15. type BaseAuthXyController struct {
  16. web.Controller
  17. SysUser *system.Admin
  18. Appid string
  19. }
  20. func (this *BaseAuthXyController) Prepare() {
  21. fmt.Println("enter prepare")
  22. method := this.Ctx.Input.Method()
  23. uri := this.Ctx.Input.URI()
  24. ip := this.Ctx.Input.IP()
  25. fmt.Println("Url:", uri)
  26. if method != "HEAD" {
  27. var nonce, timestamp, appid, signature string
  28. headers := this.Ctx.Request.Header
  29. for name, values := range headers {
  30. fmt.Println(fmt.Sprintf("%s: %v\n", name, values))
  31. switch strings.ToLower(name) {
  32. case `nonce`:
  33. nonce = values[0]
  34. case `timestamp`:
  35. timestamp = values[0]
  36. case `appid`:
  37. appid = values[0]
  38. case `signature`:
  39. signature = values[0]
  40. }
  41. }
  42. //校验签名
  43. //nonce := this.Ctx.Input.Header("nonce")
  44. //timestamp := this.Ctx.Input.Header("timestamp")
  45. //appid := this.Ctx.Input.Header("appid")
  46. //signature := this.Ctx.Input.Header("signature")
  47. this.Appid = appid
  48. checkSign, errMsg, err := getCheckSignStr(appid, nonce, timestamp, ip)
  49. if err != nil {
  50. this.JSON(xy.BaseResponse{ReturnCode: "E", Status: "E", Msg: errMsg, ErrMsg: errMsg}, false, false)
  51. this.StopRun()
  52. return
  53. }
  54. if signature != checkSign {
  55. utils.FileLog.Debug("用户提交签名:%s;\n系统生成签名:%s\n", signature, checkSign)
  56. errMsg := "签名错误"
  57. this.JSON(xy.BaseResponse{ReturnCode: "E", Status: "E", Msg: errMsg, ErrMsg: errMsg}, false, false)
  58. this.StopRun()
  59. return
  60. }
  61. if method != "GET" && method != "POST" {
  62. errMsg := "无效的请求方式"
  63. this.JSON(xy.BaseResponse{ReturnCode: "E", Status: "E", Msg: "无效的请求方式", ErrMsg: errMsg}, false, false)
  64. this.StopRun()
  65. return
  66. }
  67. } else {
  68. this.JSON(xy.BaseResponse{ReturnCode: "E", Status: "E", Msg: "系统异常,请联系客服!", ErrMsg: "method:" + method}, false, false)
  69. this.StopRun()
  70. return
  71. }
  72. }
  73. func (c *BaseAuthXyController) ServeJSON(encoding ...bool) {
  74. var (
  75. hasIndent = false
  76. hasEncoding = false
  77. )
  78. if web.BConfig.RunMode == web.PROD {
  79. hasIndent = false
  80. }
  81. if len(encoding) > 0 && encoding[0] == true {
  82. hasEncoding = true
  83. }
  84. if c.Data["json"] == nil {
  85. //go utils.SendEmail("异常提醒:", "接口:"+"URI:"+c.Ctx.Input.URI()+";无返回值", utils.EmailSendToUsers)
  86. //body := "接口:" + "URI:" + c.Ctx.Input.URI() + ";无返回值"
  87. //go alarm_msg.SendAlarmMsg(body, 1)
  88. return
  89. }
  90. c.JSON(c.Data["json"], hasIndent, hasEncoding)
  91. }
  92. func (c *BaseAuthXyController) JSON(data interface{}, hasIndent bool, coding bool) error {
  93. c.Ctx.Output.Header("Content-Type", "application/json; charset=utf-8")
  94. var content []byte
  95. var err error
  96. if hasIndent {
  97. content, err = json.MarshalIndent(data, "", " ")
  98. } else {
  99. content, err = json.Marshal(data)
  100. }
  101. if err != nil {
  102. http.Error(c.Ctx.Output.Context.ResponseWriter, err.Error(), http.StatusInternalServerError)
  103. return err
  104. }
  105. ip := c.Ctx.Input.IP()
  106. requestBody, err := url.QueryUnescape(string(c.Ctx.Input.RequestBody))
  107. if err != nil {
  108. requestBody = string(c.Ctx.Input.RequestBody)
  109. }
  110. if requestBody == "" {
  111. requestBody = c.Ctx.Input.URI()
  112. }
  113. c.logUri(content, requestBody, ip)
  114. if coding {
  115. content = []byte(utils.StringsToJSON(string(content)))
  116. }
  117. return c.Ctx.Output.Body(content)
  118. }
  119. func (c *BaseAuthXyController) logUri(respContent []byte, requestBody, ip string) {
  120. var reqData interface{}
  121. err := json.Unmarshal([]byte(requestBody), &reqData)
  122. if err != nil {
  123. utils.ApiLog.Info("uri:%s, requestBody:%s, ip:%s", c.Ctx.Input.URI(), requestBody, ip)
  124. } else {
  125. utils.ApiLog.Info("uri:%s, requestBody:%s, ip:%s", c.Ctx.Input.URI(), requestBody, ip)
  126. }
  127. return
  128. }
  129. // getCheckSignStr
  130. // @Description: 获取校验签名字符串
  131. // @author: Roc
  132. // @datetime 2025-07-03 16:51:30
  133. // @param appid string
  134. // @param nonce string
  135. // @param timestamp string
  136. // @param ip string
  137. // @return checkSignStr string
  138. // @return errMsg string
  139. // @return err error
  140. func getCheckSignStr(appid, nonce, timestamp, ip string) (checkSignStr, errMsg string, err error) {
  141. if nonce == "" {
  142. errMsg = "随机字符串不能为空"
  143. err = errors.New(errMsg)
  144. return
  145. }
  146. if timestamp == "" {
  147. errMsg = "时间戳不能为空"
  148. err = errors.New(errMsg)
  149. return
  150. }
  151. if appid == "" {
  152. errMsg = "appid不能为空"
  153. err = errors.New(errMsg)
  154. return
  155. }
  156. secret := utils.Secret
  157. if appid != utils.AppId {
  158. openApiUserInfo, tmpErr := models.GetByAppid(appid)
  159. if tmpErr != nil {
  160. if utils.IsErrNoRow(tmpErr) {
  161. errMsg = "商家AppId错误,请核查"
  162. } else {
  163. err = errors.New("系统异常,请联系管理员")
  164. }
  165. err = errors.New(errMsg)
  166. return
  167. }
  168. if openApiUserInfo.Appid == `` {
  169. errMsg = "商家AppId错误,请核查"
  170. err = errors.New(errMsg)
  171. return
  172. }
  173. //如果有ip限制,那么就添加ip
  174. if openApiUserInfo.Ip != "" {
  175. if !strings.Contains(openApiUserInfo.Ip, ip) {
  176. errMsg = fmt.Sprintf("无权限访问该接口,ip:%v,请联系管理员", ip)
  177. err = errors.New(errMsg)
  178. return
  179. }
  180. }
  181. secret = openApiUserInfo.Secret
  182. }
  183. checkSignStr = utils.GetSignV2(nonce, timestamp, appid, secret)
  184. return
  185. }