From de4df98d64dc2e7eea4bad3eb6e8c7a1ddc11662 Mon Sep 17 00:00:00 2001 From: huangjiajun <582604932@qq.com> Date: Fri, 22 Nov 2024 11:59:52 +0800 Subject: [PATCH] =?UTF-8?q?=E7=9F=AD=E4=BF=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- {sms => aliyun}/captcha.go | 2 +- aliyun/comm.go | 27 +++++++++ aliyun/fast_login.go | 116 ++++++++++++++++++++++++++++++++++++ sms/api.go => aliyun/sms.go | 27 +-------- 4 files changed, 147 insertions(+), 25 deletions(-) rename {sms => aliyun}/captcha.go (99%) create mode 100644 aliyun/comm.go create mode 100644 aliyun/fast_login.go rename sms/api.go => aliyun/sms.go (76%) diff --git a/sms/captcha.go b/aliyun/captcha.go similarity index 99% rename from sms/captcha.go rename to aliyun/captcha.go index a99f896..e930e72 100644 --- a/sms/captcha.go +++ b/aliyun/captcha.go @@ -1,4 +1,4 @@ -package sms +package aliyun import ( "crypto/hmac" diff --git a/aliyun/comm.go b/aliyun/comm.go new file mode 100644 index 0000000..ad1f5d9 --- /dev/null +++ b/aliyun/comm.go @@ -0,0 +1,27 @@ +package aliyun + +import ( + openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client" + dypnsapi20170525 "github.com/alibabacloud-go/dypnsapi-20170525/v2/client" + "github.com/alibabacloud-go/tea/tea" +) + +// Description: +// 使用AK&SK初始化账号Client +// @return Client +// @throws Exception +func CreateClient(accessKeyId, accessKeySecret string) (_result *dypnsapi20170525.Client, _err error) { + // 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。 + // 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378661.html。 + config := &openapi.Config{ + // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。 + AccessKeyId: tea.String(accessKeyId), + // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。 + AccessKeySecret: tea.String(accessKeySecret), + } + // Endpoint 请参考 https://api.aliyun.com/product/Dypnsapi + config.Endpoint = tea.String("dypnsapi.aliyuncs.com") + _result = &dypnsapi20170525.Client{} + _result, _err = dypnsapi20170525.NewClient(config) + return _result, _err +} diff --git a/aliyun/fast_login.go b/aliyun/fast_login.go new file mode 100644 index 0000000..2894793 --- /dev/null +++ b/aliyun/fast_login.go @@ -0,0 +1,116 @@ +package aliyun + +import ( + "encoding/json" + "errors" + "fmt" + dypnsapi20170525 "github.com/alibabacloud-go/dypnsapi-20170525/v2/client" + util "github.com/alibabacloud-go/tea-utils/v2/service" + "github.com/alibabacloud-go/tea/tea" + "strings" +) + +// 一键登录取号 +func AliyunFastPhone(accessKeyId, accessKeySecret, token string) (string, error) { + client, _err := CreateClient(accessKeyId, accessKeySecret) + if _err != nil { + return "", _err + } + getMobileRequest := &dypnsapi20170525.GetMobileRequest{ + AccessToken: tea.String(token), + } + runtime := &util.RuntimeOptions{} + phone, tryErr := func() (phone string, _e error) { + defer func() { + if r := tea.Recover(recover()); r != nil { + _e = r + } + }() + // 复制代码运行请自行打印 API 的返回值 + _result, _err := client.GetMobileWithOptions(getMobileRequest, runtime) + if _err != nil { + return "", _err + } + phone = *_result.Body.GetMobileResultDTO.Mobile + return phone, nil + }() + + if tryErr != nil { + var error = &tea.SDKError{} + if _t, ok := tryErr.(*tea.SDKError); ok { + error = _t + } else { + error.Message = tea.String(tryErr.Error()) + } + // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。 + // 错误 message + fmt.Println(tea.StringValue(error.Message)) + // 诊断地址 + var data interface{} + d := json.NewDecoder(strings.NewReader(tea.StringValue(error.Data))) + d.Decode(&data) + if m, ok := data.(map[string]interface{}); ok { + recommend, _ := m["Recommend"] + fmt.Println(recommend) + } + _, _err = util.AssertAsString(error.Message) + if _err != nil { + return "", _err + } + return "", errors.New("获取手机号失败") + } + return phone, _err +} + +// 本机号码校验 +func AliyunCheckFastPhone(accessKeyId, accessKeySecret, token, phone string) (string, error) { + client, _err := CreateClient(accessKeyId, accessKeySecret) + if _err != nil { + return "", _err + } + getMobileRequest := &dypnsapi20170525.VerifyMobileRequest{ + AccessCode: tea.String(token), + PhoneNumber: tea.String(phone), + } + runtime := &util.RuntimeOptions{} + state, tryErr := func() (state string, _e error) { + defer func() { + if r := tea.Recover(recover()); r != nil { + _e = r + } + }() + // 复制代码运行请自行打印 API 的返回值 + _result, _err := client.VerifyMobileWithOptions(getMobileRequest, runtime) + if _err != nil { + return "", _err + } + state = *_result.Body.GateVerifyResultDTO.VerifyResult + return state, nil + }() + + if tryErr != nil { + var error = &tea.SDKError{} + if _t, ok := tryErr.(*tea.SDKError); ok { + error = _t + } else { + error.Message = tea.String(tryErr.Error()) + } + // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。 + // 错误 message + fmt.Println(tea.StringValue(error.Message)) + // 诊断地址 + var data interface{} + d := json.NewDecoder(strings.NewReader(tea.StringValue(error.Data))) + d.Decode(&data) + if m, ok := data.(map[string]interface{}); ok { + recommend, _ := m["Recommend"] + fmt.Println(recommend) + } + _, _err = util.AssertAsString(error.Message) + if _err != nil { + return "", _err + } + return "", errors.New("校验失败") + } + return state, _err +} diff --git a/sms/api.go b/aliyun/sms.go similarity index 76% rename from sms/api.go rename to aliyun/sms.go index 61d5f43..fea1948 100644 --- a/sms/api.go +++ b/aliyun/sms.go @@ -1,22 +1,21 @@ -package sms +package aliyun import ( "encoding/json" "errors" "fmt" - openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client" dypnsapi20170525 "github.com/alibabacloud-go/dypnsapi-20170525/v2/client" util "github.com/alibabacloud-go/tea-utils/v2/service" "github.com/alibabacloud-go/tea/tea" "strings" ) -/** +/* +* 发送短信 https://help.aliyun.com/zh/pnvs/developer-reference/api-dypnsapi-2017-05-25-sendsmsverifycode?spm=a2c4g.11186623.0.0.4f51a5d7doAoA4 templateParam= {"code":"##code##"} 可不填 */ - func AliyunSendSms(accessKeyId, accessKeySecret, phone, signName, templateCode, templateParam string) error { client, _err := CreateClient(accessKeyId, accessKeySecret) if _err != nil { @@ -129,23 +128,3 @@ func AliyunCheckSms(accessKeyId, accessKeySecret, phone, verifyCode string) erro } return _err } - -// Description: -// 使用AK&SK初始化账号Client -// @return Client -// @throws Exception -func CreateClient(accessKeyId, accessKeySecret string) (_result *dypnsapi20170525.Client, _err error) { - // 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。 - // 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378661.html。 - config := &openapi.Config{ - // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。 - AccessKeyId: tea.String(accessKeyId), - // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。 - AccessKeySecret: tea.String(accessKeySecret), - } - // Endpoint 请参考 https://api.aliyun.com/product/Dypnsapi - config.Endpoint = tea.String("dypnsapi.aliyuncs.com") - _result = &dypnsapi20170525.Client{} - _result, _err = dypnsapi20170525.NewClient(config) - return _result, _err -}