130 lines
4.2 KiB
Go
130 lines
4.2 KiB
Go
package ali_auth
|
||
|
||
import (
|
||
"fmt"
|
||
|
||
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
|
||
"github.com/spf13/viper"
|
||
|
||
cloudauth "github.com/alibabacloud-go/cloudauth-20190307/v3/client"
|
||
|
||
util "github.com/alibabacloud-go/tea-utils/v2/service"
|
||
"github.com/alibabacloud-go/tea/tea"
|
||
)
|
||
|
||
func CreateClient(endpoint *string) (_result *cloudauth.Client, _err error) {
|
||
// 初始化Client。
|
||
config := &openapi.Config{
|
||
// 使用Credential配置凭证。
|
||
AccessKeyId: tea.String(viper.GetString("aliyun.sms.accessKeyId")),
|
||
AccessKeySecret: tea.String(viper.GetString("aliyun.sms.accessKeySecret")),
|
||
ConnectTimeout: tea.Int(3000),
|
||
ReadTimeout: tea.Int(6000),
|
||
// RegionId: tea.String("cn-shanghai"),
|
||
Endpoint: endpoint,
|
||
}
|
||
//_result = &cloudauth.Client{}
|
||
_result, _err = cloudauth.NewClient(config)
|
||
return _result, _err
|
||
}
|
||
|
||
func getReturnUrl() string {
|
||
return viper.GetString("aliyun.face-verify.returnUrl")
|
||
}
|
||
|
||
func AliFaceVerify(token, userId, orderNo, metaInfo string) (res *cloudauth.InitFaceVerifyResponse, _err error) {
|
||
request := &cloudauth.InitFaceVerifyRequest{
|
||
SceneId: tea.Int64(1000014704),
|
||
OuterOrderNo: tea.String(orderNo),
|
||
ProductCode: tea.String("LR_FR"),
|
||
Model: tea.String("LIVENESS"),
|
||
// CertifyUrlType: tea.String("H5"),
|
||
CertifyUrlStyle: tea.String("S"),
|
||
CallbackToken: tea.String(orderNo),
|
||
// LIVENESS(默认):眨眼 PHOTINUS_LIVENESS:眨眼 + 炫彩 MULTI_ACTION:眨眼 + 摇头 (眨眼和摇头顺序随机) MULTI_PHOTINUS:眨眼 + 摇头 (眨眼和摇头顺序随机)+ 炫彩活体检测。 MOVE_ACTION(推荐):远近移动 + 眨眼 (远近和眨眼顺序随机) MULTI_FAPTCHA:眨眼 + 形迹
|
||
UserId: tea.String(userId),
|
||
MetaInfo: tea.String(metaInfo),
|
||
ReturnUrl: tea.String(fmt.Sprintf(getReturnUrl(), token, orderNo)),
|
||
}
|
||
client, _err := CreateClient(tea.String("cloudauth.aliyuncs.com"))
|
||
if _err != nil {
|
||
return nil, _err
|
||
}
|
||
response, _err := client.InitFaceVerify(request)
|
||
// 不支持服务自动路由。
|
||
// response, _err := InitFaceVerify(tea.String("cloudauth.cn-shanghai.aliyuncs.com"), request)
|
||
if _err != nil {
|
||
return nil, _err
|
||
}
|
||
// fmt.Println(*response.Body.RequestId)
|
||
// fmt.Println(*response.Body.Code)
|
||
// fmt.Println(*response.Body.Message)
|
||
// fmt.Println(*response.Body.RequestId)
|
||
// fmt.Println(*response.Body.ResultObject.CertifyId)
|
||
// fmt.Println(*response.Body.ResultObject.CertifyUrl)
|
||
return response, _err
|
||
}
|
||
|
||
func AliFaceVerifyQuery(certifyId string) (res *cloudauth.DescribeFaceVerifyResponse, _err error) {
|
||
client, _err := CreateClient(tea.String("cloudauth.aliyuncs.com"))
|
||
if _err != nil {
|
||
return nil, _err
|
||
}
|
||
return client.DescribeFaceVerify(&cloudauth.DescribeFaceVerifyRequest{
|
||
SceneId: tea.Int64(1000014704),
|
||
CertifyId: tea.String(certifyId),
|
||
})
|
||
}
|
||
|
||
func InitFaceVerifyAutoRoute(request *cloudauth.InitFaceVerifyRequest) (_result *cloudauth.InitFaceVerifyResponse, _err error) {
|
||
endpoints := []*string{tea.String("cloudauth.aliyuncs.com"), tea.String("cloudauth-dualstack.aliyuncs.com")}
|
||
var lastResponse *cloudauth.InitFaceVerifyResponse
|
||
for _, endpoint := range endpoints {
|
||
response, _err := InitFaceVerify(endpoint, request)
|
||
lastResponse = response
|
||
if _err != nil {
|
||
var err = &tea.SDKError{}
|
||
if _t, ok := _err.(*tea.SDKError); ok {
|
||
err = _t
|
||
// 系统异常,切换到下个地域调用。
|
||
if *err.StatusCode == 500 {
|
||
continue
|
||
}
|
||
}
|
||
return _result, _err
|
||
}
|
||
|
||
if *response.StatusCode == 500 {
|
||
continue
|
||
}
|
||
|
||
if *response.Body.Code == "500" {
|
||
continue
|
||
}
|
||
|
||
_result = response
|
||
return _result, _err
|
||
|
||
}
|
||
_result = lastResponse
|
||
return _result, _err
|
||
}
|
||
|
||
func InitFaceVerify(endpoint *string, request *cloudauth.InitFaceVerifyRequest) (_result *cloudauth.InitFaceVerifyResponse, _err error) {
|
||
client, _err := CreateClient(endpoint)
|
||
if _err != nil {
|
||
return _result, _err
|
||
}
|
||
// 创建RuntimeObject实例并设置运行参数。
|
||
runtime := &util.RuntimeOptions{}
|
||
runtime.ReadTimeout = tea.Int(10000)
|
||
runtime.ConnectTimeout = tea.Int(10000)
|
||
_result = &cloudauth.InitFaceVerifyResponse{}
|
||
_body, _err := client.InitFaceVerifyWithOptions(request, runtime)
|
||
if _err != nil {
|
||
return _result, _err
|
||
}
|
||
_result = _body
|
||
return _result, _err
|
||
}
|