package ali_auth import ( "fmt" "strings" "gitea.ddegame.cn/open/servicebase/pkg/log" "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 }