| | |
| | | package com.ruoyi.user.controller; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.validation.annotation.Validated; |
| | | import org.springframework.web.bind.annotation.DeleteMapping; |
| | | import org.springframework.web.bind.annotation.GetMapping; |
| | | import org.springframework.web.bind.annotation.PathVariable; |
| | | import org.springframework.web.bind.annotation.PostMapping; |
| | | import org.springframework.web.bind.annotation.PutMapping; |
| | | import org.springframework.web.bind.annotation.RequestBody; |
| | | import org.springframework.web.bind.annotation.RequestMapping; |
| | | import org.springframework.web.bind.annotation.RestController; |
| | | import com.ruoyi.common.core.domain.R; |
| | | import com.ruoyi.common.core.exception.ServiceException; |
| | | import com.ruoyi.common.core.web.controller.BaseController; |
| | | import com.ruoyi.common.core.web.page.TableDataInfo; |
| | | import com.ruoyi.common.security.utils.SecurityUtils; |
| | | import com.ruoyi.user.domain.AccountAddress; |
| | | import com.ruoyi.user.domain.dto.AccountAddressDto; |
| | | import com.ruoyi.user.domain.dto.AccountCertificationApplyDto; |
| | | import com.ruoyi.user.domain.dto.AccountUserAvatarDto; |
| | | import com.ruoyi.user.domain.dto.CompanyCertificationApplyDto; |
| | | import com.ruoyi.user.domain.vo.AccountAddressVo; |
| | | import com.ruoyi.user.domain.vo.AccountCertificationStatusVo; |
| | | import com.ruoyi.user.domain.vo.AccountInviteeVo; |
| | | import com.ruoyi.user.domain.vo.AccountReferrerVo; |
| | |
| | | { |
| | | return R.ok(accountUserService.getReferrer(SecurityUtils.getUserId())); |
| | | } |
| | | |
| | | /** |
| | | * 查询本人收货地址列表 |
| | | * |
| | | * 默认地址排前,其余按 id 升序;地址数量少,不分页。 |
| | | * 会员身份取自登录态,不接受前端传入 userId。 |
| | | * |
| | | * @return 地址列表(无地址返回空集合) |
| | | */ |
| | | @GetMapping("/addresses") |
| | | public R<List<AccountAddressVo>> addressList() |
| | | { |
| | | List<AccountAddress> list = accountUserService.getAddressList(SecurityUtils.getUserId()); |
| | | return R.ok(toAddressVoList(list)); |
| | | } |
| | | |
| | | /** |
| | | * 查询本人单条收货地址详情 |
| | | * |
| | | * 供编辑页回填;校验归属,非本人地址抛业务异常。 |
| | | * 会员身份取自登录态,不接受前端传入 userId。 |
| | | * |
| | | * @param id 地址ID |
| | | * @return 地址详情 |
| | | */ |
| | | @GetMapping("/addresses/{id}") |
| | | public R<AccountAddressVo> addressDetail(@PathVariable("id") Long id) |
| | | { |
| | | AccountAddress address = accountUserService.getAddressDetail(SecurityUtils.getUserId(), id); |
| | | return R.ok(toAddressVo(address)); |
| | | } |
| | | |
| | | /** |
| | | * 新增本人收货地址 |
| | | * |
| | | * 首条地址自动设为默认;会员身份取自登录态,不接受前端传入 userId。 |
| | | * |
| | | * @param dto 地址入参 |
| | | * @return 操作结果 |
| | | */ |
| | | @PostMapping("/addresses") |
| | | public R<Void> createAddress(@Validated @RequestBody AccountAddressDto dto) |
| | | { |
| | | accountUserService.insertMyAddress(buildAddress(dto, SecurityUtils.getUserId())); |
| | | return R.ok(); |
| | | } |
| | | |
| | | /** |
| | | * 编辑本人收货地址 |
| | | * |
| | | * 复用后台改地址事务逻辑(含默认唯一约束);会员身份取自登录态。 |
| | | * |
| | | * @param dto 地址入参(必含 id) |
| | | * @return 操作结果 |
| | | */ |
| | | @PutMapping("/addresses") |
| | | public R<Void> updateAddress(@Validated @RequestBody AccountAddressDto dto) |
| | | { |
| | | if (dto.getId() == null) |
| | | { |
| | | throw new ServiceException("地址ID不能为空"); |
| | | } |
| | | accountUserService.updateAccountAddress(buildAddress(dto, SecurityUtils.getUserId())); |
| | | return R.ok(); |
| | | } |
| | | |
| | | /** |
| | | * 删除本人收货地址 |
| | | * |
| | | * 校验归属后逻辑删除;会员身份取自登录态,不接受前端传入 userId。 |
| | | * |
| | | * @param id 地址ID |
| | | * @return 操作结果 |
| | | */ |
| | | @DeleteMapping("/addresses/{id}") |
| | | public R<Void> deleteAddress(@PathVariable("id") Long id) |
| | | { |
| | | accountUserService.deleteMyAddress(SecurityUtils.getUserId(), id); |
| | | return R.ok(); |
| | | } |
| | | |
| | | /** |
| | | * DTO 转实体(userId 由登录态注入,不接受前端传值) |
| | | */ |
| | | private AccountAddress buildAddress(AccountAddressDto dto, Long userId) |
| | | { |
| | | AccountAddress address = new AccountAddress(); |
| | | address.setId(dto.getId()); |
| | | address.setUserId(userId); |
| | | address.setConsignee(dto.getConsignee()); |
| | | address.setConsigneePhone(dto.getConsigneePhone()); |
| | | address.setAddress(dto.getAddress()); |
| | | address.setIsDefault(dto.getIsDefault()); |
| | | return address; |
| | | } |
| | | |
| | | /** |
| | | * 实体转出参 VO(列表) |
| | | */ |
| | | private List<AccountAddressVo> toAddressVoList(List<AccountAddress> list) |
| | | { |
| | | List<AccountAddressVo> voList = new ArrayList<>(list.size()); |
| | | for (AccountAddress address : list) |
| | | { |
| | | voList.add(toAddressVo(address)); |
| | | } |
| | | return voList; |
| | | } |
| | | |
| | | /** |
| | | * 实体转出参 VO(单条) |
| | | */ |
| | | private AccountAddressVo toAddressVo(AccountAddress address) |
| | | { |
| | | AccountAddressVo vo = new AccountAddressVo(); |
| | | vo.setId(address.getId()); |
| | | vo.setConsignee(address.getConsignee()); |
| | | vo.setConsigneePhone(address.getConsigneePhone()); |
| | | vo.setAddress(address.getAddress()); |
| | | vo.setIsDefault(address.getIsDefault()); |
| | | return vo; |
| | | } |
| | | } |