133 lines
3.7 KiB
Go
133 lines
3.7 KiB
Go
package login
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"likedream-goframe/internal/application/cookiejar"
|
|
"likedream-goframe/internal/application/httpclient"
|
|
"likedream-goframe/internal/common/errors"
|
|
"likedream-goframe/internal/utils"
|
|
"strings"
|
|
|
|
"github.com/samber/oops"
|
|
"resty.dev/v3"
|
|
)
|
|
|
|
func ActivateSession(ctx context.Context, host string, client *resty.Client) {
|
|
client = client.SetRedirectPolicy(resty.FlexibleRedirectPolicy(20))
|
|
|
|
host = strings.TrimSuffix(host, "/")
|
|
|
|
activateStep1 := func() {
|
|
sessionResponse, err := client.R().
|
|
WithContext(ctx).
|
|
Get(fmt.Sprintf("%s/wec-im-group/group/groupMember/checkImInitFlag", host))
|
|
|
|
utils.OkOrPanic(err)
|
|
|
|
defer sessionResponse.Body.Close()
|
|
|
|
if !sessionResponse.IsSuccess() {
|
|
utils.OkOrPanic(errors.NewBusinessError(503, "Session激活失败"),
|
|
func(err error) error {
|
|
return oops.
|
|
WithContext(ctx).
|
|
With("host", host).
|
|
Wrap(err)
|
|
})
|
|
}
|
|
}
|
|
|
|
activateStep2 := func() {
|
|
sessionResponse, err := client.R().
|
|
WithContext(ctx).
|
|
SetBody(map[string]any{}).
|
|
Post(fmt.Sprintf("%s/wec-counselor-attendance-apps/student/attendance/getStuSignInfosByWeekMonth", host))
|
|
|
|
utils.OkOrPanic(err)
|
|
|
|
defer sessionResponse.Body.Close()
|
|
|
|
if !sessionResponse.IsSuccess() {
|
|
if sessionResponse.StatusCode() == 405 {
|
|
return
|
|
}
|
|
|
|
utils.OkOrPanic(errors.NewBusinessError(503, "Session激活失败"),
|
|
func(err error) error {
|
|
return oops.
|
|
WithContext(ctx).
|
|
With("host", host).
|
|
Wrap(err)
|
|
})
|
|
}
|
|
|
|
responseContentType := sessionResponse.Header().Get("Content-Type")
|
|
if !strings.Contains(responseContentType, "json") {
|
|
utils.OkOrPanic(errors.NewBusinessError(503, "Session激活失败"),
|
|
func(err error) error {
|
|
return oops.
|
|
WithContext(ctx).
|
|
With("host", host).
|
|
With("response_content_type", responseContentType).
|
|
Wrap(err)
|
|
})
|
|
}
|
|
}
|
|
|
|
activateStep1()
|
|
activateStep2()
|
|
|
|
client = client.SetRedirectPolicy(resty.NoRedirectPolicy())
|
|
}
|
|
|
|
func CookieEnabled(ctx context.Context, host string, client *resty.Client) bool {
|
|
client = client.SetRedirectPolicy(resty.FlexibleRedirectPolicy(20))
|
|
defer func() {
|
|
client = client.SetRedirectPolicy(resty.NoRedirectPolicy())
|
|
}()
|
|
host = strings.TrimSuffix(host, "/")
|
|
sessionResponse, err := client.R().
|
|
WithContext(ctx).
|
|
SetBody(map[string]any{}).
|
|
Post(fmt.Sprintf("%s/wec-counselor-attendance-apps/student/attendance/getStuSignInfosByWeekMonth", host))
|
|
|
|
utils.OkOrPanic(err)
|
|
defer sessionResponse.Body.Close()
|
|
|
|
if !sessionResponse.IsSuccess() {
|
|
return sessionResponse.StatusCode() == 405
|
|
}
|
|
responseContentType := sessionResponse.Header().Get("Content-Type")
|
|
return strings.Contains(responseContentType, "json")
|
|
}
|
|
|
|
func GetClient(ctx context.Context, cookie string) *resty.Client {
|
|
cookieJar := cookiejar.NewPersistCookieJar()
|
|
cookieJar.Deserialize(cookie)
|
|
client := httpclient.Cpdaily.Clone(ctx)
|
|
client.SetCookieJar(cookieJar)
|
|
return client
|
|
}
|
|
|
|
func SendDynamicCodeByPhone(ctx context.Context, username string, host string, client *resty.Client) {
|
|
host = strings.TrimSuffix(host, "/")
|
|
response, err := client.R().
|
|
WithContext(ctx).
|
|
SetFormData(map[string]string{
|
|
"userName": username,
|
|
"authCodeTypeName": "reAuthDynamicCodeType",
|
|
}).
|
|
Post(fmt.Sprintf("%s/authserver/dynamicCode/getDynamicCodeByReauth.do", host))
|
|
utils.OkOrPanic(err, func(err error) error {
|
|
return oops.
|
|
In("多因子短信发送").
|
|
With("username", username).
|
|
Wrap(err)
|
|
})
|
|
responseJson := utils.ReadGjsonResult(response.Body)
|
|
if responseJson.Get("res").String() != "success" {
|
|
utils.OkOrPanic(errors.NewBusinessError(500, "多因子短信发送失败,原因是"+responseJson.Get("returnMessage").String()))
|
|
}
|
|
}
|