| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- <?php
- namespace common\traits;
- use Yii;
- use yii\data\Pagination;
- use yii\base\InvalidConfigException;
- use common\helpers\ResultHelper;
- use common\enums\StatusEnum;
- use common\helpers\ArrayHelper;
- /**
- * Trait Curd
- * @property \yii\db\ActiveRecord|\yii\base\Model $modelClass
- * @package common\traits
- */
- trait MerchantCurd
- {
- /**
- * @throws InvalidConfigException
- * @throws \yii\base\ErrorException
- */
- public function init()
- {
- parent::init();
- if ($this->modelClass === null) {
- throw new InvalidConfigException('"modelClass" 属性必须设置.');
- }
- }
- /**
- * 首页
- *
- * @return mixed
- */
- public function actionIndex()
- {
- $data = $this->modelClass::find()
- ->where(['>=', 'status', StatusEnum::DISABLED])
- ->andWhere(['merchant_id' => $this->getMerchantId()]);
- $pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => $this->pageSize]);
- $models = $data->offset($pages->offset)
- ->orderBy('id desc')
- ->limit($pages->limit)
- ->all();
- return $this->render($this->action->id, [
- 'models' => $models,
- 'pages' => $pages
- ]);
- }
- /**
- * 编辑/创建
- *
- * @return mixed
- */
- public function actionEdit()
- {
- $id = Yii::$app->request->get('id', null);
- $model = $this->findModel($id);
- if ($model->load(Yii::$app->request->post()) && $model->save()) {
- return $this->referrer();
- }
- return $this->render($this->action->id, [
- 'model' => $model,
- ]);
- }
- /**
- * 伪删除
- *
- * @param $id
- * @return mixed
- */
- public function actionDestroy($id)
- {
- if (!($model = $this->modelClass::findOne($id))) {
- return $this->message("找不到数据", $this->redirect(Yii::$app->request->referrer), 'error');
- }
- $model->status = StatusEnum::DELETE;
- if ($model->save()) {
- return $this->message("删除成功", $this->redirect(Yii::$app->request->referrer));
- }
- return $this->message("删除失败", $this->redirect(Yii::$app->request->referrer), 'error');
- }
- /**
- * 删除
- *
- * @param $id
- * @return mixed
- * @throws \Throwable
- * @throws \yii\db\StaleObjectException
- */
- public function actionDelete($id)
- {
- if ($this->findModel($id)->delete()) {
- return $this->message("删除成功", $this->redirect(Yii::$app->request->referrer));
- }
- return $this->message("删除失败", $this->redirect(Yii::$app->request->referrer), 'error');
- }
- /**
- * ajax更新排序/状态
- *
- * @param $id
- * @return array
- */
- public function actionAjaxUpdate($id)
- {
- if (!($model = $this->modelClass::findOne($id))) {
- return ResultHelper::json(404, '找不到数据');
- }
- $model->attributes = ArrayHelper::filter(Yii::$app->request->get(), ['sort', 'status']);
- if (!$model->save()) {
- return ResultHelper::json(422, $this->getError($model));
- }
- return ResultHelper::json(200, '修改成功');
- }
- /**
- * ajax编辑/创建
- *
- * @return mixed|string|\yii\web\Response
- * @throws \yii\base\ExitException
- */
- public function actionAjaxEdit()
- {
- $id = Yii::$app->request->get('id');
- $model = $this->findModel($id);
- // ajax 校验
- $this->activeFormValidate($model);
- if ($model->load(Yii::$app->request->post())) {
- return $model->save()
- ? $this->redirect(Yii::$app->request->referrer)
- : $this->message($this->getError($model), $this->redirect(Yii::$app->request->referrer), 'error');
- }
- return $this->renderAjax($this->action->id, [
- 'model' => $model,
- ]);
- }
- /**
- * 返回模型
- *
- * @param $id
- * @return \yii\db\ActiveRecord
- */
- protected function findModel($id)
- {
- /* @var $model \yii\db\ActiveRecord */
- if (empty($id) || empty($model = $this->modelClass::find()
- ->where(['id' => $id])
- ->andFilterWhere(['merchant_id' => $this->getMerchantId()])
- ->one())
- ) {
- $model = new $this->modelClass;
- return $model->loadDefaultValues();
- }
- return $model;
- }
- }
|