448 lines
14 KiB
PHP
448 lines
14 KiB
PHP
<?php
|
||
|
||
# @Author: fm453
|
||
# @Date: 2024/7/16
|
||
# @updated: 下午3:55
|
||
# @Email: 1280880631@qq.com
|
||
|
||
namespace backend\controllers;
|
||
|
||
use Yii;
|
||
use addons\models\AcNewsCat;
|
||
|
||
class NewscatController extends Common{
|
||
public $enableCsrfValidation = false;
|
||
public function actionIndex(){}
|
||
|
||
public function actionList()
|
||
{
|
||
$options = $this->_tableOptions();
|
||
$columns = $options['columns'];
|
||
$operator = $options['operator'];
|
||
|
||
$tpl = 'list';
|
||
|
||
return $this->render($tpl, [
|
||
'columns' => json_encode($columns,JSON_UNESCAPED_UNICODE),
|
||
'operator' => json_encode($operator,JSON_UNESCAPED_UNICODE)
|
||
]);
|
||
}
|
||
|
||
private function _tableOptions(){
|
||
$columns = [];
|
||
$columns['id'] = [
|
||
'field'=>'id',
|
||
'title'=>'ID',
|
||
'align'=>'center',
|
||
'vlign'=>'middle',
|
||
'halign'=>'center',
|
||
'width'=>30,
|
||
'widthUnit'=>'px',
|
||
'footerFormatter'=>'CountTotalFooter',
|
||
'sortable'=>true,
|
||
'searchable'=>false
|
||
];
|
||
$columns['pcat_id'] = [
|
||
'field'=>'pcat_id',
|
||
'title'=>'上级分类',
|
||
'align'=>'center',
|
||
'vlign'=>'middle',
|
||
'halign'=>'center',
|
||
'width'=>120,
|
||
'widthUnit'=>'px',
|
||
'class'=>"resize_able",
|
||
'formatter'=>'catFormatter',
|
||
'filterControl'=>"select",
|
||
'sortable'=>true,
|
||
'searchable'=>false
|
||
];
|
||
$columns['pcat_title'] = [
|
||
'field'=>'pcat_title',
|
||
'title'=>'上级分类',
|
||
'align'=>'center',
|
||
'vlign'=>'middle',
|
||
'halign'=>'center',
|
||
'width'=>150,
|
||
'widthUnit'=>'px',
|
||
'class'=>"resize_able",
|
||
'filterControl'=>"select",
|
||
'visible'=>false,
|
||
'sortable'=>true,
|
||
'searchable'=>false
|
||
];
|
||
$columns['title'] = [
|
||
'field'=>'title',
|
||
'title'=>'标题',
|
||
'align'=>'center',
|
||
'vlign'=>'middle',
|
||
'halign'=>'center',
|
||
// 'width'=>120,
|
||
// 'widthUnit'=>'px',
|
||
'class'=>"resize_able",
|
||
'cellStyle'=>'cellStyle',
|
||
'editable'=>'textEditor',
|
||
//该列可编辑
|
||
'formatter'=>'textFormatter',
|
||
// 'filterControl'=>"input",
|
||
'searchFormatter'=>true,
|
||
'searchHighlightFormatter'=>'customSearchFmt',
|
||
'searchable'=>true
|
||
];
|
||
$columns['des'] = [
|
||
'field'=>'des',
|
||
'title'=>'简述',
|
||
'align'=>'center',
|
||
'vlign'=>'middle',
|
||
'halign'=>'center',
|
||
// 'width'=>120,
|
||
// 'widthUnit'=>'px',
|
||
'titleTooltip'=>'英文简写的代码,后期用于权限判断等场景',
|
||
'class'=>"resize_able",
|
||
'cellStyle'=>'cellStyle',
|
||
'editable'=>'textEditor',
|
||
'formatter'=>'textFormatter',
|
||
// 'filterControl'=>"select",
|
||
'searchFormatter'=>true,
|
||
'searchHighlightFormatter'=>'customSearchFmt',
|
||
'searchable'=>true
|
||
];
|
||
$columns['orderby'] = [
|
||
'field'=>'orderby',
|
||
'title'=>'排序',
|
||
'width'=>40,
|
||
'widthUnit'=>'px',
|
||
'editable'=>'textEditor',
|
||
'formatter'=>'numFormatter',
|
||
'sortable'=>true,
|
||
'searchable'=>false
|
||
];
|
||
$columns['create_at'] = [
|
||
'field'=>'create_at',
|
||
'title'=>'创建日期',
|
||
'formatter'=>'dateFormatter',
|
||
'sortable'=>true,
|
||
'searchable'=>false
|
||
];
|
||
$columns['update_at'] = [
|
||
'field'=>'update_at',
|
||
'title'=>'更新时间',
|
||
'formatter'=>'timeFormatter',
|
||
'sortable'=>true,
|
||
'searchable'=>false
|
||
];
|
||
|
||
$operator = [];
|
||
$operator['showDetail'] = [
|
||
'class'=>'view',
|
||
'title'=>'概览',
|
||
'icon'=>'eye',
|
||
'func'=>false
|
||
];
|
||
$operator['save'] = [
|
||
'class'=>'save',
|
||
'title'=>'保存',
|
||
'icon'=>'save',
|
||
'func'=>false
|
||
];
|
||
$operator['remove'] = [
|
||
'class'=>'remove',
|
||
'title'=>'移除',
|
||
'icon'=>'trash',
|
||
'func'=>false
|
||
];
|
||
|
||
return [
|
||
'columns' => $columns,
|
||
'operator' => $operator
|
||
];
|
||
}
|
||
|
||
public function actionSelect()
|
||
{
|
||
/**
|
||
* 选择分类
|
||
* @id [int] 来源页的元素数据唯一标识ID
|
||
* @cid [int] 来源页的传入的已迁选中的分类ID
|
||
* @callback [string] 来源页的元素标签标识
|
||
*/
|
||
$pid = $this->pid;
|
||
$id = Yii::$app->request->get('id');
|
||
$callback = !empty(Yii::$app->request->get('callback')) ? Yii::$app->request->get('callback') : 'newsCat';
|
||
if($id) $callback .= '-'.$id;
|
||
|
||
$cid = Yii::$app->request->get('cid');
|
||
|
||
$options = $this->_tableOptions();
|
||
$columns = $options['columns'];
|
||
unset($columns['pcat_title']);
|
||
$operator = $options['operator'];
|
||
$operator['select'] = [
|
||
'class'=>'select',
|
||
'title'=>'选择',
|
||
'icon'=>'check-square-o',
|
||
'func'=>false
|
||
];
|
||
unset($operator['remove']);
|
||
|
||
//不使用模板框架
|
||
// $this->layout = '@app/views/layouts/table';
|
||
$tpl = 'select';
|
||
return $this->render($tpl, [
|
||
'id' => $id,
|
||
'selected_id'=>$cid,
|
||
'columns' => json_encode($columns,JSON_UNESCAPED_UNICODE),
|
||
'operator' => json_encode($operator,JSON_UNESCAPED_UNICODE),
|
||
'callback'=>$callback
|
||
]);
|
||
}
|
||
|
||
public function actionPcat(){
|
||
/**
|
||
* 选择上级分类
|
||
* @id [int] 当前来源子类ID
|
||
*/
|
||
$pid = $this->pid;
|
||
$id = Yii::$app->request->get('id');
|
||
$model = new AcNewsCat();
|
||
$cat = $model->findOne($id);
|
||
$pcat_id = 0;
|
||
if($cat->pcat_id) $pcat_id = $cat->pcat_id;
|
||
|
||
$callback = !empty(Yii::$app->request->get('callback')) ? Yii::$app->request->get('callback') : 'newsCat';
|
||
if($id) $callback .= '-'.$id;
|
||
|
||
$options = $this->_tableOptions();
|
||
$columns = $options['columns'];
|
||
unset($columns['pcat_title']);
|
||
$operator = $options['operator'];
|
||
$operator['select'] = [
|
||
'class'=>'select',
|
||
'title'=>'选择',
|
||
'icon'=>'check-square-o',
|
||
'func'=>false
|
||
];
|
||
unset($operator['remove']);
|
||
|
||
//不使用模板框架
|
||
// $this->layout = '@app/views/layouts/table';
|
||
$tpl = 'pcat';
|
||
return $this->render($tpl, [
|
||
'id' => $id,
|
||
'selected_id'=>$pcat_id,
|
||
'columns' => json_encode($columns,JSON_UNESCAPED_UNICODE),
|
||
'operator' => json_encode($operator,JSON_UNESCAPED_UNICODE),
|
||
'callback'=>$callback
|
||
]);
|
||
}
|
||
|
||
public function actionJson(){
|
||
$res = $this->Search();
|
||
$list = $res['list'];
|
||
$ids = $res['ids'];
|
||
$totalCount = $res['total'];
|
||
$pcats = $res['pcats'];
|
||
$cats = $res['cats'];
|
||
$newList = [];
|
||
foreach($ids as $id){
|
||
$row = $list[$id];
|
||
if($row['pcat_id']){
|
||
$row['pcat_title'] = $list[$row['pcat_id']]['title'];
|
||
}
|
||
$newList[] = $row;
|
||
}
|
||
|
||
//客户端表格类型 bootstrapTable 或 dataTable;
|
||
// $table_type = "dataTable";
|
||
$table_type = "bootstrapTable";
|
||
$data = [];
|
||
if($table_type=="dataTable"){
|
||
$data['draw'] = 1; //客户端表是否需要重绘
|
||
$data['recordsTotal'] = $totalCount;
|
||
$data['recordsFiltered'] = $totalCount;
|
||
$data['pcats'] = $pcats;
|
||
$data['cats'] = $cats;
|
||
$data['data'] = $newList;
|
||
} elseif($table_type=="bootstrapTable"){
|
||
$data['total'] = $totalCount;
|
||
$data['totalNotFiltered'] = $totalCount;
|
||
$data['pcats'] = $pcats;
|
||
$data['cats'] = $cats;
|
||
$data['rows'] = $newList;
|
||
}
|
||
|
||
header('Content-Type:application/json');
|
||
exit(json_encode($data,JSON_UNESCAPED_UNICODE)); //注意不要加强制object参数
|
||
}
|
||
|
||
private function search(){
|
||
$pid = $this->pid;
|
||
$session = Yii::$app->session;
|
||
$model = new AcNewsCat();
|
||
$where = [];
|
||
$where[]='and';
|
||
$where[] = ['=','pid',$pid];
|
||
$post = $this->postdata;
|
||
$get = Yii::$app->request->get();
|
||
$searchSession = 'newscat::search';
|
||
if (isset($_GET['reset']) && $_GET['reset']==1) {
|
||
$post['search'] = [];
|
||
}
|
||
if (isset($post['search'])) {
|
||
$search = $post['search'];
|
||
$session->set($searchSession, $search);
|
||
} elseif ($session->get($searchSession)) {
|
||
$search = $session->get($searchSession);
|
||
}
|
||
|
||
$search['title'] = isset($search['title']) ? trim($search['title']) : '';
|
||
if (!empty($search['title'])) {
|
||
$where[] = ['LIKE','title',$search['title']];
|
||
}
|
||
|
||
$deleted = 0;
|
||
if (isset($_GET['deleted'])) {
|
||
$deleted = isset($_GET['deleted']) ? ((int)$_GET['deleted']>=0 ? 1 : 0) : 0;
|
||
}
|
||
$where[]=['=','deleted',$deleted];
|
||
|
||
$data = $model->find()->where($where);
|
||
$totalCount = $data->count();
|
||
$_orderby = 'orderby DESC';
|
||
$res = $data->orderby($_orderby)->all();
|
||
|
||
$list = $cids = $pcids = [];
|
||
$cats = $pcats = [];
|
||
if ($res) {
|
||
foreach ($res as $r) {
|
||
if($r->pcat_id){
|
||
//重排数组,保留查询结果的排序
|
||
$pcids[] = $r->pcat_id; //父类
|
||
$cids[$r->pcat_id][] = $r->id; //子分类,按父类分组
|
||
}else{
|
||
$cids['0'][] = $r->id; //无父类的
|
||
}
|
||
$ids[] = $r->id;
|
||
$list[$r->id] = $r->toArray(); //内容按ID列出,以便后用
|
||
}
|
||
}
|
||
|
||
if($pcids){
|
||
foreach ($pcids as $pcid) {
|
||
$pcats[] = $list[$pcid];
|
||
foreach($cids[$pcid] as $k=>$id){
|
||
$cats[$pcid][] = $list[$id];
|
||
}
|
||
}
|
||
foreach($cids as $pcid=>$ccids){
|
||
foreach($ccids as $k=>$id){
|
||
$cats[$pcid][] = $list[$id];
|
||
}
|
||
}
|
||
}
|
||
|
||
return ['ids'=>$ids,'list'=>$list,'total'=>$totalCount,'pcats'=>$pcats,'cats'=>$cats];
|
||
}
|
||
|
||
public function actionSave(){
|
||
$post = $this->postdata;
|
||
$id = isset($post['id']) ? (int)$post['id'] : 0;
|
||
$pid = Yii::$app->session->get('pid');
|
||
//格式化数据
|
||
//目标键=》POST键
|
||
$cols = ['title'=>'title','des'=>'des'];
|
||
foreach($cols as $col=>$key){
|
||
if(isset($post[$key])){
|
||
$data[$col] = trim($post[$key]);
|
||
}elseif(!$id){
|
||
$data[$col] = '';
|
||
}
|
||
}
|
||
|
||
$cols = ['orderby'=>'orderby','deleted'=>'deleted','pcat_id'=>'pcat_id'];
|
||
foreach($cols as $col=>$key){
|
||
$data[$col] = isset($post[$key]) ? (int)$post[$key] : 0;
|
||
}
|
||
|
||
$cols = ['is_show'=>'is_show'];
|
||
foreach($cols as $col=>$key){
|
||
if(!$id){
|
||
$data[$col] = isset($post[$key]) ? (int)$post[$key] : 1;
|
||
}elseif(isset($post[$col])){
|
||
$data[$col] = (int)$post[$key];
|
||
}
|
||
}
|
||
if(!$id){
|
||
$data['create_at'] = time();
|
||
}
|
||
$data['update_at'] = time();
|
||
$data['pid'] = $pid;
|
||
|
||
//保存
|
||
$msg = '数据保存失败';
|
||
$res = false;
|
||
$cpde = 0;
|
||
$model = new AcNewsCat();
|
||
if($id){
|
||
$_rec = $model::findOne($id);
|
||
if($_rec){
|
||
foreach($data as $key=>$val){
|
||
$_rec->$key = $val;
|
||
}
|
||
$_rec->id = $id;
|
||
$res = $_rec->save();
|
||
if($res){
|
||
$msg = '数据保存成功';
|
||
$code = 200;
|
||
}
|
||
}
|
||
}else{
|
||
foreach($data as $key=>$val){
|
||
$model->$key = $val;
|
||
}
|
||
$res = $model->save();
|
||
if($res){
|
||
$msg = '数据新增成功';
|
||
$id = $model->attributes['id']; //获取插入后id
|
||
$code = 200;
|
||
}
|
||
}
|
||
$data['id'] = $id;
|
||
|
||
$this->result($msg,$data,200);
|
||
}
|
||
|
||
public function actionAjax(){
|
||
$op = $_GET['op']; //操作标识,只允许客户端GET传入
|
||
$post = $this->postdata;
|
||
$msg = '删除失败';
|
||
if(!isset($post['id'])){
|
||
$this->result($msg,$post,200);
|
||
}
|
||
$id = $post['id'];
|
||
$model = new AcNewsCat();
|
||
$data = $model->findOne($id);
|
||
if(!$data){
|
||
$msg = '数据不存在,操作失败';
|
||
$this->result($msg,$post,200);
|
||
}
|
||
if($data->pid != $this->pid){
|
||
$msg = '归属平台不匹配,禁止操作';
|
||
$this->result($msg,$post,200);
|
||
}
|
||
switch($op){
|
||
case 'del':
|
||
if($data->deleted >0){
|
||
$msg = '数据已被删除过,操作无效';
|
||
$this->result($msg,$post,200);
|
||
}
|
||
$data->deleted += 1;
|
||
$_res = $data->save();
|
||
$msg = $_res ? '删除成功' : '操作失败,数据更新没有成功!';
|
||
$res = $data;
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
$this->result($msg,$res,200);
|
||
}
|
||
} |