From 15462ce0ba21f48fc7c0a5c875862290ce6f9ffe Mon Sep 17 00:00:00 2001 From: caolin <1149034574@qq.com> Date: Sun, 13 Aug 2023 00:19:00 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8E=B7=E5=8F=96=E9=AA=8C=E8=AF=81=E7=A0=81?= =?UTF-8?q?=20=E5=8F=8A=E7=99=BB=E9=99=86=E7=99=BB=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../driver/server/config/SwaggerConfig.java | 1 - .../jwl/driver/server/constant/Constants.java | 3 + .../controller/TdSysUserController.java | 10 +++- .../exception/GlobalExceptionHandler.java | 6 ++ .../server/service/ITdSysUserService.java | 8 +++ .../service/impl/TdSysUserServiceImpl.java | 55 ++++++++++++++++--- .../com/jwl/driver/server/util/SmsUtil.java | 24 +++++++- src/main/resources/application.yml | 10 ++++ 8 files changed, 105 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/jwl/driver/server/config/SwaggerConfig.java b/src/main/java/com/jwl/driver/server/config/SwaggerConfig.java index 2acb01f..2d398e8 100644 --- a/src/main/java/com/jwl/driver/server/config/SwaggerConfig.java +++ b/src/main/java/com/jwl/driver/server/config/SwaggerConfig.java @@ -32,7 +32,6 @@ public class SwaggerConfig { @Order(value = 1) public Docket adminDocket(){ return new Docket(DocumentationType.SWAGGER_2) - .pathMapping("/driver-api") .enable(true) .apiInfo(groupApiInfo()) .select() diff --git a/src/main/java/com/jwl/driver/server/constant/Constants.java b/src/main/java/com/jwl/driver/server/constant/Constants.java index 3177d8c..311ba66 100644 --- a/src/main/java/com/jwl/driver/server/constant/Constants.java +++ b/src/main/java/com/jwl/driver/server/constant/Constants.java @@ -6,5 +6,8 @@ package com.jwl.driver.server.constant; public class Constants { + public static String MESSAGE_CODE_PREFIX = "code_"; + + public static String MESSAGE_TEMPLATE = "SMS_198880447"; } diff --git a/src/main/java/com/jwl/driver/server/controller/TdSysUserController.java b/src/main/java/com/jwl/driver/server/controller/TdSysUserController.java index c1e334b..d19acde 100644 --- a/src/main/java/com/jwl/driver/server/controller/TdSysUserController.java +++ b/src/main/java/com/jwl/driver/server/controller/TdSysUserController.java @@ -12,6 +12,7 @@ import org.springframework.web.bind.annotation.*; import org.springframework.stereotype.Controller; +import javax.validation.Valid; import java.time.LocalDateTime; /** @@ -31,10 +32,17 @@ public class TdSysUserController { @Autowired private ITdSysUserService userService; + @ApiOperation("用户登陆") + @GetMapping("/code") + public BaseResponse code(@RequestParam String phone) { + log.info("获取验证码======>phone:{}", phone); + return BaseResponse.success(userService.code(phone)); + } + @ApiOperation("用户登陆") @PostMapping("/login") - public BaseResponse login(@RequestBody LoginUserDto loginUserDto) { + public BaseResponse login(@RequestBody @Valid LoginUserDto loginUserDto) { log.info("用户登录======>loginUserDto:{}", loginUserDto); return BaseResponse.success(userService.login(loginUserDto)); } diff --git a/src/main/java/com/jwl/driver/server/exception/GlobalExceptionHandler.java b/src/main/java/com/jwl/driver/server/exception/GlobalExceptionHandler.java index 86afd31..4114bd3 100644 --- a/src/main/java/com/jwl/driver/server/exception/GlobalExceptionHandler.java +++ b/src/main/java/com/jwl/driver/server/exception/GlobalExceptionHandler.java @@ -1,6 +1,7 @@ package com.jwl.driver.server.exception; +import com.jwl.driver.server.enums.StatusEnum; import com.jwl.driver.server.response.BaseResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -44,4 +45,9 @@ public class GlobalExceptionHandler { public BaseResponse signatureException(SignatureException e) { return BaseResponse.fail("E004", "token is invalid"); } + + @ExceptionHandler(Exception.class) + public BaseResponse exception(Exception e) { + return BaseResponse.fail(StatusEnum.FAIL.getCode(), null, null); + } } diff --git a/src/main/java/com/jwl/driver/server/service/ITdSysUserService.java b/src/main/java/com/jwl/driver/server/service/ITdSysUserService.java index 0b32459..c45214a 100644 --- a/src/main/java/com/jwl/driver/server/service/ITdSysUserService.java +++ b/src/main/java/com/jwl/driver/server/service/ITdSysUserService.java @@ -15,6 +15,13 @@ import com.jwl.driver.server.vo.LoginUserVo; */ public interface ITdSysUserService extends IService { + /** + * 获取短信验证码 + * @param phone + * @return + */ + String code(String phone); + /** * 用户登陆 * @param loginUserDto @@ -27,4 +34,5 @@ public interface ITdSysUserService extends IService { * @return */ Boolean loginOut(); + } diff --git a/src/main/java/com/jwl/driver/server/service/impl/TdSysUserServiceImpl.java b/src/main/java/com/jwl/driver/server/service/impl/TdSysUserServiceImpl.java index 4bde82a..132269c 100644 --- a/src/main/java/com/jwl/driver/server/service/impl/TdSysUserServiceImpl.java +++ b/src/main/java/com/jwl/driver/server/service/impl/TdSysUserServiceImpl.java @@ -1,7 +1,9 @@ package com.jwl.driver.server.service.impl; +import cn.hutool.core.util.PhoneUtil; import cn.hutool.core.util.StrUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.jwl.driver.server.constant.Constants; import com.jwl.driver.server.dto.LoginUserDto; import com.jwl.driver.server.entity.TdSysUser; import com.jwl.driver.server.exception.BusinessException; @@ -9,15 +11,18 @@ import com.jwl.driver.server.mapper.TdSysUserMapper; import com.jwl.driver.server.redis.RedisCache; import com.jwl.driver.server.service.ITdSysUserService; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.jwl.driver.server.util.SmsUtil; import com.jwl.driver.server.util.TokenThreadUtil; import com.jwl.driver.server.vo.LoginUserVo; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.time.LocalDateTime; import java.util.UUID; +import java.util.concurrent.TimeUnit; /** *

@@ -33,42 +38,76 @@ public class TdSysUserServiceImpl extends ServiceImpl cond = new LambdaQueryWrapper() - .eq(TdSysUser::getPhone,"18255439337"); + .eq(TdSysUser::getPhone, "18255439337"); //用户不存在则直接注册登陆 TdSysUser tdSysUser = this.baseMapper.selectOne(cond); - if (tdSysUser == null){ + //todo 用户基础信息填写这里还要完善一下 + if (tdSysUser == null) { tdSysUser = new TdSysUser() .setUserName("车友") .setAvatar("") .setPhone(loginUserDto.getPhone()) .setCreateTime(LocalDateTime.now()); int insert = this.getBaseMapper().insert(tdSysUser); - if (insert != 1){ + if (insert != 1) { throw new BusinessException("用户注册异常"); } } String token = UUID.randomUUID().toString(); - redisCache.setCacheObject(token,"123456",365*24*60*60); LoginUserVo loginUserVo = new LoginUserVo(); - BeanUtils.copyProperties(tdSysUser,loginUserVo); + BeanUtils.copyProperties(tdSysUser, loginUserVo); loginUserVo.setToken(token); - + // 存入redis + redisCache.setCacheObject(token, tdSysUser, tokenExpireTime, TimeUnit.DAYS); return loginUserVo; } @Override public Boolean loginOut() { String token = TokenThreadUtil.getToken(); - if (StrUtil.isNotBlank(token)){ + if (StrUtil.isNotBlank(token)) { redisCache.deleteObject(token); } return Boolean.TRUE; } + } diff --git a/src/main/java/com/jwl/driver/server/util/SmsUtil.java b/src/main/java/com/jwl/driver/server/util/SmsUtil.java index dc967ba..71af4be 100644 --- a/src/main/java/com/jwl/driver/server/util/SmsUtil.java +++ b/src/main/java/com/jwl/driver/server/util/SmsUtil.java @@ -8,6 +8,9 @@ import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse; import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.profile.DefaultProfile; import com.aliyuncs.profile.IClientProfile; +import com.jwl.driver.server.constant.Constants; +import com.jwl.driver.server.exception.BusinessException; +import jdk.vm.ci.meta.Constant; import lombok.Data; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -37,6 +40,21 @@ public class SmsUtil { //用于存储短信验证码 private static Map codeMap = new HashMap<>(); + + /** + * 发送验证码 + * @param sendPhone + * @return + */ + public static String sendMessage(String sendPhone) { + String code = getMessageCode(sendPhone); + Map param = new HashMap<>(); + param.put("code", code); + //发送短信 + currencySendMessage(sendPhone, Constants.MESSAGE_TEMPLATE, JSONObject.toJSONString(param)); + return code; + } + /** * 生成短信验证码并存储 */ @@ -87,12 +105,14 @@ public class SmsUtil { } catch (ClientException e) { logger.error("发送短信失败:"+e.getErrMsg()+"\n"+e.getMessage()); e.printStackTrace(); + throw new BusinessException("获取验证码失败"); } if (sendSmsResponse!=null){ logger.info("message:"+sendSmsResponse.getMessage()+"\n"+ "code:"+sendSmsResponse.getCode()+"\n"); }else{ logger.info("发送短信响应为null"); + throw new BusinessException("获取验证码失败"); } return JSONObject.toJSONString(sendSmsResponse); @@ -117,8 +137,8 @@ public class SmsUtil { public static void main(String[] args) throws IOException { Map param = new HashMap<>(); - param.put("code", getMessageCode("18855146869")); - SmsUtil.currencySendMessage("18855146869", "SMS_198880447", JSONObject.toJSONString(param) ); + param.put("code", getMessageCode("18255439337")); + SmsUtil.currencySendMessage("18255439337", "SMS_198880447", JSONObject.toJSONString(param) ); } } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 0b533ea..8a7bd1b 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -41,6 +41,16 @@ driver: - /driver-api/v2/api-docs - /driver-api/swagger-resources - /driver-api/favicon.ico + - /driver-api/tdSysUser/code # 需要权限校验url集合 needAuthEndPoints: + +# token 有效期1年 +token: + expireTime: 365 + +# 短信验证码失效时间5分钟 +message: + code: + expireTime: 300