91 lines
2.4 KiB
Go
91 lines
2.4 KiB
Go
package login
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"likedream-goframe/internal/application/httpclient"
|
|
"likedream-goframe/internal/common/cst"
|
|
"likedream-goframe/internal/common/errors"
|
|
"likedream-goframe/internal/utils"
|
|
|
|
"github.com/bytedance/sonic"
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
"github.com/gogf/gf/v2/os/gctx"
|
|
"github.com/samber/oops"
|
|
"github.com/tidwall/gjson"
|
|
"resty.dev/v3"
|
|
)
|
|
|
|
type CasSliderCaptchaReponse struct {
|
|
BigImage string `json:"bigImage"`
|
|
SmallImage string `json:"smallImage"`
|
|
TagWidth int `json:"tagWidth"`
|
|
YHeight int `json:"yHeight"`
|
|
}
|
|
|
|
type CasSliderResult struct {
|
|
ErrorCode int `json:"errorCode"`
|
|
ErrorMsg string `json:"errorMsg"`
|
|
}
|
|
|
|
func ResolveCasSliderCaptcha(ctx context.Context, captchaUrl string, verifyUrl string, client *resty.Client) {
|
|
res, err := client.R().
|
|
WithContext(ctx).
|
|
Get(captchaUrl)
|
|
utils.OkOrPanic(err, func(err error) error {
|
|
return oops.
|
|
With("captchaUrl", captchaUrl).
|
|
WithContext(ctx).
|
|
Wrap(err)
|
|
})
|
|
defer res.Body.Close()
|
|
bytes, _ := io.ReadAll(res.Body)
|
|
var captcha CasSliderCaptchaReponse
|
|
sonic.Unmarshal(bytes, &captcha)
|
|
//width := slider.VerifySliderWidthByBase64(captcha.BigImage, captcha.SmallImage)
|
|
width := ApiVerifySliderWidthByBase64(captcha.BigImage, captcha.SmallImage)
|
|
res2, err2 := client.R().
|
|
WithContext(ctx).
|
|
SetFormData(map[string]string{
|
|
"canvasLength": "280",
|
|
"moveLength": width,
|
|
}).
|
|
Post(verifyUrl)
|
|
utils.OkOrPanic(err2, func(err error) error {
|
|
return oops.
|
|
With("verifyUrl", verifyUrl).
|
|
WithContext(ctx).
|
|
Wrap(err)
|
|
})
|
|
defer res2.Body.Close()
|
|
bytes, _ = io.ReadAll(res2.Body)
|
|
str := string(bytes)
|
|
if len(str) > 0 {
|
|
var result CasSliderResult
|
|
sonic.Unmarshal(bytes, &result)
|
|
if result.ErrorCode != 1 {
|
|
utils.OkOrPanic(oops.WithContext(ctx).Wrap(errors.NewBusinessError(500, "滑块解析错误")))
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
func ApiVerifySliderWidthByBase64(bigImage string, smallImage string) string {
|
|
ctx := gctx.GetInitCtx()
|
|
baseUrl := g.Config().MustGetWithEnv(ctx, cst.ConfigCaptchaBaseUrl).String() + "/slider"
|
|
client := httpclient.Cpdaily.Clone(ctx)
|
|
client.SetDoNotParseResponse(false)
|
|
post, err := client.R().SetBody(map[string]any{
|
|
"big_image": bigImage,
|
|
"small_image": smallImage,
|
|
}).Post(baseUrl)
|
|
utils.OkOrPanic(err, func(err error) error {
|
|
return oops.
|
|
With("AutoOCRVerification", baseUrl).
|
|
WithContext(ctx).
|
|
Wrap(err)
|
|
})
|
|
result := post.String()
|
|
return gjson.Get(result, "data").String()
|
|
}
|