完成了登录注册

This commit is contained in:
chen-xin-zhi 2024-10-25 18:35:51 +08:00
parent e95cb1d3a5
commit 93262400a6
13 changed files with 493 additions and 1 deletions

24
pom.xml
View File

@ -104,6 +104,30 @@
<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
<version>3.5.5</version>
</dependency>
<!-- apache工具包-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.apache.commons</groupId>-->
<!-- <artifactId>commons-collections4</artifactId>-->
<!-- <version>4.4</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.ansj</groupId>-->
<!-- <artifactId>ansj_seg</artifactId>-->
<!-- <version>5.1.1</version>-->
<!-- </dependency>-->
</dependencies>
<build>

View File

@ -1,9 +1,11 @@
package com.cultural.heritage;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.cultural.heritage.mapper")
public class HeritageApplication {
public static void main(String[] args) {

View File

@ -0,0 +1,19 @@
package com.cultural.heritage.constant;
/**
* 用户常量
*/
@SuppressWarnings("all")
public interface UserConstant {
/**
* 盐值混淆密码
*/
String SALT = "feiyi";
/**
* 用户登录键
*/
String USER_LOGIN_STATE = "feiyi";
}

View File

@ -0,0 +1,84 @@
package com.cultural.heritage.controller.userinfo;
import com.cultural.heritage.common.BaseResponse;
import com.cultural.heritage.common.ErrorCode;
import com.cultural.heritage.common.ResultUtils;
import com.cultural.heritage.exception.BusinessException;
import com.cultural.heritage.model.dto.user.UserLoginRequest;
import com.cultural.heritage.model.entity.User;
import com.cultural.heritage.model.vo.UserVO;
import com.cultural.heritage.service.userinfo.UserService;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* 用户接口
* @author 陈新知
*/
@Slf4j
@RestController
@Tag(name = "用户接口")
@RequestMapping("/user")
public class UserController {
@Resource
private UserService userService;
@GetMapping("/test")
public String test(){
return "匠承非遗";
}
@GetMapping("/list")
public BaseResponse<List<User>> listUser(){
return ResultUtils.success(userService.list());
}
/**
* 登录
*/
@PostMapping("/login")
public BaseResponse<UserVO> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request){
if(userLoginRequest == null) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
String userAccount = userLoginRequest.getUserAccount();
String userPassword = userLoginRequest.getUserPassword();
if(StringUtils.isAnyBlank(userAccount, userPassword)){
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
UserVO userVO = userService.userLogin(userAccount, userPassword, request);
return ResultUtils.success(userVO);
}
/**
* 退出登录
*/
@PostMapping("/logout")
public BaseResponse<Boolean> userLogout(HttpServletRequest request) {
if(request == null) {
throw new BusinessException(ErrorCode.PARAMS_ERROR);
}
boolean result = userService.userLogout(request);
return ResultUtils.success(result);
}
}

View File

@ -0,0 +1,8 @@
package com.cultural.heritage.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.cultural.heritage.model.entity.User;
public interface UserMapper extends BaseMapper<User> {
}

View File

@ -0,0 +1,18 @@
package com.cultural.heritage.model.dto.user;
import lombok.Data;
import java.io.Serial;
import java.io.Serializable;
@Data
public class UserLoginRequest implements Serializable {
private String userAccount;
private String userPassword;
@Serial
private static final long serialVersionUID = 1L;
}

View File

@ -0,0 +1,78 @@
package com.cultural.heritage.model.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@TableName(value = "good")
@Data
public class Good {
/**
* 商品编号
*/
@TableId(type = IdType.AUTO)
private Long id;
/**
* 商品名
*/
private String name;
/**
* 商品类型
*/
private String type;
/**
* 商品图片
*/
private String goodImg;
/**
* 商品简介
*/
private String intro;
/**
* 商品详情简介
*/
private String introDetail;
/**
* 商品详情图片
*/
private String detailImg;
/**
* 商品标签
*/
private String label;
/**
* 商品库存量
*/
private Integer inventory;
/**
* 是否是商品类型
*/
private Integer isGoodType;
/**
* 节日限定序号
*/
private Integer festivalOrder;
/**
* 预约日期
*/
private String reserveDate;
@TableField(exist = false)
private static final long serialVersionUID = 1L;
}

View File

@ -0,0 +1,85 @@
package com.cultural.heritage.model.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.io.Serial;
import java.io.Serializable;
import java.util.Date;
/**
* 用户
* @TableName user
*/
@TableName(value = "user")
@Data
public class User implements Serializable {
/**
* 用户编号
*/
@TableId(type = IdType.AUTO)
private Long id;
/**
* 账号
*/
private String userAccount;
/**
* 密码
*/
private String userPassword;
/**
* 微信开放平台id
*/
private String unionId;
/**
* 小程序openId
*/
private String miniOpenId;
/**
* 用户昵称
*/
private String userName;
/**
* 用户头像
*/
private String userAvatar;
/**
* 手机号
*/
private String phone;
/**
* 用户角色user/admin/ban
*/
private String userRole;
/**
* 创建时间
*/
private Date createTime;
/**
* 更新时间
*/
private Date updateTime;
/**
* 是否删除
*/
private Integer isDelete;
@Serial
@TableField(exist = false)
private static final long serialVersionUID = 1L;
}

View File

@ -0,0 +1,37 @@
package com.cultural.heritage.model.vo;
import lombok.Data;
import java.io.Serializable;
@Data
public class UserVO implements Serializable {
/**
* 主键
*/
private Long id;
/**
* 用户昵称
*/
private String userName;
/**
* 用户头像
*/
private String userAvatar;
/**
* 手机号
*/
private String phone;
/**
* 用户身份
*/
private String userRole;
private static final long serialVersionUID = 1L;
}

View File

@ -0,0 +1,28 @@
package com.cultural.heritage.service.userinfo;
import com.baomidou.mybatisplus.extension.service.IService;
import com.cultural.heritage.model.entity.User;
import com.cultural.heritage.model.vo.UserVO;
import jakarta.servlet.http.HttpServletRequest;
public interface UserService extends IService<User> {
/**
* 用户登录
* @param userAccount 用户账户
* @param userPassword 用户密码
*/
UserVO userLogin(String userAccount, String userPassword, HttpServletRequest request);
/**
* 获取用户信息
*/
UserVO getUserVO(User user);
/**
* 用户退出
* @return 是否退出成功
*/
boolean userLogout(HttpServletRequest request);
}

View File

@ -0,0 +1,82 @@
package com.cultural.heritage.service.userinfo.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cultural.heritage.common.ErrorCode;
import com.cultural.heritage.constant.UserConstant;
import com.cultural.heritage.exception.BusinessException;
import com.cultural.heritage.mapper.UserMapper;
import com.cultural.heritage.model.entity.User;
import com.cultural.heritage.model.vo.UserVO;
import com.cultural.heritage.service.userinfo.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import static com.cultural.heritage.constant.UserConstant.SALT;
import static com.cultural.heritage.constant.UserConstant.USER_LOGIN_STATE;
/**
* @author 陈新知
* @description 针对表user(用户)的数据库操作Service实现
*/
@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
@Override
public UserVO userLogin(String userAccount, String userPassword, HttpServletRequest request) {
if(userAccount.length() < 4) {
throw new BusinessException(ErrorCode.PARAMS_ERROR, "账号错误");
}
if(userPassword.length() < 4) {
throw new BusinessException(ErrorCode.PARAMS_ERROR, "密码错误");
}
String encryptPassword = DigestUtils.md5DigestAsHex((SALT + userPassword).getBytes());
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("userAccount", userAccount);
queryWrapper.eq("userPassword", encryptPassword);
User user = this.baseMapper.selectOne(queryWrapper);
if (user == null) {
log.info("user login failed, userAccount cannot match userPassword");
throw new BusinessException(ErrorCode.PARAMS_ERROR, "用户不存在或密码错误");
}
HttpSession session = request.getSession();
session.setAttribute(USER_LOGIN_STATE, user);
return this.getUserVO(user);
}
@Override
public UserVO getUserVO(User user) {
if (user == null) {
return null;
}
UserVO userVO = new UserVO();
BeanUtils.copyProperties(user, userVO);
return userVO;
}
/**
*
* @param request
* @return
*/
@Override
public boolean userLogout(HttpServletRequest request) {
HttpSession session = request.getSession();
if (session.getAttribute(USER_LOGIN_STATE) == null) {
throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
}
session.removeAttribute(USER_LOGIN_STATE);
return true;
}
}

View File

@ -3,4 +3,24 @@ spring:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://123.249.108.160:3306/feiyi?serverTimezone=Asia/Shanghai
username: feiyi
password: 123456asd
password: 123456asd
server:
port: 8888
servlet:
context-path: /api
# cookie 30 天过期
session:
cookie:
max-age: 2592000
mybatis-plus:
mapper-locations: classpath:mapper/*.xml
configuration:
map-underscore-to-camel-case: false

View File

@ -0,0 +1,7 @@
<?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.cultural.heritage.mapper.UserMapper">
</mapper>