118 lines
3.3 KiB
PHP
Executable File
118 lines
3.3 KiB
PHP
Executable File
<?php
|
||
|
||
# @Author: 嗨噜客(三亚) <fm453>
|
||
# @Date: 2024-08-03T16:38:33+08:00
|
||
# @Email: 1280880631@qq.com
|
||
# @Last modified by: fm453
|
||
# @Last modified time: 2024-08-09T08:59:58+08:00
|
||
# @Copyright: www.hiluker.cn
|
||
|
||
namespace api\controllers\client\v1;
|
||
|
||
use addons\models\AcNewsCat;
|
||
use yii\data\Pagination;
|
||
|
||
class NewscatController extends Common
|
||
{
|
||
public function beforeAction($action)
|
||
{
|
||
if (!$this->pid) {
|
||
$this->result('您正使用本系统内部接口,禁止非法链接使用!');
|
||
}
|
||
return parent::beforeAction($action);
|
||
}
|
||
|
||
public function actionIndex()
|
||
{
|
||
$apis = [
|
||
'list'=>'分类清单'
|
||
];
|
||
$this->result('您正使用CMTS-CLIENT系统内容分类管理接口!', $apis, 200);
|
||
}
|
||
|
||
//列表
|
||
public function actionList()
|
||
{
|
||
$s = $this->search(['is_show' => 1]);
|
||
$res = $s['res'];
|
||
if (!$res) {
|
||
$this->result('没有查询到相应的数据!', [], 0);
|
||
}
|
||
$list = $this->formatList($res);
|
||
|
||
$return = [
|
||
'cats' => $list
|
||
];
|
||
$this->result('通知查询成功!', $return, 200);
|
||
}
|
||
|
||
private function search($ops=[])
|
||
{
|
||
$pid = $this->pid;
|
||
$model = new AcNewsCat();
|
||
$where = 'pid = :pid';
|
||
$params = [':pid'=>$pid];
|
||
$post = $this->postdata;
|
||
$search = isset($post['search']) ? $post['search'] : [];
|
||
// 优先处理$ops传参,并同时删除$search中相应元素
|
||
if ($ops) {
|
||
foreach ($ops as $key => $value) {
|
||
$where .= ' & ' . $key . ' = :' . $key;
|
||
$params[':' . $key] = $value;
|
||
if (isset($search[$key])) {
|
||
unset($search[$key]);
|
||
}
|
||
}
|
||
}
|
||
|
||
if (!empty($search['title'])) {
|
||
$search['title'] = trim($search['title']);
|
||
$where .= ' & title like :title';
|
||
$params[':title'] = $search['title'];
|
||
}
|
||
|
||
if (!isset($params[':deleted'])) {
|
||
$params[':deleted'] = 0;
|
||
$where .= ' & deleted = :deleted';
|
||
}
|
||
|
||
$page = 1;
|
||
$pageSize = 10;
|
||
$data = $model->find()->where($where, $params);
|
||
$pages = new Pagination(['totalCount' =>$data->count(), 'pageSize' => $pageSize]);
|
||
$pages->setPage($page-1, true); //设置分页的当前页面值
|
||
$_orderby = 'orderby DESC,update_at DESC,id DESC';
|
||
$res = $data->offset($pages->offset)->limit($pages->limit)->orderby($_orderby)->all();
|
||
return ['res'=>$res,'data'=>$data];
|
||
}
|
||
|
||
|
||
private function formatList($res)
|
||
{
|
||
$unsets = ['pid','create_at','deleted','update_at'];
|
||
$list = [];
|
||
foreach ($res as $s) {
|
||
$s = $s->toArray();
|
||
foreach ($unsets as $us) {
|
||
unset($s[$us]);
|
||
}
|
||
$list[] = $s;
|
||
}
|
||
unset($s);
|
||
return $list;
|
||
}
|
||
|
||
private function formatDetail($s = [])
|
||
{
|
||
$unsets = ['pid','create_at','deleted','is_show'];
|
||
foreach ($unsets as $us) {
|
||
unset($s[$us]);
|
||
}
|
||
$weekdays = [0 => '日', 1 => '一', 2 => '二', 3 => '三', 4 => '四', 5 => '五', 6 => '六'];
|
||
$wk = date('w', $s['update_at']);
|
||
$s['updateTime'] = date('Y-m-d ', $s['update_at']) . '星期' . $weekdays[$wk];
|
||
$s['des'] = htmlspecialchars_decode($s['des']);
|
||
return $s;
|
||
}
|
||
}
|