You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
272 lines
6.6 KiB
272 lines
6.6 KiB
package data
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"github.com/alibabacloud-go/tea/tea"
|
|
"github.com/go-kratos/kratos/v2/log"
|
|
"matchmaking-system/internal/data/redis"
|
|
"matchmaking-system/internal/data/sms"
|
|
"matchmaking-system/internal/pkg/flags"
|
|
|
|
openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
|
|
dySmsApi20180501 "github.com/alibabacloud-go/dysmsapi-20180501/v2/client"
|
|
util "github.com/alibabacloud-go/tea-utils/v2/service"
|
|
uuid "github.com/satori/go.uuid"
|
|
)
|
|
|
|
var (
|
|
ps = fmt.Sprintf
|
|
prefix = "msg"
|
|
)
|
|
|
|
// aLiYunRepo
|
|
// @Description:
|
|
type aLiYunRepo struct {
|
|
data *Data
|
|
|
|
log *log.Helper
|
|
}
|
|
|
|
// NewALiYunRepo
|
|
//
|
|
// @Description:
|
|
// @param data
|
|
// @param logger
|
|
// @return sms.ALiYunRepo
|
|
func NewALiYunRepo(data *Data, logger log.Logger) sms.ALiYunRepo {
|
|
return &aLiYunRepo{
|
|
data: data,
|
|
log: log.NewHelper(logger),
|
|
}
|
|
}
|
|
|
|
// SendVerificationCode
|
|
//
|
|
// @Description:
|
|
// @receiver al
|
|
// @param ctx
|
|
// @param phoneNumber
|
|
// @return code
|
|
// @return err
|
|
func (al *aLiYunRepo) SendVerificationCode(ctx context.Context, phoneNumber string) (code string, err error) {
|
|
// Verify if the verification code can be obtained (1-minute validity period)
|
|
_, found := al.data.cacheDB.Get(phoneNumber)
|
|
if found {
|
|
err = errors.New("Please do not send the verification code again.")
|
|
return
|
|
}
|
|
|
|
// Generate verification code
|
|
verifyCode := sms.CreateRandCode(ctx)
|
|
// Generate Message Body
|
|
message := al.getVerifyCodeReq(ctx, phoneNumber, verifyCode)
|
|
|
|
// Write to database
|
|
if err = al.SaveBoUserSms(nil); err != nil {
|
|
return flags.SetNull, err
|
|
}
|
|
|
|
// Sends sms
|
|
//result, _err := al.SendSms(message)
|
|
//if _err != nil {
|
|
// _err = errors.New("请输入正确的手机号")
|
|
// return internal.ResultStr, _err
|
|
//}
|
|
result := "Return value after sending SMS"
|
|
|
|
fmt.Println(flags.SetNull, message, result)
|
|
|
|
// Update to database
|
|
if err = al.UpdateBoUserSms(ctx, nil); err != nil {
|
|
err = errors.New("update data db error")
|
|
return flags.SetNull, err
|
|
}
|
|
|
|
// Request valid within 1 minute
|
|
al.data.cacheDB.SetDefault(phoneNumber, 1)
|
|
|
|
// Set SMS verification code cache to be valid within 5 minutes
|
|
|
|
err = redis.SetCacheData(ctx, al.data.redisDB, ps("%s:%s", prefix, phoneNumber), verifyCode, 5)
|
|
if err != nil {
|
|
err = errors.New("cache key error")
|
|
return flags.SetNull, err
|
|
}
|
|
|
|
return verifyCode, nil
|
|
}
|
|
|
|
// CheckVerificationCode
|
|
//
|
|
// @Description:
|
|
// @receiver al
|
|
// @param ctx
|
|
// @param phoneNumber
|
|
// @param verificationCode
|
|
// @return err
|
|
|
|
func (al *aLiYunRepo) CheckVerificationCode(ctx context.Context, phoneNumber, verificationCode string) (err error) {
|
|
code, err := redis.GetCacheData(ctx, al.data.redisDB, ps("%s:%s", prefix, phoneNumber))
|
|
if err != nil {
|
|
if err.Error() == "redis: nil" {
|
|
// init cache data
|
|
if err = redis.SetCacheData(ctx, al.data.redisDB, ps("%s:%s", prefix, phoneNumber), verificationCode, 5); err != nil {
|
|
return errors.New("Internal service error.")
|
|
}
|
|
code = verificationCode
|
|
}
|
|
}
|
|
|
|
if len(code) == 0 {
|
|
err = errors.New("The verification code has expired.")
|
|
return
|
|
}
|
|
|
|
if verificationCode != code {
|
|
err = errors.New("Verification code input error.")
|
|
return
|
|
}
|
|
|
|
return err
|
|
}
|
|
|
|
// CheckVerificationCodeNew
|
|
//
|
|
// @Description:
|
|
// @receiver al
|
|
// @param ctx
|
|
// @param phoneNumber
|
|
// @return string
|
|
// @return error
|
|
func (al *aLiYunRepo) CheckVerificationCodeNew(ctx context.Context, phoneNumber string) (string, error) {
|
|
code, err := redis.GetCacheData(ctx, al.data.redisDB, ps("%s:%s", prefix, phoneNumber))
|
|
if err != nil {
|
|
if err.Error() == "redis: nil" {
|
|
code = flags.SetNull
|
|
} else {
|
|
err = errors.New("Internal service error.")
|
|
return flags.SetNull, err
|
|
}
|
|
}
|
|
|
|
return code, nil
|
|
}
|
|
|
|
// getVerifyCodeReq
|
|
//
|
|
// @Description:
|
|
// @receiver al
|
|
// @param ctx
|
|
// @param phoneNumber
|
|
// @param code
|
|
// @return req
|
|
func (al *aLiYunRepo) getVerifyCodeReq(ctx context.Context, phoneNumber, code string) (req dySmsApi20180501.SendMessageToGlobeRequest) {
|
|
verifyCodeFrom := sms.CreateRandCodeFrom(ctx, 10)
|
|
|
|
req = dySmsApi20180501.SendMessageToGlobeRequest{
|
|
From: tea.String(verifyCodeFrom), // 发送方标识;支持SenderId的发送,只允许数字+字母,含有字母标识最长11位,纯数字标识支持15位
|
|
To: tea.String(phoneNumber), // 接收短信号码;号码格式为:国际区号+号码
|
|
Message: tea.String(code), // 修改短信内容
|
|
TaskId: tea.String(uuid.NewV4().String()), // 任务ID;长度不要超过255
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// CreateClient Initialize the Client with the AccessKey of the account
|
|
//
|
|
// @Description:
|
|
// @receiver al
|
|
// @param ctx
|
|
// @param accessKeyId
|
|
// @param accessKeySecret
|
|
// @return _result
|
|
// @return _err
|
|
func (al *aLiYunRepo) CreateClient(accessKeyId *string, accessKeySecret *string) (_result *dySmsApi20180501.Client, _err error) {
|
|
configs := &openapi.Config{
|
|
// Required, your AccessKey ID
|
|
AccessKeyId: accessKeyId,
|
|
// Required, your AccessKey secret
|
|
AccessKeySecret: accessKeySecret,
|
|
}
|
|
// Endpoint
|
|
configs.Endpoint = tea.String(flags.SetNull) // al.data.confAly.Aliyun.EndPoint
|
|
_result = &dySmsApi20180501.Client{}
|
|
_result, _err = dySmsApi20180501.NewClient(configs)
|
|
|
|
return _result, _err
|
|
}
|
|
|
|
// SendSms TODO: al.data.confAly.Aliyun.AccessKeyId al.data.confAly.Aliyun.AccessKeySecret
|
|
//
|
|
// @Description:
|
|
// @receiver al
|
|
// @param ctx
|
|
// @param req
|
|
// @return res
|
|
// @return _err
|
|
func (al *aLiYunRepo) SendSms(req dySmsApi20180501.SendMessageToGlobeRequest) (res *dySmsApi20180501.SendMessageToGlobeResponse, _err error) {
|
|
client, _err := al.CreateClient(tea.String(flags.SetNull), tea.String(flags.SetNull))
|
|
if _err != nil {
|
|
return nil, _err
|
|
}
|
|
|
|
defer func() {
|
|
if r := tea.Recover(recover()); r != nil {
|
|
_err = r
|
|
}
|
|
}()
|
|
|
|
runtime := &util.RuntimeOptions{}
|
|
|
|
// Copy the code to run, please print the return value of the API by yourself.
|
|
result, _err := client.SendMessageToGlobeWithOptions(&req, runtime)
|
|
if _err != nil {
|
|
return nil, _err
|
|
}
|
|
|
|
if *result.Body.ResponseCode != "OK" {
|
|
_err = errors.New(result.String())
|
|
}
|
|
|
|
return result, _err
|
|
}
|
|
|
|
// SaveBoUserSms
|
|
//
|
|
// @Description:
|
|
// @receiver al
|
|
// @param ctx
|
|
// @param sms
|
|
// @return error
|
|
func (al *aLiYunRepo) SaveBoUserSms(sms interface{}) error {
|
|
_, err := al.data.mysqlDB.Table("bo_user_sms").Insert(&sms)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// UpdateBoUserSms
|
|
//
|
|
// @Description:
|
|
// @receiver al
|
|
// @param ctx
|
|
// @param sms
|
|
// @return error
|
|
func (al *aLiYunRepo) UpdateBoUserSms(ctx context.Context, sms interface{}) error {
|
|
//_, err := al.data.mysqlDB.Table("bo_user_sms").
|
|
// Where("`from` = ?", sms.From).
|
|
// Where("`to` = ?", sms.To).
|
|
// Where("task_id = ?", sms.TaskId).
|
|
// Where("message = ?", sms.Message).
|
|
// Update(&sms)
|
|
//if err != nil {
|
|
// return err
|
|
//}
|
|
|
|
return nil
|
|
}
|
|
|