用户在确认订单页面,可以针对收货地址做如下操作:
1. 查询用户的所有收货地址列表
2. 新增收货地址
3. 删除收货地址
4. 修改收货地址
5. 设置默认地址
步骤1:创建对应用户地址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;
}
步骤2:创建实现类和对应的接口
import com.one.bo.AddressBO;
import com.one.pojo.UserAddress;
import java.util.List;
public interface AddressService {/*** 根据用户id查询用户的收货地址列表* @param userId* @return*/public List<UserAddress> queryAll(String userId);/*** 用户新增地址* @param addressBO*/public void addNewUserAddress(AddressBO addressBO);/*** 用户修改地址* @param addressBO*/public void updateUserAddress(AddressBO addressBO);/*** 根据用户id和地址id,删除对应的用户地址信息* @param userId* @param addressId*/public void deleteUserAddress(String userId, String addressId);/*** 修改默认地址* @param userId* @param addressId*/public void updateUserAddressToBeDefault(String userId, String addressId);/*** 根据用户id和地址id,查询具体的用户地址对象信息* @param userId* @param addressId* @return*/public UserAddress queryUserAddres(String userId, String addressId);
}
===================================================================
import com.one.bo.AddressBO;
import com.one.enums.YesOrNo;
import com.one.mapper.UserAddressMapper;
import com.one.pojo.UserAddress;
import com.one.service.address.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 {@Autowiredprivate UserAddressMapper userAddressMapper;@Autowiredprivate Sid sid; //获取唯一主键对象@Transactional(propagation = Propagation.SUPPORTS)@Overridepublic List<UserAddress> queryAll(String userId) {UserAddress ua = new UserAddress();ua.setUserId(userId);return userAddressMapper.select(ua);}@Transactional(propagation = Propagation.REQUIRED)@Overridepublic 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)@Overridepublic 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)@Overridepublic void deleteUserAddress(String userId, String addressId) {UserAddress address = new UserAddress();address.setId(addressId);address.setUserId(userId);userAddressMapper.delete(address);}@Transactional(propagation = Propagation.REQUIRED)@Overridepublic void updateUserAddressToBeDefault(String userId, String addressId) {// 1. 查找默认地址,设置为不默认UserAddress queryAddress = new UserAddress();queryAddress.setUserId(userId);queryAddress.setIsDefault(YesOrNo.YES.type); //1List<UserAddress> list = userAddressMapper.select(queryAddress);for (UserAddress ua : list) {ua.setIsDefault(YesOrNo.NO.type); //0userAddressMapper.updateByPrimaryKeySelective(ua);}// 2. 根据地址id修改为默认的地址UserAddress defaultAddress = new UserAddress();defaultAddress.setId(addressId);defaultAddress.setUserId(userId);defaultAddress.setIsDefault(YesOrNo.YES.type);userAddressMapper.updateByPrimaryKeySelective(defaultAddress);}@Transactional(propagation = Propagation.SUPPORTS)@Overridepublic UserAddress queryUserAddres(String userId, String addressId) {UserAddress singleAddress = new UserAddress();singleAddress.setId(addressId);singleAddress.setUserId(userId);return userAddressMapper.selectOne(singleAddress);}
}
步骤3:创建对应接口访问类controller类
import com.one.bo.AddressBO;
import com.one.pojo.UserAddress;
import com.one.service.address.AddressService;
import com.one.utils.JSONResult;
import com.one.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. 设置默认地址*/@Autowiredprivate AddressService addressService;@ApiOperation(value = "根据用户id查询收货地址列表", notes = "根据用户id查询收货地址列表", httpMethod = "POST")@PostMapping("/list")public JSONResult list(@RequestParam String userId) {if (StringUtils.isBlank(userId)) {return JSONResult.errorMsg("");}List<UserAddress> list = addressService.queryAll(userId);return JSONResult.ok(list);}@ApiOperation(value = "用户新增地址", notes = "用户新增地址", httpMethod = "POST")@PostMapping("/add")public JSONResult add(@RequestBody AddressBO addressBO) {JSONResult checkRes = checkAddress(addressBO);if (checkRes.getStatus() != 200) {return checkRes;}addressService.addNewUserAddress(addressBO);return JSONResult.ok();}@ApiOperation(value = "用户修改地址", notes = "用户修改地址", httpMethod = "POST")@PostMapping("/update")public JSONResult update(@RequestBody AddressBO addressBO) {if (StringUtils.isBlank(addressBO.getAddressId())) {return JSONResult.errorMsg("修改地址错误:addressId不能为空");}JSONResult checkRes = checkAddress(addressBO);if (checkRes.getStatus() != 200) {return checkRes;}addressService.updateUserAddress(addressBO);return JSONResult.ok();}@ApiOperation(value = "用户删除地址", notes = "用户删除地址", httpMethod = "POST")@PostMapping("/delete")public JSONResult delete(@RequestParam String userId, @RequestParam String addressId) {if (StringUtils.isBlank(userId) || StringUtils.isBlank(addressId)) {return JSONResult.errorMsg("");}addressService.deleteUserAddress(userId, addressId);return JSONResult.ok();}@ApiOperation(value = "用户设置默认地址", notes = "用户设置默认地址", httpMethod = "POST")@PostMapping("/setDefalut")public JSONResult setDefalut(@RequestParam String userId, @RequestParam String addressId) {if (StringUtils.isBlank(userId) || StringUtils.isBlank(addressId)) {return JSONResult.errorMsg("");}addressService.updateUserAddressToBeDefault(userId, addressId);return JSONResult.ok();}private JSONResult checkAddress(AddressBO addressBO) {String receiver = addressBO.getReceiver();if (StringUtils.isBlank(receiver)) {return JSONResult.errorMsg("收货人不能为空");}if (receiver.length() > 12) {return JSONResult.errorMsg("收货人姓名不能太长");}String mobile = addressBO.getMobile();if (StringUtils.isBlank(mobile)) {return JSONResult.errorMsg("收货人手机号不能为空");}if (mobile.length() != 11) {return JSONResult.errorMsg("收货人手机号长度不正确");}boolean isMobileOk = MobileEmailUtils.checkMobileIsOk(mobile);if (!isMobileOk) {return JSONResult.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 JSONResult.errorMsg("收货地址信息不能为空");}return JSONResult.ok();}}
步骤4:手机和邮箱验证工具类
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;}
}