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.

67 lines
1.9 KiB

  1. <?php
  2. namespace api\logic;
  3. use backend\models\ars\Address;
  4. use Yii;
  5. use yii\base\Component;
  6. use yii\web\BadRequestHttpException;
  7. use yii\web\NotFoundHttpException;
  8. use yii\web\ServerErrorHttpException;
  9. /**
  10. * @author iron
  11. * @email weiriron@gmail.com
  12. * Class CartLogic
  13. * @package api\logic
  14. */
  15. class AddressLogic extends Component
  16. {
  17. public $viewAction = 'view';
  18. /**
  19. * @return Address
  20. * @throws BadRequestHttpException
  21. * @throws ServerErrorHttpException
  22. */
  23. public function create()
  24. {
  25. $data['consignee'] = Yii::$app->request->getBodyParam('consignee');
  26. $data['phone'] = Yii::$app->request->getBodyParam('phone');
  27. $data['province'] = Yii::$app->request->getBodyParam('province');
  28. $data['city'] = Yii::$app->request->getBodyParam('city');
  29. $data['district'] = Yii::$app->request->getBodyParam('district');
  30. $data['address'] = Yii::$app->request->getBodyParam('address');
  31. if (empty($data['consignee']) || empty($data['phone']) || empty($data['province']) ||
  32. empty($data['city']) || empty($data['district']) || empty($data['address'])) {
  33. throw new BadRequestHttpException(Helper::REQUEST_BAD_PARAMS);
  34. }
  35. $address = new Address();
  36. $address->user_id = Yii::$app->user->getId();
  37. $address->load($data, '');
  38. if (!$address->save()) {
  39. throw new ServerErrorHttpException('地址添加失败');
  40. }
  41. Helper::createdResponse($address, $this->viewAction);
  42. return $address;
  43. }
  44. private function setDefaultAddress($address)
  45. {
  46. }
  47. private function findAddress()
  48. {
  49. $id = Yii::$app->request->getQueryParam('id');
  50. $address = Address::find()
  51. ->where(['id' => $id])
  52. ->andWhere(['user_id' => Yii::$app->user->getId()])
  53. ->one();
  54. if (!$address) {
  55. throw new NotFoundHttpException('地址未找到');
  56. }
  57. }
  58. }