SussBi.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package outServer
  2. import (
  3. "bp.jydev.jianyu360.cn/BaseService/gateway/core/router"
  4. "bytes"
  5. "fmt"
  6. "github.com/gogf/gf/v2/net/ghttp"
  7. "github.com/gogf/gf/v2/util/gconv"
  8. "golang.org/x/net/publicsuffix"
  9. "io"
  10. "io/ioutil"
  11. "net/http"
  12. "net/http/cookiejar"
  13. "net/url"
  14. "strings"
  15. )
  16. type sussBi struct {
  17. addr string
  18. user string
  19. pwd string
  20. Url *url.URL
  21. jar *cookiejar.Jar
  22. }
  23. func InitSussBi(address, user, password string) *sussBi {
  24. sussCookie, _ := cookiejar.New(&cookiejar.Options{
  25. PublicSuffixList: publicsuffix.List,
  26. })
  27. u, _ := url.Parse(address)
  28. return &sussBi{
  29. addr: address,
  30. user: user,
  31. Url: u,
  32. pwd: password,
  33. jar: sussCookie,
  34. }
  35. }
  36. // AutoLogin 自动登录
  37. func (s *sussBi) AutoLogin() error {
  38. client := &http.Client{
  39. Jar: s.jar,
  40. }
  41. resp, err := client.Get(fmt.Sprintf("%s/?:user=%s&:password=%s", s.addr, s.user, s.pwd))
  42. if err != nil {
  43. return err
  44. }
  45. if !(resp.StatusCode == 302 || resp.StatusCode == 200) {
  46. return fmt.Errorf("自动登录异常")
  47. }
  48. return nil
  49. }
  50. // RequestLogin 装配登录状态
  51. func (s *sussBi) RequestLogin(r *ghttp.Request) error {
  52. if cookies := s.jar.Cookies(s.Url); len(cookies) > 0 {
  53. r.Request.AddCookie(cookies[0])
  54. }
  55. return nil
  56. }
  57. // CheckLoginOut 检测登录状态是否过期
  58. func (s *sussBi) CheckLoginOut(r *ghttp.Request) bool {
  59. if r.Response.Status == 401 {
  60. return true
  61. }
  62. return false
  63. }
  64. func (s *sussBi) Filter(r *ghttp.Request) error {
  65. ctx := router.GetGContext(r.GetCtx())
  66. if ctx.Sess.NewUid != 0 {
  67. if r.Request.Method == http.MethodPost {
  68. bodyBytes, err := io.ReadAll(r.Request.Body)
  69. if err != nil {
  70. return err
  71. }
  72. if len(bodyBytes) > 0 {
  73. replaceMap := map[string]interface{}{
  74. "jyUserPositionId": ctx.Sess.UserPositionId,
  75. "jyUserAccountId": ctx.Sess.UserAccountId,
  76. "jyEntPositionId": ctx.Sess.EntPositionId,
  77. "jyEntAccountId": ctx.Sess.EntAccountId,
  78. "jyUserName": ctx.Sess.UserName,
  79. "jyEntName": ctx.Sess.EntName,
  80. "jyEntId": ctx.Sess.EntId,
  81. }
  82. finalBytes := bytes.ReplaceAll(bodyBytes, []byte(`"jyUserId"`), []byte(fmt.Sprintf(`"%d"`, ctx.Sess.NewUid)))
  83. for k, v := range replaceMap {
  84. finalBytes = bytes.ReplaceAll(finalBytes, []byte(`"`+k+`"`), []byte(`"`+fmt.Sprint(v)+`"`))
  85. }
  86. r.ContentLength = gconv.Int64(len(finalBytes))
  87. r.Request.Header.Set("Content-Length", fmt.Sprintf("%d", len(finalBytes)))
  88. //fmt.Printf("before len:%d value:%s\nafter: len:%d value:%s\n", len(bodyBytes), string(bodyBytes), len(finalBytes), string(finalBytes))
  89. //fmt.Printf("header %+v\n", r.Request.Header)
  90. r.Request.Body = ioutil.NopCloser(bytes.NewReader(finalBytes))
  91. }
  92. } else if r.Request.Method == http.MethodGet {
  93. // 年终报告pdf增加职位id
  94. if strings.HasPrefix(r.RequestURI, "/api/meta/services/convertFileToPDF") {
  95. newValues, err := url.ParseQuery(r.URL.RawQuery)
  96. fmt.Printf("%v err:%v\n", newValues, err)
  97. if err == nil && len(newValues) > 0 {
  98. fmt.Println("b")
  99. if _, ok := newValues["QUERY_PARAM_M_POSITION_ID"]; ok {
  100. newValues["QUERY_PARAM_M_POSITION_ID"] = []string{fmt.Sprintf("%d", ctx.Sess.UserPositionId)}
  101. }
  102. if _, ok := newValues["position_id"]; ok {
  103. newValues["position_id"] = []string{fmt.Sprintf("%d", ctx.Sess.UserPositionId)}
  104. }
  105. r.URL.RawQuery = newValues.Encode()
  106. }
  107. }
  108. }
  109. }
  110. return nil
  111. }