项目三阶段:用户功能(未测试版本)

grs
yjxx 1 month ago
parent 038760b094
commit 1b19f8ee70

@ -0,0 +1,14 @@
package jty.expressdistributionsystem.DTO;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
@Data
public class AddresseeDTO {
@NotNull
private Long goodsId;
@NotBlank(message = "取件码不能为空")
private String claimCode;
}

@ -0,0 +1,29 @@
package jty.expressdistributionsystem.DTO;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
@Data
public class GoodsDTO {
@NotBlank(message = "请选择快递公司")
private String company;
@JsonSerialize(using = ToStringSerializer.class)
@NotNull
private Long sendUserId;
@NotBlank(message = "收件人名字不能为空")
private String getUserName;
@NotBlank(message = "快递名称不能为空")
private String name;
@NotBlank(message = "快递重量不能为空")
private String quality;
@NotBlank(message = "运送地址不能为空")
private String address;
}

@ -0,0 +1,16 @@
package jty.expressdistributionsystem.DTO;
import jakarta.validation.constraints.NotBlank;
import lombok.Data;
@Data
public class PickUpExpressDTO {
@NotBlank(message = "收件人名字不能为空")
private String master;
@NotBlank(message = "快递码不能为空")
private String expressCode;
@NotBlank(message = "取件码不能为空")
private String claimCode;
}

@ -4,13 +4,12 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import jty.expressdistributionsystem.DTO.SendMessageDTO; import jty.expressdistributionsystem.DTO.SendMessageDTO;
import jty.expressdistributionsystem.config.MyMetaObjectHandler;
import jty.expressdistributionsystem.entity.Message; import jty.expressdistributionsystem.entity.Message;
import jty.expressdistributionsystem.entity.Result; import jty.expressdistributionsystem.entity.Result;
import jty.expressdistributionsystem.entity.User; import jty.expressdistributionsystem.entity.User;
import jty.expressdistributionsystem.service.MessageService; import jty.expressdistributionsystem.service.MessageService;
import jty.expressdistributionsystem.service.UserService; import jty.expressdistributionsystem.service.UserService;
import jty.expressdistributionsystem.utils.SendMessage; import jty.expressdistributionsystem.utils.SendMessageUtil;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
@RestController @RestController
@ -62,7 +61,7 @@ public class AdminController {
// 管理员发送消息 // 管理员发送消息
@PostMapping("/sendMessage") @PostMapping("/sendMessage")
public Result<String> sendMessage(@RequestBody SendMessageDTO sendMessageDTO) { public Result<String> sendMessage(@RequestBody SendMessageDTO sendMessageDTO) {
Message message = SendMessage.sendMessage(sendMessageDTO); Message message = SendMessageUtil.sendMessage(sendMessageDTO);
messageService.save(message); messageService.save(message);
return new Result<>(200, "发送成功", ""); return new Result<>(200, "发送成功", "");
} }

@ -1,155 +1,198 @@
package jty.expressdistributionsystem.controller; package jty.expressdistributionsystem.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import jty.expressdistributionsystem.DTO.UserLoginByAccountDTO; import jty.expressdistributionsystem.DTO.AddresseeDTO;
import jty.expressdistributionsystem.DTO.UserModifyInfoDTO; import jty.expressdistributionsystem.DTO.GoodsDTO;
import jty.expressdistributionsystem.DTO.UserModifyPasswordDTO; import jty.expressdistributionsystem.DTO.PickUpExpressDTO;
import jty.expressdistributionsystem.entity.Result; import jty.expressdistributionsystem.DTO.SendMessageDTO;
import jty.expressdistributionsystem.entity.User; import jty.expressdistributionsystem.entity.*;
import jty.expressdistributionsystem.service.UserService; import jty.expressdistributionsystem.service.*;
import jty.expressdistributionsystem.utils.ClaimCodeUtil;
import jty.expressdistributionsystem.utils.ExpressCodeUtil;
import jty.expressdistributionsystem.utils.GetIdUtil; import jty.expressdistributionsystem.utils.GetIdUtil;
import jty.expressdistributionsystem.utils.JwtUtil; import jty.expressdistributionsystem.utils.SendMessageUtil;
import jty.expressdistributionsystem.utils.Md5Util;
import jty.expressdistributionsystem.utils.ThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime; import java.util.List;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
@RestController @RestController
@RequestMapping("/user") @RequestMapping("/user")
@Slf4j
public class UserController { public class UserController {
@Resource @Resource
private UserService userService; private UserService userService;
@Resource @Resource
private StringRedisTemplate stringRedisTemplate; private MessageService messageService;
// 注册 @Resource
@PostMapping("/register") private RecordsService recordsService;
public Result<String> register(@RequestBody @Validated @NotNull User user) {
// 查找用户是否存在 @Resource
QueryWrapper<User> queryWrapper = new QueryWrapper<>(); private CodeService codeService;
queryWrapper.eq("user_name", user.getUserName())
.or() @Resource
.eq("account", user.getAccount()) private GoodsService goodsService;
.or()
.eq("phone", user.getPhone()); @Resource
User u = userService.getOne(queryWrapper); private AddressService addressService;
if (u != null) {
return new Result<>(409, "账号或用户名或手机号已存在", ""); // 用户给快递员发消息
} @PostMapping("/sendMessage")
// 密码加密 public Result<String> sendMessage(@Validated @RequestBody SendMessageDTO sendMessageDTO) {
user.setPassword(Md5Util.getMD5String(user.getPassword())); Message message = SendMessageUtil.sendMessage(sendMessageDTO);
// 用户数据入库 messageService.save(message);
userService.save(user); return new Result<>(200, "发送成功", "");
return new Result<>(200, "注册成功", "");
} }
// 通过账号登录 // 给快递员点赞
@PostMapping("/loginByAccount") @PostMapping("/likes")
public Result<String> login(@RequestBody @Validated @NotNull UserLoginByAccountDTO userLoginByAccountDTO) { public Result<String> like(Long id) {
// 查询用户是否存在 User employee = userService.getById(id);
Optional<User> optionalUser = Optional.ofNullable(userService.getOne(new QueryWrapper<User>() employee.setLikes(employee.getLikes() + 1);
.eq("account", userLoginByAccountDTO.getAccount()))); userService.save(employee);
if (optionalUser.isEmpty() || !Md5Util.getMD5String(userLoginByAccountDTO.getPassword()).equals(optionalUser.get().getPassword())) { return new Result<>(200, "点赞成功", "");
return new Result<>(401, "账号或者密码错误", "");
}
User user = optionalUser.get();
if(user.getDisabled() == 1){
return new Result<>(403, "该账号已被禁用, 请联系管理员", "");
}
user.setLoginTime(LocalDateTime.now());
userService.update(user, new UpdateWrapper<User>().eq("id", user.getId()));
// 校验通过
Map<String, Object> claims = new HashMap<>();
claims.put("id", user.getId());
claims.put("account", userLoginByAccountDTO.getAccount());
// 获取token
String token = JwtUtil.genToken(claims);
// 存放token到redis当中进行持久化存储
ValueOperations<String, String> stringStringValueOperations = stringRedisTemplate.opsForValue();
// 设置过期时间(1天)
stringStringValueOperations.set(token, token, 1, TimeUnit.DAYS);
// 返回token给前端
return new Result<>(200, "登录成功", token);
} }
// 修改个人信息 // 用户查看自己的快递记录
@PutMapping("/modify") @GetMapping("/history")
public Result<Object> modifyInfo(@RequestBody @Validated @NotNull UserModifyInfoDTO userModifyInfoDTO) { public Result<Page<Records>> getHistory(@RequestParam int page, @RequestParam int pageSize) {
// 获取登录用户id if (page <= 0 || pageSize == 0) {
return new Result<>(400, "页码和每页大小必须大于0", null);
}
Long id = GetIdUtil.getId(); Long id = GetIdUtil.getId();
User user = userService.getById(id); Page<Records> pageInfo = new Page<>(page, pageSize);
// 检查phone和userName是否与当前用户信息一致 LambdaQueryWrapper<Records> queryWrapper = new LambdaQueryWrapper<>();
boolean isPhoneSame = user.getPhone().equals(userModifyInfoDTO.getPhone()); queryWrapper.eq(Records::getSendUserId, id);
boolean isUserNameSame = user.getUserName().equals(userModifyInfoDTO.getUserName()); queryWrapper.orderByDesc(Records::getCreateTime);
if (isPhoneSame && isUserNameSame) { recordsService.page(pageInfo, queryWrapper);
return new Result<>(400, "请修改您的信息", ""); return new Result<>(200, "", pageInfo);
}
// 用户收件
@PostMapping("/addressee")
public Result<String> addressee(@Validated @RequestBody @NotNull AddresseeDTO addresseeDTO) {
Long goodsId = addresseeDTO.getGoodsId();
QueryWrapper<Code> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("goods_id", goodsId);
Code code = codeService.getOne(queryWrapper);
// 判断取件码是否输入正确
if (!code.getClaim().equals(addresseeDTO.getClaimCode())) {
return new Result<>(401, "请输入正确的取件码", "");
} }
QueryWrapper<User> queryWrapper = new QueryWrapper<>(); QueryWrapper<Records> recordsQueryWrapper = new QueryWrapper<>();
if (!isPhoneSame) { recordsQueryWrapper.eq("goods_id", goodsId);
queryWrapper.eq("phone", userModifyInfoDTO.getPhone()); Records records = recordsService.getOne(recordsQueryWrapper);
if (userService.getOne(queryWrapper) != null) { records.setMark(1);
return new Result<>(400, "该手机号已被注册", ""); // 设置取件人
} records.setAddresseeId(GetIdUtil.getId());
user.setPhone(userModifyInfoDTO.getPhone()); recordsService.save(records);
return new Result<>(200, "取件成功", "");
}
// 用户代取快递
@PostMapping("/pickUp")
public Result<String> pickUpExpress(@RequestBody @Validated @NotNull PickUpExpressDTO pickUpExpressDTO) {
// 查询快递原主人信息
QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
userQueryWrapper.eq("user_name", pickUpExpressDTO.getMaster());
User master = userService.getOne(userQueryWrapper);
if (master == null) {
return new Result<>(404, "该用户不存在", "");
} }
queryWrapper.clear(); // 查询快递信息
if (!isUserNameSame) { QueryWrapper<Goods> goodsQueryWrapper = new QueryWrapper<>();
queryWrapper.eq("user_name", userModifyInfoDTO.getUserName()); goodsQueryWrapper.eq("express_code", pickUpExpressDTO.getExpressCode());
if (userService.getOne(queryWrapper) != null) { Goods goods = goodsService.getOne(goodsQueryWrapper);
return new Result<>(400, "该用户名已被使用", ""); if (goods == null) {
} return new Result<>(404, "快递不存在", "");
user.setUserName(userModifyInfoDTO.getUserName());
} }
user.setUpdateTime(null); // 代取
userService.updateById(user); QueryWrapper<Records> recordsQueryWrapper = new QueryWrapper<>();
return new Result<>(200, "用户信息修改成功", user); recordsQueryWrapper.eq("goods_id", goods.getId());
Records records = recordsService.getOne(recordsQueryWrapper);
records.setMark(1);
records.setAddresseeId(GetIdUtil.getId());
recordsService.save(records);
return new Result<>(200, "待取成功", "");
} }
// 修改个人密码 // 用户查看消息
@PutMapping("/rePassword") @GetMapping("/getMessage")
public Result<String> modifyPassword(@RequestHeader("Authorization") String token, public Result<Page<Message>> getMessage(@RequestParam int page, @RequestParam int pageSize) {
@RequestBody @Validated @NotNull UserModifyPasswordDTO userModifyPasswordDTO) { if (page <= 0 || pageSize == 0) {
// 判断密码 return new Result<>(400, "页码和每页大小必须大于0", null);
if (userModifyPasswordDTO.getOldPassword().equals(userModifyPasswordDTO.getNewPassword())) {
return new Result<>(409, "新密码不得和旧密码一致", "");
}
if (!userModifyPasswordDTO.getNewPassword().equals(userModifyPasswordDTO.getReNewPassword())) {
return new Result<>(409, "两次输入密码不相同", "");
} }
Long id = GetIdUtil.getId(); Long id = GetIdUtil.getId();
User user = userService.getById(id); Page<Message> messagePage = new Page<>(page, pageSize);
if (!user.getIdentity().equals(userModifyPasswordDTO.getIdentity())) { LambdaQueryWrapper<Message> messageLambdaQueryWrapper = new LambdaQueryWrapper<>();
return new Result<>(409, "身份证有误", ""); // 添加查询条件
} messageLambdaQueryWrapper.eq(Message::getSendUserId, id);
user.setPassword(Md5Util.getMD5String(userModifyPasswordDTO.getNewPassword())); // 排序条件:先按 mark 对应值排序,然后按时间排序
user.setUpdateTime(null); messageLambdaQueryWrapper.orderByDesc(Message::getMark) // mark 值大的排前面
userService.updateById(user); .orderByDesc(Message::getCreateTime); // 时间新的排前面
// 删除redis当中的token messageService.page(messagePage, messageLambdaQueryWrapper);
stringRedisTemplate.delete(token); return new Result<>(200, "获取成功", messagePage);
// 删除当前线程的存储信息
ThreadLocalUtil.remove();
return new Result<>(200, "密码修改成功, 请重新登陆", "");
} }
// 获取个人信息 // 用户确认查看消息
@GetMapping("/info") @PostMapping("/confirmMessage")
public Result<User> getInfo() { public Result<String> confirmMessage(@RequestParam Long id) {
Long id = GetIdUtil.getId(); Records records = recordsService.getById(id);
User user = userService.getById(id); records.setMark(0);
return new Result<>(200, "", user); recordsService.save(records);
return new Result<>(200, "消息查看成功", "");
}
// 用户获取自己写过的地址
@GetMapping("/getAddress")
public Result<List<Address>> listAddress() {
LambdaQueryWrapper<Address> addressLambdaQueryWrapper = new LambdaQueryWrapper<>();
addressLambdaQueryWrapper.eq(Address::getUserId, GetIdUtil.getId())
.orderByDesc(Address::getAddress);
List<Address> list = addressService.list(addressLambdaQueryWrapper);
return new Result<>(200, "", list);
}
// 用户寄件
@PostMapping("/shipment")
public Result<String> shipmentExpress(@RequestBody @Validated @NotNull GoodsDTO goodsDTO) {
// 判断用户是否存在
LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
userLambdaQueryWrapper.eq(User::getUserName, goodsDTO.getGetUserName());
User getUser = userService.getOne(userLambdaQueryWrapper);
if(getUser == null) {
return new Result<>(404, "用户不存在", null);
}
// 生成快递码
String expressCode = ExpressCodeUtil.getExpressCode(goodsDTO.getCompany());
// 生成取件码
String claimCode = ClaimCodeUtil.getClaimCode(goodsDTO.getCompany());
Goods goods = new Goods();
goods.setSendUserId(GetIdUtil.getId());
goods.setGetUserId(getUser.getId());
goods.setExpressCode(expressCode);
goods.setName(goodsDTO.getName());
goods.setQuality(goodsDTO.getQuality());
goodsService.save(goods);
// 存进历史记录
Records records = new Records();
records.setSendUserId(GetIdUtil.getId());
records.setGetUserId(getUser.getId());
LambdaQueryWrapper<Address> addressLambdaQueryWrapper = new LambdaQueryWrapper<>();
addressLambdaQueryWrapper.eq(Address::getAddress, goodsDTO.getAddress());
Address address = addressService.getOne(addressLambdaQueryWrapper);
records.setAddresseeId(address.getId());
records.setGoodsId(goods.getId());
recordsService.save(records);
Code code = new Code();
code.setClaim(claimCode);
code.setGoodsId(goods.getId());
codeService.save(code);
return new Result<>(200, "寄件成功", "");
} }
} }

@ -30,7 +30,7 @@ public class Goods implements Serializable {
@TableField("get_user_id") @TableField("get_user_id")
@JsonSerialize(using = ToStringSerializer.class) @JsonSerialize(using = ToStringSerializer.class)
@NotNull @NotNull
private String getUserId; private Long getUserId;
@TableField("express_code") @TableField("express_code")
@NotBlank(message = "快递码未生成, 请重新填写信息") @NotBlank(message = "快递码未生成, 请重新填写信息")

@ -1,5 +1,6 @@
package jty.expressdistributionsystem.entity; package jty.expressdistributionsystem.entity;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.annotation.TableName;
@ -37,6 +38,13 @@ public class Records implements Serializable {
@NotNull @NotNull
private Long goodsId; private Long goodsId;
@TableField("create_time") @TableField("mark")
private Integer mark;
@TableField("addressee_id")
@JsonSerialize(using = ToStringSerializer.class)
private Long addresseeId;
@TableField(value = "create_time", fill = FieldFill.INSERT)
private LocalDateTime createTime; private LocalDateTime createTime;
} }

@ -0,0 +1,7 @@
package jty.expressdistributionsystem.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import jty.expressdistributionsystem.entity.Code;
public interface CodeMapper extends BaseMapper<Code> {
}

@ -0,0 +1,7 @@
package jty.expressdistributionsystem.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import jty.expressdistributionsystem.entity.Records;
public interface RecordsMapper extends BaseMapper<Records> {
}

@ -0,0 +1,7 @@
package jty.expressdistributionsystem.service;
import com.baomidou.mybatisplus.extension.service.IService;
import jty.expressdistributionsystem.entity.Code;
public interface CodeService extends IService<Code> {
}

@ -0,0 +1,7 @@
package jty.expressdistributionsystem.service;
import com.baomidou.mybatisplus.extension.service.IService;
import jty.expressdistributionsystem.entity.Records;
public interface RecordsService extends IService<Records> {
}

@ -0,0 +1,11 @@
package jty.expressdistributionsystem.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jty.expressdistributionsystem.entity.Code;
import jty.expressdistributionsystem.mapper.CodeMapper;
import jty.expressdistributionsystem.service.CodeService;
import org.springframework.stereotype.Service;
@Service
public class CodeServiceImpl extends ServiceImpl<CodeMapper, Code> implements CodeService {
}

@ -0,0 +1,11 @@
package jty.expressdistributionsystem.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jty.expressdistributionsystem.entity.Records;
import jty.expressdistributionsystem.mapper.RecordsMapper;
import jty.expressdistributionsystem.service.RecordsService;
import org.springframework.stereotype.Service;
@Service
public class RecordsServiceImpl extends ServiceImpl<RecordsMapper, Records> implements RecordsService {
}

@ -0,0 +1,19 @@
package jty.expressdistributionsystem.utils;
import org.jetbrains.annotations.NotNull;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
public class ClaimCodeUtil {
public static @NotNull String getClaimCode(String company) {
// 获取当前时间
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String timeStamp = sdf.format(new Date());
// 生成唯一标识符
String uniqueId = UUID.randomUUID().toString().replace("-", "").substring(0, 6);
// 将公司名、时间戳和唯一ID组合成取件码
return company.toUpperCase() + timeStamp + uniqueId;
}
}

@ -0,0 +1,18 @@
package jty.expressdistributionsystem.utils;
import org.jetbrains.annotations.NotNull;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
public class ExpressCodeUtil {
public static @NotNull String getExpressCode(String company) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String timeStamp = sdf.format(new Date());
// 生成唯一标识符
String uniqueId = UUID.randomUUID().toString().replace("-", "").substring(0, 8);
// 将公司名、时间戳和唯一ID组合成快递码
return company.toUpperCase() + timeStamp + uniqueId;
}
}

@ -4,7 +4,7 @@ import jty.expressdistributionsystem.DTO.SendMessageDTO;
import jty.expressdistributionsystem.entity.Message; import jty.expressdistributionsystem.entity.Message;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
public class SendMessage { public class SendMessageUtil {
public static Message sendMessage(@NotNull SendMessageDTO sendMessageDTO) { public static Message sendMessage(@NotNull SendMessageDTO sendMessageDTO) {
Long sendId = GetIdUtil.getId(); Long sendId = GetIdUtil.getId();
Loading…
Cancel
Save