SpecController.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace addons\TinyShop\merchant\modules\common\controllers;
  3. use Yii;
  4. use common\enums\StatusEnum;
  5. use common\models\base\SearchModel;
  6. use common\traits\MerchantCurd;
  7. use common\helpers\ResultHelper;
  8. use addons\TinyShop\common\models\common\Spec;
  9. use addons\TinyShop\merchant\controllers\BaseController;
  10. use addons\TinyShop\merchant\modules\common\forms\SpecForm;
  11. /**
  12. * Class SpecController
  13. * @package addons\TinyShop\merchant\modules\common\controllers
  14. */
  15. class SpecController extends BaseController
  16. {
  17. use MerchantCurd;
  18. /**
  19. * @var Spec
  20. */
  21. public $modelClass = Spec::class;
  22. /**
  23. * 首页
  24. *
  25. * @return string
  26. * @throws \yii\web\NotFoundHttpException
  27. */
  28. public function actionIndex()
  29. {
  30. $searchModel = new SearchModel([
  31. 'model' => $this->modelClass,
  32. 'scenario' => 'default',
  33. 'partialMatchAttributes' => ['title'], // 模糊查询
  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::ENABLED])
  44. ->andWhere(['is_tmp' => StatusEnum::DISABLED])
  45. ->andWhere(['merchant_id' => $this->getMerchantId()])
  46. ->with(['value']);
  47. return $this->render($this->action->id, [
  48. 'dataProvider' => $dataProvider,
  49. 'searchModel' => $searchModel,
  50. ]);
  51. }
  52. /**
  53. * 编辑/创建
  54. *
  55. * @return mixed
  56. */
  57. public function actionEdit()
  58. {
  59. $request = Yii::$app->request;
  60. $id = $request->get('id', null);
  61. $model = $this->findFormModel($id);
  62. if ($model->load($request->post()) && $model->save()) {
  63. return $this->referrer();
  64. }
  65. return $this->render($this->action->id, [
  66. 'model' => $model,
  67. ]);
  68. }
  69. /**
  70. * 创建
  71. *
  72. * @param $title
  73. * @param $type
  74. * @return array|mixed
  75. */
  76. public function actionCreate($title, $type)
  77. {
  78. $model = new Spec();
  79. $model = $model->loadDefaultValues();
  80. $model->title = $title;
  81. $model->type = $type;
  82. $model->is_tmp = StatusEnum::ENABLED;
  83. $model->save();
  84. return ResultHelper::json(200, 'ok', $model);
  85. }
  86. /**
  87. * @return array|mixed
  88. */
  89. public function actionSearch($title)
  90. {
  91. $data = $this->modelClass::find()
  92. ->where(['status' => StatusEnum::ENABLED])
  93. ->andWhere(['like', 'title', $title])
  94. ->orderBy('sort asc')
  95. ->with(['value'])
  96. ->limit(8)
  97. ->asArray()
  98. ->all();
  99. return ResultHelper::json(200, 'ok', $data);
  100. }
  101. /**
  102. * 返回模型
  103. *
  104. * @param $id
  105. * @return \yii\db\ActiveRecord
  106. */
  107. protected function findFormModel($id)
  108. {
  109. /* @var $model \yii\db\ActiveRecord */
  110. if (empty($id) || empty(($model = SpecForm::findOne(['id' => $id, 'merchant_id' => $this->getMerchantId()])))) {
  111. $model = new SpecForm();
  112. return $model->loadDefaultValues();
  113. }
  114. return $model;
  115. }
  116. }
粤ICP备19079148号