所以web请求统一使用一个req的client
This commit is contained in:
parent
40a192f18b
commit
498c7c24c1
25
lib/core.go
25
lib/core.go
|
@ -17,7 +17,6 @@ import (
|
||||||
|
|
||||||
qrcodeTerminal "github.com/Baozisoftware/qrcode-terminal-go"
|
qrcodeTerminal "github.com/Baozisoftware/qrcode-terminal-go"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/imroc/req/v3"
|
|
||||||
"github.com/makiuchi-d/gozxing"
|
"github.com/makiuchi-d/gozxing"
|
||||||
"github.com/makiuchi-d/gozxing/qrcode"
|
"github.com/makiuchi-d/gozxing/qrcode"
|
||||||
"github.com/nfnt/resize"
|
"github.com/nfnt/resize"
|
||||||
|
@ -28,6 +27,7 @@ import (
|
||||||
|
|
||||||
"github.com/johlanse/study_xxqg/conf"
|
"github.com/johlanse/study_xxqg/conf"
|
||||||
"github.com/johlanse/study_xxqg/model"
|
"github.com/johlanse/study_xxqg/model"
|
||||||
|
"github.com/johlanse/study_xxqg/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Core
|
// Core
|
||||||
|
@ -89,8 +89,7 @@ func (c *Core) Init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetToken(code, sign, pushId string) (bool, error) {
|
func GetToken(code, sign, pushId string) (bool, error) {
|
||||||
client := req.C()
|
client := utils.GetClient()
|
||||||
client.SetCommonHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36")
|
|
||||||
response, err := client.R().SetQueryParams(map[string]string{
|
response, err := client.R().SetQueryParams(map[string]string{
|
||||||
"code": code,
|
"code": code,
|
||||||
"state": sign + uuid.New().String(),
|
"state": sign + uuid.New().String(),
|
||||||
|
@ -134,12 +133,7 @@ func GetToken(code, sign, pushId string) (bool, error) {
|
||||||
* @return error
|
* @return error
|
||||||
*/
|
*/
|
||||||
func (c *Core) GenerateCode() (string, string, error) {
|
func (c *Core) GenerateCode() (string, string, error) {
|
||||||
client := req.C()
|
client := utils.GetClient()
|
||||||
client.OnAfterResponse(func(client *req.Client, response *req.Response) error {
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
client.SetCommonHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36")
|
|
||||||
|
|
||||||
g := new(gennerateResp)
|
g := new(gennerateResp)
|
||||||
_, err := client.R().SetResult(g).Get("https://login.xuexi.cn/user/qrcode/generate")
|
_, err := client.R().SetResult(g).Get("https://login.xuexi.cn/user/qrcode/generate")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -168,12 +162,7 @@ func (c *Core) GenerateCode() (string, string, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Core) CheckQrCode(code, pushID string) (*model.User, bool, error) {
|
func (c *Core) CheckQrCode(code, pushID string) (*model.User, bool, error) {
|
||||||
client := req.C()
|
client := utils.GetClient()
|
||||||
client.OnAfterResponse(func(client *req.Client, response *req.Response) error {
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
client.SetCommonHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36")
|
|
||||||
|
|
||||||
checkQrCode := func() (bool, string) {
|
checkQrCode := func() (bool, string) {
|
||||||
res := new(checkQrCodeResp)
|
res := new(checkQrCodeResp)
|
||||||
_, err := client.R().SetResult(res).SetFormData(map[string]string{
|
_, err := client.R().SetResult(res).SetFormData(map[string]string{
|
||||||
|
@ -239,12 +228,6 @@ func (c *Core) L(retryTimes int, pushID string) (*model.User, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
client := req.C()
|
|
||||||
client.OnAfterResponse(func(client *req.Client, response *req.Response) error {
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
client.SetCommonHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36")
|
|
||||||
|
|
||||||
for i := 0; i < 150; i++ {
|
for i := 0; i < 150; i++ {
|
||||||
user, b, err := c.CheckQrCode(codeData, pushID)
|
user, b, err := c.CheckQrCode(codeData, pushID)
|
||||||
if b && err == nil {
|
if b && err == nil {
|
||||||
|
|
62
lib/score.go
62
lib/score.go
|
@ -1,14 +1,14 @@
|
||||||
package lib
|
package lib
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/guonaihong/gout"
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
|
|
||||||
|
"github.com/johlanse/study_xxqg/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Score struct {
|
type Score struct {
|
||||||
|
@ -25,42 +25,64 @@ type Data struct {
|
||||||
func GetUserScore(cookies []*http.Cookie) (Score, error) {
|
func GetUserScore(cookies []*http.Cookie) (Score, error) {
|
||||||
var score Score
|
var score Score
|
||||||
var resp []byte
|
var resp []byte
|
||||||
// 获取用户总分
|
|
||||||
err := gout.GET(userTotalscoreUrl).SetCookies(cookies...).SetHeader(gout.H{
|
header := map[string]string{
|
||||||
"Cache-Control": "no-cache",
|
"Cache-Control": "no-cache",
|
||||||
}).BindBody(&resp).Do()
|
}
|
||||||
|
|
||||||
|
client := utils.GetClient()
|
||||||
|
response, err := client.R().SetCookies(cookies...).SetHeaders(header).Get(userTotalscoreUrl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorln("获取用户总分错误" + err.Error())
|
log.Errorln("获取用户总分错误" + err.Error())
|
||||||
|
|
||||||
return Score{}, err
|
return Score{}, err
|
||||||
}
|
}
|
||||||
data := string(resp)
|
resp = response.Bytes()
|
||||||
log.Infoln(data)
|
// 获取用户总分
|
||||||
if !gjson.GetBytes(resp, "ok").Bool() {
|
// err := gout.GET(userTotalscoreUrl).SetCookies(cookies...).SetHeader(gout.H{}).BindBody(&resp).Do()
|
||||||
return Score{}, errors.New("token check failed")
|
// if err != nil {
|
||||||
}
|
// log.Errorln("获取用户总分错误" + err.Error())
|
||||||
|
|
||||||
|
// return Score{}, err
|
||||||
|
// }
|
||||||
|
// data := string(resp)
|
||||||
|
// log.Infoln(data)
|
||||||
|
// if !gjson.GetBytes(resp, "ok").Bool() {
|
||||||
|
// return Score{}, errors.New("token check failed")
|
||||||
|
// }
|
||||||
// log.Debugln(gjson.GetBytes(resp, "@this|@pretty"))
|
// log.Debugln(gjson.GetBytes(resp, "@this|@pretty"))
|
||||||
score.TotalScore = int(gjson.GetBytes(resp, "data.score").Int())
|
score.TotalScore = int(gjson.GetBytes(resp, "data.score").Int())
|
||||||
|
|
||||||
// 获取用户今日得分
|
// 获取用户今日得分
|
||||||
err = gout.GET(userTodaytotalscoreUrl).SetCookies(cookies...).SetHeader(gout.H{
|
// err = gout.GET(userTodaytotalscoreUrl).SetCookies(cookies...).SetHeader(gout.H{
|
||||||
"Cache-Control": "no-cache",
|
// "Cache-Control": "no-cache",
|
||||||
}).BindBody(&resp).Do()
|
// }).BindBody(&resp).Do()
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
log.Errorln("获取用户每日总分错误" + err.Error())
|
// log.Errorln("获取用户每日总分错误" + err.Error())
|
||||||
|
|
||||||
|
// return Score{}, err
|
||||||
|
// }
|
||||||
|
response, err = client.R().SetCookies(cookies...).SetHeaders(header).Get(userTodaytotalscoreUrl)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorln("获取用户总分错误" + err.Error())
|
||||||
return Score{}, err
|
return Score{}, err
|
||||||
}
|
}
|
||||||
|
resp = response.Bytes()
|
||||||
// log.Debugln(gjson.GetBytes(resp, "@this|@pretty"))
|
// log.Debugln(gjson.GetBytes(resp, "@this|@pretty"))
|
||||||
score.TodayScore = int(gjson.GetBytes(resp, "data.score").Int())
|
score.TodayScore = int(gjson.GetBytes(resp, "data.score").Int())
|
||||||
|
|
||||||
err = gout.GET(userRatescoreUrl).SetCookies(cookies...).SetHeader(gout.H{
|
// err = gout.GET(userRatescoreUrl).SetCookies(cookies...).SetHeader(gout.H{
|
||||||
"Cache-Control": "no-cache",
|
// "Cache-Control": "no-cache",
|
||||||
}).BindBody(&resp).Do()
|
// }).BindBody(&resp).Do()
|
||||||
|
// if err != nil {
|
||||||
|
// log.Errorln("获取用户积分出现错误" + err.Error())
|
||||||
|
// return Score{}, err
|
||||||
|
// }
|
||||||
|
response, err = client.R().SetCookies(cookies...).SetHeaders(header).Get(userRatescoreUrl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorln("获取用户积分出现错误" + err.Error())
|
log.Errorln("获取用户总分错误" + err.Error())
|
||||||
return Score{}, err
|
return Score{}, err
|
||||||
}
|
}
|
||||||
|
resp = response.Bytes()
|
||||||
// log.Debugln(gjson.GetBytes(resp, "@this|@pretty"))
|
// log.Debugln(gjson.GetBytes(resp, "@this|@pretty"))
|
||||||
datas := gjson.GetBytes(resp, "data.taskProgress").Array()
|
datas := gjson.GetBytes(resp, "data.taskProgress").Array()
|
||||||
m := make(map[string]Data, 7)
|
m := make(map[string]Data, 7)
|
||||||
|
|
|
@ -0,0 +1,39 @@
|
||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/imroc/req/v3"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
var client *req.Client
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
client = req.C()
|
||||||
|
client.SetProxy(http.ProxyFromEnvironment)
|
||||||
|
if log.GetLevel() == log.DebugLevel {
|
||||||
|
client.DebugLog = true
|
||||||
|
}
|
||||||
|
client.SetLogger(&myLog{})
|
||||||
|
client.SetCommonHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36")
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetClient() *req.Client {
|
||||||
|
return client
|
||||||
|
}
|
||||||
|
|
||||||
|
type myLog struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m myLog) Errorf(format string, v ...interface{}) {
|
||||||
|
log.Errorf(format, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m myLog) Warnf(format string, v ...interface{}) {
|
||||||
|
log.Warnf(format, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m myLog) Debugf(format string, v ...interface{}) {
|
||||||
|
log.Debugf(format, v)
|
||||||
|
}
|
|
@ -1,35 +1 @@
|
||||||
package utils
|
package utils
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"sync"
|
|
||||||
|
|
||||||
"github.com/johlanse/study_xxqg/lib"
|
|
||||||
)
|
|
||||||
|
|
||||||
// 该文件的方法为保存当前正在学习的用户
|
|
||||||
|
|
||||||
var (
|
|
||||||
state sync.Map
|
|
||||||
)
|
|
||||||
|
|
||||||
func Add(uid string, core *lib.Core) error {
|
|
||||||
_, ok := state.Load(uid)
|
|
||||||
if ok {
|
|
||||||
return errors.New("the user is studying")
|
|
||||||
} else {
|
|
||||||
state.Store(uid, core)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func Delete(uid string) error {
|
|
||||||
state.Delete(uid)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func Item(item func(uid string, core *lib.Core) bool) {
|
|
||||||
state.Range(func(key, value interface{}) bool {
|
|
||||||
return item(key.(string), value.(*lib.Core))
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
|
@ -20,7 +20,6 @@ func Restart() {
|
||||||
cmd.Start()
|
cmd.Start()
|
||||||
os.Exit(3)
|
os.Exit(3)
|
||||||
}()
|
}()
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetAbout() string {
|
func GetAbout() string {
|
||||||
|
|
|
@ -291,8 +291,15 @@ func getLog() gin.HandlerFunc {
|
||||||
|
|
||||||
func sign() gin.HandlerFunc {
|
func sign() gin.HandlerFunc {
|
||||||
return func(ctx *gin.Context) {
|
return func(ctx *gin.Context) {
|
||||||
response, err := http.Get("https://pc-api.xuexi.cn/open/api/sns/sign") //nolint:bodyclose
|
response, err := utils.GetClient().R().Get("https://pc-api.xuexi.cn/open/api/sns/sign")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
ctx.JSON(403, Resp{
|
||||||
|
Code: 403,
|
||||||
|
Message: "",
|
||||||
|
Data: nil,
|
||||||
|
Success: false,
|
||||||
|
Error: err.Error(),
|
||||||
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ctx.Writer.WriteHeader(200)
|
ctx.Writer.WriteHeader(200)
|
||||||
|
|
|
@ -83,8 +83,8 @@ func RouterInit() *gin.Engine {
|
||||||
router.GET("/log", check(), getLog())
|
router.GET("/log", check(), getLog())
|
||||||
|
|
||||||
// 登录xxqg的三个接口
|
// 登录xxqg的三个接口
|
||||||
router.GET("/sign/", check(), sign())
|
router.GET("/sign/", sign())
|
||||||
router.GET("/login/*proxyPath", check(), generate())
|
router.GET("/login/*proxyPath", generate())
|
||||||
router.POST("/login/*proxyPath", check(), generate())
|
router.POST("/login/*proxyPath", check(), generate())
|
||||||
return router
|
return router
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue