用户登录移动端,需要消费者填写并维护自己的地址信息,此功能需要实现一个用户可以有多个地址但是只能存在一个默认的地址。
数据模型
涉及的一张地址表:address_book
对应的AddressBook.Java实体
- package com.itheima.reggie.entity;
-
- import com.baomidou.mybatisplus.annotation.FieldFill;
- import com.baomidou.mybatisplus.annotation.TableField;
- import lombok.Data;
- import java.io.Serializable;
- import java.time.LocalDateTime;
-
- /**
- * 地址簿
- */
- @Data
- public class AddressBook implements Serializable {
-
- private static final long serialVersionUID = 1L;
-
- private Long id;
-
- //用户id
- private Long userId;
-
- //收货人
- private String consignee;
-
- //手机号
- private String phone;
-
- //性别 0 女 1 男
- private String sex;
-
- //省级区划编号
- private String provinceCode;
-
- //省级名称
- private String provinceName;
-
- //市级区划编号
- private String cityCode;
-
- //市级名称
- private String cityName;
-
- //区级区划编号
- private String districtCode;
-
- //区级名称
- private String districtName;
-
- //详细地址
- private String detail;
-
- //标签
- private String label;
-
- //是否默认 0 否 1是
- private Integer isDefault;
-
- //创建时间
- @TableField(fill = FieldFill.INSERT)
- private LocalDateTime createTime;
-
- //更新时间
- @TableField(fill = FieldFill.INSERT_UPDATE)
- private LocalDateTime updateTime;
-
- //创建人
- @TableField(fill = FieldFill.INSERT)
- private Long createUser;
-
- //修改人
- @TableField(fill = FieldFill.INSERT_UPDATE)
- private Long updateUser;
-
- //是否删除
- private Integer isDeleted;
- }
点击添加收获地址,可以添加用户的收货地址:
API
请求网址: | http://localhost:8080/addressBook/ |
请求方法 | GET |
在AddressBookController.java中添加新增地址的代码:
- package com.itheima.reggie.controller;
-
- /**
- * @author jektong
- * @date 2022年05月25日 20:46
- */
-
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
- import com.itheima.reggie.common.BaseContext;
- import com.itheima.reggie.common.R;
- import com.itheima.reggie.entity.AddressBook;
- import com.itheima.reggie.service.AddressBookService;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.web.bind.annotation.*;
-
- import javax.annotation.Resource;
- import java.util.List;
-
- /**
- * 地址簿管理
- */
- @Slf4j
- @RestController
- @RequestMapping("/addressBook")
- public class AddressBookController {
-
- @Resource
- private AddressBookService addressBookService;
-
- /**
- * 新增用户地址
- */
- @PostMapping
- public R<AddressBook> save(@RequestBody AddressBook addressBook) {
- // 通过上下文获取用户ID
- addressBook.setUserId(BaseContext.getCurrentId());
- log.info("addressBook:{}", addressBook);
- addressBookService.save(addressBook);
- return R.success(addressBook);
- }
-
- }
用户设置默认地址,并查出用户所设置的默认地址以及用户自己添加的所有地址:
在AddressBookController.java中添加:
-
- /**
- * 设置默认地址
- */
- @PutMapping("/default")
- public R<AddressBook> setDefault(@RequestBody AddressBook addressBook) {
- log.info("addressBook:{}", addressBook);
- // 构建更新条件
- LambdaUpdateWrapper<AddressBook> wrapper = new LambdaUpdateWrapper<>();
- wrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
- wrapper.set(AddressBook::getIsDefault, 0);
- //SQL:update address_book set is_default = 0 where user_id = ?
- addressBookService.update(wrapper);
- // 设置默认地址为1
- addressBook.setIsDefault(1);
- //SQL:update address_book set is_default = 1 where id = ?
- addressBookService.updateById(addressBook);
- return R.success(addressBook);
- }
-
- /**
- * 根据id查询地址
- */
- @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);
-
- //SQL:select * from address_book where user_id = ? and is_default = 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);
-
- //SQL:select * from address_book where user_id = ? order by update_time desc
- return R.success(addressBookService.list(queryWrapper));
- }
移动端菜品展示页的左端展示菜品的分类,右侧展示分类的菜品信息,若菜品设置了口味信息,需要展示选择规格的按钮。
菜品展示交互流程:
当首页记载完毕后,会发送一个请求去加载购物车的数据,这个代码并未开发,放在一旁,我们可以使用加载静态JSON文件的方式获取数据:
- //获取购物车内商品的集合
- function cartListApi(data) {
- return $axios({
- //'url': '/shoppingCart/list',
- 'url': '/front/cartData.json',
- 'method': 'get',
- params:{...data}
- })
- }
这部分的代码之前已经实现,做以上更改之后,页面会展示动态数据(未展示的使用CTRL+F5)清楚浏览器页面缓存。
同时希望图中的加号按钮变为:
其实就是查询出菜品所对应的菜系口味数据:
DishController.java添加如下代码:
- @GetMapping("/list")
- public R<List<DishDto>> list(Dish dish){
- // 构造条件
- LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper();
- queryWrapper.eq(Dish::getStatus, CommonsConst.DISH_OPEN);
- queryWrapper.eq(dish.getCategoryId()!=null, Dish::getCategoryId, dish.getCategoryId());
- // 添加排序条件
- queryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
- List<Dish> dishList = dishService.list(queryWrapper);
-
- List<DishDto> dishDtoList = dishList.stream().map((item)->{
- DishDto dishDto = new DishDto();
- BeanUtils.copyProperties(item,dishDto);
- // 获取categoryId
- Long categoryId = item.getCategoryId();
- // 给categoryName赋值
- Category category = categoryService.getById(categoryId);
- if(category!=null){
- dishDto.setCategoryName(category.getName());
- }
- // 当前菜品ID
- Long dishId = item.getId();
- LambdaQueryWrapper<DishFlavor> dishFlavorQueryWrapper= new LambdaQueryWrapper<>();
- dishFlavorQueryWrapper.eq(DishFlavor::getDishId,dishId);
- List<DishFlavor> dishFlavorList = flavorService.list(dishFlavorQueryWrapper);
- dishDto.setFlavors(dishFlavorList);
- return dishDto;
- }).collect(Collectors.toList());
- return R.success(dishDtoList);
- }
上面还有一个小Bug就是套餐分类的代码还未开发展示出菜品套餐,在SetmealController.java中添加:
- /**
- * 根据条件查询套餐数据
- * @param setmeal
- * @return
- */
- @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);
- }
测试完成: