183 lines
5.1 KiB
Go
183 lines
5.1 KiB
Go
package submitcaptcha
|
|
|
|
//
|
|
//import (
|
|
// "context"
|
|
// "io"
|
|
// "likedream-goframe/internal/application/cpdaily/types"
|
|
// "likedream-goframe/internal/application/recognizer/classifier"
|
|
// "likedream-goframe/internal/common/errors"
|
|
// "likedream-goframe/internal/utils"
|
|
// "strings"
|
|
//
|
|
// "github.com/bytedance/sonic"
|
|
// "resty.dev/v3"
|
|
//)
|
|
//
|
|
//type AIResolver struct {
|
|
// client *resty.Client
|
|
// host string
|
|
// deviceId string
|
|
// taskType types.TaskType
|
|
//}
|
|
//
|
|
//func NewAIResolver(client *resty.Client, host, deviceId string, taskType types.TaskType) SubmitCaptchaResolver {
|
|
// return &AIResolver{
|
|
// client: client,
|
|
// host: host,
|
|
// deviceId: deviceId,
|
|
// taskType: taskType,
|
|
// }
|
|
//}
|
|
//func (r *AIResolver) GetValidationInfo(ctx context.Context) ValidationInfo {
|
|
// // GetValidationInfo 获取验证信息
|
|
// reqData := map[string]string{
|
|
// "deviceId": r.deviceId,
|
|
// }
|
|
//
|
|
// resp, err := r.client.R().
|
|
// SetBody(reqData).
|
|
// Post(r.getValidateUrl())
|
|
// utils.OkOrPanic(err)
|
|
//
|
|
// apiResp := utils.ReadJson[ApiResponse](resp.Body)
|
|
//
|
|
// if apiResp.Datas == nil {
|
|
// message := "获取提交验证码信息失败"
|
|
// if apiResp.Message != "" {
|
|
// message = apiResp.Message
|
|
// }
|
|
// utils.OkOrPanic(errors.NewBusinessError(503, message))
|
|
// }
|
|
//
|
|
// var validationInfo ValidationInfo
|
|
// err = sonic.Unmarshal(apiResp.Datas, &validationInfo)
|
|
// utils.OkOrPanic(err)
|
|
//
|
|
// return validationInfo
|
|
//
|
|
//}
|
|
//func (r *AIResolver) GetCaptchaTicket(ctx context.Context, info ValidationInfo) string {
|
|
// captchaInfo := r.createCaptcha(info)
|
|
// codes := r.getResult(captchaInfo)
|
|
// return r.submitCaptcha(info, captchaInfo, codes)
|
|
//}
|
|
//
|
|
//func (r *AIResolver) getResult(data CpSubmitCaptchaForAI) []string {
|
|
// name := data.Name
|
|
// infos := data.ImageInfos
|
|
// var result []string
|
|
// for _, item := range infos {
|
|
// url := item.Path
|
|
// get, err := r.client.R().Get(url)
|
|
// utils.OkOrPanic(err)
|
|
// defer get.Body.Close()
|
|
// bytes, _ := io.ReadAll(get.Body)
|
|
// //todo 将图片保存方便后面训练
|
|
// //os.WriteFile(item.Code+".png", bytes, 0666)
|
|
// if classifier.ClassifyImage(bytes, name) {
|
|
// result = append(result, item.Code)
|
|
// }
|
|
// }
|
|
// return result
|
|
//}
|
|
//
|
|
//// getValidateUrl 根据任务类型获取验证URL
|
|
//func (r *AIResolver) getValidateUrl() string {
|
|
// baseUrl := strings.TrimSuffix(r.host, "/")
|
|
//
|
|
// switch r.taskType {
|
|
// case types.TaskTypeCollection:
|
|
// return baseUrl + "/wec-counselor-collector-apps/stu/collector/checkValidation"
|
|
// case types.TaskTypeSign:
|
|
// return baseUrl + "/wec-counselor-sign-apps/stu/sign/checkValidation"
|
|
// case types.TaskTypeSleepCheck:
|
|
// return baseUrl + "/wec-counselor-attendance-apps/student/attendance/checkValidation"
|
|
// default:
|
|
// utils.OkOrPanic(errors.NewBusinessError(400, "不支持的任务类型"))
|
|
// return ""
|
|
// }
|
|
//}
|
|
//
|
|
//// createCaptcha 创建验证码
|
|
//func (r *AIResolver) createCaptcha(info ValidationInfo) CpSubmitCaptchaForAI {
|
|
// baseUrl := strings.TrimSuffix(r.host, "/")
|
|
// url := baseUrl + "/captcha-open-api/v1/captcha/create/scenesImage"
|
|
//
|
|
// resp, err := r.client.R().
|
|
// SetHeader("deviceId", r.deviceId).
|
|
// SetMultipartFormData(map[string]string{
|
|
// "accountKey": info.AccountKey,
|
|
// "sceneCode": info.SceneCode,
|
|
// "tenantId": info.TenantId,
|
|
// "userId": info.UserId,
|
|
// }).
|
|
// Post(url)
|
|
// utils.OkOrPanic(err)
|
|
//
|
|
// apiResp := utils.ReadJson[ApiResponse](resp.Body)
|
|
//
|
|
// if apiResp.Result == nil {
|
|
// message := "获取提交验证码失败"
|
|
// if apiResp.Message != "" {
|
|
// message = apiResp.Message
|
|
// }
|
|
// utils.OkOrPanic(errors.NewBusinessError(503, message))
|
|
// }
|
|
//
|
|
// var result CpSubmitCaptchaForAI
|
|
// err = sonic.Unmarshal(apiResp.Result, &result)
|
|
// utils.OkOrPanic(err)
|
|
// return result
|
|
//}
|
|
//
|
|
//// submitCaptcha 提交验证码
|
|
//func (r *AIResolver) submitCaptcha(info ValidationInfo, submitCaptcha CpSubmitCaptchaForAI, codes []string) string {
|
|
// baseUrl := strings.TrimSuffix(r.host, "/")
|
|
// url := baseUrl + "/captcha-open-api/v1/captcha/validate/scenesImage"
|
|
//
|
|
// formData := map[string]string{
|
|
// "accountKey": info.AccountKey,
|
|
// "sceneCode": info.SceneCode,
|
|
// "tenantId": info.TenantId,
|
|
// "userId": info.UserId,
|
|
// "scenesImageCode": submitCaptcha.Code,
|
|
// }
|
|
//
|
|
// // 创建multipart form request
|
|
// req := r.client.R().SetHeader("deviceId", r.deviceId)
|
|
//
|
|
// // 添加基本表单数据
|
|
// for key, value := range formData {
|
|
// req = req.SetMultipartFormData(map[string]string{key: value})
|
|
// }
|
|
//
|
|
// // 添加验证码列表
|
|
// for _, code := range codes {
|
|
// req = req.SetMultipartField("scenesImageCodes", "", "", strings.NewReader(code))
|
|
// }
|
|
//
|
|
// resp, err := req.Post(url)
|
|
// utils.OkOrPanic(err)
|
|
//
|
|
// apiResp := utils.ReadJson[ApiResponse](resp.Body)
|
|
//
|
|
// if apiResp.Code != "200" {
|
|
// message := "提交验证码失败"
|
|
// if apiResp.Message != "" {
|
|
// message = apiResp.Message
|
|
// }
|
|
// utils.OkOrPanic(errors.NewBusinessError(503, message))
|
|
// }
|
|
//
|
|
// if apiResp.Result == nil {
|
|
// utils.OkOrPanic(errors.NewBusinessError(503, "今日校园返回空验证码结果"))
|
|
// }
|
|
//
|
|
// var result string
|
|
// err = sonic.Unmarshal(apiResp.Result, &result)
|
|
// utils.OkOrPanic(err)
|
|
//
|
|
// return result
|
|
//}
|