Files
likedream/internal/application/cpdaily/login/ocr.go
T
2025-12-27 14:36:16 +08:00

64 lines
1.5 KiB
Go

package login
import (
"context"
"io"
"likedream-goframe/internal/application/captcha"
"likedream-goframe/internal/utils"
"github.com/samber/oops"
"resty.dev/v3"
)
func ResolveOcrCaptcha(ctx context.Context, url string, client *resty.Client) string {
byteData := DownCode2Byte(ctx, url, client)
return captcha.ApiAutoOCRVerification(byteData)
}
func ResolveCalcCaptcha(ctx context.Context, url string, client *resty.Client) (string, string) {
b64, uid := DownBase64(ctx, url, client)
return captcha.ApiAutoCalcVerification(b64), uid
}
func DownBase64(ctx context.Context, url string, client *resty.Client) (string, string) {
res, err := client.R().
WithContext(ctx).
Get(url)
utils.OkOrPanic(err, func(err error) error {
return oops.
With("captchaUrl", url).
WithContext(ctx).
Wrap(err)
})
defer res.Body.Close()
type CaptchResponse struct {
KaptchaType string `json:"kaptchaType"`
UID string `json:"uid"`
Content string `json:"content"`
Timeout int `json:"timeout"`
}
json := utils.ReadJson[CaptchResponse](res.Body)
return json.Content, json.UID
}
func DownCode2Byte(ctx context.Context, url string, client *resty.Client) []byte {
res, err := client.R().
WithContext(ctx).
Get(url)
utils.OkOrPanic(err, func(err error) error {
return oops.
With("captchaUrl", url).
WithContext(ctx).
Wrap(err)
})
defer res.Body.Close()
all, err := io.ReadAll(res.Body)
utils.OkOrPanic(err, func(err error) error {
return oops.
With("captchaUrl", url).
WithContext(ctx).
Wrap(err)
})
return all
}