AddressController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace addons\Member\merchant\controllers;
  3. use Yii;
  4. use yii\data\Pagination;
  5. use yii\helpers\ArrayHelper;
  6. use common\helpers\ResultHelper;
  7. use common\enums\StatusEnum;
  8. use common\models\member\Address;
  9. use common\models\member\Member;
  10. /**
  11. * 收货地址
  12. *
  13. * Class AddressController
  14. * @package addons\Member\merchant\controllers
  15. * @author jianyan74 <751393839@qq.com>
  16. */
  17. class AddressController extends BaseController
  18. {
  19. protected $member_id;
  20. /**
  21. * @var Member
  22. */
  23. protected $member;
  24. /**
  25. * @throws \yii\base\InvalidConfigException
  26. */
  27. public function init()
  28. {
  29. $this->member_id = Yii::$app->request->get('member_id');
  30. $this->member = Yii::$app->services->member->findById($this->member_id);
  31. parent::init();
  32. }
  33. /**
  34. * 首页
  35. *
  36. * @return mixed
  37. */
  38. public function actionIndex()
  39. {
  40. $data = Address::find()
  41. ->where(['>=', 'status', StatusEnum::DISABLED])
  42. ->andWhere(['member_id' => $this->member_id]);
  43. $pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => $this->pageSize]);
  44. $models = $data->offset($pages->offset)
  45. ->orderBy('id desc')
  46. ->limit($pages->limit)
  47. ->all();
  48. return $this->render($this->action->id, [
  49. 'models' => $models,
  50. 'pages' => $pages,
  51. 'member_id' => $this->member_id,
  52. ]);
  53. }
  54. /**
  55. * 编辑/创建
  56. *
  57. * @return mixed
  58. */
  59. public function actionEdit()
  60. {
  61. $request = Yii::$app->request;
  62. $id = $request->get('id', null);
  63. $model = $this->findModel($id);
  64. if ($model->load($request->post()) && $model->save()) {
  65. return $this->redirect(['index', 'member_id' => $this->member_id]);
  66. }
  67. return $this->render($this->action->id, [
  68. 'model' => $model,
  69. 'member_id' => $this->member_id,
  70. ]);
  71. }
  72. /**
  73. * 伪删除
  74. *
  75. * @param $id
  76. * @return mixed
  77. */
  78. public function actionDestroy($id)
  79. {
  80. if (!($model = Address::findOne($id))) {
  81. return $this->message("找不到数据", $this->redirect(['index']), 'error');
  82. }
  83. $model->status = StatusEnum::DELETE;
  84. if ($model->save()) {
  85. return $this->message("删除成功", $this->redirect(['index', 'member_id' => $model->member_id]));
  86. }
  87. return $this->message("删除失败", $this->redirect(['index', 'member_id' => $model->member_id]), 'error');
  88. }
  89. /**
  90. * 更新排序/状态字段
  91. *
  92. * @param $id
  93. * @return array
  94. */
  95. public function actionAjaxUpdate($id)
  96. {
  97. if (!($model = Address::findOne($id))) {
  98. return ResultHelper::json(404, '找不到数据');
  99. }
  100. $model->attributes = ArrayHelper::filter(Yii::$app->request->get(), ['sort', 'status']);
  101. if (!$model->save()) {
  102. return ResultHelper::json(422, $this->getError($model));
  103. }
  104. return ResultHelper::json(200, '修改成功');
  105. }
  106. /**
  107. * 编辑/创建
  108. *
  109. * @return mixed|string|\yii\web\Response
  110. * @throws \yii\base\ExitException
  111. */
  112. public function actionAjaxEdit()
  113. {
  114. $id = Yii::$app->request->get('id');
  115. $model = $this->findModel($id);
  116. // ajax 校验
  117. $this->activeFormValidate($model);
  118. if ($model->load(Yii::$app->request->post())) {
  119. return $model->save()
  120. ? $this->redirect(['index', 'member_id' => $model->member_id])
  121. : $this->message($this->getError($model), $this->redirect(['index', 'member_id' => $model->member_id]), 'error');
  122. }
  123. return $this->renderAjax($this->action->id, [
  124. 'model' => $model,
  125. ]);
  126. }
  127. /**
  128. * 返回模型
  129. *
  130. * @param $id
  131. * @return mixed
  132. */
  133. protected function findModel($id)
  134. {
  135. if (empty($id) || empty(($model = Address::findOne($id)))) {
  136. $model = new Address;
  137. $model = $model->loadDefaultValues();
  138. $model->member_id = $this->member_id;
  139. $model->merchant_id = $this->member->merchant_id;
  140. return $model;
  141. }
  142. return $model;
  143. }
  144. }
粤ICP备19079148号