diff --git a/bwie-api/bwie-user-api/src/main/java/com/bwie/user/RemoteUserService.java b/bwie-api/bwie-user-api/src/main/java/com/bwie/user/RemoteUserService.java index 3dfb57f..1b4a25a 100644 --- a/bwie-api/bwie-user-api/src/main/java/com/bwie/user/RemoteUserService.java +++ b/bwie-api/bwie-user-api/src/main/java/com/bwie/user/RemoteUserService.java @@ -15,4 +15,6 @@ import org.springframework.web.bind.annotation.PostMapping; public interface RemoteUserService { @PostMapping("/findName/{username}") public Result findName(@PathVariable("username") String username); + @PostMapping("/findTel/{tel}") + public Result findTel(@PathVariable String tel); } diff --git a/bwie-auth/src/main/java/com/bwie/controller/AuthController.java b/bwie-auth/src/main/java/com/bwie/controller/AuthController.java index 09ecf60..1e32ff1 100644 --- a/bwie-auth/src/main/java/com/bwie/controller/AuthController.java +++ b/bwie-auth/src/main/java/com/bwie/controller/AuthController.java @@ -1,12 +1,11 @@ package com.bwie.controller; +import com.bwie.common.domain.request.TelLogin; import com.bwie.common.domain.request.UserRequest; import com.bwie.common.result.Result; import com.bwie.service.impl.AuthService; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; /** * @author gxb @@ -18,10 +17,37 @@ public class AuthController { @Autowired private AuthService authService; /** - * 登录 + * 用户名 + 密码 = 登录 */ @PostMapping("/login") public Result login(@RequestBody UserRequest userRequest){ return authService.login(userRequest); } + + /** + * 发送验证码 + * @param tel + * @return + */ + @PostMapping("/sendCode/{tel}") + public Result sendCode(@PathVariable String tel){ + return authService.sendCode(tel); + } + + /** + * 手机号 + 验证码 = 登录 + * @param telLogin + * @return + */ + @PostMapping("/TelLogin") + public Result TelLogin(@RequestBody TelLogin telLogin){ + return authService.TelLogin(telLogin); + } + /** + * 获取当前登录人 + */ + @GetMapping("/info") + public Result info(){ + return authService.info(); + } } diff --git a/bwie-auth/src/main/java/com/bwie/service/impl/AuthService.java b/bwie-auth/src/main/java/com/bwie/service/impl/AuthService.java index d1262fd..b1cf92d 100644 --- a/bwie-auth/src/main/java/com/bwie/service/impl/AuthService.java +++ b/bwie-auth/src/main/java/com/bwie/service/impl/AuthService.java @@ -1,5 +1,6 @@ package com.bwie.service.impl; +import com.bwie.common.domain.request.TelLogin; import com.bwie.common.domain.request.UserRequest; import com.bwie.common.result.Result; @@ -11,4 +12,10 @@ import com.bwie.common.result.Result; public interface AuthService { Result login(UserRequest userRequest); + Result sendCode(String tel); + + Result TelLogin(TelLogin telLogin); + + Result info(); + } diff --git a/bwie-auth/src/main/java/com/bwie/service/impl/AuthServicelmpl.java b/bwie-auth/src/main/java/com/bwie/service/impl/AuthServicelmpl.java index f078a31..88871a8 100644 --- a/bwie-auth/src/main/java/com/bwie/service/impl/AuthServicelmpl.java +++ b/bwie-auth/src/main/java/com/bwie/service/impl/AuthServicelmpl.java @@ -5,9 +5,12 @@ import com.bwie.common.constants.JwtConstants; import com.bwie.common.constants.TokenConstants; import com.bwie.common.domain.User; import com.bwie.common.domain.request.JwtRequest; +import com.bwie.common.domain.request.TelLogin; import com.bwie.common.domain.request.UserRequest; import com.bwie.common.result.Result; import com.bwie.common.utils.JwtUtils; +import com.bwie.common.utils.OptionalUtils; +import com.bwie.common.utils.StringUtils; import com.bwie.user.RemoteUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.StringRedisTemplate; @@ -16,6 +19,8 @@ import org.springframework.stereotype.Service; import javax.servlet.http.HttpServletRequest; import java.sql.Time; import java.util.HashMap; +import java.util.Optional; +import java.util.Random; import java.util.concurrent.TimeUnit; /** @@ -31,9 +36,25 @@ public class AuthServicelmpl implements AuthService{ private HttpServletRequest request; @Autowired private StringRedisTemplate redisTemplate; + @Autowired + private OptionalUtils optionalUtils; + /** + * 用户名 + 密码 = 登录 + * @param userRequest + * @return + */ @Override public Result login(UserRequest userRequest) { + //判断姓名是否为空 + Optional optional = Optional.ofNullable(userRequest.getUsername()); + + //判断 + if (!optional.isPresent()){ + //为null定义一个默认值 + String defaultName = optional.orElse("张三"); + userRequest.setUsername(defaultName); + } User user = remoteUserService.findName(userRequest.getUsername()).getData(); if (null==user){ return Result.error("用户名不存在!!!!!!!"); @@ -44,6 +65,76 @@ public class AuthServicelmpl implements AuthService{ JwtRequest token = this.getToken(user); return Result.success(token,"登录成功"); } + + /** + * 发送验证码 + * @param tel + * @return + */ + @Override + public Result sendCode(String tel) { + //判断手机号是否为空 + Optional optional = Optional.ofNullable(tel); + if (!optional.isPresent()){ + //默认值 + String defaultPhone = optional.orElse("18098734205"); + tel = defaultPhone; + } + //格式验证 + if (!tel.matches("^1[3-9]\\d{9}$")){ + return Result.error("手机号格式有误"); + } + User user = remoteUserService.findTel(tel).getData(); + if (null==user){ + return Result.error("手机号不存在"); + } + String code = ""; + for (int i = 0; i < 4; i++) { + code += new Random().nextInt(10); + } + redisTemplate.opsForValue().set(tel,code,10,TimeUnit.MINUTES); + return Result.success("验证码:"+code+",十分钟内有效期,谨防假冒,未泄露与他人"); + } + + /** + * 手机号 + 验证码 = 登录 + * @param telLogin + * @return + */ + @Override + public Result TelLogin(TelLogin telLogin) { + //判断手机号是否为空 + Optional optional = Optional.ofNullable(telLogin.getTel()); + if (!optional.isPresent()){ + //默认值 + String defaultPhone = optional.orElse("18098734205"); + telLogin.setTel(defaultPhone); + } + //格式验证 + if (!telLogin.getTel().matches("^1[3-9]\\d{9}$")){ + return Result.error("手机号格式有误"); + } + User user = remoteUserService.findTel(telLogin.getTel()).getData(); + if (null==user){ + return Result.error("手机号不存在"); + } + //验证码验证码 + if (!redisTemplate.hasKey(telLogin.getTel())){ + return Result.error("验证码过期了"); + } + String getCode = redisTemplate.opsForValue().get(telLogin.getTel()); + if (!getCode.equals(telLogin.getCode())){ + return Result.error("验证码错误"); + } + JwtRequest token = this.getToken(user); + return Result.success(token,"登录成功"); + } + + @Override + public Result info() { + return null; + } + /** * 生成token */ diff --git a/bwie-common/src/main/java/com/bwie/common/domain/request/TelLogin.java b/bwie-common/src/main/java/com/bwie/common/domain/request/TelLogin.java new file mode 100644 index 0000000..da6d744 --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/common/domain/request/TelLogin.java @@ -0,0 +1,14 @@ +package com.bwie.common.domain.request; + +import lombok.Data; + +/** + * @author gxb + * @description TODO + * @date 2024-01-08 8:51 + */ +@Data +public class TelLogin { + private String tel; + private String code; +} diff --git a/bwie-common/src/main/java/com/bwie/common/utils/OptionalUtils.java b/bwie-common/src/main/java/com/bwie/common/utils/OptionalUtils.java new file mode 100644 index 0000000..f7741f8 --- /dev/null +++ b/bwie-common/src/main/java/com/bwie/common/utils/OptionalUtils.java @@ -0,0 +1,55 @@ +package com.bwie.common.utils; + +import org.springframework.stereotype.Component; + +import java.util.Optional; +import java.util.function.Consumer; +import java.util.function.Function; + +/** + * @author gxb + * @description TODO + * @date 2024-01-07 9:29 + */ +@Component +public class OptionalUtils { + // 私有构造函数,避免被实例化 + private OptionalUtils() {} + + /** + * 获取 Optional 对象中的值,如果为空,则返回默认值 + * + * @param optional Optional 对象 + * @param defaultValue 默认值 + * @param 值的类型 + * @return Optional 对象中的值或默认值 + */ + public static T getOrDefault(Optional optional, T defaultValue) { + return optional.orElse(defaultValue); + } + + /** + * 如果 Optional 对象中的值存在,则执行给定的操作 + * + * @param optional Optional 对象 + * @param action 要执行的操作 + * @param 值的类型 + */ + public static void ifPresent(Optional optional, Consumer action) { + optional.ifPresent(action); + } + + /** + * 将 Optional 对象中的值转换为另一种类型 + * + * @param optional Optional 对象 + * @param mapper 转换函数 + * @param 原始值的类型 + * @param 转换后的值的类型 + * @return 转换后的 Optional 对象 + */ + public static Optional map(Optional optional, Function mapper) { + return optional.map(mapper); + } + +} diff --git a/bwie-modules/bwie-user/src/main/java/com/bwie/user/controller/UserController.java b/bwie-modules/bwie-user/src/main/java/com/bwie/user/controller/UserController.java index e6a3335..7641b53 100644 --- a/bwie-modules/bwie-user/src/main/java/com/bwie/user/controller/UserController.java +++ b/bwie-modules/bwie-user/src/main/java/com/bwie/user/controller/UserController.java @@ -17,8 +17,24 @@ import org.springframework.web.bind.annotation.RestController; public class UserController { @Autowired private UserService userService; + + /** + * 用户名查询 + * @param username + * @return + */ @PostMapping("/findName/{username}") public Result findName(@PathVariable("username") String username){ return userService.findName(username); } + + /** + * 手机号查询 + * @param tel + * @return + */ + @PostMapping("/findTel/{tel}") + public Result findTel(@PathVariable String tel){ + return userService.findTel(tel); + } } diff --git a/bwie-modules/bwie-user/src/main/java/com/bwie/user/mapper/UserMapper.java b/bwie-modules/bwie-user/src/main/java/com/bwie/user/mapper/UserMapper.java index 5aeb206..19daf7c 100644 --- a/bwie-modules/bwie-user/src/main/java/com/bwie/user/mapper/UserMapper.java +++ b/bwie-modules/bwie-user/src/main/java/com/bwie/user/mapper/UserMapper.java @@ -12,5 +12,17 @@ import org.springframework.stereotype.Component; @Component @Mapper public interface UserMapper { + /** + * 用户名查询 + * @param username + * @return + */ User findName(String username); + + /** + * 手机号查询 + * @param tel + * @return + */ + User findTel(String tel); } diff --git a/bwie-modules/bwie-user/src/main/java/com/bwie/user/service/impl/UserService.java b/bwie-modules/bwie-user/src/main/java/com/bwie/user/service/impl/UserService.java index 38e998f..83483d4 100644 --- a/bwie-modules/bwie-user/src/main/java/com/bwie/user/service/impl/UserService.java +++ b/bwie-modules/bwie-user/src/main/java/com/bwie/user/service/impl/UserService.java @@ -11,4 +11,5 @@ import com.bwie.common.result.Result; public interface UserService { Result findName(String username); + Result findTel(String tel); } diff --git a/bwie-modules/bwie-user/src/main/java/com/bwie/user/service/impl/UserServicelmpl.java b/bwie-modules/bwie-user/src/main/java/com/bwie/user/service/impl/UserServicelmpl.java index 02838a6..f3bb6d2 100644 --- a/bwie-modules/bwie-user/src/main/java/com/bwie/user/service/impl/UserServicelmpl.java +++ b/bwie-modules/bwie-user/src/main/java/com/bwie/user/service/impl/UserServicelmpl.java @@ -15,9 +15,26 @@ import org.springframework.stereotype.Service; public class UserServicelmpl implements UserService{ @Autowired private UserMapper userMapper; + + /** + * 用户名查询 + * @param username + * @return + */ @Override public Result findName(String username) { User user = userMapper.findName(username); return Result.success(user); } + + /** + * 手机号查询 + * @param tel + * @return + */ + @Override + public Result findTel(String tel) { + User user = userMapper.findTel(tel); + return Result.success(user); + } } diff --git a/bwie-modules/bwie-user/src/main/resources/mapper/UserMapper.xml b/bwie-modules/bwie-user/src/main/resources/mapper/UserMapper.xml index c2a233f..b1fd900 100644 --- a/bwie-modules/bwie-user/src/main/resources/mapper/UserMapper.xml +++ b/bwie-modules/bwie-user/src/main/resources/mapper/UserMapper.xml @@ -1,10 +1,16 @@ - + + user_id,username,password,tel,role + +