base_auth.go 6.2 KB

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