AreaController.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace backend\controllers;
  3. use common\models\Area;
  4. use yii;
  5. use yii\filters\AccessControl;
  6. /**
  7. * 城市管理
  8. * @package backend\controllers
  9. */
  10. class AreaController extends BaseController
  11. {
  12. public $layout = 'iframe';
  13. const PAGESIZE = 20;//分页数
  14. public function behaviors()
  15. {
  16. return [
  17. 'access' => [
  18. 'class' => AccessControl::className(),
  19. 'rules' => [
  20. [
  21. 'actions' => [],
  22. 'allow' => true,
  23. 'roles' => ['@'],
  24. ],
  25. ],
  26. ],
  27. ];
  28. }
  29. /**
  30. * 列表
  31. * @return string
  32. */
  33. public function actionIndex()
  34. {
  35. $search = Yii::$app->request->get();
  36. $models = Area::find()->where(['type'=>2]);
  37. if(!empty($search['province']))
  38. $models->andWhere("father_area_id = :father_area_id",[':father_area_id'=>$search['province']]);
  39. if(!empty($search['display']))
  40. $models->andWhere("display = :display",[':display'=>$search['display']]);
  41. if(!empty($search['name']))
  42. $models->andWhere("area like :name",[':name'=>"%".$search['name']."%"]);
  43. $pages = new yii\data\Pagination(["totalCount" => $models->count(), "pageSize" => self::PAGESIZE]);
  44. $models = $models->offset($pages->offset)->limit($pages->limit)->orderBy("ishot DESC")->all();
  45. return $this->render('index', ['models' => $models, 'pages' => $pages,'name'=>'城市管理','search'=>$search]);
  46. }
  47. /**
  48. * 更新状态
  49. * @return string
  50. */
  51. public function actionUpdateStatus()
  52. {
  53. $id = Yii::$app->request->post('id');
  54. $model = Area::find()->where(['id'=>$id])->one();
  55. if($model->display==Area::DISPLAY_YES)
  56. $model->display = Area::DISPLAY_NO;
  57. else
  58. $model->display = Area::DISPLAY_YES;
  59. if($model->save())
  60. $result = ['sign'=>1,'msg'=>'修改状态成功'];
  61. else
  62. $result = ['sign'=>0,'msg'=>'修改状态失败'];
  63. return json_encode($result);
  64. }
  65. }