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.

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