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.2 KiB

  1. <?php
  2. namespace api\logic;
  3. use backend\modules\shop\models\ars\Address;
  4. use Yii;
  5. use yii\base\BaseObject;
  6. use yii\db\Exception;
  7. use yii\web\BadRequestHttpException;
  8. use yii\web\NotFoundHttpException;
  9. use yii\web\ServerErrorHttpException;
  10. /**
  11. * @author iron
  12. * @email weiriron@gmail.com
  13. * Class CartLogic
  14. * @package api\logic
  15. */
  16. class AddressLogic extends BaseObject
  17. {
  18. const SET_DEFAULT_ADDRESS = 1;
  19. public $viewAction = 'view';
  20. /**
  21. * @return Address
  22. * @throws BadRequestHttpException
  23. * @throws ServerErrorHttpException
  24. * 创建地址
  25. */
  26. public function create()
  27. {
  28. $data = $this->getParams();
  29. $address = new Address();
  30. $address->user_id = Yii::$app->user->getId();
  31. $address->load($data, '');
  32. if (!$address->save()) {
  33. throw new ServerErrorHttpException('地址添加失败');
  34. }
  35. if (!$this->getDefaultAddress()) {
  36. $this->setDefaultAddress($address);
  37. }
  38. Helper::createdResponse($address, $this->viewAction);
  39. return $address;
  40. }
  41. /**
  42. * @return bool
  43. * @throws BadRequestHttpException
  44. * @throws NotFoundHttpException
  45. * @throws ServerErrorHttpException
  46. * 更新地址
  47. */
  48. public function update()
  49. {
  50. $type = Yii::$app->request->getQueryParam('type');
  51. $address = $this->findAddress();
  52. if ($type == self::SET_DEFAULT_ADDRESS) {
  53. return $this->setDefaultAddress($address);
  54. }
  55. $data = $this->getParams();
  56. $address->load($data, '');
  57. if (!$address->save()) {
  58. throw new ServerErrorHttpException('地址添加失败');
  59. }
  60. return true;
  61. }
  62. /**
  63. * @param Address $address
  64. * @return bool
  65. * @throws ServerErrorHttpException
  66. * 设置默认地址
  67. */
  68. private function setDefaultAddress($address)
  69. {
  70. $tra = Yii::$app->db->beginTransaction();
  71. try {
  72. $oldDefault = $this->getDefaultAddress();
  73. if ($oldDefault) {
  74. $oldDefault->status = Address::STATUS_NOT_DEFAULT;
  75. if (!$oldDefault->save()) {
  76. throw new Exception('服务器取消默认地址失败');
  77. }
  78. }
  79. $address->status = Address::STATUS_DEFAULT;
  80. if (!$address->save()) {
  81. throw new Exception('服务器设置默认地址失败');
  82. }
  83. $tra->commit();
  84. return true;
  85. } catch (\Exception $e) {
  86. $tra->rollBack();
  87. throw new ServerErrorHttpException($e->getMessage());
  88. }
  89. }
  90. /**
  91. * @return mixed
  92. * @throws BadRequestHttpException
  93. */
  94. private function getParams()
  95. {
  96. $data['consignee'] = Yii::$app->request->getBodyParam('consignee');
  97. $data['phone'] = Yii::$app->request->getBodyParam('phone');
  98. $data['province'] = Yii::$app->request->getBodyParam('province');
  99. $data['city'] = Yii::$app->request->getBodyParam('city');
  100. $data['district'] = Yii::$app->request->getBodyParam('district');
  101. $data['address'] = Yii::$app->request->getBodyParam('address');
  102. if (empty($data['consignee']) || empty($data['phone']) || empty($data['province']) ||
  103. empty($data['city']) || empty($data['district']) || empty($data['address'])) {
  104. throw new BadRequestHttpException(Helper::REQUEST_BAD_PARAMS);
  105. }
  106. return $data;
  107. }
  108. /**
  109. * @return array|Address|null
  110. * @throws NotFoundHttpException
  111. */
  112. private function findAddress()
  113. {
  114. $id = Yii::$app->request->getQueryParam('id');
  115. $address = Address::find()
  116. ->where(['id' => $id])
  117. ->andWhere(['user_id' => Yii::$app->user->getId()])
  118. ->one();
  119. if (!$address) {
  120. throw new NotFoundHttpException('地址未找到');
  121. }
  122. return $address;
  123. }
  124. /**
  125. * @return array|Address|null
  126. */
  127. private function getDefaultAddress()
  128. {
  129. return Address::find()
  130. ->where(['user_id' => Yii::$app->user->getId()])
  131. ->andWhere(['status' => Address::STATUS_DEFAULT])
  132. ->one();
  133. }
  134. }