github.com/SupenBysz/gf-admin-community@v0.7.4/internal/logic/sys_sms/sys_sms.go (about)

     1  package sys_sms
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"github.com/SupenBysz/gf-admin-community/sys_model/sys_dao"
     7  	"github.com/SupenBysz/gf-admin-community/sys_service"
     8  	"github.com/gogf/gf/v2/frame/g"
     9  	"github.com/kysion/base-library/base_model/base_enum"
    10  )
    11  
    12  type sSysSms struct {
    13  	cachePrefix string
    14  }
    15  
    16  func init() {
    17  	sys_service.RegisterSysSms(New())
    18  }
    19  
    20  func New() sys_service.ISysSms {
    21  	return &sSysSms{
    22  		cachePrefix: sys_dao.SysSmsLogs.Table() + "_",
    23  	}
    24  }
    25  
    26  // Verify 校验验证码
    27  func (s *sSysSms) Verify(ctx context.Context, mobile string, captcha string, typeIdentifier ...base_enum.CaptchaType) (bool, error) {
    28  	if mobile == "" {
    29  		return false, sys_service.SysLogs().ErrorSimple(ctx, nil, "手机号码不能为空", "Sms")
    30  	}
    31  	if captcha == "" {
    32  		return false, sys_service.SysLogs().ErrorSimple(ctx, nil, "验证码不能为空", "Sms")
    33  	}
    34  
    35  	key := ""
    36  	if len(typeIdentifier) > 0 {
    37  		key = typeIdentifier[0].Description() + "_" + mobile
    38  	} else {
    39  		key = mobile
    40  	}
    41  
    42  	code, err := g.DB().GetCache().Get(ctx, key)
    43  
    44  	fmt.Println("验证码:", code.String())
    45  
    46  	if err != nil || code.String() != captcha {
    47  		return false, sys_service.SysLogs().ErrorSimple(ctx, nil, "验证码错误", "Sms")
    48  	}
    49  
    50  	// 成功、清除该缓存
    51  	_, _ = g.DB().GetCache().Remove(ctx, key)
    52  
    53  	// 此验证码类型是复用类型
    54  	//if (typeIdentifier[0].Code() & base_enum.Captcha.Type.ForgotUserNameAndPassword.Code()) == base_enum.Captcha.Type.ForgotUserNameAndPassword.Code() {
    55  	//	cacheKey := base_enum.Captcha.Type.SetPassword.Description() + "_" + mobile
    56  	//
    57  	//	// 重新保持验证码到缓存
    58  	//	_, err := g.Redis().Set(ctx, cacheKey, code)
    59  	//	if err != nil {
    60  	//		return false, sys_service.SysLogs().ErrorSimple(ctx, err, "再次设置忘记密码验证码至缓存失败", "Sms")
    61  	//	}
    62  	//	// 设置验证码缓存时间
    63  	//	_, _ = g.Redis().Do(ctx, "EXPIRE", cacheKey, time.Minute*5)
    64  	//}
    65  
    66  	return true, nil
    67  }