84 lines
2.4 KiB
Go
84 lines
2.4 KiB
Go
package xuexitong
|
|
|
|
import (
|
|
"context"
|
|
"likedream-goframe/internal/application/cookiejar"
|
|
"likedream-goframe/internal/application/httpclient"
|
|
"likedream-goframe/internal/application/xuexitong/cryptoutil"
|
|
"likedream-goframe/internal/framework/errors"
|
|
"likedream-goframe/internal/framework/logger"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/tidwall/gjson"
|
|
"go.uber.org/zap"
|
|
"resty.dev/v3"
|
|
)
|
|
|
|
const (
|
|
ApiLoginWeb = "https://passport2.chaoxing.com/fanyalogin"
|
|
)
|
|
|
|
type XuexitongLogin struct {
|
|
client *resty.Client
|
|
}
|
|
|
|
func NewXuexitongLogin(ctx context.Context) *XuexitongLogin {
|
|
return &XuexitongLogin{
|
|
client: httpclient.NewXuexitongClient(),
|
|
}
|
|
}
|
|
func (c *XuexitongLogin) Login(ctx context.Context, username, password string, retry int) (err error) {
|
|
jar := cookiejar.NewAllSavePersistCookieJar()
|
|
c.client.SetCookieJar(jar)
|
|
usernameEncrypted, err := cryptoutil.LoginEncrypt(username)
|
|
if err != nil {
|
|
logger.Logger.Error("LoginEncrypt err:", zap.Error(err))
|
|
return err
|
|
}
|
|
passwordEncrypted, err := cryptoutil.LoginEncrypt(password)
|
|
if err != nil {
|
|
logger.Logger.Error("LoginEncrypt err:", zap.Error(err))
|
|
return err
|
|
}
|
|
resp, err := c.client.R().SetFormData(map[string]string{
|
|
"fid": "-1",
|
|
"uname": usernameEncrypted,
|
|
"password": passwordEncrypted,
|
|
"refer": "http%3A%2F%2Fi.mooc.chaoxing.com",
|
|
"t": "true",
|
|
"forbidotherlogin": "0",
|
|
"validate": "",
|
|
"doubleFactorLogin": "0",
|
|
"independentId": "0",
|
|
"independentNameId": "0",
|
|
}).Post(ApiLoginWeb)
|
|
if err != nil {
|
|
logger.Logger.Error("LoginPost err:", zap.Error(err))
|
|
return err
|
|
}
|
|
body := resp.String()
|
|
//触发风控直接重新登录
|
|
if strings.Contains(body, "很抱歉,您所浏览的页面暂时不能访问!") && retry > 0 {
|
|
time.Sleep(time.Duration(retry*5) * time.Second)
|
|
return c.Login(ctx, username, password, retry-1)
|
|
}
|
|
if !gjson.Get(body, "status").Bool() {
|
|
return errors.NewBusinessErrorWithDetail(401, "登录失败", gjson.Get(body, "msg2").String())
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (c *XuexitongLogin) GetCookieJar() http.CookieJar {
|
|
return c.client.CookieJar()
|
|
}
|
|
func (c *XuexitongLogin) GetCookieString() string {
|
|
cookieJar := c.GetCookieJar()
|
|
serializableJar := cookieJar.(*cookiejar.PersistCookieJar)
|
|
return serializableJar.Serialize()
|
|
}
|
|
func (c *XuexitongLogin) GetClient() *resty.Client {
|
|
return c.client.SetRedirectPolicy(resty.NoRedirectPolicy())
|
|
}
|