需求分析
后台系统
菜品管理(批量删除、起售停售)
套餐管理(修改、起售停售)
订单明细
员工管理
前台系统
个人中心(退出登录、最新订单查询、历史订单、地址管理-修改地址、地址管理-删除地址)
购物车(删除购物车中的商品)
技术架构
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-dSWFcRuN-1654599626144)(E:\CodeStudy\学习笔记\笔记图库\博客项目图库\image-20220425122837673.png)]
构建数据库
数据库表说明
后台开发
公共返回结果类
定义一个通用返回结果,服务端响应的数据最终都会分装成此对象,通过此结果类与前端进行交互。使用泛型,可以兼容不同的实体类。
@Data
public class R<T> {
private Integer code;
private String msg;
private T data;
private Map map = new HashMap();
public static <T> R<T> success(T object) {
R<T> r = new R<T>();
r.data = object;
r.code = 1;
return r;
}
public static <T> R<T> error(String msg) {
R r = new R();
r.msg = msg;
r.code = 0;
return r;
}
public R<T> add(String key, Object value) {
this.map.put(key, value);
return this;
}
}
登录退出功能
登录功能的逻辑判断
package com.yawn.reggie.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yawn.reggie.common.R;
import com.yawn.reggie.entity.Employee;
import com.yawn.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
@Autowired
private EmployeeService employeeService;
@PostMapping("/login")
public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
String password = employee.getPassword();
password = DigestUtils.md5DigestAsHex(password.getBytes());
LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(Employee::getUsername,employee.getUsername());
Employee emp = employeeService.getOne(queryWrapper);
if(emp == null){
return R.error("登陆失败");
}
if(!password.equals(emp.getPassword())){
return R.error("密码错误");
}
if(emp.getStatus()==0){
return R.error("账号已禁用");
}
request.getSession().setAttribute("employee",emp.getId());
return R.success(emp);
}
}
登录功能完善
使用过滤器或拦截器,判断用户是否已经登录。步骤如下:
1.创建自定义的 LoginCheckFilter
2.在启动类上加入 @ServletComponentScan注解
3.完善过滤器处理逻辑,逻辑如下:
package com.yawn.reggie.filter;
import com.alibaba.fastjson.JSON;
import com.yawn.reggie.common.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.AntPathMatcher;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebFilter(filterName = "LoginCheckFilter",urlPatterns = "/*")
@Slf4j
public class LoginCheckFilter implements Filter {
private static final AntPathMatcher PATH_MATCHER = new AntPathMatcher();
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest)servletRequest;
HttpServletResponse response = (HttpServletResponse)servletResponse;
String requestURI = request.getRequestURI();
log.info("拦截到请求:{}",requestURI);
String[] urls = new String[]{
"/employee/login",
"/employee/logout",
"/backend/**",
"/front/**"
};
boolean check = check(urls, requestURI);
if(check){
log.info("本次请求{}不需要处理",requestURI);
filterChain.doFilter(request,response);
return;
}
if(request.getSession().getAttribute("employee")!=null){
log.info("用户已登录,用户id为:{}",request.getSession().getAttribute("employee"));
filterChain.doFilter(request,response);
return;
}
log.info("用户未登录");
response.getWriter().write(JSON.toJSONString(R.error("NOTLOGIN")));
return;
}
public boolean check(String[] urls, String requestURI){
for (String url : urls) {
boolean match = PATH_MATCHER.match(url, requestURI);
if(match) return true;
}
return false;
}
}
后台退出功能
1.清理 Session 中的用户 id
2.返回结果
@PostMapping("/logout")
public R<String> logout(HttpServletRequest request){
request.getSession().removeAttribute("employee");
return R.success("退出成功");
}
员工管理
新增员工
@PostMapping
public R<String> save(HttpServletRequest request, @RequestBody Employee employee){
log.info("新增员工,员工信息:{}",employee.toString());
employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
employee.setCreateTime(LocalDateTime.now());
employee.setUpdateTime(LocalDateTime.now());
Long empId = (Long) request.getSession().getAttribute("employee");
employee.setCreateUser(empId);
employee.setUpdateUser(empId);
employeeService.save(employee);
return R.success("添加成功");
}
全局异常处理器
编写一个异常处理器,处理整个项目的异常情况,每个模块发生异常都可以进行捕捉。这样就不用重复得在每个模块中使用 try…catch 捕获异常,减轻代码量。基本原理是将异常处理功能通过 AOP 进行封装后,切入各个业务模块中。
@ControllerAdvice(annotations = {RestController.class, Controller.class})
@ResponseBody
@Slf4j
public class GlobalExceptionHandler {
@ExceptionHandler(SQLIntegrityConstraintViolationException.class)
public R<String> exceptionHandler(SQLIntegrityConstraintViolationException ex){
log.error(ex.getMessage());
if(ex.getMessage().contains("Duplicate entry")){
String[] split = ex.getMessage().split(" ");
String msg = split[2] + "已存在";
return R.error(msg);
}
return R.error("未知错误");
}
}
总结
这是典型的请求响应式流程。
员工信息分页查询
小心得:使用 Vue 这种框架开发,真的要做好交互一致,很多在前端代码中定义好的参数、变量名等,后端代码也和其保持一致,不可以自己随便定义,所以对于后端开发者来说,基本的前端阅读能力也是很重要的,最好能系统学习一下前端。
使用 MyBatisPlus 分页插件
@Configuration
public class MyBatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return interceptor;
}
}
编写 Controller
@GetMapping("/page")
public R<Page> page(int page, int pageSize, String name){
log.info("page = {},pageSize = {},name = {}" ,page,pageSize,name);
Page pageInfo = new Page(page,pageSize);
LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper();
queryWrapper.like(!StringUtils.isEmpty(name),Employee::getName,name);
queryWrapper.orderByDesc(Employee::getUpdateTime);
employeeService.page(pageInfo,queryWrapper);
return R.success(pageInfo);
}
启用/禁用员工账号
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-mgqMFAAO-1654599626145)(E:\CodeStudy\学习笔记\笔记图库\博客项目图库\image-20220501120322702.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HFOZT91v-1654599626145)(E:\CodeStudy\学习笔记\笔记图库\博客项目图库\image-20220501120322702.png)]
注意点:js 处理 long 型数据会出现精度丢失的情况(js 16位,SQL 19位),导致提交的 id 与数据库 id 不一致。解决办法是在服务端给页面响应 json 数据时,将 long 型数据统一转换为 String 字符串。
@Override
protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
log.info("扩展消息转换器...");
MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
messageConverter.setObjectMapper(new JacksonObjectMapper());
converters.add(0,messageConverter);
}
@PutMapping
public R<String> update(HttpServletRequest request, @RequestBody Employee employee){
log.info(employee.toString());
Long empId = (Long)request.getSession().getAttribute("employee");
employee.setUpdateTime(LocalDateTime.now());
employee.setUpdateUser(empId);
employeeService.updateById(employee);
return R.success("员工信息修改成功");
}
编辑员工信息
简单来说就是,修改和新增是同一个页面,点击“编辑”按钮,这时会先判断是否存在 id ,有 id 是进入编辑页面,无 id 是保存页面。然后前端页面会发送一个带有用户 id 的请求,并且要求服务端进行信息查询,服务端接收请求后根据用户 id 查询数据后,以 json 格式返回用户数据,前端页面拿到后进行数据回显,然后用户进行修改,点击“保存”按钮,由于进入的是编辑页面,所以保存按钮请求的是服务端的更新操作。
@GetMapping("/{id}")
public R<Employee> getById(@PathVariable Long id){
log.info("根据id查询员工信息...");
Employee employee = employeeService.getById(id);
if(employee != null){
return R.success(employee);
}
return R.error("没有查询到对应员工信息");
}
分类管理
公共字段自动填充
获取当前用户 id 的解决方案
由于在 MyMetaObjecthandler 类中是不能获得 HttpSession 对象的,因此就无法通过 session 获得用户 id,可以用 ThreadLocal 解决这个问题,它是 JDK 中提供的一个类。
1.编写工具类
public class BaseContext {
private static ThreadLocal<Long> threadLocal = new ThreadLocal<>();
public static void setCurrentId(Long id){
threadLocal.set(id);
}
public static Long getCurrentId(){
return threadLocal.get();
}
}
2.在 LoginCheckFilter 过滤器中获取 id
Long empId = (Long) request.getSession().getAttribute("employee");
BaseContext.setCurrentId(empId);
3.编写自定义元数据对象处理器
@Component
@Slf4j
public class MyMetaObjecthandler implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
log.info("公共字段自动填充[insert]...");
log.info(metaObject.toString());
metaObject.setValue("createTime", LocalDateTime.now());
metaObject.setValue("updateTime",LocalDateTime.now());
metaObject.setValue("createUser", BaseContext.getCurrentId());
metaObject.setValue("updateUser",BaseContext.getCurrentId());
}
@Override
public void updateFill(MetaObject metaObject) {
log.info("公共字段自动填充[update]...");
log.info(metaObject.toString());
long id = Thread.currentThread().getId();
log.info("线程id为:{}",id);
metaObject.setValue("updateTime",LocalDateTime.now());
metaObject.setValue("updateUser",BaseContext.getCurrentId());
}
}
新增分类
首先搭建框架,步骤与员工管理相同,省略…
然后编写控制器:
@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {
@Autowired
private CategoryService categoryService;
@PostMapping
public R<String> save(@RequestBody Category category){
log.info("category:{}",category);
categoryService.save(category);
return R.success("新增分类成功");
}
}
分类信息分页查询
逻辑与员工信息分页查询一样,只是查询的表发生了变化。
@GetMapping("/page")
public R<Page> page(int page, int pageSize){
Page<Category> pageInfo = new Page<>(page,pageSize);
LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.orderByAsc(Category::getSort);
categoryService.page(pageInfo,queryWrapper);
return R.success(pageInfo);
}
删除分类
删除分类时,需要先判断该类别中是否有菜品,如果有不能删除。通过在业务代码中实现自己编写的类完成这个功能。
1.自定义删除菜品分类方法,先在菜品业务接口中定义方法,再在实现类中实现重写方法。
public interface CategoryService extends IService<Category> {
public void remove(Long id);
}
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
@Autowired
private DishService dishService;
@Autowired
private SetmealService setmealService;
@Override
public void remove(Long id) {
LambdaQueryWrapper<Dish> queryWrapper1 = new LambdaQueryWrapper<>();
queryWrapper1.eq(Dish::getCategoryId,id);
int count1 = dishService.count(queryWrapper1);
if(count1>0){
throw new CustomException("当前分类下关联了菜品,不能删除");
}
LambdaQueryWrapper<Setmeal> queryWrapper2 = new LambdaQueryWrapper<>();
queryWrapper2.eq(Setmeal::getCategoryId,id);
int count2 = setmealService.count(queryWrapper2);
if(count2>0){
throw new CustomException("当前分类下关联了套餐,不能删除");
}
super.removeById(id);
}
}
2.自定义业务异常类,放在 common 包下。
public class CustomException extends RuntimeException {
public CustomException(String message){
super(message);
}
}
3.在全局异常处理器中添加该异常。
@ExceptionHandler(CustomException.class)
public R<String> exceptionHandler(CustomException ex){
log.error(ex.getMessage());
return R.error(ex.getMessage());
}
4.编写控制器。
@DeleteMapping
public R<String> delete(Long id){
log.info("删除分类,id为:{}",id);
categoryService.remove(id);
return R.success("分类信息删除成功");
}
修改分类
@PutMapping
public R<String> update(@RequestBody Category category){
log.info("修改分类信息:{}",category);
categoryService.updateById(category);
return R.success("修改分类信息成功");
}
菜品业务管理
文件上传下载
Spring 框架在 spring-web 包中对文件上传进行了封装,大大简化了服务端代码,我们只需要在 Controller 的方法中声明一个 MultipartFile 类型的参数即可接收上传的文件。
1.在 application.yml 配置文件中配置默认路径
reggie:
path: E:\CodeStudy\Others\外卖项目\1 瑞吉外卖项目\代码\day04\
2.编写控制层代码
@RestController
@RequestMapping("/common")
@Slf4j
public class CommonController {
@Value("${reggie.path}")
private String basePath;
@PostMapping("/upload")
public R<String> upload(MultipartFile file){
log.info(file.toString());
String originalFilename = file.getOriginalFilename();
String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
String fileName = UUID.randomUUID().toString() + suffix;
File dir = new File(basePath);
if(!dir.exists()){
dir.mkdirs();
}
try {
file.transferTo(new File(basePath + fileName));
} catch (IOException e) {
e.printStackTrace();
}
return R.success(fileName);
}
@GetMapping("/download")
public void download(String name, HttpServletResponse response){
try {
FileInputStream fileInputStream = new FileInputStream(new File(basePath + name));
ServletOutputStream outputStream = response.getOutputStream();
response.setContentType("image/jpeg");
int len = 0;
byte[] bytes = new byte[1024];
while ((len = fileInputStream.read(bytes)) != -1){
outputStream.write(bytes,0,len);
outputStream.flush();
}
outputStream.close();
fileInputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
新增菜品
1.添加菜品时需要查询菜品分类信息,因为需要编写查询菜品分类方法,在 CategoryController 中添加方法。
@GetMapping("/list")
public R<List<Category>> list(Category category){
LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper();
queryWrapper.eq(category.getType() != null,Category::getType,category.getType());
queryWrapper.orderByAsc(Category::getSort).orderByAsc(Category::getUpdateTime);
List<Category> list = categoryService.list(queryWrapper);
return R.success(list);
}
2.编写菜品DTO
分析:
- Dish 表中没有专门的口味属性(DishFlavors),如果保存方法传入的参数直接为 Dish,是无法保存的
- 采用和做个人博客项目时类似的方法,个人博客是在显示博客详情页的时候重新封装了一个类,类里面有博客类属性、分类属性等;而这里是在新的类中放入菜品基本信息和口味信息,用于保存和显示。
@Data
public class DishDto extends Dish {
private List<DishFlavor> flavors = new ArrayList<>();
private String categoryName;
private Integer copies;
}
小心得:
- 善于使用 Debug 和日志排查问题,Debug 和日志可以看到具体的流程和数据生成过程
- @RequestBody 注解是为了保证控制器接受到前端页面的请求数据为 Json 格式
3.在菜品业务服务层编写实现方法(先写接口方法,再写实现类)
注意,用到了事务需要在启动类开启事务支持,即在启动类添加 @EnableTransactionManagement 注解
@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
@Autowired
private DishFlavorService dishFlavorService;
@Override
@Transactional
public void saveWithFlavor(DishDto dishDto) {
this.save(dishDto);
Long dishId = dishDto.getId();
List<DishFlavor> dishFlavorList = dishDto.getFlavors();
dishFlavorList.stream().map((item) -> {
item.setDishId(dishId);
return item;
}).collect(Collectors.toList());
dishFlavorService.saveBatch(dishFlavorList);
}
}
4.控制层代码编写(选择哪种请求方式是根据前端代码决定的)
@PostMapping
public R<String> save(@RequestBody DishDto dishDto){
log.info(dishDto.toString());
dishService.saveWithFlavor(dishDto);
return R.success("保存成功");
}
菜品信息分页查询
分析:菜品分类列显示的是分类名称,但是数据其实是代码,无法直接显示,因此需要转换,方法就是使用 dishDto 类。过程稍微有些复杂,但是简单来说就是创建一个 dishDto 类型的 Page;然后将 dish 类型的 Page 除了 records 的属性赋值给 dishDto 类型的 Page,因为两个类型的 Page 中 records 代表的是两个类型的记录,类型不一样,不能直接进行赋值;再对 records 单独处理,将菜品名称取出赋给 dishDto 中的 categoryName 属性;最后返回的是dishDto 类型的 Page。
@GetMapping("/page")
public R<Page> page(int page, int pageSize, String name){
Page<Dish> pageInfo = new Page<>(page,pageSize);
Page<DishDto> dishDtoPage = new Page<>();
LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.like(name != null,Dish::getName,name);
queryWrapper.orderByAsc(Dish::getUpdateTime);
dishService.page(pageInfo,queryWrapper);
BeanUtils.copyProperties(pageInfo,dishDtoPage,"records");
List<Dish> records = pageInfo.getRecords();
List<DishDto> list = records.stream().map((item)->{
DishDto dishDto = new DishDto();
BeanUtils.copyProperties(item,dishDto);
Long categoryId = item.getCategoryId();
Category category = categoryService.getById(categoryId);
if(category != null){
dishDto.setCategoryName(category.getName());
}
return dishDto;
}).collect(Collectors.toList());
dishDtoPage.setRecords(list);
return R.success(dishDtoPage);
}
修改菜品
分析:与新增菜品共用一个页面,但需要先进行数据回显,因此这里稍微复杂一些的是设计回显的方法和数据表结构。
1.在 DishService 接口中新增一个 getByIdWithFlavor 方法,然后在实现类中设计具体实现方法。
接口:public DishDto getByIdWithFlavor(Long id);
实现类:
@Override
public DishDto getByIdWithFlavor(Long id) {
Dish dish = this.getById(id);
DishDto dishDto = new DishDto();
BeanUtils.copyProperties(dish,dishDto);
LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(DishFlavor::getDishId,dish.getId());
List<DishFlavor> flavors = dishFlavorService.list(queryWrapper);
dishDto.setFlavors(flavors);
return dishDto;
}
2.在 DishService 接口中新增一个 updateWithFlavor方法,然后在实现类中设计具体实现方法。
接口:public void updateWithFlavor(DishDto dishDto);
方法:
@Override
@Transactional
public void updateWithFlavor(DishDto dishDto) {
this.updateById(dishDto);
LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper();
queryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
dishFlavorService.remove(queryWrapper);
List<DishFlavor> flavors = dishDto.getFlavors();
flavors = flavors.stream().map((item) -> {
item.setDishId(dishDto.getId());
return item;
}).collect(Collectors.toList());
dishFlavorService.saveBatch(flavors);
}
3.控制器
@GetMapping("/{id}")
public R<DishDto> get(@PathVariable Long id){
DishDto dishDto = dishService.getByIdWithFlavor(id);
return R.success(dishDto);
}
@PutMapping
public R<String> update(@RequestBody DishDto dishDto){
log.info(dishDto.toString());
dishService.updateWithFlavor(dishDto);
return R.success("新增菜品成功");
}
套餐业务管理
新增套餐
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-rOjRU3da-1654599626146)(E:\CodeStudy\学习笔记\笔记图库\博客项目图库\image-20220504132400496.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ZyPtNr7G-1654599626146)(E:\CodeStudy\学习笔记\笔记图库\博客项目图库\image-20220504132400496.png)]
1.框架搭建,包括创建实体类,Dto类,Mapper,Service,Controller
2.业务代码和新增分类相似,编写业务代码和控制层代码
①在 DishController 中添加响应查询菜品信息的方法
@GetMapping("/list")
public R<List<Dish>> list(Dish dish){
log.info("dish:{}", dish);
LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(null != dish.getCategoryId(),Dish::getCategoryId,dish.getCategoryId());
queryWrapper.eq(Dish::getStatus, 1);
queryWrapper.orderByDesc(Dish::getUpdateTime);
List<Dish> list = dishService.list(queryWrapper);
return R.success(list);
}
②在 SetmealServiceImpl 中添加如下方法
@Service
@Slf4j
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
@Autowired
private SetmealDishService setmealDishService;
@Override
@Transactional
public void saveWithDish(SetmealDto setmealDto) {
this.save(setmealDto);
List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
setmealDishes.stream().map((item)->{
item.setSetmealId(setmealDto.getId());
return item;
}).collect(Collectors.toList());
setmealDishService.saveBatch(setmealDishes);
}
}
③在 SetmealController 中添加如下方法
@PostMapping
public R<String> save(@RequestBody SetmealDto setmealDto){
setmealService.saveWithDish(setmealDto);
return R.success("新增套餐成功");
}
套餐分页查询
@GetMapping("/page")
public R<Page> page(int page, int pageSize, String name){
Page<Setmeal> pageInfo = new Page<>(page,pageSize);
Page<SetmealDto> setmealDtoPage = new Page<>();
LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.like(name != null,Setmeal::getName,name);
queryWrapper.orderByDesc(Setmeal::getUpdateTime);
setmealService.page(pageInfo,queryWrapper);
BeanUtils.copyProperties(pageInfo,setmealDtoPage,"records");
List<Setmeal> records = pageInfo.getRecords();
List<SetmealDto> list = records.stream().map((item)->{
SetmealDto setmealDto = new SetmealDto();
BeanUtils.copyProperties(item,setmealDto);
Long categoryId = item.getCategoryId();
Category category = categoryService.getById(categoryId);
if(category != null){
String categoryName = category.getName();
setmealDto.setCategoryName(categoryName);
}
return setmealDto;
}).collect(Collectors.toList());
setmealDtoPage.setRecords(list);
return R.success(setmealDtoPage);
}
删除套餐
需要注意套餐与菜品之间存在关联关系,因为删除套餐的同时要需要把响应的菜品信息删除,当然是setmeal_dish表中的菜品
@Override
@Transactional
public void removeWithDish(List<Long> ids) {
LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.in(Setmeal::getId,ids);
queryWrapper.eq(Setmeal::getStatus,1);
int count = this.count(queryWrapper);
if(count > 0){
throw new CustomException("套餐正在售卖中,不能删除");
}
this.removeByIds(ids);
LambdaQueryWrapper<SetmealDish> queryWrapper2 = new LambdaQueryWrapper<>();
queryWrapper2.in(SetmealDish::getSetmealId,ids);
setmealDishService.remove(queryWrapper2);
}
@DeleteMapping
public R<String> delete(@RequestParam List<Long> ids){
setmealService.removeWithDish(ids);
return R.success("套餐数据删除成功");
}
修改套餐
和分类修改类似,暂不写。
停起售套餐
分析:传入的是 id 号,需要根据 id 号 更新整条记录。
@Override
@Transactional
public void changeStatus(List<Long> ids) {
for (Long id:ids) {
Setmeal setmeal = new Setmeal();
Setmeal setmeal1 = this.getById(id);
BeanUtils.copyProperties(setmeal1,setmeal,"status");
Integer status = setmeal1.getStatus();
if(status == 0){
setmeal.setStatus(1);
}else {
setmeal.setStatus(0);
}
LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(Setmeal::getId,id);
this.update(setmeal,queryWrapper);
}
}
@PostMapping("/status/{params.status}")
public R<String> change(@RequestParam List<Long> ids){
setmealService.changeStatus(ids);
return R.success("状态修改成功");
}
心得:
- 一定要结合前端代码编写方法,同时利用页面的响应工具抓请求状态,搞清楚自己这次请求响应传输的到底是什么数据
- 很多功能写不出来可能是对封装的方法不熟悉,因为要善于运用别人分享的资源,多查
手机验证码登录
短信发送
使用阿里云的短信服务,具体申请步骤不难,但是审核麻烦,这里没有申请,了解如何调用 API 即可。
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.5.16</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-dysmsapi</artifactId>
<version>2.1.0</version>
</dependency>
public class SMSUtils {
public static void sendMessage(String signName, String templateCode,String phoneNumbers,String param){
DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "", "");
IAcsClient client = new DefaultAcsClient(profile);
SendSmsRequest request = new SendSmsRequest();
request.setSysRegionId("cn-hangzhou");
request.setPhoneNumbers(phoneNumbers);
request.setSignName(signName);
request.setTemplateCode(templateCode);
request.setTemplateParam("{\"code\":\""+param+"\"}");
try {
SendSmsResponse response = client.getAcsResponse(request);
System.out.println("短信发送成功");
}catch (ClientException e) {
e.printStackTrace();
}
}
}
验证码登录
1.搭建框架,包括编写用户实体类,Mapper 层、业务层、控制层,代码和前面类似,这里省略
2.完善登录过滤器,过滤未登录的用户,放行短信发送请求和登录请求;同时判断用户是否登录,登录则放行
if(request.getSession().getAttribute("user")!=null){
log.info("用户已登录,用户id为:{}",request.getSession().getAttribute("user"));
Long userId = (Long) request.getSession().getAttribute("user");
BaseContext.setCurrentId(userId);
filterChain.doFilter(request,response);
return;
}
3.客户端页面使用H5开发,调试页面时,将页面设置为移动端模式
4.编写控制器
分析:保存登录信息时,直接传递用户类是不行的,因为用户类中没有验证码信息。解决方法有两种,一个是像之前一样编写一个用户Dto类,另一个是使用 Map 保存,这样更方便一些。
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {
@Autowired
private UserService userService;
@PostMapping("/sendMsg")
public R<String> sendMsg(@RequestBody User user, HttpSession session) {
String phone = user.getPhone();
if(phone.length()>0){
String code = ValidateCodeUtils.generateValidateCode(4).toString();
log.info("code={}",code);
session.setAttribute(phone,code);
R.success("手机验证码短信发送成功");
}
return R.error("短信发送失败");
}
@PostMapping("/login")
public R<User> login(@RequestBody Map map, HttpSession session){
log.info(map.toString());
String phone = map.get("phone").toString();
String code = map.get("code").toString();
Object code1 = session.getAttribute(phone);
if(code1 != null && code1.equals(code)){
LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(User::getPhone,phone);
User user = userService.getOne(queryWrapper);
if(user == null){
user = new User();
user.setPhone(phone);
user.setStatus(1);
userService.save(user);
}
session.setAttribute("user",user.getId());
return R.success(user);
}
return R.error("登录失败");
}
}
前端页面: getCode() 方法中获取code 的方法换成 sendMsgApi({phone:this.form.phone}),另外在 api 包下的 login.js 文件下添加 sendMsgApi 方法。
5.bug记录
- 总是出现 sendMsg 未定义,原因是没有清理浏览器缓存…,前端项目清理缓存有时候很重要!
- 可以通过注释掉front下的login.html的66行,也就是生成随机验证码的那一行,然后加上两行,
const res = sendMsgApi({phone:this.form.phone}) sessionStorage.setItem(“code”,res) 然后在btnlogin()这个函数中,const res = await loginApi({phone:this.form.phone,code:this.form.code})
导入用户地址簿
1.首先搭建框架,省略
2.控制层代码编写
@RestController
@RequestMapping("/addressBook")
@Slf4j
public class AddressBookController {
@Autowired
private AddressBookService addressBookService;
@PostMapping
public R<AddressBook> save(@RequestBody AddressBook addressBook) {
addressBook.setUserId(BaseContext.getCurrentId());
addressBookService.save(addressBook);
log.info("addressBook:{}", addressBook);
return R.success(addressBook);
}
@PutMapping("default")
public R<AddressBook> setDefault(@RequestBody AddressBook addressBook){
log.info("addressBook:{}", addressBook);
Long currentId = BaseContext.getCurrentId();
LambdaUpdateWrapper<AddressBook> queryWrapper = new LambdaUpdateWrapper<>();
queryWrapper.eq(AddressBook::getUserId,currentId);
queryWrapper.set(AddressBook::getIsDefault,0);
addressBookService.update(queryWrapper);
addressBook.setIsDefault(1);
addressBookService.updateById(addressBook);
return R.success(addressBook);
}
@GetMapping("/{id}")
public R get(@PathVariable Long id) {
AddressBook addressBook = addressBookService.getById(id);
if (addressBook != null) {
return R.success(addressBook);
} else {
return R.error("没有找到该对象");
}
}
@GetMapping("default")
public R<AddressBook> getDefault() {
LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
queryWrapper.eq(AddressBook::getIsDefault, 1);
AddressBook addressBook = addressBookService.getOne(queryWrapper);
if (null == addressBook) {
return R.error("没有找到该对象");
} else {
return R.success(addressBook);
}
}
@GetMapping("/list")
public R<List<AddressBook>> list(AddressBook addressBook) {
addressBook.setUserId(BaseContext.getCurrentId());
log.info("addressBook:{}", addressBook);
LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(null != addressBook.getUserId(), AddressBook::getUserId, addressBook.getUserId());
queryWrapper.orderByDesc(AddressBook::getUpdateTime);
return R.success(addressBookService.list(queryWrapper));
}
@PutMapping
private R<AddressBook> update(@RequestBody AddressBook addressBook){
log.info("addressBook:{}", addressBook);
Long currentId = BaseContext.getCurrentId();
LambdaUpdateWrapper<AddressBook> queryWrapper = new LambdaUpdateWrapper<>();
queryWrapper.eq(AddressBook::getUserId,currentId);
queryWrapper.eq(AddressBook::getId,addressBook.getId());
addressBookService.updateById(addressBook);
return R.success(addressBook);
}
}
菜品展示
1.修改菜品展示表,在 DishController 中修改 list 方法。
分析:后台菜品展示列表返回类型为 Dish,其中不包含口味信息,这样用户在前台添加菜品进入购物车时就无法添加口味信息,因此需要进行修改。
@GetMapping("/list")
public R<List<DishDto>> list(Dish dish){
LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(dish.getCategoryId() != null ,Dish::getCategoryId,dish.getCategoryId());
queryWrapper.eq(Dish::getStatus,1);
queryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
List<Dish> list = dishService.list(queryWrapper);
List<DishDto> dishDtoList = list.stream().map((item) -> {
DishDto dishDto = new DishDto();
BeanUtils.copyProperties(item,dishDto);
Long categoryId = item.getCategoryId();
Category category = categoryService.getById(categoryId);
if(category != null){
String categoryName = category.getName();
dishDto.setCategoryName(categoryName);
}
Long dishId = item.getId();
LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
lambdaQueryWrapper.eq(DishFlavor::getDishId,dishId);
List<DishFlavor> dishFlavorList = dishFlavorService.list(lambdaQueryWrapper);
dishDto.setFlavors(dishFlavorList);
return dishDto;
}).collect(Collectors.toList());
return R.success(dishDtoList);
}
2.展示套餐信息,在 SetmealController 中添加 list 方法,用于信息套餐具体的菜品信息。
@GetMapping("/list")
public R<List<Setmeal>> list(Setmeal setmeal){
LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
queryWrapper.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());
queryWrapper.orderByDesc(Setmeal::getUpdateTime);
List<Setmeal> list = setmealService.list(queryWrapper);
return R.success(list);
}
购物车
@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {
@Autowired
private ShoppingCartService shoppingCartService;
@PostMapping("/add")
public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
log.info("购物车数据:{}",shoppingCart);
shoppingCart.setUserId(BaseContext.getCurrentId());
LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
Long dishId = shoppingCart.getDishId();
if(dishId != null){
queryWrapper.eq(ShoppingCart::getDishId,dishId);
}else {
queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
}
ShoppingCart shoppingCart1 = shoppingCartService.getOne(queryWrapper);
if(shoppingCart1 != null){
Integer number = shoppingCart1.getNumber();
shoppingCart1.setNumber(number+1);
shoppingCartService.updateById(shoppingCart1);
}else {
shoppingCart.setNumber(1);
shoppingCart.setCreateTime(LocalDateTime.now());
shoppingCart1 = shoppingCart;
shoppingCartService.save(shoppingCart1);
}
return R.success(shoppingCart1);
}
@GetMapping("/list")
public R<List<ShoppingCart>> list(){
log.info("查看购物车...");
LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
queryWrapper.orderByAsc(ShoppingCart::getCreateTime);
List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
return R.success(list);
}
@DeleteMapping("/clean")
public R<String> clean(){
LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
shoppingCartService.remove(queryWrapper);
return R.success("清空购物车成功");
}
}
下单
注:具体的支付功能并没有开发,因为需要申请资质,这里只是将订单信息保存即可。
1.业务层实现
分析:这里主要是业务层代码比较复杂,需要考虑下单时的具体的实体类信息的设置,前端传递的信息不完整,因此需要手动设置订单表属性和订单明细表属性,两个表存放的数据属性不一样。而这些属性内容主要来自于用户表,购物车表还有地址表。一些细节的处理也需要学习,比如金额总数相关操作使用原子整型类,生成订单号的工具类,对购物车数据遍历求总金额的同时设置订单详细数据等。
@Service
@Slf4j
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Orders> implements OrderService {
@Autowired
private ShoppingCartService shoppingCartService;
@Autowired
private UserService userService;
@Autowired
private AddressBookService addressBookService;
@Autowired
private OrderDetailService orderDetailService;
@Override
@Transactional
public void submit(Orders orders) {
Long currentId = BaseContext.getCurrentId();
LambdaQueryWrapper<ShoppingCart> queryWrapper =new LambdaQueryWrapper<>();
queryWrapper.eq(ShoppingCart::getUserId,currentId);
List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
if(list == null || list.size() == 0){
throw new CustomException("购物车为空,不能下单");
}
User user = userService.getById(currentId);
Long addressBookId = orders.getAddressBookId();
AddressBook addressBook = addressBookService.getById(addressBookId);
if(addressBook == null){
throw new CustomException("用户地址信息有误,不能下单");
}
long orderId = IdWorker.getId();
AtomicInteger amount = new AtomicInteger(0);
List<OrderDetail> orderDetails = list.stream().map((item) -> {
OrderDetail orderDetail = new OrderDetail();
orderDetail.setOrderId(orderId);
orderDetail.setNumber(item.getNumber());
orderDetail.setDishFlavor(item.getDishFlavor());
orderDetail.setDishId(item.getDishId());
orderDetail.setSetmealId(item.getSetmealId());
orderDetail.setName(item.getName());
orderDetail.setImage(item.getImage());
orderDetail.setAmount(item.getAmount());
amount.addAndGet(item.getAmount().multiply(new BigDecimal(item.getNumber())).intValue());
return orderDetail;
}).collect(Collectors.toList());
orders.setId(orderId);
orders.setOrderTime(LocalDateTime.now());
orders.setCheckoutTime(LocalDateTime.now());
orders.setStatus(2);
orders.setAmount(new BigDecimal(amount.get()));
orders.setUserId(currentId);
orders.setNumber(String.valueOf(orderId));
orders.setUserName(user.getName());
orders.setConsignee(addressBook.getConsignee());
orders.setPhone(addressBook.getPhone());
orders.setAddress((addressBook.getProvinceName() == null ? "" : addressBook.getProvinceName())
+ (addressBook.getCityName() == null ? "" : addressBook.getCityName())
+ (addressBook.getDistrictName() == null ? "" : addressBook.getDistrictName())
+ (addressBook.getDetail() == null ? "" : addressBook.getDetail()));
this.save(orders);
orderDetailService.saveBatch(orderDetails);
shoppingCartService.remove(queryWrapper);
}
}
rderId(orderId); orderDetail.setNumber(item.getNumber()); orderDetail.setDishFlavor(item.getDishFlavor()); orderDetail.setDishId(item.getDishId()); orderDetail.setSetmealId(item.getSetmealId()); orderDetail.setName(item.getName()); orderDetail.setImage(item.getImage()); orderDetail.setAmount(item.getAmount()); amount.addAndGet(item.getAmount().multiply(new BigDecimal(item.getNumber())).intValue()); return orderDetail; }).collect(Collectors.toList());
//设置订单详细数据
orders.setId(orderId);
orders.setOrderTime(LocalDateTime.now());
orders.setCheckoutTime(LocalDateTime.now());
orders.setStatus(2);
orders.setAmount(new BigDecimal(amount.get()));//总金额
orders.setUserId(currentId);
orders.setNumber(String.valueOf(orderId));
orders.setUserName(user.getName());
orders.setConsignee(addressBook.getConsignee());
orders.setPhone(addressBook.getPhone());
orders.setAddress((addressBook.getProvinceName() == null ? "" : addressBook.getProvinceName())
+ (addressBook.getCityName() == null ? "" : addressBook.getCityName())
+ (addressBook.getDistrictName() == null ? "" : addressBook.getDistrictName())
+ (addressBook.getDetail() == null ? "" : addressBook.getDetail()));
//向订单表插入一条订单数据
this.save(orders);
//向订单明细表插入购物车中的多条具体菜品数据
orderDetailService.saveBatch(orderDetails);
//清空购物车
shoppingCartService.remove(queryWrapper);
}
}
2.控制层,直接调用方法即可,内容略。
|