sms_tool
This commit is contained in:
190
sms_tool/sms_client_test.go
Normal file
190
sms_tool/sms_client_test.go
Normal file
@@ -0,0 +1,190 @@
|
||||
package sms_tool
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"os"
|
||||
"strconv"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
type RedisCacheAdapter struct {
|
||||
client *redis.Client
|
||||
}
|
||||
|
||||
func NewRedisCacheAdapter(addr string, password string, db int) *RedisCacheAdapter {
|
||||
rdb := redis.NewClient(&redis.Options{
|
||||
Addr: addr,
|
||||
Password: password,
|
||||
DB: db,
|
||||
})
|
||||
return &RedisCacheAdapter{client: rdb}
|
||||
}
|
||||
func (r *RedisCacheAdapter) Set(ctx context.Context, key string, value interface{}, expire time.Duration) error {
|
||||
data, err := json.Marshal(value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return r.client.Set(ctx, key, data, expire).Err()
|
||||
}
|
||||
func (r *RedisCacheAdapter) Get(ctx context.Context, key string) (interface{}, error) {
|
||||
data, err := r.client.Get(ctx, key).Bytes()
|
||||
if err != nil {
|
||||
if errors.Is(err, redis.Nil) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var value interface{}
|
||||
err = json.Unmarshal(data, &value)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
func (r *RedisCacheAdapter) Del(ctx context.Context, key string) error {
|
||||
return r.client.Del(ctx, key).Err()
|
||||
}
|
||||
|
||||
var (
|
||||
//SMS
|
||||
accessKeyId = os.Getenv("SMS_ALIBABA_CLOUD_ACCESS_KEY_ID")
|
||||
accessKeySecret = os.Getenv("SMS_ALIBABA_CLOUD_ACCESS_KEY_SECRET")
|
||||
phoneNumbers = os.Getenv("SMS_PHONE_NUMBERS")
|
||||
signName = os.Getenv("SMS_SIGN_NAME")
|
||||
templateCode = os.Getenv("SMS_TEMPLATE_CODE")
|
||||
|
||||
//REDIS
|
||||
redisHost = os.Getenv("REDIS_HOST")
|
||||
redisPort = os.Getenv("REDIS_PORT")
|
||||
redisPassword = os.Getenv("REDIS_PASSWORD")
|
||||
redisDb = os.Getenv("REDIS_DB")
|
||||
//redisKeyPrefix = os.Getenv("REDIS_KEY_PREFIX")
|
||||
|
||||
codeKey = fmt.Sprintf("User:Login:Code:%v", phoneNumbers)
|
||||
)
|
||||
|
||||
func TestSmsClient_SendSms(t *testing.T) {
|
||||
config := &SmsConfig{
|
||||
AccessKeyId: accessKeyId,
|
||||
AccessKeySecret: accessKeySecret,
|
||||
}
|
||||
smsClient, err := NewSmsClient(config)
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
return
|
||||
}
|
||||
templateParam := make(map[string]interface{})
|
||||
templateParam["code"] = "123456"
|
||||
templateParamStr, err := json.Marshal(templateParam)
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
return
|
||||
}
|
||||
params := &SendSmsParams{
|
||||
PhoneNumbers: phoneNumbers,
|
||||
SignName: signName,
|
||||
TemplateCode: templateCode,
|
||||
TemplateParam: string(templateParamStr),
|
||||
}
|
||||
res, err := smsClient.SendSms(params)
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
return
|
||||
}
|
||||
fmt.Println(res)
|
||||
}
|
||||
|
||||
func TestSmsClient_GetCode(t *testing.T) {
|
||||
config := &SmsConfig{
|
||||
AccessKeyId: accessKeyId,
|
||||
AccessKeySecret: accessKeySecret,
|
||||
}
|
||||
smsClient, err := NewSmsClient(config)
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
return
|
||||
}
|
||||
addr := fmt.Sprintf("%s:%s", redisHost, redisPort)
|
||||
db, _ := strconv.Atoi(redisDb)
|
||||
goRedis := NewRedisCacheAdapter(addr, redisPassword, db)
|
||||
smsClient.Cache = goRedis
|
||||
code, err := smsClient.GetCode(context.Background(), codeKey)
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
return
|
||||
}
|
||||
fmt.Println(code)
|
||||
}
|
||||
|
||||
func TestSmsClient_SaveCode(t *testing.T) {
|
||||
config := &SmsConfig{
|
||||
AccessKeyId: accessKeyId,
|
||||
AccessKeySecret: accessKeySecret,
|
||||
}
|
||||
smsClient, err := NewSmsClient(config)
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
return
|
||||
}
|
||||
addr := fmt.Sprintf("%s:%s", redisHost, redisPort)
|
||||
db, _ := strconv.Atoi(redisDb)
|
||||
goRedis := NewRedisCacheAdapter(addr, redisPassword, db)
|
||||
smsClient.Cache = goRedis
|
||||
|
||||
err = smsClient.SaveCode(context.Background(), codeKey, "123456", time.Minute*5, time.Minute)
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
return
|
||||
}
|
||||
fmt.Println("保存成功")
|
||||
}
|
||||
|
||||
func TestSmsClient_VerifyCode(t *testing.T) {
|
||||
config := &SmsConfig{
|
||||
AccessKeyId: accessKeyId,
|
||||
AccessKeySecret: accessKeySecret,
|
||||
}
|
||||
smsClient, err := NewSmsClient(config)
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
return
|
||||
}
|
||||
addr := fmt.Sprintf("%s:%s", redisHost, redisPort)
|
||||
db, _ := strconv.Atoi(redisDb)
|
||||
goRedis := NewRedisCacheAdapter(addr, redisPassword, db)
|
||||
smsClient.Cache = goRedis
|
||||
ok, err := smsClient.VerifyCode(context.Background(), codeKey, "123456")
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
return
|
||||
}
|
||||
fmt.Println(ok)
|
||||
}
|
||||
|
||||
func TestSmsClient_DeleteCode(t *testing.T) {
|
||||
config := &SmsConfig{
|
||||
AccessKeyId: accessKeyId,
|
||||
AccessKeySecret: accessKeySecret,
|
||||
}
|
||||
smsClient, err := NewSmsClient(config)
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
return
|
||||
}
|
||||
addr := fmt.Sprintf("%s:%s", redisHost, redisPort)
|
||||
db, _ := strconv.Atoi(redisDb)
|
||||
goRedis := NewRedisCacheAdapter(addr, redisPassword, db)
|
||||
smsClient.Cache = goRedis
|
||||
err = smsClient.DeleteCode(context.Background(), codeKey)
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
return
|
||||
}
|
||||
fmt.Println("删除成功")
|
||||
}
|
Reference in New Issue
Block a user