Files
ctms-yii/ctms-api/controllers/gm/v1/ImageController.php
T
fm453 270b881437 ADD:添加后台管理端接口;
DEL:删除不必要的头部注释;
FIX:修正若干已知错误;
2025-07-13 19:40:10 +08:00

99 lines
3.0 KiB
PHP
Executable File

<?php
namespace api\controllers\gm\v1;
use Yii;
use yii\helpers\Url;
use yii\data\Pagination;
use yii\web\UploadedFile;
use common\models\CAttachment;
class ImageController extends Common
{
public function beforeAction($action)
{
if (!$this->pid) {
$this->result('您正使用本系统内部接口,禁止非法链接使用!');
}
return parent::beforeAction($action);
}
public function actionIndex()
{
$apis = [
'list' => '图片列表',
];
$this->result('您正使用CMTS-GM系统图库管理接口!', $apis, 200);
}
public function actionList()
{
$s = $this->search();
$res = $s['res'];
if (!$res) {
$this->result('没有查询到相应的数据!', [], 0);
}
//附件网址前缀
$src = Url::base(TRUE) . "/../../upload"; //在网站根目录下使用upload/pics目录;适合使用yii框架主目录入口而非绑定子域名的情形
if ($_SERVER['DOCUMENT_ROOT'] . '/upload' != Yii::getAlias("@upload")) {
// $src = Url::toRoute(['attach/index','img'=>'']); //换用网络加载转换的方式-反应慢
$src = Yii::$app->params['attachSrc'] . Yii::$app->params['attachDir'];
}
$data= [
'total'=>$s['query']->count(),
'images' => $res,
'src' => $src,
'page'=>$this->page
];
$this->result('图片查询成功', $data, 200);
}
private function search(){
$pid = $this->pid;
$siteId = Yii::$app->params['siteId'];
$return = [];
$model = new CAttachment();
$where = [];
$where[] = 'and';
$where[] = ['=', 'status_code', 1];
$where[] = ['=', 'type', 0];
$where[] = ['=', 'siteid', $siteId];
$where[] = ['=', 'pid', $pid];
$post = $this->postdata;
$search = $post['search'] ?? [];
if ($search) {
if ($search['create_m']) {
$start = strtotime($search['create_m']);
$s = date("Y-m-d H:i:s", $start);
$ldm = strtotime("last day of " . $s);
$m = date("Y-m-d", $ldm) . " 23:59:59";
$end = strtotime($m);
$where[] = ['between', 'create_at', $start, $end];
} else if (!empty($search['create_after']) && !empty($search['create_before'])) {
$start = strtotime($search['create_after']);
$end = strtotime($search['create_before']);
$where[] = ['between', 'create_at', $start, $end];
} else if (!empty($search['create_after'])) {
$start = strtotime($search['create_after']);
$where[] = ['>=', 'create_at', $start];
} else if (!empty($search['create_before'])) {
$end = strtotime($search['create_before']);
$where[] = ['<=', 'create_at', $end];
}
if ($search['title']) {
$where[] = ['LIKE', 'title', $search['title']];
}
}
$data = $model->find()->where($where);
$pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => $this->pageSize]);
$_orderby = 'id DESC';
$res = $data->offset($pages->offset)->limit($pages->limit)->orderBy($_orderby)->all();
return ['res'=>$res,'query'=>$data];
}
}