From 93262400a6f1d537a699e9cd860d09d202386ebd Mon Sep 17 00:00:00 2001 From: chen-xin-zhi <3588068430@qq.com> Date: Fri, 25 Oct 2024 18:35:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E4=BA=86=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E6=B3=A8=E5=86=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 24 ++++++ .../heritage/HeritageApplication.java | 2 + .../heritage/constant/UserConstant.java | 19 +++++ .../controller/userinfo/UserController.java | 84 ++++++++++++++++++ .../cultural/heritage/mapper/UserMapper.java | 8 ++ .../model/dto/user/UserLoginRequest.java | 18 ++++ .../cultural/heritage/model/entity/Good.java | 78 +++++++++++++++++ .../cultural/heritage/model/entity/User.java | 85 +++++++++++++++++++ .../cultural/heritage/model/vo/UserVO.java | 37 ++++++++ .../service/userinfo/UserService.java | 28 ++++++ .../userinfo/impl/UserServiceImpl.java | 82 ++++++++++++++++++ src/main/resources/application.yml | 22 ++++- src/main/resources/mapper/UserMapper.xml | 7 ++ 13 files changed, 493 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/cultural/heritage/constant/UserConstant.java create mode 100644 src/main/java/com/cultural/heritage/controller/userinfo/UserController.java create mode 100644 src/main/java/com/cultural/heritage/mapper/UserMapper.java create mode 100644 src/main/java/com/cultural/heritage/model/dto/user/UserLoginRequest.java create mode 100644 src/main/java/com/cultural/heritage/model/entity/Good.java create mode 100644 src/main/java/com/cultural/heritage/model/entity/User.java create mode 100644 src/main/java/com/cultural/heritage/model/vo/UserVO.java create mode 100644 src/main/java/com/cultural/heritage/service/userinfo/UserService.java create mode 100644 src/main/java/com/cultural/heritage/service/userinfo/impl/UserServiceImpl.java create mode 100644 src/main/resources/mapper/UserMapper.xml diff --git a/pom.xml b/pom.xml index 97e017e..2c2b8a7 100644 --- a/pom.xml +++ b/pom.xml @@ -104,6 +104,30 @@ mybatis-plus-spring-boot3-starter 3.5.5 + + + + org.apache.commons + commons-lang3 + 3.12.0 + + + + org.springframework.boot + spring-boot-starter-data-redis + + + + + + + + + + + + + diff --git a/src/main/java/com/cultural/heritage/HeritageApplication.java b/src/main/java/com/cultural/heritage/HeritageApplication.java index d883cc4..a6deaea 100644 --- a/src/main/java/com/cultural/heritage/HeritageApplication.java +++ b/src/main/java/com/cultural/heritage/HeritageApplication.java @@ -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) { diff --git a/src/main/java/com/cultural/heritage/constant/UserConstant.java b/src/main/java/com/cultural/heritage/constant/UserConstant.java new file mode 100644 index 0000000..7e95657 --- /dev/null +++ b/src/main/java/com/cultural/heritage/constant/UserConstant.java @@ -0,0 +1,19 @@ +package com.cultural.heritage.constant; + +/** + * 用户常量 + */ +@SuppressWarnings("all") +public interface UserConstant { + + /** + * 盐值,混淆密码 + */ + String SALT = "feiyi"; + + + /** + * 用户登录键 + */ + String USER_LOGIN_STATE = "feiyi"; +} diff --git a/src/main/java/com/cultural/heritage/controller/userinfo/UserController.java b/src/main/java/com/cultural/heritage/controller/userinfo/UserController.java new file mode 100644 index 0000000..0aa8669 --- /dev/null +++ b/src/main/java/com/cultural/heritage/controller/userinfo/UserController.java @@ -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> listUser(){ + return ResultUtils.success(userService.list()); + } + + + /** + * 登录 + */ + @PostMapping("/login") + public BaseResponse 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 userLogout(HttpServletRequest request) { + if(request == null) { + throw new BusinessException(ErrorCode.PARAMS_ERROR); + } + boolean result = userService.userLogout(request); + return ResultUtils.success(result); + } + + + + + + +} diff --git a/src/main/java/com/cultural/heritage/mapper/UserMapper.java b/src/main/java/com/cultural/heritage/mapper/UserMapper.java new file mode 100644 index 0000000..ab6b35e --- /dev/null +++ b/src/main/java/com/cultural/heritage/mapper/UserMapper.java @@ -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 { + +} diff --git a/src/main/java/com/cultural/heritage/model/dto/user/UserLoginRequest.java b/src/main/java/com/cultural/heritage/model/dto/user/UserLoginRequest.java new file mode 100644 index 0000000..46baa3d --- /dev/null +++ b/src/main/java/com/cultural/heritage/model/dto/user/UserLoginRequest.java @@ -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; +} diff --git a/src/main/java/com/cultural/heritage/model/entity/Good.java b/src/main/java/com/cultural/heritage/model/entity/Good.java new file mode 100644 index 0000000..c42c4cb --- /dev/null +++ b/src/main/java/com/cultural/heritage/model/entity/Good.java @@ -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; + +} diff --git a/src/main/java/com/cultural/heritage/model/entity/User.java b/src/main/java/com/cultural/heritage/model/entity/User.java new file mode 100644 index 0000000..79b78af --- /dev/null +++ b/src/main/java/com/cultural/heritage/model/entity/User.java @@ -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; +} diff --git a/src/main/java/com/cultural/heritage/model/vo/UserVO.java b/src/main/java/com/cultural/heritage/model/vo/UserVO.java new file mode 100644 index 0000000..9e7ec4f --- /dev/null +++ b/src/main/java/com/cultural/heritage/model/vo/UserVO.java @@ -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; +} + diff --git a/src/main/java/com/cultural/heritage/service/userinfo/UserService.java b/src/main/java/com/cultural/heritage/service/userinfo/UserService.java new file mode 100644 index 0000000..ff566b0 --- /dev/null +++ b/src/main/java/com/cultural/heritage/service/userinfo/UserService.java @@ -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 { + + /** + * 用户登录 + * @param userAccount 用户账户 + * @param userPassword 用户密码 + */ + UserVO userLogin(String userAccount, String userPassword, HttpServletRequest request); + + + /** + * 获取用户信息 + */ + UserVO getUserVO(User user); + + /** + * 用户退出 + * @return 是否退出成功 + */ + boolean userLogout(HttpServletRequest request); +} diff --git a/src/main/java/com/cultural/heritage/service/userinfo/impl/UserServiceImpl.java b/src/main/java/com/cultural/heritage/service/userinfo/impl/UserServiceImpl.java new file mode 100644 index 0000000..be3618e --- /dev/null +++ b/src/main/java/com/cultural/heritage/service/userinfo/impl/UserServiceImpl.java @@ -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 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 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; + } + +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 6d42188..6d35d53 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -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 \ No newline at end of file + 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 + + + diff --git a/src/main/resources/mapper/UserMapper.xml b/src/main/resources/mapper/UserMapper.xml new file mode 100644 index 0000000..bdd6bd7 --- /dev/null +++ b/src/main/resources/mapper/UserMapper.xml @@ -0,0 +1,7 @@ + + + + + \ No newline at end of file