base_auth.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/beego/beego/v2/server/web"
  6. "hongze/hongze_cygx/models"
  7. "hongze/hongze_cygx/services"
  8. "hongze/hongze_cygx/utils"
  9. "net/http"
  10. "net/url"
  11. "strconv"
  12. "strings"
  13. )
  14. //var apiLog *log.Log
  15. //func init() {
  16. // if utils.RunMode == "release" {
  17. // logDir := `/data/rdlucklog/hongze_cygx`
  18. // apiLog = log.Init("20060102.api", logDir)
  19. // } else {
  20. // apiLog = log.Init("20060102.api")
  21. // }
  22. //}
  23. type BaseAuthController struct {
  24. web.Controller
  25. User *models.WxUserItem
  26. Token string
  27. }
  28. func (this *BaseAuthController) Prepare() {
  29. //fmt.Println("enter prepare")
  30. method := this.Ctx.Input.Method()
  31. uri := this.Ctx.Input.URI()
  32. fmt.Println("Url:", uri)
  33. if method != "HEAD" {
  34. if method == "POST" || method == "GET" {
  35. authorization := this.Ctx.Input.Header("Authorization")
  36. inviteCompany := this.Ctx.Input.Header("From")
  37. if authorization == "" {
  38. authorization = this.GetString("Authorization")
  39. }
  40. this.Token = authorization
  41. if authorization == "" {
  42. this.JSON(models.BaseResponse{Ret: 408, Msg: "请重新授权!", ErrMsg: "请重新授权:Token is empty or account is empty"}, false, false)
  43. this.StopRun()
  44. return
  45. }
  46. session, err := models.GetSessionByToken(authorization)
  47. if err != nil {
  48. if err.Error() == utils.ErrNoRow() {
  49. this.JSON(models.BaseResponse{Ret: 408, Msg: "信息已变更,请重新登陆!", ErrMsg: "Token 信息已变更:Token: " + authorization}, false, false)
  50. this.StopRun()
  51. return
  52. }
  53. this.JSON(models.BaseResponse{Ret: 408, Msg: "网络异常,请稍后重试!", ErrMsg: "获取用户信息异常,Eerr:" + err.Error()}, false, false)
  54. this.StopRun()
  55. return
  56. }
  57. if session == nil {
  58. this.JSON(models.BaseResponse{Ret: 408, Msg: "网络异常,请稍后重试!", ErrMsg: "sesson is empty "}, false, false)
  59. this.StopRun()
  60. return
  61. }
  62. //wxUser, err := models.GetWxUserItemByUserId(session.UserId)
  63. wxUser, err := services.GetWxUserItemByOpenId(session.OpenId)
  64. if err != nil && err != services.ERR_USER_NOT_BIND {
  65. if err.Error() == utils.ErrNoRow() {
  66. this.JSON(models.BaseResponse{Ret: 408, Msg: "信息已变更,请重新登陆!", ErrMsg: "获取信息失败 " + strconv.Itoa(session.UserId)}, false, false)
  67. this.StopRun()
  68. return
  69. }
  70. this.JSON(models.BaseResponse{Ret: 408, Msg: "网络异常,请稍后重试!", ErrMsg: "获取wx_user信息异常,Eerr:" + err.Error()}, false, false)
  71. this.StopRun()
  72. return
  73. }
  74. if wxUser == nil {
  75. this.JSON(models.BaseResponse{Ret: 408, Msg: "网络异常,请稍后重试!", ErrMsg: "admin is empty "}, false, false)
  76. this.StopRun()
  77. return
  78. }
  79. wxUser.InviteCompany = inviteCompany
  80. this.User = wxUser
  81. go services.AddCygxPageHistoryRecord(this.User, this.Ctx)
  82. } else {
  83. this.JSON(models.BaseResponse{Ret: 408, Msg: "请求异常,请联系客服!", ErrMsg: "POST之外的请求,暂不支持"}, false, false)
  84. this.StopRun()
  85. return
  86. }
  87. }
  88. }
  89. func (c *BaseAuthController) ServeJSON(encoding ...bool) {
  90. var (
  91. hasIndent = false
  92. hasEncoding = false
  93. )
  94. if web.BConfig.RunMode == web.PROD {
  95. hasIndent = false
  96. }
  97. if len(encoding) > 0 && encoding[0] == true {
  98. hasEncoding = true
  99. }
  100. if c.Data["json"] == nil {
  101. go utils.SendEmail(utils.APPNAME+" "+utils.RunMode+"异常提醒:", "接口:"+"URI:"+c.Ctx.Input.URI()+";无返回值", utils.EmailSendToUsers)
  102. return
  103. }
  104. baseRes := c.Data["json"].(*models.BaseResponse)
  105. if baseRes != nil && baseRes.Ret != 200 && baseRes.Ret != 408 && baseRes.IsSendEmail {
  106. body, _ := json.Marshal(baseRes)
  107. msgBody := "URI:" + c.Ctx.Input.URI() + "<br/> ErrMsg:" + baseRes.ErrMsg + ";<br/>Msg:" + baseRes.Msg + ";<br/> Body:" + string(body) + ";<br/>" + c.Token
  108. go utils.SendEmail(utils.APPNAME+" "+utils.RunMode+" 失败提醒", msgBody, utils.EmailSendToUsers)
  109. }
  110. c.JSON(c.Data["json"], hasIndent, hasEncoding)
  111. }
  112. func (c *BaseAuthController) JSON(data interface{}, hasIndent bool, coding bool) error {
  113. c.Ctx.Output.Header("Content-Type", "application/json; charset=utf-8")
  114. var content []byte
  115. var err error
  116. if hasIndent {
  117. content, err = json.MarshalIndent(data, "", " ")
  118. } else {
  119. content, err = json.Marshal(data)
  120. }
  121. if err != nil {
  122. http.Error(c.Ctx.Output.Context.ResponseWriter, err.Error(), http.StatusInternalServerError)
  123. return err
  124. }
  125. ip := c.Ctx.Input.IP()
  126. requestBody, _ := url.QueryUnescape(string(c.Ctx.Input.RequestBody))
  127. c.logUri(content, requestBody, ip)
  128. //apiLog.Println("请求地址:", c.Ctx.Input.URI(), "Authorization:", c.Ctx.Input.Header("Authorization"), "RequestBody:", requestBody, "ResponseBody", string(content), "IP:", ip)
  129. if coding {
  130. content = []byte(utils.StringsToJSON(string(content)))
  131. }
  132. return c.Ctx.Output.Body(content)
  133. }
  134. func (c *BaseAuthController) logUri(respContent []byte, requestBody, ip string) {
  135. authorization := ""
  136. method := c.Ctx.Input.Method()
  137. uri := c.Ctx.Input.URI()
  138. //fmt.Println("Url:", uri)
  139. if method != "HEAD" {
  140. if method == "POST" || method == "GET" {
  141. authorization = c.Ctx.Input.Header("authorization")
  142. if authorization == "" {
  143. authorization = c.Ctx.Input.Header("Authorization")
  144. }
  145. if authorization == "" {
  146. newAuthorization := c.GetString("authorization")
  147. if newAuthorization != `` {
  148. authorization = "authorization=" + newAuthorization
  149. } else {
  150. newAuthorization = c.GetString("Authorization")
  151. authorization = "authorization=" + newAuthorization
  152. }
  153. } else {
  154. if strings.Contains(authorization, ";") {
  155. authorization = strings.Replace(authorization, ";", "$", 1)
  156. }
  157. }
  158. if authorization == "" {
  159. strArr := strings.Split(uri, "?")
  160. for k, v := range strArr {
  161. fmt.Println(k, v)
  162. }
  163. if len(strArr) > 1 {
  164. authorization = strArr[1]
  165. authorization = strings.Replace(authorization, "Authorization", "authorization", -1)
  166. fmt.Println(authorization)
  167. }
  168. }
  169. }
  170. }
  171. utils.ApiLog.Info("uri:%s, authorization:%s, requestBody:%s, responseBody:%s, ip:%s", c.Ctx.Input.URI(), authorization, requestBody, respContent, ip)
  172. return
  173. }