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.

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