在 foodie-dev-pojo 模块的 com.imooc.pojo.bo 包内创建 AddressBO 类
package com.imooc.pojo.bo;
/**
* @author 92578
* @since 1.0
*/
public class AddressBO {
private String addressId;
private String userId;
private String receiver;
private String mobile;
private String province;
private String city;
private String district;
private String detail;
public String getAddressId() {
return addressId;
}
public void setAddressId(String addressId) {
this.addressId = addressId;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getReceiver() {
return receiver;
}
public void setReceiver(String receiver) {
this.receiver = receiver;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getDistrict() {
return district;
}
public void setDistrict(String district) {
this.district = district;
}
public String getDetail() {
return detail;
}
public void setDetail(String detail) {
this.detail = detail;
}
}
在 foodie-dev-service 模块内完善 AddressService 接口,新增 addNewUserAddress 方法
package com.imooc.service;
import com.imooc.pojo.UserAddress;
import com.imooc.pojo.bo.AddressBO;
import java.util.List;
/**
* Created by 92578 on 2020/8/22 16:17
**/
public interface AddressService {
/**
* 根据用户 id 查询用户的收获地址列表
*
* @param userId
* @return
*/
public List<UserAddress> queryAll(String userId);
/**
* 用户新增地址
*
* @param addressBO
*/
public void addNewUserAddress(AddressBO addressBO);
}
在 foodie-dev-service 模块内完善 AddressServiceImpl 类,实现 addNewUserAddress 方法
package com.imooc.service.impl;
import com.imooc.mapper.UserAddressMapper;
import com.imooc.pojo.UserAddress;
import com.imooc.pojo.bo.AddressBO;
import com.imooc.service.AddressService;
import org.n3r.idworker.Sid;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;
/**
* Created by 92578 on 2020/8/22 16:18
**/
@Service
public class AddressServiceImpl implements AddressService {
@Autowired
private UserAddressMapper userAddressMapper;
@Autowired
private Sid sid;
/**
* 根据用户 id 查询用户的收获地址列表
*
* @param userId
* @return
*/
@Transactional(propagation = Propagation.SUPPORTS)
@Override
public List<UserAddress> queryAll(String userId) {
UserAddress ua = new UserAddress();
ua.setUserId(userId);
return userAddressMapper.select(ua);
}
/**
* 用户新增地址
*
* @param addressBO
*/
@Transactional(propagation = Propagation.REQUIRED)
@Override
public void addNewUserAddress(AddressBO addressBO) {
// 1. 判断当前用户是否存在地址,如果没有,则新增为“默认地址”
Integer isDefault = 0;
List<UserAddress> addressList = this.queryAll(addressBO.getUserId());
if (addressList == null || addressList.isEmpty()) {
isDefault = 1;
}
String addressId = sid.nextShort();
// 2. 保存地址到数据库
UserAddress newAddress = new UserAddress();
BeanUtils.copyProperties(addressBO, newAddress);
newAddress.setId(addressId);
newAddress.setIsDefault(isDefault);
newAddress.setCreatedTime(new Date());
newAddress.setUpdatedTime(new Date());
userAddressMapper.insert(newAddress);
}
}
在 foodie-dev-common 模块的 com.imooc.utils 包内新增 MobileEmailUtils 类
package com.imooc.utils;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MobileEmailUtils {
public static boolean checkMobileIsOk(String mobile) {
String regex = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(17[013678])|(18[0,5-9]))\\d{8}$";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(mobile);
boolean isMatch = m.matches();
return isMatch;
}
public static boolean checkEmailIsOk(String email) {
boolean isMatch = true;
if (!email.matches("[\\w\\.\\-]+@([\\w\\-]+\\.)+[\\w\\-]+")) {
isMatch = false;
}
return isMatch;
}
}
在 foodie-dev-api 模块下完善 AddressController 类,新增 add 方法
package com.imooc.controller;
import com.imooc.pojo.UserAddress;
import com.imooc.pojo.bo.AddressBO;
import com.imooc.service.AddressService;
import com.imooc.utils.IMOOCJSONResult;
import com.imooc.utils.MobileEmailUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* @author 92578
* @since 1.0
*/
@Api(value = "地址相关", tags = {"地址相关的 api 接口"})
@RequestMapping("address")
@RestController
public class AddressController {
@Autowired
private AddressService addressService;
@ApiOperation(value = "根据用户 id 查询收货地址列表", notes = "根据用户 id 查询收货地址列表", httpMethod = "POST")
@PostMapping("/list")
public IMOOCJSONResult list(@RequestParam String userId) {
if (StringUtils.isBlank(userId)) {
return IMOOCJSONResult.errorMsg("");
}
List<UserAddress> list = addressService.queryAll(userId);
return IMOOCJSONResult.ok(list);
}
@ApiOperation(value = "用户新增地址", notes = "用户新增地址", httpMethod = "POST")
@PostMapping("/add")
public IMOOCJSONResult add(@RequestBody AddressBO addressBO) {
IMOOCJSONResult checkRes = checkAddress(addressBO);
if (checkRes.getStatus() != 200) {
return checkRes;
}
addressService.addNewUserAddress(addressBO);
return IMOOCJSONResult.ok();
}
private IMOOCJSONResult checkAddress(AddressBO addressBO) {
String receiver = addressBO.getReceiver();
if (StringUtils.isBlank(receiver)) {
return IMOOCJSONResult.errorMsg("收货人不能为空");
}
if (receiver.length() > 12) {
return IMOOCJSONResult.errorMsg("收货人姓名不能太长");
}
String mobile = addressBO.getMobile();
if (StringUtils.isBlank(mobile)) {
return IMOOCJSONResult.errorMsg("收货人手机号不能为空");
}
if (mobile.length() != 11) {
return IMOOCJSONResult.errorMsg("收货人手机号长度不正确");
}
boolean isMobileOk = MobileEmailUtils.checkMobileIsOk(mobile);
if (!isMobileOk) {
return IMOOCJSONResult.errorMsg("收货人手机号格式不正确");
}
String province = addressBO.getProvince();
String city = addressBO.getCity();
String district = addressBO.getDistrict();
String detail = addressBO.getDetail();
if (StringUtils.isBlank(province) || StringUtils.isBlank(city) || StringUtils.isBlank(district) || StringUtils.isBlank(detail)) {
return IMOOCJSONResult.errorMsg("收货地址信息不能为空");
}
return IMOOCJSONResult.ok();
}
}
启动项目,打开浏览器,访问 http://localhost:8080/foodie-shop/index.html
登录账户“imooc”,密码“123123”,随便添加某商品到购物车,打开购物车界面后进行结算,点击“使用新地址”,输入内容后进行保存
打开数据库,找到 user_address 表,地址信息已经保存入库
再新建一个地址,保存后发现这次不是默认地址
打开数据库,发现 is_default 字段保存的内容不同