ctms/ctms-admin/controllers/PlatController.php
2025-04-10 23:19:13 +08:00

261 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-11 07:41:49
* @Email: fm453@lukegzs.com
*/
namespace backend\controllers;
use Yii;
use yii\data\Pagination;
use yii\helpers\Url;
use addons\models\AcPlat;
class PlatController 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']);
}
}
//主界面
public function actionIndex()
{
return $this->render('../layouts/dev',[]);
}
public function actionList()
{
$model = new AcPlat();
$search = $where = [];
$where[] = 'and';
if(isset($_POST['title']) && $_POST['title']!=''){
$search['title'] = $_POST['title'];
$where[] = ['LIKE','title',$search['title']];
}
$where[] = ['=','deleted',0];
$data = $model->find()->where($where);
$pages = new Pagination(['totalCount' =>$data->count(), 'pageSize' => '5']);
$res = $data->orderby('orderby DESC')->offset($pages->offset)->limit($pages->limit)->all();
$status = Yii::$app->params['CommonStatus'];
$plats = [];
foreach($res as $r){
$plats[$r->id] = $r->toArray();
}
return $this->render('list',[
'plats'=>$plats,
'pager' => $pages,
'search'=>$search,
'status'=>$status
]);
}
public function actionNew()
{
return $this->render('modify',[]);
}
public function actionEdit()
{
$model = new AcPlat();
$id = Yii::$app->request->get('id');
$res = $model->findOne($id);
if($res){
$res = $res->toArray();
}
return $this->render('modify',['detail'=>$res]);
}
public function actionSave()
{
$post = Yii::$app->request->post();
$data = [];
$id = $post['id'];
//目标键=》POST键
$cols = ['title'=>'title','thumb'=>'thumb'];
foreach($cols as $col=>$key){
$data[$col] = isset($post[$key]) ? trim($post[$key]) : '';
}
$cols = ['orderby'=>'orderby','deleted'=>'deleted'];
foreach($cols as $col=>$key){
$data[$col] = isset($post[$key]) ? (int)$post[$key] : 0;
}
$data['update_at'] = time();
$data['create_at'] = $data['update_at'];
$model = new AcPlat();
foreach($data as $key=>$val){
$model->$key = $val;
}
if($id){
$model->id = $id;
$res = $model->updateAll($data,['id'=>$id]);
}else{
$res = $model->save();
$id = $model->attributes['id']; //获取插入后id
}
if($res){
\Yii::$app->getSession()->setFlash('success', '平台保存成功!');
return $this->redirect(['edit','id'=>$id]);
}else{
\Yii::$app->getSession()->setFlash('warning', '平台保存失败!');
return $this->goBack();
}
}
public function actionExt()
{
$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','pid'=>0]);
header('location:'.$url);
exit;
}
$model = new AcPlat();
$res = $model->findOne($pid);
$detail = [];
if($res){
$detail = json_decode($res->remark,true);
}
return $this->render('ext',['detail'=>$detail]);
}
public function actionExtsave()
{
$user_id=Yii::$app->user->identity->getId();
if($user_id !=1){
\Yii::$app->getSession()->setFlash('warning', '修改失败,平台配置仅支持超级管理员修改!');
return $this->redirect(['plat/ext']);
}
$post = Yii::$app->request->post();
$session = Yii::$app->session;
$pid = $session->get('pid');
$data = [];
//目标键=》POST键
$cols = ['cw_phone'=>'cw_phone'];
foreach($cols as $col=>$key){
$data[$col] = isset($post[$key]) ? trim($post[$key]) : '';
}
$remark = json_encode($data,JSON_UNESCAPED_UNICODE);
$model = new AcPlat();
$model->remark = $remark;
$res = $model->updateAll(['remark'=>$remark],['id'=>$pid]);
if($res){
\Yii::$app->getSession()->setFlash('success', '参数保存成功!');
}else{
\Yii::$app->getSession()->setFlash('warning', '参数保存失败!');
}
return $this->redirect(['plat/ext']);
}
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 AcPlat();
foreach($data as $key=>$val){
$model->$key = $val;
}
$model->id = $id;
$res = $model->updateAll($data,$where);
$return = [];
$return['url'] = Url::toRoute('plat/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['id'] = $id;
$data['update_at'] = time();
$model = new AcPlat();
$res = $model->findOne($id);
$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));
}
}