251 lines
7.6 KiB
PHP
Executable File
251 lines
7.6 KiB
PHP
Executable File
<?php
|
|
|
|
/**
|
|
* @Author: fm453
|
|
* @Date: 2018-08-17 19:39:41
|
|
# @Last modified by: fm453
|
|
# @Last modified time: 2021-09-15T16:40:15+08:00
|
|
* @Email: fm453@lukegzs.com
|
|
*/
|
|
namespace backend\controllers;
|
|
|
|
use Yii;
|
|
use yii\data\Pagination;
|
|
use yii\helpers\Url;
|
|
use backend\models\FmHotel as Hotel;
|
|
|
|
class HotelController extends \yii\web\Controller
|
|
{
|
|
//替代常规的_construct 析构函数;其他方法调用前执行
|
|
public function init()
|
|
{
|
|
parent::init();
|
|
$session = Yii::$app->session;
|
|
if(isset($_GET['pid']) && (int)$_GET['pid']>0){
|
|
$session->set('pid',(int)$_GET['pid']);
|
|
}
|
|
$pid = $session->get('pid');
|
|
if(!$pid){
|
|
$url = Url::toRoute('/index/index');
|
|
return $this->redirect($url);
|
|
}
|
|
}
|
|
//主界面
|
|
public function actionIndex()
|
|
{
|
|
return $this->render('../layouts/dev',[]);
|
|
}
|
|
|
|
public function actionList()
|
|
{
|
|
$model = new Hotel();
|
|
$where = [];
|
|
$where['pid'] = Yii::$app->session->get('pid');
|
|
$citys = Yii::$app->params['citys'];
|
|
|
|
$where['deleted'] = 0;
|
|
$data = $model->find()->where($where);
|
|
$pages = new Pagination(['totalCount' =>$data->count(), 'pageSize' => '20']);
|
|
$res = $data->offset($pages->offset)->limit($pages->limit)->all();
|
|
|
|
$status = ['0'=>'隐藏','1'=>'正常'];
|
|
return $this->render('list',[
|
|
'hotels'=>$res,
|
|
'pager' => $pages,
|
|
'citys'=>$citys,
|
|
'status'=>$status
|
|
]);
|
|
}
|
|
|
|
public function actionSelect()
|
|
{
|
|
$model = new Hotel();
|
|
$where = [];
|
|
$where['pid'] = Yii::$app->session->get('pid');
|
|
$citys = Yii::$app->params['citys'];
|
|
|
|
$where['deleted'] = 0;
|
|
$where['status_code'] = 1;
|
|
$data = $model->find()->where($where);
|
|
$pages = new Pagination(['totalCount' =>$data->count(), 'pageSize' => '20']);
|
|
$res = $data->offset($pages->offset)->limit($pages->limit)->all();
|
|
$status = ['0'=>'隐藏','1'=>'正常'];
|
|
$callback = !empty(Yii::$app->session->get('callback')) ? Yii::$app->session->get('callback') : 'hotel';
|
|
return $this->render('select',[
|
|
'hotels'=>$res,
|
|
'pager' => $pages,
|
|
'citys'=>$citys,
|
|
'status'=>$status,
|
|
'callback'=>$callback
|
|
]);
|
|
}
|
|
|
|
public function actionNew()
|
|
{
|
|
return $this->render('modify',[]);
|
|
}
|
|
|
|
public function actionEdit()
|
|
{
|
|
$model = new Hotel();
|
|
$id = Yii::$app->request->get('id');
|
|
$res = $model->find()->where(['id' => $id])->one();
|
|
if($res){
|
|
$res = $res->toArray();
|
|
}
|
|
$citys = Yii::$app->params['citys'];
|
|
$res['city_title'] = isset($citys[$res['city']]['name']) ? $citys[$res['city']]['name'] : '';
|
|
|
|
return $this->render('modify',['detail'=>$res]);
|
|
}
|
|
|
|
public function actionSave()
|
|
{
|
|
$post = Yii::$app->request->post();
|
|
$data = [];
|
|
$data['pid'] = Yii::$app->session->get('pid');
|
|
$id = $post['id'];
|
|
//目标键=》POST键
|
|
$cols = ['title'=>'title','longt'=>'lng','lat'=>'lat','addr'=>'addr'];
|
|
foreach($cols as $col=>$key){
|
|
$data[$col] = isset($post[$key]) ? trim($post[$key]) : '';
|
|
}
|
|
$cols = ['city'=>'city','deleted'=>'deleted'];
|
|
foreach($cols as $col=>$key){
|
|
$data[$col] = isset($post[$key]) ? (int)$post[$key] : 0;
|
|
}
|
|
$data['update_at'] = time();
|
|
$data['create_at'] = time();
|
|
$data['status_code'] = 1;
|
|
|
|
$model = new Hotel();
|
|
foreach($data as $key=>$val){
|
|
$model->$key = $val;
|
|
}
|
|
if($id){
|
|
$model->id = $id;
|
|
$res = $model->updateAll($data,['id'=>$id]);
|
|
}else{
|
|
$res = $model->save();
|
|
}
|
|
|
|
$return = [];
|
|
if($res){
|
|
$id = $model->attributes['id']; //获取插入后id
|
|
$return['msg'] = '酒店保存成功';
|
|
$return['errorcode'] = 200;
|
|
$return['url'] = Url::toRoute(['hotel/edit','id'=>$id]);
|
|
$return['buttons'] = [
|
|
['title'=>'好的,我知道了','class'=>'primary','url'=>$return['url']]
|
|
];
|
|
}else{
|
|
$return['msg'] = '酒店保存失败';
|
|
$return['errorcode'] = 0;
|
|
$return['url'] = Url::toRoute(['hotel/new',$post]);
|
|
$return['buttons'] = [
|
|
['title'=>'好的,我知道了','class'=>'info','url'=>$return['url']]
|
|
];
|
|
}
|
|
|
|
$classes = ['0'=>'warning','200'=>'success'];
|
|
$return['content'] = $return['msg'];
|
|
$return['class'] = $classes[$return['errorcode']];
|
|
|
|
Yii::$app->request->setBodyParams($return);
|
|
return Yii::$app->runAction('index/msg');
|
|
}
|
|
|
|
public function actionDelete()
|
|
{
|
|
$get = Yii::$app->request->get();
|
|
$post = Yii::$app->request->post();
|
|
$id = (int)$get['id'];
|
|
$data = $where = [];
|
|
$where['pid'] = Yii::$app->session->get('pid');
|
|
$where['id'] = $id;
|
|
$data['update_at'] = time();
|
|
$data['deleted'] = 1;
|
|
|
|
$model = new Hotel();
|
|
foreach($data as $key=>$val){
|
|
$model->$key = $val;
|
|
}
|
|
|
|
$model->id = $id;
|
|
$res = $model->updateAll($data,$where);
|
|
|
|
$return = [];
|
|
$return['url'] = Url::toRoute('hotel/list');
|
|
$return['timeout'] = 3; //3秒后自动跳转
|
|
$return['status'] = 0;
|
|
if($res){
|
|
$return['msg'] = '酒店删除成功';
|
|
$return['errorcode'] = 200;
|
|
$return['data'] = $id;
|
|
}else{
|
|
$return['msg'] = '酒店删除失败';
|
|
$return['errorcode'] = 0;
|
|
}
|
|
exit(json_encode($return));
|
|
}
|
|
|
|
public function actionAjax()
|
|
{
|
|
$get = Yii::$app->request->get();
|
|
$post = Yii::$app->request->post();
|
|
$id = (int)$get['id'];
|
|
$data = $where = [];
|
|
$where['pid'] = Yii::$app->session->get('pid');
|
|
$where['id'] = $id;
|
|
$data['update_at'] = time();
|
|
|
|
$model = new Hotel();
|
|
$res = $model->find()->where(['id' => $id])->one();
|
|
$return = [];
|
|
$return['timeout'] = 1; //3秒后自动跳转
|
|
$return['status'] = 0;
|
|
$return['ajax'] = 1;
|
|
if($res){
|
|
$res = $res->toArray();
|
|
}else{
|
|
$return['msg'] = '酒店数据不存在';
|
|
$return['errorcode'] = 404;
|
|
exit(json_encode($return));
|
|
}
|
|
|
|
switch($get['do'])
|
|
{
|
|
case 'hide':
|
|
$data['status_code'] = 0;
|
|
$model->id = $id;
|
|
$res = $model->updateAll($data,$where);
|
|
if($res){
|
|
$return['msg'] = '酒店隐藏成功';
|
|
$return['errorcode'] = 200;
|
|
$return['data'] = ['css'=>'info'];
|
|
}else{
|
|
$return['msg'] = '酒店隐藏失败';
|
|
$return['errorcode'] = 0;
|
|
}
|
|
break;
|
|
case 'show':
|
|
$data['status_code'] = 1;
|
|
$model->id = $id;
|
|
$res = $model->updateAll($data,$where);
|
|
if($res){
|
|
$return['msg'] = '酒店显示成功';
|
|
$return['errorcode'] = 200;
|
|
$return['data'] = ['css'=>'default'];
|
|
}else{
|
|
$return['msg'] = '酒店显示失败';
|
|
$return['errorcode'] = 0;
|
|
}
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
exit(json_encode($return));
|
|
}
|
|
|
|
}
|