SussBi.go 2.6 KB

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