AddressController.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace addons\TinyShop\api\modules\v1\controllers\member;
  3. use Yii;
  4. use api\controllers\UserAuthController;
  5. use common\models\member\Address;
  6. use common\helpers\ResultHelper;
  7. /**
  8. * 地址
  9. *
  10. * Class AddressController
  11. * @package addons\TinyShop\api\controllers\member
  12. * @author jianyan74 <751393839@qq.com>
  13. */
  14. class AddressController extends UserAuthController
  15. {
  16. /**
  17. * @var Address
  18. */
  19. public $modelClass = Address::class;
  20. /**
  21. * @return array|Address|mixed|\yii\db\ActiveRecord
  22. */
  23. public function actionCreate()
  24. {
  25. /* @var $model Address */
  26. $model = new $this->modelClass();
  27. $model->attributes = Yii::$app->request->post();
  28. $model->member_id = Yii::$app->user->identity->member_id;
  29. $model->merchant_id = Yii::$app->user->identity->merchant_id;
  30. list($province_id, $city_id, $area_id) = Yii::$app->services->provinces->getParentIdsByAreaId($model->area_id);
  31. $model->province_id = $province_id;
  32. $model->city_id = $city_id;
  33. $model->area_id = $area_id;
  34. if (!$model->save()) {
  35. return ResultHelper::json(422, $this->getError($model));
  36. }
  37. return $model;
  38. }
  39. /**
  40. * @param $id
  41. * @return array|mixed|\yii\db\ActiveRecord
  42. * @throws \yii\web\NotFoundHttpException
  43. */
  44. public function actionUpdate($id)
  45. {
  46. $model = $this->findModel($id);
  47. $model->attributes = Yii::$app->request->post();
  48. list($province_id, $city_id, $area_id) = Yii::$app->services->provinces->getParentIdsByAreaId($model->area_id);
  49. $model->province_id = $province_id;
  50. $model->city_id = $city_id;
  51. $model->area_id = $area_id;
  52. if (!$model->save()) {
  53. return ResultHelper::json(422, $this->getError($model));
  54. }
  55. return $model;
  56. }
  57. /**
  58. * @return array|\yii\db\ActiveRecord|null
  59. */
  60. public function actionDefault()
  61. {
  62. return Yii::$app->services->memberAddress->findDefaultByMemberId(Yii::$app->user->identity->member_id);
  63. }
  64. }
粤ICP备19079148号