Browse Source

短信

master
huangjiajun 1 month ago
parent
commit
de4df98d64
4 changed files with 147 additions and 25 deletions
  1. +1
    -1
      aliyun/captcha.go
  2. +27
    -0
      aliyun/comm.go
  3. +116
    -0
      aliyun/fast_login.go
  4. +3
    -24
      aliyun/sms.go

sms/captcha.go → aliyun/captcha.go View File

@@ -1,4 +1,4 @@
package sms
package aliyun

import (
"crypto/hmac"

+ 27
- 0
aliyun/comm.go View File

@@ -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
}

+ 116
- 0
aliyun/fast_login.go View File

@@ -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
}

sms/api.go → aliyun/sms.go View File

@@ -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
}

Loading…
Cancel
Save