// +---------------------------------------------------------------------- declare (strict_types = 1); namespace app\index\controller; use app\index\service\User as UserService; use app\index\model\UserAddress as UserAddressModel; use app\common\exception\BaseException; /** * 收货地址管理 * Class Address * @package app\api\controller */ class Address extends Controller { /** * 收货地址列表 * @return array|\think\response\Json * @throws BaseException * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function list() { // 获取收货地址列表 $model = new UserAddressModel; $list = $model->getList(); return $this->renderSuccess(compact('list')); } /** * 获取当前用户默认收货地址 * @return array|\think\response\Json * @throws BaseException */ public function defaultId() { $useInfo = UserService::getCurrentLoginUser(true); return $this->renderSuccess(['defaultId' => $useInfo['address_id']]); } /** * 收货地址详情 * @param int $addressId 地址ID * @return array|\think\response\Json * @throws BaseException */ public function detail(int $addressId) { $detail = UserAddressModel::detail($addressId); return $this->renderSuccess(compact('detail')); } /** * 添加收货地址 * @return array|\think\response\Json * @throws BaseException */ public function add() { $model = new UserAddressModel; $res = $model->add($this->postForm()); if ($res['address_id']) { return $this->renderSuccess($res, 'Successful'); } return $this->renderError($model->getError() ?: 'Failed,please try again later'); } /** * 编辑收货地址 * @param int $addressId 地址ID * @return array|\think\response\Json * @throws BaseException */ public function edit(int $addressId) { $model = UserAddressModel::detail($addressId); if ($model->edit($this->postForm())) { return $this->renderSuccess([], 'Successful'); } return $this->renderError($model->getError() ?: 'Failed,please try again later'); } /** * 设为默认地址 * @param int $addressId 地址ID * @return array|\think\response\Json * @throws BaseException */ public function setDefault(int $addressId) { $model = UserAddressModel::detail($addressId); if ($model->setDefault((int)$model['address_id'])) { return $this->renderSuccess([], 'Successful'); } return $this->renderError($model->getError() ?: 'Failed,please try again later'); } /** * 删除收货地址 * @param int $addressId 地址ID * @return array|\think\response\Json * @throws BaseException */ public function remove(int $addressId) { $model = UserAddressModel::detail($addressId); if ($model->remove()) { return $this->renderSuccess([], 'Successful'); } return $this->renderError($model->getError() ?: 'Failed,please try again later'); } }