ctms/ctms-api/controllers/client/v1/NewsController.php
fm453 314745edf8 优化ctms-api语法、修复已知BUG;
主要修复ctms-api、dacms对PHP新版本的支持问题
2025-04-10 23:19:15 +08:00

202 lines
5.7 KiB
PHP
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
# @Author: 嗨噜客(三亚) <fm453>
# @Date: 2024-08-03T15:34:01+08:00
# @Email: 1280880631@qq.com
# @Last modified by: fm453
# @Last modified time: 2024-08-09T09:00:02+08:00
# @Copyright: www.hiluker.cn
namespace api\controllers\client\v1;
use addons\models\AcNews;
use addons\models\AcNewsCat;
use Yii;
use yii\data\Pagination;
use yii\helpers\Url;
class NewsController extends Common
{
public function beforeAction($action)
{
if (!$this->pid) {
$this->result('您正使用本系统内部接口,禁止非法链接使用!');
}
return parent::beforeAction($action);
}
public function actionIndex()
{
$apis = [
'list'=>'文章清单',
'detail'=>'文章详情'
];
$this->result('您正使用CMTS-CLIENT系统文章内容管理接口', $apis, 200);
}
//订单列表
public function actionList()
{
$s = $this->search(['is_show' => 1]);
$res = $s['res'];
if (!$res) {
$this->result('没有查询到相应的数据!', [], 0);
}
$data = $s['data'];
$list = $this->formatList($res);
$return = [
'total'=>$data->count(),
'news' => $list,
'page'=>$this->page
];
$this->result('内容查询成功!', $return, 200);
}
public function actionDetail()
{
$pid = $this->pid;
$post = $this->postdata;
$id = $post['id'] ?? 0;
if ($id<=0) {
$this->result('查询参数错误!');
}
$model = new AcNews();
$detail = $model->findOne($id);
$s = $this->formatDetail($detail->toArray());
$this->result('查询成功!', $s, 200);
}
public function actionAjax()
{
$post = $this->postdata;
$id = $post['id'] ?? 0;
if ($id<=0) {
$this->result('未指定内容!');
}
$model = new AcNews();
$detail = $model->findOne($id);
if (!$detail) {
$this->result('指定的内容不存在!');
}
if ($detail->deleted) {
$this->result('指定的内容已被删除!');
}
$op = $post['op'] ?? '';
if (!$op) {
$this->result('无有效指令!');
}
switch ($op) {
case 'read':
$detail->reading += 1;
break;
case 'no_read':
$detail->reading -= 1;
$detail->reading = $detail->reading < 0 ? 0 : $detail->reading;
break;
case 'like':
$detail->liked += 1;
break;
case 'no_like':
$detail->liked -= 1;
$detail->liked = $detail->liked < 0 ? 0 : $detail->liked;
break;
case 'view':
$detail->viewed += 1;
break;
default:
break;
}
$detail->save();
$data = $this->formatDetail($detail->toArray());
$this->result('已执行', $data, 200);
}
private function search($ops=[])
{
$pid = $this->pid;
$model = new AcNews();
$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 .= ' AND `' . $key . '` = :' . $key;
$params[':' . $key] = $value;
if (isset($search[$key])) {
unset($search[$key]);
}
}
}
if (!empty($search['title'])) {
$search['title'] = trim($search['title']);
$where .= ' AND `title` like :title';
$params[':title'] = '%' . $search['title'] . '%';
}
if (!empty($search['cid'])) {
$search['cid'] = intval($search['cid']);
$where .= ' AND `cid` = :cid';
$params[':cid'] = $search['cid'];
}
if (!isset($params[':deleted'])) {
$params[':deleted'] = 0;
$where .= ' AND `deleted` = :deleted';
}
$page = $this->page;
$pageSize = $this->pageSize;
$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'];
$list = [];
foreach ($res as $s) {
$s = $s->toArray();
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['content'] = $s['content'] ? htmlspecialchars_decode($s['content']) : '';
$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['content'] = htmlspecialchars_decode($s['content']);
// 'src="/upload/pics/'
$upload_url = 'https://dacms.hiluker.cn';
$s['content'] = str_replace('src="/upload/pics/', 'src="' . $upload_url . '/upload/pics/', $s['content']);
return $s;
}
}