package sms import ( "eta/eta_mini_ht_api/common/component/cache" logger "eta/eta_mini_ht_api/common/component/log" "eta/eta_mini_ht_api/common/utils/redis" "eta/eta_mini_ht_api/models/sms" ) const ( secondToMinute = 60 ) var ( redisCache *cache.RedisCache ) type CodeDTO struct { Mobile string Code string ExpireMinute int } func rd() *cache.RedisCache { if redisCache == nil { redisCache = cache.GetInstance() } return redisCache } func TryExpireCode(codeDTO CodeDTO) (err error) { verificationRecord := convertCodeDTO(codeDTO) verificationRecord.Status = sms.StatusExpired return sms.UpdateVerificationRecordByCode(codeDTO.Mobile, codeDTO.Code, verificationRecord) } func TryVerifiedCode(codeDTO CodeDTO) (err error) { verificationRecord := convertCodeDTO(codeDTO) verificationRecord.Status = sms.StatusVerified return sms.UpdateVerificationRecordByCode(codeDTO.Mobile, codeDTO.Code, verificationRecord) } func VerifiedCodeTask() (err error) { return sms.ExpiredCodesByTask() } func SendSMSCode(codeDTO CodeDTO) (smid int, err error) { verificationRecord := convertCodeDTO(codeDTO) //写库 smid, err = sms.InsertVerificationRecord(verificationRecord) if err != nil { return } //设置redis err = rd().SetString(redis.GenerateSmsKey(codeDTO.Mobile), codeDTO.Code, codeDTO.ExpireMinute*secondToMinute) if err != nil { logger.Error("redis验证记录操作失败:", err) return } return } func convertCodeDTO(codeDTO CodeDTO) sms.VerificationRecord { return sms.VerificationRecord{ Mobile: codeDTO.Mobile, Code: codeDTO.Code, ExpiredDuration: codeDTO.ExpireMinute, Status: sms.StatusPending, } }