MerchantAddressController.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace addons\TinyShop\merchant\modules\common\controllers;
  3. use Yii;
  4. use common\traits\MerchantCurd;
  5. use common\enums\StatusEnum;
  6. use common\models\base\SearchModel;
  7. use addons\TinyShop\merchant\controllers\BaseController;
  8. use addons\TinyShop\common\models\common\MerchantAddress;
  9. use addons\TinyShop\merchant\modules\common\forms\MerchantAddressForm;
  10. /**
  11. * Class MerchantAddressController
  12. * @package addons\TinyShop\merchant\modules\common\controllers
  13. * @author jianyan74 <751393839@qq.com>
  14. */
  15. class MerchantAddressController extends BaseController
  16. {
  17. use MerchantCurd;
  18. /**
  19. * @var MerchantAddressForm
  20. */
  21. public $modelClass = MerchantAddressForm::class;
  22. /**
  23. * 首页
  24. *
  25. * @return string
  26. * @throws \yii\web\NotFoundHttpException
  27. */
  28. public function actionIndex()
  29. {
  30. $searchModel = new SearchModel([
  31. 'model' => MerchantAddress::class,
  32. 'scenario' => 'default',
  33. 'partialMatchAttributes' => ['contacts', 'mobile'], // 模糊查询
  34. 'defaultOrder' => [
  35. 'sort' => SORT_ASC,
  36. 'id' => SORT_DESC,
  37. ],
  38. 'pageSize' => $this->pageSize,
  39. ]);
  40. $dataProvider = $searchModel
  41. ->search(Yii::$app->request->queryParams);
  42. $dataProvider->query
  43. ->andWhere(['>=', 'status', StatusEnum::DISABLED])
  44. ->andWhere(['merchant_id' => Yii::$app->services->merchant->getNotNullId()]);
  45. return $this->render($this->action->id, [
  46. 'dataProvider' => $dataProvider,
  47. 'searchModel' => $searchModel,
  48. ]);
  49. }
  50. /**
  51. * 编辑/创建
  52. *
  53. * @return mixed
  54. */
  55. public function actionEdit()
  56. {
  57. $model = $this->findModel($this->getMerchantId());
  58. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  59. return $this->message('保存成功', $this->redirect(['index']));
  60. }
  61. return $this->render($this->action->id, [
  62. 'model' => $model,
  63. ]);
  64. }
  65. /**
  66. * 返回模型
  67. *
  68. * @param $id
  69. * @return \yii\db\ActiveRecord
  70. */
  71. protected function findModel($id)
  72. {
  73. /* @var $model \yii\db\ActiveRecord */
  74. if (empty(($model = $this->modelClass::findOne(['merchant_id' => $id])))) {
  75. $model = new $this->modelClass;
  76. return $model->loadDefaultValues();
  77. }
  78. return $model;
  79. }
  80. }
粤ICP备19079148号