You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
146 lines
4.1 KiB
146 lines
4.1 KiB
<?php
|
|
|
|
namespace api\logic;
|
|
|
|
|
|
use backend\modules\shop\models\ars\Address;
|
|
use Yii;
|
|
use yii\base\BaseObject;
|
|
use yii\db\Exception;
|
|
use yii\web\BadRequestHttpException;
|
|
use yii\web\NotFoundHttpException;
|
|
use yii\web\ServerErrorHttpException;
|
|
|
|
/**
|
|
* @author iron
|
|
* @email weiriron@gmail.com
|
|
* @package api\logic
|
|
*/
|
|
class AddressLogic extends BaseObject
|
|
{
|
|
const SET_DEFAULT_ADDRESS = 1;
|
|
|
|
public $viewAction = 'view';
|
|
|
|
/**
|
|
* @return Address
|
|
* @throws BadRequestHttpException
|
|
* @throws ServerErrorHttpException
|
|
* 创建地址
|
|
*/
|
|
public function create()
|
|
{
|
|
$data = $this->getParams();
|
|
$address = new Address();
|
|
$address->user_id = Yii::$app->user->getId();
|
|
$address->load($data, '');
|
|
if (!$address->save()) {
|
|
throw new ServerErrorHttpException('地址添加失败');
|
|
}
|
|
if (!$this->getDefaultAddress()) {
|
|
$this->setDefaultAddress($address);
|
|
}
|
|
Helper::createdResponse($address, $this->viewAction);
|
|
return $address;
|
|
}
|
|
|
|
/**
|
|
* @return bool
|
|
* @throws BadRequestHttpException
|
|
* @throws NotFoundHttpException
|
|
* @throws ServerErrorHttpException
|
|
* 更新地址
|
|
*/
|
|
public function update()
|
|
{
|
|
$type = Yii::$app->request->getQueryParam('type');
|
|
$address = $this->findAddress();
|
|
if ($type == self::SET_DEFAULT_ADDRESS) {
|
|
return $this->setDefaultAddress($address);
|
|
}
|
|
$data = $this->getParams();
|
|
$address->load($data, '');
|
|
if (!$address->save()) {
|
|
throw new ServerErrorHttpException('地址添加失败');
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* @param Address $address
|
|
* @return bool
|
|
* @throws ServerErrorHttpException
|
|
* 设置默认地址
|
|
*/
|
|
private function setDefaultAddress($address)
|
|
{
|
|
$tra = Yii::$app->db->beginTransaction();
|
|
try {
|
|
$oldDefault = $this->getDefaultAddress();
|
|
if ($oldDefault) {
|
|
$oldDefault->status = Address::STATUS_NOT_DEFAULT;
|
|
if (!$oldDefault->save()) {
|
|
throw new Exception('服务器取消默认地址失败');
|
|
}
|
|
}
|
|
$address->status = Address::STATUS_DEFAULT;
|
|
if (!$address->save()) {
|
|
throw new Exception('服务器设置默认地址失败');
|
|
}
|
|
$tra->commit();
|
|
return true;
|
|
} catch (\Exception $e) {
|
|
$tra->rollBack();
|
|
throw new ServerErrorHttpException($e->getMessage());
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* @return mixed
|
|
* @throws BadRequestHttpException
|
|
*/
|
|
private function getParams()
|
|
{
|
|
$data['consignee'] = Yii::$app->request->getBodyParam('consignee');
|
|
$data['phone'] = Yii::$app->request->getBodyParam('phone');
|
|
$data['province'] = Yii::$app->request->getBodyParam('province');
|
|
$data['city'] = Yii::$app->request->getBodyParam('city');
|
|
$data['district'] = Yii::$app->request->getBodyParam('district');
|
|
$data['address'] = Yii::$app->request->getBodyParam('address');
|
|
if (empty($data['consignee']) || empty($data['phone']) || empty($data['province']) ||
|
|
empty($data['city']) || empty($data['district']) || empty($data['address'])) {
|
|
throw new BadRequestHttpException(Helper::REQUEST_BAD_PARAMS);
|
|
}
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* @return array|Address|null
|
|
* @throws NotFoundHttpException
|
|
*/
|
|
private function findAddress()
|
|
{
|
|
$id = Yii::$app->request->getQueryParam('id');
|
|
$address = Address::find()
|
|
->where(['id' => $id])
|
|
->andWhere(['user_id' => Yii::$app->user->getId()])
|
|
->one();
|
|
if (!$address) {
|
|
throw new NotFoundHttpException('地址未找到');
|
|
}
|
|
return $address;
|
|
}
|
|
|
|
/**
|
|
* @return array|Address|null
|
|
*/
|
|
private function getDefaultAddress()
|
|
{
|
|
return Address::find()
|
|
->where(['user_id' => Yii::$app->user->getId()])
|
|
->andWhere(['status' => Address::STATUS_DEFAULT])
|
|
->one();
|
|
}
|
|
|
|
}
|