diff --git a/esua-epdc/epdc-commons/epdc-commons-tools/src/main/java/com/elink/esua/epdc/commons/tools/constant/NumConstant.java b/esua-epdc/epdc-commons/epdc-commons-tools/src/main/java/com/elink/esua/epdc/commons/tools/constant/NumConstant.java index 4880860e0..110c2b089 100644 --- a/esua-epdc/epdc-commons/epdc-commons-tools/src/main/java/com/elink/esua/epdc/commons/tools/constant/NumConstant.java +++ b/esua-epdc/epdc-commons/epdc-commons-tools/src/main/java/com/elink/esua/epdc/commons/tools/constant/NumConstant.java @@ -14,6 +14,7 @@ public interface NumConstant { int TWO = 2; int THREE = 3; int FOUR = 4; + int SIX = 6; int TEN = 10; int FIFTEEN = 15; diff --git a/esua-epdc/epdc-commons/epdc-commons-tools/src/main/java/com/elink/esua/epdc/commons/tools/redis/RedisKeys.java b/esua-epdc/epdc-commons/epdc-commons-tools/src/main/java/com/elink/esua/epdc/commons/tools/redis/RedisKeys.java index 8e0b10c1d..3610e433a 100644 --- a/esua-epdc/epdc-commons/epdc-commons-tools/src/main/java/com/elink/esua/epdc/commons/tools/redis/RedisKeys.java +++ b/esua-epdc/epdc-commons/epdc-commons-tools/src/main/java/com/elink/esua/epdc/commons/tools/redis/RedisKeys.java @@ -102,4 +102,16 @@ public class RedisKeys { public static String getSimpleDictKey(String dictType) { return rootPrefix.concat("config:simple:dict:").concat(dictType); } + + /** + * 拼接手机验证码key + * + * @param phone + * @return java.lang.String + * @author yujintao + * @date 2019/9/6 17:03 + */ + public static String getPhoneSmsCodeKey(String phone) { + return rootPrefix.concat("phone:sms:code:").concat(phone); + } } diff --git a/esua-epdc/epdc-commons/epdc-commons-tools/src/main/java/com/elink/esua/epdc/commons/tools/redis/RedisUtils.java b/esua-epdc/epdc-commons/epdc-commons-tools/src/main/java/com/elink/esua/epdc/commons/tools/redis/RedisUtils.java index e09629633..f9fd94eea 100644 --- a/esua-epdc/epdc-commons/epdc-commons-tools/src/main/java/com/elink/esua/epdc/commons/tools/redis/RedisUtils.java +++ b/esua-epdc/epdc-commons/epdc-commons-tools/src/main/java/com/elink/esua/epdc/commons/tools/redis/RedisUtils.java @@ -29,13 +29,33 @@ public class RedisUtils { @Autowired private RedisTemplate redisTemplate; - /** 默认过期时长为24小时,单位:秒 */ + /** + * 默认过期时长为24小时,单位:秒 + */ public final static long DEFAULT_EXPIRE = 60 * 60 * 24L; - /** 过期时长为1小时,单位:秒 */ + /** + * 过期时长为1小时,单位:秒 + */ public final static long HOUR_ONE_EXPIRE = 60 * 60 * 1L; - /** 过期时长为6小时,单位:秒 */ + /** + * 过期时长为6小时,单位:秒 + */ public final static long HOUR_SIX_EXPIRE = 60 * 60 * 6L; - /** 不设置过期时长 */ + /** + * 过期时长为5分钟,单位:秒 + */ + public final static long MINUTE_FIVE_EXPIRE = 60 * 5 * 1L; + /** + * 过期时长为10分钟,单位:秒 + */ + public final static long MINUTE_TEN_EXPIRE = 60 * 10 * 1L; + /** + * 过期时长为30分钟,单位:秒 + */ + public final static long MINUTE_THIRTY_EXPIRE = 60 * 30 * 1L; + /** + * 不设置过期时长 + */ public final static long NOT_EXPIRE = -1L; public void set(String key, Object value, long expire) { diff --git a/esua-epdc/epdc-gateway/src/main/resources/application.yml b/esua-epdc/epdc-gateway/src/main/resources/application.yml index da2c4d266..60baba171 100644 --- a/esua-epdc/epdc-gateway/src/main/resources/application.yml +++ b/esua-epdc/epdc-gateway/src/main/resources/application.yml @@ -179,6 +179,7 @@ renren: - /activiti/modeler.html - /activiti/service/** - /activiti/editor-app/** + - /message/sms/sendCode #发送验证码 - /heart/** diff --git a/esua-epdc/epdc-module/epdc-message/epdc-message-server/pom.xml b/esua-epdc/epdc-module/epdc-message/epdc-message-server/pom.xml index 107a750c3..cfc621b5b 100644 --- a/esua-epdc/epdc-module/epdc-message/epdc-message-server/pom.xml +++ b/esua-epdc/epdc-module/epdc-message/epdc-message-server/pom.xml @@ -124,7 +124,7 @@ epdc - elink888 + elink833066 false 47.104.224.45:8848 @@ -147,7 +147,7 @@ epdc - elink888 + elink833066 false 47.104.224.45:8848 diff --git a/esua-epdc/epdc-module/epdc-message/epdc-message-server/src/main/java/com/elink/esua/epdc/controller/SmsController.java b/esua-epdc/epdc-module/epdc-message/epdc-message-server/src/main/java/com/elink/esua/epdc/controller/SmsController.java index 2bac5161a..96888e989 100644 --- a/esua-epdc/epdc-module/epdc-message/epdc-message-server/src/main/java/com/elink/esua/epdc/controller/SmsController.java +++ b/esua-epdc/epdc-module/epdc-message/epdc-message-server/src/main/java/com/elink/esua/epdc/controller/SmsController.java @@ -1,8 +1,8 @@ /** * Copyright (c) 2018 人人开源 All rights reserved. - * + *

* https://www.renren.io - * + *

* 版权所有,侵权必究! */ @@ -19,7 +19,6 @@ import com.elink.esua.epdc.commons.tools.validator.group.AliyunGroup; import com.elink.esua.epdc.commons.tools.validator.group.QcloudGroup; import com.elink.esua.epdc.dto.SysSmsDTO; import com.elink.esua.epdc.enums.PlatformEnum; -import com.elink.esua.epdc.remote.ParamsRemoteService; import com.elink.esua.epdc.service.SysSmsService; import com.elink.esua.epdc.sms.SmsConfig; import com.elink.esua.epdc.utils.ModuleConstant; @@ -27,6 +26,7 @@ import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; +import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import springfox.documentation.annotations.ApiIgnore; @@ -41,79 +41,96 @@ import java.util.Map; */ @RestController @RequestMapping("sms") -@Api(tags="短信服务") +@Api(tags = "短信服务") public class SmsController { - @Autowired - private SysSmsService sysSmsService; + @Autowired + private SysSmsService sysSmsService; @Autowired private ParamsRemoteService paramsRemoteService; private final static String KEY = ModuleConstant.SMS_CONFIG_KEY; - @GetMapping("page") - @ApiOperation("分页") - @ApiImplicitParams({ - @ApiImplicitParam(name = Constant.PAGE, value = "当前页码,从1开始", paramType = "query", required = true, dataType="int") , - @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") , - @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") , - @ApiImplicitParam(name = Constant.ORDER, value = "排序方式,可选值(asc、desc)", paramType = "query", dataType="String") , - @ApiImplicitParam(name = "mobile", value = "mobile", paramType = "query", dataType="String"), - @ApiImplicitParam(name = "status", value = "status", paramType = "query", dataType="String") - }) - public Result> page(@ApiIgnore @RequestParam Map params){ - PageData page = sysSmsService.page(params); - - return new Result>().ok(page); - } + @GetMapping("page") + @ApiOperation("分页") + @ApiImplicitParams({ + @ApiImplicitParam(name = Constant.PAGE, value = "当前页码,从1开始", paramType = "query", required = true, dataType = "int"), + @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query", required = true, dataType = "int"), + @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType = "String"), + @ApiImplicitParam(name = Constant.ORDER, value = "排序方式,可选值(asc、desc)", paramType = "query", dataType = "String"), + @ApiImplicitParam(name = "mobile", value = "mobile", paramType = "query", dataType = "String"), + @ApiImplicitParam(name = "status", value = "status", paramType = "query", dataType = "String") + }) + public Result> page(@ApiIgnore @RequestParam Map params) { + PageData page = sysSmsService.page(params); + + return new Result>().ok(page); + } @GetMapping("config") - @ApiOperation("获取配置短信") - public Result config(){ - SmsConfig config = paramsRemoteService.getValueObject(KEY, SmsConfig.class); + @ApiOperation("获取配置短信") + public Result config() { + SmsConfig config = paramsRemoteService.getValueObject(KEY, SmsConfig.class); return new Result().ok(config); } - @PostMapping("saveConfig") - @ApiOperation("保存配置短信") - @LogOperation("保存配置短信") - public Result saveConfig(@RequestBody SmsConfig config){ - //校验类型 - ValidatorUtils.validateEntity(config); + @PostMapping("saveConfig") + @ApiOperation("保存配置短信") + @LogOperation("保存配置短信") + public Result saveConfig(@RequestBody SmsConfig config) { + //校验类型 + ValidatorUtils.validateEntity(config); - if(config.getPlatform() == PlatformEnum.ALIYUN.value()){ - //校验阿里云数据 - ValidatorUtils.validateEntity(config, AliyunGroup.class); - }else if(config.getPlatform() == PlatformEnum.QCLOUD.value()){ - //校验腾讯云数据 - ValidatorUtils.validateEntity(config, QcloudGroup.class); - } + if (config.getPlatform() == PlatformEnum.ALIYUN.value()) { + //校验阿里云数据 + ValidatorUtils.validateEntity(config, AliyunGroup.class); + } else if (config.getPlatform() == PlatformEnum.QCLOUD.value()) { + //校验腾讯云数据 + ValidatorUtils.validateEntity(config, QcloudGroup.class); + } - paramsRemoteService.updateValueByCode(KEY, JSON.toJSONString(config)); + paramsRemoteService.updateValueByCode(KEY, JSON.toJSONString(config)); - return new Result(); - } + return new Result(); + } @PostMapping("send") - @ApiOperation("发送短信") - @LogOperation("发送短信") - @ApiImplicitParams({ - @ApiImplicitParam(name = "mobile", value = "手机好号", paramType = "query", required = true, dataType="String"), - @ApiImplicitParam(name = "params", value = "参数", paramType = "query", required = true, dataType="String") - }) - public Result send(String mobile, String params){ + @ApiOperation("发送短信") + @LogOperation("发送短信") + @ApiImplicitParams({ + @ApiImplicitParam(name = "mobile", value = "手机好号", paramType = "query", required = true, dataType = "String"), + @ApiImplicitParam(name = "params", value = "参数", paramType = "query", required = true, dataType = "String") + }) + public Result send(String mobile, String params) { sysSmsService.send(mobile, params); return new Result(); } - @DeleteMapping - @ApiOperation("删除") - @LogOperation("删除") - public Result delete(@RequestBody Long[] ids){ - sysSmsService.deleteBatchIds(Arrays.asList(ids)); + /** + * 发送手机验证码 + * + * @param mobile + * @return com.elink.esua.epdc.commons.tools.utils.Result + * @author yujintao + * @date 2019/9/6 16:50 + */ + @GetMapping("sendCode") + public Result sendCode(String mobile) { + if (StringUtils.isBlank(mobile)) { + return new Result().error(); + } + sysSmsService.sendCode(mobile); + return new Result(); + } + + @DeleteMapping + @ApiOperation("删除") + @LogOperation("删除") + public Result delete(@RequestBody Long[] ids) { + sysSmsService.deleteBatchIds(Arrays.asList(ids)); - return new Result(); - } + return new Result(); + } } diff --git a/esua-epdc/epdc-module/epdc-message/epdc-message-server/src/main/java/com/elink/esua/epdc/redis/SmsRedis.java b/esua-epdc/epdc-module/epdc-message/epdc-message-server/src/main/java/com/elink/esua/epdc/redis/SmsRedis.java new file mode 100644 index 000000000..6894c6a54 --- /dev/null +++ b/esua-epdc/epdc-module/epdc-message/epdc-message-server/src/main/java/com/elink/esua/epdc/redis/SmsRedis.java @@ -0,0 +1,52 @@ +/** + * Copyright 2018 人人开源 https://www.renren.io + *

+ * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + *

+ * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + *

+ * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.elink.esua.epdc.redis; + +import com.elink.esua.epdc.commons.tools.redis.RedisKeys; +import com.elink.esua.epdc.commons.tools.redis.RedisUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + + +/** + * 区划信息表 + * + * @author yujintao yujintao@elink-cn.com + * @since v1.0.0 2019-05-08 + */ +@Component +public class SmsRedis { + + @Autowired + private RedisUtils redisUtils; + + /** + * 缓存短信验证码(十分钟过期) + * + * @param phone 手机号 + * @param smsCode 验证码 + * @return void + * @author yujintao + * @date 2019/9/6 17:01 + */ + public void setSmsCode(String phone, String smsCode) { + String key = RedisKeys.getPhoneSmsCodeKey(phone); + redisUtils.set(key, smsCode, RedisUtils.MINUTE_TEN_EXPIRE); + } + +} \ No newline at end of file diff --git a/esua-epdc/epdc-module/epdc-message/epdc-message-server/src/main/java/com/elink/esua/epdc/service/SysSmsService.java b/esua-epdc/epdc-module/epdc-message/epdc-message-server/src/main/java/com/elink/esua/epdc/service/SysSmsService.java index 75ef6aef4..30ff88111 100644 --- a/esua-epdc/epdc-module/epdc-message/epdc-message-server/src/main/java/com/elink/esua/epdc/service/SysSmsService.java +++ b/esua-epdc/epdc-module/epdc-message/epdc-message-server/src/main/java/com/elink/esua/epdc/service/SysSmsService.java @@ -1,14 +1,13 @@ /** * Copyright (c) 2018 人人开源 All rights reserved. - * + *

* https://www.renren.io - * + *

* 版权所有,侵权必究! */ package com.elink.esua.epdc.service; -import com.elink.esua.epdc.commons.mybatis.service.BaseService; import com.elink.esua.epdc.commons.mybatis.service.BaseService; import com.elink.esua.epdc.commons.tools.page.PageData; import com.elink.esua.epdc.dto.SysSmsDTO; @@ -28,18 +27,30 @@ public interface SysSmsService extends BaseService { /** * 发送短信 - * @param mobile 手机号 - * @param params 短信参数 + * + * @param mobile 手机号 + * @param params 短信参数 */ void send(String mobile, String params); /** * 保存短信发送记录 - * @param platform 平台 + * + * @param platform 平台 * @param mobile 手机号 * @param params 短信参数 * @param status 发送状态 */ void save(Integer platform, String mobile, LinkedHashMap params, Integer status); + + /** + * 发送手机短信验证吗 + * + * @param mobile 手机号 + * @return void + * @author yujintao + * @date 2019/9/6 16:48 + */ + void sendCode(String mobile); } diff --git a/esua-epdc/epdc-module/epdc-message/epdc-message-server/src/main/java/com/elink/esua/epdc/service/impl/SysSmsServiceImpl.java b/esua-epdc/epdc-module/epdc-message/epdc-message-server/src/main/java/com/elink/esua/epdc/service/impl/SysSmsServiceImpl.java index 8bbd52f64..e7e906087 100644 --- a/esua-epdc/epdc-module/epdc-message/epdc-message-server/src/main/java/com/elink/esua/epdc/service/impl/SysSmsServiceImpl.java +++ b/esua-epdc/epdc-module/epdc-message/epdc-message-server/src/main/java/com/elink/esua/epdc/service/impl/SysSmsServiceImpl.java @@ -1,32 +1,34 @@ /** * Copyright (c) 2018 人人开源 All rights reserved. - * + *

* https://www.renren.io - * + *

* 版权所有,侵权必究! */ package com.elink.esua.epdc.service.impl; import cn.hutool.core.map.MapUtil; import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.elink.esua.epdc.commons.mybatis.service.impl.BaseServiceImpl; +import com.elink.esua.epdc.commons.tools.constant.NumConstant; import com.elink.esua.epdc.commons.tools.exception.ErrorCode; +import com.elink.esua.epdc.commons.tools.utils.RandomUtil; import com.elink.esua.epdc.exception.ModuleErrorCode; -import com.elink.esua.epdc.commons.mybatis.service.impl.BaseServiceImpl; import com.elink.esua.epdc.commons.tools.constant.Constant; -import com.elink.esua.epdc.commons.tools.exception.ErrorCode; import com.elink.esua.epdc.commons.tools.exception.RenException; import com.elink.esua.epdc.commons.tools.page.PageData; import com.elink.esua.epdc.dao.SysSmsDao; import com.elink.esua.epdc.dto.SysSmsDTO; import com.elink.esua.epdc.entity.SysSmsEntity; -import com.elink.esua.epdc.exception.ModuleErrorCode; +import com.elink.esua.epdc.redis.SmsRedis; import com.elink.esua.epdc.service.SysSmsService; import com.elink.esua.epdc.sms.AbstractSmsService; import com.elink.esua.epdc.sms.SmsFactory; import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.LinkedHashMap; @@ -35,18 +37,21 @@ import java.util.Map; @Service public class SysSmsServiceImpl extends BaseServiceImpl implements SysSmsService { + @Autowired + private SmsRedis smsRedis; + @Override public PageData page(Map params) { IPage page = baseDao.selectPage( - getPage(params, Constant.CREATE_DATE, false), - getWrapper(params) + getPage(params, Constant.CREATE_DATE, false), + getWrapper(params) ); return getPageData(page, SysSmsDTO.class); } - private QueryWrapper getWrapper(Map params){ - String mobile = (String)params.get("mobile"); - String status = (String)params.get("status"); + private QueryWrapper getWrapper(Map params) { + String mobile = (String) params.get("mobile"); + String status = (String) params.get("status"); QueryWrapper wrapper = new QueryWrapper<>(); wrapper.like(StringUtils.isNotBlank(mobile), "mobile", mobile); @@ -60,13 +65,13 @@ public class SysSmsServiceImpl extends BaseServiceImpl LinkedHashMap map; try { map = JSON.parseObject(params, LinkedHashMap.class); - }catch (Exception e){ + } catch (Exception e) { throw new RenException(ErrorCode.JSON_FORMAT_ERROR); } //短信服务 AbstractSmsService service = SmsFactory.build(); - if(service == null){ + if (service == null) { throw new RenException(ModuleErrorCode.SMS_CONFIG); } @@ -74,6 +79,16 @@ public class SysSmsServiceImpl extends BaseServiceImpl service.sendSms(mobile, map); } + @Override + public void sendCode(String mobile) { + String smsCode = RandomUtil.getNewRandomCode(NumConstant.SIX); + // 缓存手机验证码 + smsRedis.setSmsCode(mobile, smsCode); + JSONObject param = new JSONObject(); + param.put("code", smsCode); + this.send(mobile, param.toJSONString()); + } + @Override public void save(Integer platform, String mobile, LinkedHashMap params, Integer status) { SysSmsEntity sms = new SysSmsEntity(); @@ -81,16 +96,16 @@ public class SysSmsServiceImpl extends BaseServiceImpl sms.setMobile(mobile); //设置短信参数 - if(MapUtil.isNotEmpty(params)){ + if (MapUtil.isNotEmpty(params)) { int index = 1; - for(String value : params.values()){ - if(index == 1){ + for (String value : params.values()) { + if (index == 1) { sms.setParams1(value); - }else if(index == 2){ + } else if (index == 2) { sms.setParams2(value); - }else if(index == 3){ + } else if (index == 3) { sms.setParams3(value); - }else if(index == 4){ + } else if (index == 4) { sms.setParams4(value); } index++;