init
This commit is contained in:
		| @ -0,0 +1,33 @@ | ||||
| package org.dromara.common.sms.config; | ||||
|  | ||||
| import org.dromara.common.redis.config.RedisConfiguration; | ||||
| import org.dromara.common.sms.core.dao.PlusSmsDao; | ||||
| import org.dromara.common.sms.handler.SmsExceptionHandler; | ||||
| import org.dromara.sms4j.api.dao.SmsDao; | ||||
| import org.springframework.boot.autoconfigure.AutoConfiguration; | ||||
| import org.springframework.context.annotation.Bean; | ||||
| import org.springframework.context.annotation.Primary; | ||||
|  | ||||
| /** | ||||
|  * 短信配置类 | ||||
|  * | ||||
|  * @author Feng | ||||
|  */ | ||||
| @AutoConfiguration(after = {RedisConfiguration.class}) | ||||
| public class SmsAutoConfiguration { | ||||
|  | ||||
|     @Primary | ||||
|     @Bean | ||||
|     public SmsDao smsDao() { | ||||
|         return new PlusSmsDao(); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 异常处理器 | ||||
|      */ | ||||
|     @Bean | ||||
|     public SmsExceptionHandler smsExceptionHandler() { | ||||
|         return new SmsExceptionHandler(); | ||||
|     } | ||||
|  | ||||
| } | ||||
| @ -0,0 +1,72 @@ | ||||
| package org.dromara.common.sms.core.dao; | ||||
|  | ||||
| import org.dromara.common.core.constant.GlobalConstants; | ||||
| import org.dromara.common.redis.utils.RedisUtils; | ||||
| import org.dromara.sms4j.api.dao.SmsDao; | ||||
|  | ||||
| import java.time.Duration; | ||||
|  | ||||
| /** | ||||
|  * SmsDao缓存配置 (使用框架自带RedisUtils实现 协议统一) | ||||
|  * <p>主要用于短信重试和拦截的缓存 | ||||
|  * | ||||
|  * @author Feng | ||||
|  */ | ||||
| public class PlusSmsDao implements SmsDao { | ||||
|  | ||||
|     /** | ||||
|      * 存储 | ||||
|      * | ||||
|      * @param key       键 | ||||
|      * @param value     值 | ||||
|      * @param cacheTime 缓存时间(单位:秒) | ||||
|      */ | ||||
|     @Override | ||||
|     public void set(String key, Object value, long cacheTime) { | ||||
|         RedisUtils.setCacheObject(GlobalConstants.GLOBAL_REDIS_KEY + key, value, Duration.ofSeconds(cacheTime)); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 存储 | ||||
|      * | ||||
|      * @param key   键 | ||||
|      * @param value 值 | ||||
|      */ | ||||
|     @Override | ||||
|     public void set(String key, Object value) { | ||||
|         RedisUtils.setCacheObject(GlobalConstants.GLOBAL_REDIS_KEY + key, value, true); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 读取 | ||||
|      * | ||||
|      * @param key 键 | ||||
|      * @return 值 | ||||
|      */ | ||||
|     @Override | ||||
|     public Object get(String key) { | ||||
|         return RedisUtils.getCacheObject(GlobalConstants.GLOBAL_REDIS_KEY + key); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * remove | ||||
|      * <p> 根据key移除缓存 | ||||
|      * | ||||
|      * @param key 缓存键 | ||||
|      * @return 被删除的value | ||||
|      * @author :Wind | ||||
|      */ | ||||
|     @Override | ||||
|     public Object remove(String key) { | ||||
|         return RedisUtils.deleteObject(GlobalConstants.GLOBAL_REDIS_KEY + key); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * 清空 | ||||
|      */ | ||||
|     @Override | ||||
|     public void clean() { | ||||
|         RedisUtils.deleteKeys(GlobalConstants.GLOBAL_REDIS_KEY + "sms:*"); | ||||
|     } | ||||
|  | ||||
| } | ||||
| @ -0,0 +1,30 @@ | ||||
| package org.dromara.common.sms.handler; | ||||
|  | ||||
| import cn.hutool.http.HttpStatus; | ||||
| import jakarta.servlet.http.HttpServletRequest; | ||||
| import lombok.extern.slf4j.Slf4j; | ||||
| import org.dromara.common.core.domain.R; | ||||
| import org.dromara.sms4j.comm.exception.SmsBlendException; | ||||
| import org.springframework.web.bind.annotation.ExceptionHandler; | ||||
| import org.springframework.web.bind.annotation.RestControllerAdvice; | ||||
|  | ||||
| /** | ||||
|  * SMS异常处理器 | ||||
|  * | ||||
|  * @author AprilWind | ||||
|  */ | ||||
| @Slf4j | ||||
| @RestControllerAdvice | ||||
| public class SmsExceptionHandler { | ||||
|  | ||||
|     /** | ||||
|      * sms异常 | ||||
|      */ | ||||
|     @ExceptionHandler(SmsBlendException.class) | ||||
|     public R<Void> handleSmsBlendException(SmsBlendException e, HttpServletRequest request) { | ||||
|         String requestURI = request.getRequestURI(); | ||||
|         log.error("请求地址'{}',发生sms短信异常.", requestURI, e); | ||||
|         return R.fail(HttpStatus.HTTP_INTERNAL_ERROR, "短信发送失败,请稍后再试..."); | ||||
|     } | ||||
|  | ||||
| } | ||||
| @ -0,0 +1 @@ | ||||
| org.dromara.common.sms.config.SmsAutoConfiguration | ||||
		Reference in New Issue
	
	Block a user