Skip to content

Commit

Permalink
refactor: 重构了图书馆相关接口, 封装了request, 实现了请求日志
Browse files Browse the repository at this point in the history
  • Loading branch information
MangoGovo committed Dec 11, 2024
1 parent 4efa101 commit 011cee3
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 86 deletions.
11 changes: 7 additions & 4 deletions app/service/libraryService/borrowHistory.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@ package libraryService

import (
"funnel/app/apis/library"
"github.com/go-resty/resty/v2"
"funnel/app/errors"
"funnel/app/service/libraryService/request"
)

// GetBorrowHistory 获取图书馆当前借书记录
func GetBorrowHistory(username string, password string, page int) (interface{}, error) {
var ret Result
cookies, err := OAuthLogin(username, password)
if err != nil {
return nil, err
return nil, errors.ERR_WRONG_PASSWORD
}
client := resty.New()
_, err = client.R().

client := request.New()

_, err = client.Request().
SetBody(map[string]interface{}{
"page": page,
"rows": 10,
Expand Down
9 changes: 5 additions & 4 deletions app/service/libraryService/currentBorrow.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ package libraryService

import (
"funnel/app/apis/library"
"github.com/go-resty/resty/v2"
"funnel/app/errors"
"funnel/app/service/libraryService/request"
)

// GetCurrentBorrow 获取图书馆当前借书记录
func GetCurrentBorrow(username string, password string, page int) (interface{}, error) {
var ret Result
cookies, err := OAuthLogin(username, password)
if err != nil {
return nil, err
return nil, errors.ERR_WRONG_PASSWORD
}
client := resty.New()
_, err = client.R().
client := request.New()
_, err = client.Request().
SetBody(map[string]interface{}{
"page": page,
"rows": 10,
Expand Down
36 changes: 23 additions & 13 deletions app/service/libraryService/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,29 @@ package libraryService
import (
"bytes"
"funnel/app/apis/library"
"funnel/app/errors"
"funnel/app/service/libraryService/request"
"github.com/PuerkitoBio/goquery"
"github.com/go-resty/resty/v2"
"net/http"
"net/http/cookiejar"
"net/url"
)

// OAuthLogin 统一登陆
func OAuthLogin(username string, password string) ([]*http.Cookie, error) {
client := resty.New()
client := request.New()
// 使用cookieJar管理cookie
cookieJar, _ := cookiejar.New(nil)
client.SetCookieJar(cookieJar)

// 1. 初始化请求
if _, err := client.R().
EnableTrace().
if _, err := client.Request().
Get(library.OAuthChaoXingInit); err != nil {
return nil, err
}

// 2. 登陆参数生成
resp, err := client.R().
EnableTrace().
resp, err := client.Request().
Get(library.LoginFromOAuth)
if err != nil {
return nil, err
Expand All @@ -46,8 +52,7 @@ func OAuthLogin(username string, password string) ([]*http.Cookie, error) {
"_eventId": "submit",
}
// 3. 发送登陆请求
resp, err = client.R().
EnableTrace().
resp, err = client.Request().
SetFormData(loginParams).
Post(library.LoginFromOAuth)
if err != nil {
Expand All @@ -59,13 +64,18 @@ func OAuthLogin(username string, password string) ([]*http.Cookie, error) {
// resty只能自动处理header.Location中的重定向
redirect := GetRedirectLocation(resp.String())

resp, err = client.R().Get(redirect)
resp, err = client.Request().
Get(redirect)
if err != nil {
return nil, err
}
cookies := resp.Cookies()
if !CheckCookie(cookies) {
return nil, err

// 5. 提取指定域名下的session并构造cookie列表
u, _ := url.Parse(library.BaseUrl)
for _, cookie := range cookieJar.Cookies(u) {
if cookie.Name == "SESSION" {
return []*http.Cookie{cookie}, nil
}
}
return resp.Cookies(), nil
return nil, errors.ERR_WRONG_PASSWORD
}
25 changes: 25 additions & 0 deletions app/service/libraryService/request/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package request

import (
"funnel/app/service/libraryService/request/midware"
"github.com/go-resty/resty/v2"
)

type Client struct {
*resty.Client
}

func New() Client {
s := Client{
Client: resty.New(),
}
// 利用中间件实现请求日志
s.OnAfterResponse(midware.LogMiddleware)
return s
}

func (s Client) Request() *resty.Request {
return s.R().
EnableTrace().
SetHeader("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36")
}
19 changes: 19 additions & 0 deletions app/service/libraryService/request/midware/logMidware.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package midware

import (
"fmt"
"github.com/go-resty/resty/v2"
)

// LogMiddleware 日志中间件
func LogMiddleware(client *resty.Client, resp *resty.Response) error {
fmt.Println("==================================================================")
method := resp.Request.Method
url := resp.Request.URL
fmt.Printf("%s %s \n", method, url)
//fmt.Println(resp.Request.Header)
//fmt.Println(resp)
fmt.Printf("Time Spent: %v\n", resp.Time())

return nil
}
9 changes: 4 additions & 5 deletions app/service/libraryService/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ package libraryService

import (
"funnel/app/apis/library"
"funnel/app/service/libraryService/request"
"funnel/app/utils/security"
"github.com/go-resty/resty/v2"
)

// OauthLoginGetPublicKey 获取加密密钥
func OauthLoginGetPublicKey(client *resty.Client) ([]byte, error) {
resp, err := client.R().
EnableTrace().
func OauthLoginGetPublicKey(client request.Client) ([]byte, error) {
resp, err := client.Request().
Get(library.OAuthPublicKey)
if err != nil {
return []byte{}, err
Expand All @@ -18,7 +17,7 @@ func OauthLoginGetPublicKey(client *resty.Client) ([]byte, error) {
}

// GetEncryptedPwd 密码加密
func GetEncryptedPwd(client *resty.Client, password string) (string, error) {
func GetEncryptedPwd(client request.Client, password string) (string, error) {
key, err := OauthLoginGetPublicKey(client)
if err != nil {
return "", err
Expand Down
53 changes: 6 additions & 47 deletions app/service/libraryService/userInfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package libraryService

import (
"funnel/app/apis/library"
"github.com/go-resty/resty/v2"
"funnel/app/service/libraryService/request"
"net/http"
)

Expand All @@ -11,62 +11,21 @@ type UserInfo struct {
Message string `json:"message"`
ErrCode int `json:"errCode"`
ErrorCode interface{} `json:"errorCode"`
Data struct {
LastLibCode interface{} `json:"lastLibCode"`
BranchCode interface{} `json:"branchCode"`
DeskId interface{} `json:"deskId"`
MainFlag interface{} `json:"mainFlag"`
CxuId interface{} `json:"cxuId"`
UserId int `json:"userId"`
PrimaryId string `json:"primaryId"`
Name string `json:"name"`
PicUrl interface{} `json:"picUrl"`
JobDesc interface{} `json:"jobDesc"`
JobType interface{} `json:"jobType"`
Password string `json:"password"`
Unit interface{} `json:"unit"`
Department interface{} `json:"department"`
Position interface{} `json:"position"`
Gender string `json:"gender"`
PreferredLang string `json:"preferredLang"`
Email interface{} `json:"email"`
Address interface{} `json:"address"`
Phone string `json:"phone"`
PostalCode interface{} `json:"postalCode"`
BirthDate interface{} `json:"birthDate"`
Status string `json:"status"`
RegDate int64 `json:"regDate"`
ExpirationDate int64 `json:"expirationDate"`
PurgeDate interface{} `json:"purgeDate"`
StatusDate interface{} `json:"statusDate"`
LibCode string `json:"libCode"`
GroupCode string `json:"groupCode"`
CreateBy interface{} `json:"createBy"`
CreateDate interface{} `json:"createDate"`
UpdateBy interface{} `json:"updateBy"`
UpdateDate interface{} `json:"updateDate"`
AddType interface{} `json:"addType"`
BatchAddId interface{} `json:"batchAddId"`
CollegeYear interface{} `json:"collegeYear"`
CollegeClass interface{} `json:"collegeClass"`
CollegeDept interface{} `json:"collegeDept"`
VendorId interface{} `json:"vendorId"`
EmailCheckFlag interface{} `json:"emailCheckFlag"`
MobileCheckFlag interface{} `json:"mobileCheckFlag"`
} `json:"data"`
Data interface{} `json:"data"`
}

func GetUserInfo(cookies []*http.Cookie) (UserInfo, error) {
var userInfo UserInfo
client := resty.New()
_, err := client.R().
EnableTrace().
session := request.New()
_, err := session.Request().
SetCookies(cookies).
SetResult(&userInfo).
Post(library.UserInfo)

return userInfo, err
}

// CheckCookie 判断cookie是否还有效
func CheckCookie(cookies []*http.Cookie) bool {
userInfo, err := GetUserInfo(cookies)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion app/service/zfService/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func login(username string, password string) (*model.User, error) {
} else {
URL = apis.CAPTCHA_BREAKER_URL
}
captcha, err := f.Get(URL + "?session=" + f.Cookie[0].Value + "&route=" + f.Cookie[1].Value)
captcha, err := f.Get(URL + "?request=" + f.Cookie[0].Value + "&route=" + f.Cookie[1].Value)
if err != nil {
return nil, err
}
Expand Down
9 changes: 5 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ require (
github.com/PuerkitoBio/goquery v1.6.0
github.com/gin-gonic/gin v1.6.3
github.com/go-redis/redis v6.15.9+incompatible
github.com/go-resty/resty/v2 v2.16.2
github.com/spf13/viper v1.19.0
golang.org/x/net v0.23.0
golang.org/x/text v0.14.0
golang.org/x/net v0.27.0
golang.org/x/text v0.16.0
)

require (
Expand Down Expand Up @@ -40,9 +41,9 @@ require (
github.com/ugorji/go/codec v1.1.13 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/sys v0.22.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.3.0 // indirect
Expand Down
20 changes: 12 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ github.com/go-playground/validator/v10 v10.4.0 h1:72qIR/m8ybvL8L5TIyfgrigqkrw7kV
github.com/go-playground/validator/v10 v10.4.0/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4=
github.com/go-redis/redis v6.15.9+incompatible h1:K0pv1D7EQUjfyoMql+r/jZqCLizCGKFlFgcHWWmHQjg=
github.com/go-redis/redis v6.15.9+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA=
github.com/go-resty/resty/v2 v2.16.2 h1:CpRqTjIzq/rweXUt9+GxzzQdlkqMdt8Lm/fuK/CAbAg=
github.com/go-resty/resty/v2 v2.16.2/go.mod h1:0fHAoK7JoBy/Ch36N8VFeMsK7xQOHhvWaC3iOktwmIU=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8=
Expand Down Expand Up @@ -125,8 +127,8 @@ go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI=
go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA=
golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs=
golang.org/x/crypto v0.25.0 h1:ypSNr+bnYL2YhwoMt2zPxHFmbAN1KZs/njMG3hxUp30=
golang.org/x/crypto v0.25.0/go.mod h1:T+wALwcMOSE0kXgUAnPAHqTLW+XHgcELELW8VaDgm/M=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand All @@ -135,8 +137,8 @@ golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn
golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/net v0.0.0-20201006153459-a7d1128ccaa0/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs=
golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=
golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand All @@ -149,13 +151,15 @@ golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20200519105757-fe76b779f299/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
golang.org/x/time v0.6.0 h1:eTDhh4ZXt5Qf0augr54TN6suAUudPcawVZeIAPU7D4U=
golang.org/x/time v0.6.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 h1:H2TDz8ibqkAF6YGhCdN3jS9O0/s90v0rJh3X/OLHEUk=
Expand Down

0 comments on commit 011cee3

Please sign in to comment.