第三修改

master
31353 2024-01-08 09:16:04 +08:00
parent 989f61ffdd
commit 8178dc9cae
11 changed files with 253 additions and 6 deletions

View File

@ -15,4 +15,6 @@ import org.springframework.web.bind.annotation.PostMapping;
public interface RemoteUserService {
@PostMapping("/findName/{username}")
public Result<User> findName(@PathVariable("username") String username);
@PostMapping("/findTel/{tel}")
public Result<User> findTel(@PathVariable String tel);
}

View File

@ -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();
}
}

View File

@ -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();
}

View File

@ -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<String> 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<String> 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<String> 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
*/

View File

@ -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;
}

View File

@ -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 <T>
* @return Optional
*/
public static <T> T getOrDefault(Optional<T> optional, T defaultValue) {
return optional.orElse(defaultValue);
}
/**
* Optional
*
* @param optional Optional
* @param action
* @param <T>
*/
public static <T> void ifPresent(Optional<T> optional, Consumer<? super T> action) {
optional.ifPresent(action);
}
/**
* Optional
*
* @param optional Optional
* @param mapper
* @param <T>
* @param <U>
* @return Optional
*/
public static <T, U> Optional<U> map(Optional<T> optional, Function<? super T, ? extends U> mapper) {
return optional.map(mapper);
}
}

View File

@ -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<User> findName(@PathVariable("username") String username){
return userService.findName(username);
}
/**
*
* @param tel
* @return
*/
@PostMapping("/findTel/{tel}")
public Result<User> findTel(@PathVariable String tel){
return userService.findTel(tel);
}
}

View File

@ -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);
}

View File

@ -11,4 +11,5 @@ import com.bwie.common.result.Result;
public interface UserService {
Result<User> findName(String username);
Result<User> findTel(String tel);
}

View File

@ -15,9 +15,26 @@ import org.springframework.stereotype.Service;
public class UserServicelmpl implements UserService{
@Autowired
private UserMapper userMapper;
/**
*
* @param username
* @return
*/
@Override
public Result<User> findName(String username) {
User user = userMapper.findName(username);
return Result.success(user);
}
/**
*
* @param tel
* @return
*/
@Override
public Result<User> findTel(String tel) {
User user = userMapper.findTel(tel);
return Result.success(user);
}
}

View File

@ -1,10 +1,16 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.bwie.user.mapper.UserMapper">
<sql id="user">
user_id,username,password,tel,role
</sql>
<select id="findName" resultType="com.bwie.common.domain.User">
select *
select <include refid="user"></include>
from user
where username = #{username};
</select>
<select id="findTel" resultType="com.bwie.common.domain.User">
select <include refid="user"></include>
from user where tel = #{tel}
</select>
</mapper>