base_auth_mobile.go 5.9 KB

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