AttachmentController.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace backend\modules\common\controllers;
  3. use Yii;
  4. use common\traits\MerchantCurd;
  5. use common\models\base\SearchModel;
  6. use common\models\common\Attachment;
  7. use common\enums\StatusEnum;
  8. use common\enums\AttachmentUploadTypeEnum;
  9. use backend\controllers\BaseController;
  10. /**
  11. * Class AttachmentController
  12. * @package backend\modules\common\controllers
  13. * @author jianyan74 <751393839@qq.com>
  14. */
  15. class AttachmentController extends BaseController
  16. {
  17. use MerchantCurd;
  18. /**
  19. * @var Attachment
  20. */
  21. public $modelClass = Attachment::class;
  22. /**
  23. * 首页
  24. *
  25. * @return string
  26. * @throws \yii\web\NotFoundHttpException
  27. */
  28. public function actionIndex()
  29. {
  30. $type = Yii::$app->request->get('type', AttachmentUploadTypeEnum::IMAGES);
  31. $searchModel = new SearchModel([
  32. 'model' => $this->modelClass,
  33. 'scenario' => 'default',
  34. 'partialMatchAttributes' => ['title'], // 模糊查询
  35. 'defaultOrder' => [
  36. 'id' => SORT_DESC,
  37. ],
  38. 'pageSize' => $this->pageSize,
  39. ]);
  40. $dataProvider = $searchModel
  41. ->search(Yii::$app->request->queryParams);
  42. $dataProvider->query
  43. ->andWhere(['upload_type' => $type])
  44. ->andWhere(['>=', 'status', StatusEnum::DISABLED])
  45. ->andFilterWhere(['merchant_id' => $this->getMerchantId()]);
  46. return $this->render($this->action->id, [
  47. 'dataProvider' => $dataProvider,
  48. 'searchModel' => $searchModel,
  49. 'cateMap' => Yii::$app->services->attachmentCate->getMap($type),
  50. 'type' => $type,
  51. ]);
  52. }
  53. /**
  54. * @return mixed|string|\yii\web\Response
  55. * @throws \yii\base\ExitException
  56. */
  57. public function actionUpdate()
  58. {
  59. $id = Yii::$app->request->get('id');
  60. $type = Yii::$app->request->get('type');
  61. $model = $this->findModel($id);
  62. // ajax 校验
  63. $this->activeFormValidate($model);
  64. if ($model->load(Yii::$app->request->post())) {
  65. return $model->save()
  66. ? $this->redirect(['index', 'type' => $type])
  67. : $this->message($this->getError($model), $this->redirect(['index', 'type' => $type]), 'error');
  68. }
  69. return $this->renderAjax($this->action->id, [
  70. 'model' => $model,
  71. 'cateMap' => Yii::$app->services->attachmentCate->getMap($type),
  72. ]);
  73. }
  74. /**
  75. * 返回模型
  76. *
  77. * @param $id
  78. * @return \yii\db\ActiveRecord
  79. */
  80. protected function findModel($id)
  81. {
  82. /* @var $model \yii\db\ActiveRecord */
  83. if (
  84. empty($id) || empty(
  85. ($model = $this->modelClass::find()
  86. ->where(['id' => $id])
  87. ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
  88. ->one()))
  89. ) {
  90. $model = new $this->modelClass;
  91. return $model->loadDefaultValues();
  92. }
  93. return $model;
  94. }
  95. }
粤ICP备19079148号