parent
4da125d737
commit
75c2ba154a
|
@ -29,4 +29,5 @@ public class MessageConstant {
|
|||
public static final String USER_DOES_NOT_EXIST = "用户不存在";
|
||||
public static final String USER_DOES_IS_EXIST = "用户已存在";
|
||||
public static final String VERIFICATION_CODE_IS_EMPTY = "请先发送验证码";
|
||||
public static final String LOGIN_DTO_IS_EMPTY = "登录参数不能为空";
|
||||
}
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
package com.atguigu.constant;
|
||||
|
||||
public class UserConstant {
|
||||
public static final String USER_AVATAR = "https://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eoj0hHXhgJNOTSOFsS4uZs8x1ConecaVOB8eIl115xmJZcT4oCicvia7wMEufibKtTLqiaJeanU2Lpg3w/132";
|
||||
}
|
|
@ -6,10 +6,9 @@ import lombok.Data;
|
|||
@Data
|
||||
@Schema(description = "用户登录请求参数")
|
||||
public class UserLoginDto {
|
||||
|
||||
@Schema(description = "用户名")
|
||||
private String username ;
|
||||
private String username;
|
||||
|
||||
@Schema(description = "密码")
|
||||
private String password ;
|
||||
private String password;
|
||||
}
|
|
@ -30,7 +30,7 @@ public class CorsConfig {
|
|||
config.setAllowCredentials(true);
|
||||
|
||||
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
|
||||
source.registerCorsConfiguration("/**", config);
|
||||
source.registerCorsConfiguration("/api/**", config);
|
||||
|
||||
return new CorsWebFilter(source);
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package com.atguigu.user.controller;
|
||||
|
||||
import com.atguigu.spzx.model.dto.h5.UserLoginDto;
|
||||
import com.atguigu.spzx.model.dto.h5.UserRegisterDto;
|
||||
import com.atguigu.spzx.model.vo.result.Result;
|
||||
import com.atguigu.user.service.UserInfoService;
|
||||
|
@ -24,4 +25,11 @@ public class UserInfoController {
|
|||
userInfoService.register(userRegisterDto);
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
@Operation(summary = "会员登录")
|
||||
@PostMapping("login")
|
||||
public Result<String> login(@RequestBody UserLoginDto userLoginDto) {
|
||||
String token = userInfoService.login(userLoginDto);
|
||||
return Result.success(token);
|
||||
}
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
package com.atguigu.user.service;
|
||||
|
||||
import com.atguigu.spzx.model.dto.h5.UserLoginDto;
|
||||
import com.atguigu.spzx.model.dto.h5.UserRegisterDto;
|
||||
|
||||
public interface UserInfoService {
|
||||
|
@ -9,4 +10,12 @@ public interface UserInfoService {
|
|||
* @param userRegisterDto 注册对象
|
||||
*/
|
||||
void register(UserRegisterDto userRegisterDto);
|
||||
|
||||
/**
|
||||
* 会员登录
|
||||
*
|
||||
* @param userLoginDto 用户登录请求参数
|
||||
* @return 返回token
|
||||
*/
|
||||
String login(UserLoginDto userLoginDto);
|
||||
}
|
||||
|
|
|
@ -1,7 +1,10 @@
|
|||
package com.atguigu.user.service.impl;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.atguigu.constant.MessageConstant;
|
||||
import com.atguigu.constant.UserConstant;
|
||||
import com.atguigu.exception.BunnyException;
|
||||
import com.atguigu.spzx.model.dto.h5.UserLoginDto;
|
||||
import com.atguigu.spzx.model.dto.h5.UserRegisterDto;
|
||||
import com.atguigu.spzx.model.entity.user.UserInfo;
|
||||
import com.atguigu.user.mapper.UserInfoMapper;
|
||||
|
@ -12,8 +15,11 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.DigestUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@Service
|
||||
public class UserInfoServiceImpl implements UserInfoService {
|
||||
|
@ -54,7 +60,7 @@ public class UserInfoServiceImpl implements UserInfoService {
|
|||
userInfo.setPhone(username);
|
||||
userInfo.setStatus(1);
|
||||
userInfo.setStatus(0);
|
||||
userInfo.setAvatar("https://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eoj0hHXhgJNOTSOFsS4uZs8x1ConecaVOB8eIl115xmJZcT4oCicvia7wMEufibKtTLqiaJeanU2Lpg3w/132");
|
||||
userInfo.setAvatar(UserConstant.USER_AVATAR);
|
||||
|
||||
// 保存用户信息
|
||||
userInfoMapper.save(userInfo);
|
||||
|
@ -62,4 +68,33 @@ public class UserInfoServiceImpl implements UserInfoService {
|
|||
// 删除Redis中的数据
|
||||
redisTemplate.delete(username);
|
||||
}
|
||||
|
||||
/**
|
||||
* 会员登录
|
||||
*
|
||||
* @param userLoginDto 用户登录请求参数
|
||||
* @return 返回token
|
||||
*/
|
||||
@Override
|
||||
public String login(UserLoginDto userLoginDto) {
|
||||
String password = userLoginDto.getPassword();
|
||||
String username = userLoginDto.getUsername();
|
||||
if (!StringUtils.hasText(username) || !StringUtils.hasText(password)) {
|
||||
throw new BunnyException(MessageConstant.LOGIN_DTO_IS_EMPTY);
|
||||
}
|
||||
|
||||
// 判断用户是否存在
|
||||
UserInfo userInfo = userInfoMapper.selectByUsername(username);
|
||||
emptyUtil.isEmpty(userInfo, MessageConstant.USER_DOES_NOT_EXIST);
|
||||
|
||||
// 判断密码是否相同
|
||||
String encryptPassword = DigestUtils.md5DigestAsHex(password.getBytes());
|
||||
if (!userInfo.getPassword().equals(encryptPassword)) {
|
||||
throw new BunnyException(MessageConstant.PASSWORD_ERROR);
|
||||
}
|
||||
// 存入用户信息到Redis
|
||||
String token = UUID.randomUUID().toString();
|
||||
redisTemplate.opsForValue().set(token, JSON.toJSONString(userInfo), 30, TimeUnit.DAYS);
|
||||
return token;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue