123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- <?php
- // +----------------------------------------------------------------------
- // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
- // +----------------------------------------------------------------------
- // | Author: 萤火科技 <admin@yiovo.com>
- // +----------------------------------------------------------------------
- declare (strict_types = 1);
- namespace app\api\controller;
- use app\api\service\User as UserService;
- use app\api\model\UserAddress as UserAddressModel;
- use app\common\exception\BaseException;
- use app\common\service\delivery\Express;
- /**
- * 收货地址管理
- * 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;
- if ($model->add($this->request->post())) {
- return $this->renderSuccess([], '添加成功');
- }
- return $this->renderError($model->getError() ?: '添加失败');
- }
- /**
- * 编辑收货地址
- * @param int $addressId 地址ID
- * @return array|\think\response\Json
- * @throws BaseException
- */
- public function edit(int $addressId)
- {
- $model = UserAddressModel::detail($addressId);
- if ($model->edit($this->request->post())) {
- return $this->renderSuccess([], '更新成功');
- }
- return $this->renderError($model->getError() ?: '更新失败');
- }
- /**
- * 设为默认地址
- * @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([], '设置成功');
- }
- return $this->renderError($model->getError() ?: '设置失败');
- }
- //是否在全国限制的黑名单里
- public function checkLimit(int $addressId=0){
- $userInfo = UserService::getCurrentLoginUser(true);
- $model = UserAddressModel::detail($addressId);
- if(!$model){
- return $this->renderError("地址不存在");
- }
- if($model->user_id!=$userInfo->user_id){
- return $this->renderError("地址不正确");
- }
- $city_id = $model['city_id'];
- $e = new Express($city_id,[]);
- $flag = $e->isDeliveryLimit();
- return $this->renderSuccess(compact('flag'));
- }
- /**
- * 删除收货地址
- * @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([], '删除成功');
- }
- return $this->renderError($model->getError() ?: '删除失败');
- }
- }
|