一、數據表設計

create table `foodie-shop-dev`.user_address ( id varchar(64) not null comment '地址主鍵id' primary key, user_id varchar(64) not null comment '關聯用戶id', receiver varchar(32) not null comment '收件人姓名', mobile varchar(32) not null comment '收件人手機號', province varchar(32) not null comment '省份', city varchar(32) not null comment '城市', district varchar(32) not null comment '區縣', detail varchar(128) not null comment '詳細地址', extand varchar(128) null comment '擴展字段', is_default int null comment '是否默認地址', created_time datetime not null comment '創建時間', updated_time datetime not null comment '更新時間' ) comment '用戶地址表 ' charset = utf8mb4;
二、功能說明
/** * 用戶在確認訂單頁面,可以針對收貨地址做如下操作: * 1. 查詢用戶的所有收貨地址列表 * 2. 新增收貨地址 * 3. 刪除收貨地址 * 4. 修改收貨地址 * 5. 設置默認地址 */
三、service 子模塊實現
1、接口定義

package com.imooc.service; import com.imooc.pojo.UserAddress; import com.imooc.pojo.bo.AddressBO; import java.util.List; public interface AddressService { /** *查詢用戶的所有收貨地址列表 * @return */ public List<UserAddress> queryAll(String userId); /**新增收貨地址 * * @param * @return */ public void addNewUserAddress (AddressBO userAddressBO); /**刪除收貨地址 * * @param userId * @return */ public void deleteUserAddress(String userId, String addressId); /**修改收貨地址 * * @param userAddressBO * @return */ public void updateUserAddress(AddressBO userAddressBO); /** * 設置默認地址 * @param userId * @return */ public void updateUserAddressToBeDefault(String userId, String addressId); }
2、BO定義(前端發送的數據)

package com.imooc.pojo.bo; /** * 用戶新增或修改地址的BO */ 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; } }
3、接口實現

package com.imooc.service.impl; import com.imooc.enums.YesOrNo; 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; @Service public class AddressServiceImpl implements AddressService { @Autowired private UserAddressMapper userAddressMapper; @Autowired private Sid sid; @Transactional(propagation = Propagation.SUPPORTS) @Override public List<UserAddress> queryAll(String userId) { UserAddress ua = new UserAddress(); ua.setUserId(userId); return userAddressMapper.select(ua); } @Transactional(propagation = Propagation.SUPPORTS) @Override public void addNewUserAddress(AddressBO addressBO) { // 1. 判斷當前用戶是否存在地址,如果沒有,則新增為‘默認地址’ Integer isDefault = 0; List<UserAddress> addressList = this.queryAll(addressBO.getUserId()); if (addressList == null || addressList.isEmpty() || addressList.size() == 0) { 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); } @Transactional(propagation = Propagation.REQUIRED) @Override public void deleteUserAddress(String userId, String addressId) { UserAddress userAddress=new UserAddress(); userAddress.setId(addressId); userAddress.setUserId(userId); userAddressMapper.delete(userAddress); } @Transactional(propagation = Propagation.REQUIRED) @Override public void updateUserAddress(AddressBO addressBO) { String addressId = addressBO.getAddressId(); UserAddress pendingAddress = new UserAddress(); BeanUtils.copyProperties(addressBO, pendingAddress); pendingAddress.setId(addressId); pendingAddress.setUpdatedTime(new Date()); userAddressMapper.updateByPrimaryKeySelective(pendingAddress); } @Transactional(propagation = Propagation.REQUIRED) @Override public void updateUserAddressToBeDefault(String userId, String addressId) { // 1. 查找默認地址,設置為不默認 UserAddress queryAddress = new UserAddress(); queryAddress.setUserId(userId); queryAddress.setIsDefault(YesOrNo.YES.type); List<UserAddress> list = userAddressMapper.select(queryAddress); for (UserAddress ua : list) { ua.setIsDefault(YesOrNo.NO.type); userAddressMapper.updateByPrimaryKeySelective(ua); } // 2. 根據地址id修改為默認的地址 UserAddress defaultAddress = new UserAddress(); defaultAddress.setId(addressId); defaultAddress.setUserId(userId); defaultAddress.setIsDefault(YesOrNo.YES.type); userAddressMapper.updateByPrimaryKeySelective(defaultAddress); } }
四、api子模塊實現
1、手機號碼判斷工具類

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; } }
2、Controller實現

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; @Api(value = "收貨地址相關", tags = {"收貨地址相關的api接口"}) @RequestMapping("address") @RestController public class AddressController { /** * 用戶在確認訂單頁面,可以針對收貨地址做如下操作: * 1. 查詢用戶的所有收貨地址列表 * 2. 新增收貨地址 * 3. 刪除收貨地址 * 4. 修改收貨地址 * 5. 設置默認地址 */ @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(); } @ApiOperation(value = "用戶修改地址", notes = "用戶修改地址", httpMethod = "POST") @PostMapping("/update") public IMOOCJSONResult update(@RequestBody AddressBO addressBO) { if (StringUtils.isBlank(addressBO.getAddressId())) { return IMOOCJSONResult.errorMsg("修改地址錯誤:addressId不能為空"); } IMOOCJSONResult checkRes = checkAddress(addressBO); if (checkRes.getStatus() != 200) { return checkRes; } addressService.updateUserAddress(addressBO); return IMOOCJSONResult.ok(); } @ApiOperation(value = "用戶刪除地址", notes = "用戶刪除地址", httpMethod = "POST") @PostMapping("/delete") public IMOOCJSONResult delete( @RequestParam String userId, @RequestParam String addressId) { if (StringUtils.isBlank(userId) || StringUtils.isBlank(addressId)) { return IMOOCJSONResult.errorMsg(""); } addressService.deleteUserAddress(userId, addressId); return IMOOCJSONResult.ok(); } @ApiOperation(value = "用戶設置默認地址", notes = "用戶設置默認地址", httpMethod = "POST") @PostMapping("/setDefalut") public IMOOCJSONResult setDefalut( @RequestParam String userId, @RequestParam String addressId) { if (StringUtils.isBlank(userId) || StringUtils.isBlank(addressId)) { return IMOOCJSONResult.errorMsg(""); } addressService.updateUserAddressToBeDefault(userId, addressId); return IMOOCJSONResult.ok(); } }