AttachmentCateController.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace backend\modules\common\controllers;
  3. use Yii;
  4. use yii\data\ActiveDataProvider;
  5. use common\models\common\AttachmentCate;
  6. use common\enums\AttachmentUploadTypeEnum;
  7. use backend\controllers\BaseController;
  8. use common\traits\MerchantCurd;
  9. /**
  10. * Class AttachmentCateController
  11. * @package backend\modules\common\controllers
  12. * @author jianyan74 <751393839@qq.com>
  13. */
  14. class AttachmentCateController extends BaseController
  15. {
  16. use MerchantCurd;
  17. /**
  18. * @var AttachmentCate
  19. */
  20. public $modelClass = AttachmentCate::class;
  21. /**
  22. * Lists all Tree models.
  23. * @return mixed
  24. */
  25. public function actionIndex()
  26. {
  27. $type = Yii::$app->request->get('type', AttachmentUploadTypeEnum::IMAGES);
  28. $dataProvider = new ActiveDataProvider([
  29. 'query' => $this->modelClass::find()
  30. ->where(['type' => $type])
  31. ->andWhere([
  32. 'merchant_id' => Yii::$app->services->merchant->getNotNullId(),
  33. 'store_id' => Yii::$app->services->store->getNotNullId(),
  34. ])
  35. ->orderBy('sort asc, created_at asc'),
  36. 'pagination' => false
  37. ]);
  38. return $this->render('index', [
  39. 'dataProvider' => $dataProvider,
  40. 'type' => $type,
  41. ]);
  42. }
  43. /**
  44. * 编辑/创建
  45. *
  46. * @return mixed|string|\yii\web\Response
  47. * @throws \yii\base\ExitException
  48. */
  49. public function actionAjaxEdit()
  50. {
  51. $id = Yii::$app->request->get('id', '');
  52. $type = Yii::$app->request->get('type');
  53. $model = $this->findModel($id);
  54. $model->pid = Yii::$app->request->get('pid', null) ?? $model->pid; // 父id
  55. // ajax 校验
  56. $this->activeFormValidate($model);
  57. if ($model->load(Yii::$app->request->post())) {
  58. $model->type = $type;
  59. return $model->save()
  60. ? $this->redirect(['index', 'type' => $type])
  61. : $this->message($this->getError($model), $this->redirect(['index', 'type' => $type]), 'error');
  62. }
  63. return $this->renderAjax('ajax-edit', [
  64. 'model' => $model,
  65. ]);
  66. }
  67. }
粤ICP备19079148号