Initial commit,修正
This commit is contained in:
Executable
+252
@@ -0,0 +1,252 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @Author: fm453
|
||||
* @Date: 2018-08-17 19:39:41
|
||||
* @Last Modified by: fm453
|
||||
* @Last Modified time: 2021-04-26 22:56:16
|
||||
* @Email: fm453@lukegzs.com
|
||||
*/
|
||||
namespace backend\controllers;
|
||||
|
||||
use Yii;
|
||||
use yii\data\Pagination;
|
||||
use yii\helpers\Url;
|
||||
use common\models\CAttachment;
|
||||
use common\components\Attachment;
|
||||
|
||||
class UploadController extends \yii\web\Controller
|
||||
{
|
||||
public $enableCsrfValidation = false; //取消csrf令牌验证;不取消的话文件上传会失败
|
||||
|
||||
public function checkPid()
|
||||
{
|
||||
parent::init();
|
||||
$session = Yii::$app->session;
|
||||
if(isset($_GET['pid']) && (int)$_GET['pid']>0){
|
||||
$session->set('pid',(int)$_GET['pid']);
|
||||
}
|
||||
$pid = $session->get('pid');
|
||||
if(!$pid){
|
||||
return Yii::$app->response->redirect(['index/index']);
|
||||
}
|
||||
return $pid;
|
||||
}
|
||||
|
||||
//主界面
|
||||
public function actionIndex()
|
||||
{
|
||||
return $this->render('../layouts/dev',[]);
|
||||
}
|
||||
|
||||
public function actionImage()
|
||||
{
|
||||
global $_HI,$_FM;
|
||||
$this->checkPid();
|
||||
$siteId = Yii::$app->params['siteId'];
|
||||
$session = Yii::$app->session;
|
||||
$pid = (int)$session->get('pid');
|
||||
$model = new CAttachment();
|
||||
$search = $where = [];
|
||||
$where[] = 'and';
|
||||
$where[] = ['=','status_code',1];
|
||||
$where[] = ['=','type',0];
|
||||
$where[] = ['=','siteid',$siteId];
|
||||
$where[] = ['=','pid',$pid];
|
||||
$post = Yii::$app->request->post();
|
||||
if(isset($post['search'])){
|
||||
$search = $post['search'];
|
||||
$session->set('images::search',$search);
|
||||
}elseif($session->get('images::search')){
|
||||
$search = $session->get('images::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];
|
||||
}elseif(!empty($search['create_after']) && !empty($search['create_before'])){
|
||||
$start = strtotime($search['create_after']);
|
||||
$end = strtotime($search['create_before']);
|
||||
$where[] = ['between','create_at',$start,$end];
|
||||
}elseif(!empty($search['create_after'])){
|
||||
$start = strtotime($search['create_after']);
|
||||
$where[] = ['>=','create_at',$start];
|
||||
}elseif(!empty($search['create_before'])){
|
||||
$end = strtotime($search['create_before']);
|
||||
$where[] = ['<=','create_at',$end];
|
||||
}
|
||||
if($search['title']){
|
||||
$where[] = ['LIKE','title',$search['title']];
|
||||
}
|
||||
}
|
||||
//分页
|
||||
$orderby = ['id' => SORT_DESC];
|
||||
$Attachment = new Attachment();
|
||||
$list = $Attachment->getData($model,$where,$orderby);
|
||||
$pager = $Attachment->getPagination();
|
||||
|
||||
//附件网址前缀
|
||||
$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'];
|
||||
}
|
||||
|
||||
$callback = isset($_GET['callback']) ? $_GET['callback'] : 'thumb';
|
||||
return $this->render('image',[
|
||||
'list'=>$list,
|
||||
'pager' => $pager,
|
||||
'src'=>$src,
|
||||
'callback'=>$callback
|
||||
]);
|
||||
}
|
||||
|
||||
public function actionImages()
|
||||
{
|
||||
global $_HI,$_FM;
|
||||
$this->checkPid();
|
||||
$siteId = Yii::$app->params['siteId'];
|
||||
$session = Yii::$app->session;
|
||||
$pid = (int)$session->get('pid');
|
||||
$model = new CAttachment();
|
||||
$search = $where = [];
|
||||
$where[] = 'and';
|
||||
$where[] = ['=','status_code',1];
|
||||
$where[] = ['=','type',0];
|
||||
$where[] = ['=','siteid',$siteId];
|
||||
$where[] = ['=','pid',$pid];
|
||||
$post = Yii::$app->request->post();
|
||||
if(isset($post['search'])){
|
||||
$search = $post['search'];
|
||||
$session->set('images::search',$search);
|
||||
}elseif($session->get('images::search')){
|
||||
$search = $session->get('images::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];
|
||||
}elseif(!empty($search['create_after']) && !empty($search['create_before'])){
|
||||
$start = strtotime($search['create_after']);
|
||||
$end = strtotime($search['create_before']);
|
||||
$where[] = ['between','create_at',$start,$end];
|
||||
}elseif(!empty($search['create_after'])){
|
||||
$start = strtotime($search['create_after']);
|
||||
$where[] = ['>=','create_at',$start];
|
||||
}elseif(!empty($search['create_before'])){
|
||||
$end = strtotime($search['create_before']);
|
||||
$where[] = ['<=','create_at',$end];
|
||||
}
|
||||
if($search['title']){
|
||||
$where[] = ['LIKE','title',$search['title']];
|
||||
}
|
||||
}
|
||||
//分页
|
||||
$orderby = ['id' => SORT_DESC];
|
||||
$Attachment = new Attachment();
|
||||
$list = $Attachment->getData($model,$where,$orderby);
|
||||
$pager = $Attachment->getPagination();
|
||||
|
||||
//附件网址前缀
|
||||
$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'];
|
||||
}
|
||||
|
||||
$callback = isset($_GET['callback']) ? $_GET['callback'] : 'thumb';
|
||||
return $this->render('images',[
|
||||
'list'=>$list,
|
||||
'search'=>$search,
|
||||
'pager' => $pager,
|
||||
'src'=>$src,
|
||||
'callback'=>$callback
|
||||
]);
|
||||
}
|
||||
|
||||
public function actionImage_save()
|
||||
{
|
||||
global $_HI,$_FM;
|
||||
$post = Yii::$app->request->post();
|
||||
if(!$post['root']){
|
||||
die(json_encode('没有传入文件路径'));
|
||||
}
|
||||
// 文件附件目录
|
||||
$attachDir = Yii::getAlias('@upload'); //附件所在目录
|
||||
|
||||
$targetDir = "/pics"; //开始构建目标转存目录
|
||||
if (!file_exists($attachDir . $targetDir)) {
|
||||
@mkdir($attachDir . $targetDir);
|
||||
}
|
||||
|
||||
$session = Yii::$app->session;
|
||||
$siteId = Yii::$app->params['siteId'];
|
||||
$targetDir .= '/'.$siteId;
|
||||
if (!file_exists($attachDir . $targetDir)) {
|
||||
@mkdir($attachDir . $targetDir);
|
||||
}
|
||||
|
||||
$pid = (int)$session->get('pid');
|
||||
$targetDir .= '/'.$pid;
|
||||
if (!file_exists($attachDir . $targetDir)) {
|
||||
@mkdir($attachDir . $targetDir);
|
||||
}
|
||||
|
||||
$timeDir = date('Ym');
|
||||
$targetDir .= "/".$timeDir;
|
||||
if (!file_exists($attachDir . $targetDir)) {
|
||||
@mkdir($attachDir . $targetDir);
|
||||
}
|
||||
|
||||
//处理请求的文件信息
|
||||
$title = isset($post['name']) ? $post['name'] : md5(TIMESTAMP).'.jpg';
|
||||
//取后缀名
|
||||
if($post['ext']){
|
||||
$suffix = strtolower($post['ext']);
|
||||
}else{
|
||||
$_names = explode('.',$title);
|
||||
$suffix = tolower($_names[count($_names)-1]);
|
||||
}
|
||||
$newName = date('dHis',TIMESTAMP).'_'.md5($title);
|
||||
$root = $post['root'];
|
||||
$newFile = $attachDir . $targetDir."/".$newName.".".$suffix;
|
||||
$tempfile = $post['root'];
|
||||
//文件移动,【完整文件名-含路径,目标目录】
|
||||
rename($tempfile,$newFile);
|
||||
$options = json_encode($post['params']);
|
||||
//文件入库
|
||||
$model = new CAttachment();
|
||||
$model->siteid = $siteId;
|
||||
$model->pid = $pid;
|
||||
$model->uid = $_FM['user']['uid'];
|
||||
$model->mid = 0;
|
||||
$model->type = 0;
|
||||
$model->status_code = 1;
|
||||
$model->title = $title;
|
||||
$model->filesize = $post['size'];
|
||||
$model->route = $targetDir."/".$newName.".".$suffix;
|
||||
$model->options = $options;
|
||||
$model->update_at = $model->create_at = time();
|
||||
$model->save();
|
||||
$id = $model->attributes['id']; //获取插入后id
|
||||
|
||||
$return=[];
|
||||
$return['msg'] = '';
|
||||
$return['code'] = 0;
|
||||
$return['path'] = $model->route;
|
||||
if(!$id){
|
||||
$return['code'] = 453100;
|
||||
$return['msg'] = $model->getErrors();
|
||||
}
|
||||
$return['data'] = ['id'=>$id];
|
||||
die(json_encode($return,JSON_UNESCAPED_UNICODE));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user