first commit

This commit is contained in:
Yangtao
2025-11-18 17:48:20 +08:00
commit 6e56cab848
196 changed files with 65809 additions and 0 deletions

View File

@ -0,0 +1,129 @@
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
}

View File

@ -0,0 +1,150 @@
package ali_auth
import (
"fmt"
"servicebase/pkg/log"
"strings"
"github.com/pkg/errors"
"github.com/spf13/viper"
"github.com/go-resty/resty/v2"
)
const (
url = "https://telvertify.market.alicloudapi.com/lianzhuo/telvertify"
urlSimple = "https://checkone.market.alicloudapi.com/communication/personal/10101"
urlSimpleV2 = "https://kzidcardv1.market.alicloudapi.com/api-mall/api/id_card/check"
)
type AuthResult struct {
Data struct {
Areacode string `json:"areacode"`
Isp string `json:"isp"`
ID string `json:"id"`
City string `json:"city"`
Mobile string `json:"Mobile"`
Name string `json:"Name"`
Postcode string `json:"postcode"`
} `json:"data"`
Resp struct {
Code string `json:"code"`
Desc string `json:"desc"`
} `json:"resp"`
}
type AuthSimpleResult struct {
// {"code":"10000","message":"成功","data":{"result":"1","birthday":"","address":"","sex":""},"seqNo":"YMX7AK4J250820134839851"}
Code string `json:"code"`
Message string `json:"message"`
Data struct {
Result string `json:"result"`
Birthday string `json:"birthday"`
Address string `json:"address"`
Sex string `json:"sex"`
} `json:"data"`
SeqNo string `json:"seqNo"`
}
type AuthSimpleResultV2 struct {
// {"code":"10000","message":"成功","data":{"result":"1","birthday":"","address":"","sex":""},"seqNo":"YMX7AK4J250820134839851"}
Code int `json:"code"`
Message string `json:"msg"`
Data struct {
OrderNo string `json:"orderNo"`
Result int `json:"result"`
Birthday string `json:"birthday"`
Address string `json:"address"`
Sex string `json:"sex"`
Desc string `json:"desc"`
} `json:"data"`
}
func RealAuth(name, mobile, id string) (result AuthResult, e error) {
if strings.HasPrefix(mobile, "1601234") {
result = AuthResult{Data: struct {
Areacode string `json:"areacode"`
Isp string `json:"isp"`
ID string `json:"id"`
City string `json:"city"`
Mobile string `json:"Mobile"`
Name string `json:"Name"`
Postcode string `json:"postcode"`
}{
Areacode: "",
Isp: "",
ID: id,
City: "",
Mobile: mobile,
Name: name,
Postcode: "",
}, Resp: struct {
Code string `json:"code"`
Desc string `json:"desc"`
}{
Code: "0",
Desc: "",
}}
return
}
client := resty.New()
if _, e = client.R().
SetHeader("Authorization", fmt.Sprintf("APPCODE %s", viper.GetString("aliyun.auth.appCode"))).
SetQueryParams(map[string]string{
"telnumber": mobile,
"name": name,
"id": id,
}).
SetResult(&result). // or SetResult(AuthSuccess{}).
Get(url); e != nil {
fmt.Printf("RealAuth error: %v", e)
return
}
return
}
func RealAuthSimple(name, id string) (result AuthSimpleResult, e error) {
client := resty.New()
var i *resty.Response
if i, e = client.R().
SetHeader("Authorization", fmt.Sprintf("APPCODE %s", viper.GetString("aliyun.auth.appCode"))).
SetHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8").
SetBody(fmt.Sprintf("name=%s&idcard=%s", name, id)).
SetResult(&result). // or SetResult(AuthSuccess{}).
Post(urlSimple); e != nil {
fmt.Printf("RealAuthSimple error: %v", e)
return
} else if i != nil {
fmt.Printf("RealAuthSimple success: %v", i.String())
}
return
}
// RealAuthSimpleV2 https://market.aliyun.com/detail/cmapi00066570#sku=yuncode6057000002
func RealAuthSimpleV2(name, id string) (*AuthSimpleResultV2, error) {
var (
result *AuthSimpleResultV2
err error
)
client := resty.New()
var i *resty.Response
i, err = client.R().
SetHeader("Authorization", fmt.Sprintf("APPCODE %s", viper.GetString("aliyun.auth.appCode"))).
SetHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8").
SetBody(fmt.Sprintf("name=%s&idcard=%s", name, id)).
SetResult(&result). // or SetResult(AuthSuccess{}).
Post(urlSimpleV2)
if err != nil {
fmt.Printf("RealAuthSimple error: %v", err)
return nil, errors.WithStack(err)
}
if i == nil {
log.Error("身份信息二要素返回结果为nil")
return nil, errors.New("身份信息二要素认证请求失败")
}
if i.StatusCode() != 200 {
log.ErrorF("身份信息二要素返回http code (%d) not 200", i.StatusCode())
return nil, errors.New("身份信息二要素认证请求失败")
}
return result, nil
}

View File

@ -0,0 +1,70 @@
package ali_auth
import (
"fmt"
"reflect"
"testing"
)
func TestRealAuth(t *testing.T) {
type args struct {
name string
mobile string
id string
}
tests := []struct {
name string
args args
wantResult AuthResult
}{
{name: "t1", args: struct {
name string
mobile string
id string
}{name: "123", mobile: "", id: "234"}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotResult, e := RealAuth(tt.args.name, tt.args.mobile, tt.args.id)
fmt.Printf("%++v %+v\n", gotResult, e)
})
}
}
func TestRealAuthSimple(t *testing.T) {
type args struct {
name string
id string
}
tests := []struct {
name string
args args
wantResult AuthSimpleResult
wantErr bool
}{
{name: "t1", args: struct {
name string
id string
}{name: "", id: ""}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotResult, err := RealAuthSimple(tt.args.name, tt.args.id)
if (err != nil) != tt.wantErr {
t.Errorf("RealAuthSimple() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(gotResult, tt.wantResult) {
t.Errorf("RealAuthSimple() = %v, want %v", gotResult, tt.wantResult)
}
})
}
}
func TestRealAuthSimpleV2(t *testing.T) {
v2, err := RealAuthSimpleV2("", "")
if err != nil {
t.Error(err)
}
println(v2.Data.Address)
}