Files
xk-api-yii/admin/controllers/system/AttachmentController.php
2024-09-19 11:32:39 +08:00

172 lines
6.0 KiB
PHP

<?php
/**
* Created by PhpStorm.
* User: chatfeed
* Date: 2022/5/25
* Time: 2:08 PM
*/
namespace admin\controllers\goods;
use common\core\BaseAdminController;
use admin\models\forms\AttachmentUploadForm;
use common\models\Attachment;
use common\models\AttachmentGroup;
use common\services\UploadService;
use yii\helpers\ArrayHelper;
use yii\web\UploadedFile;
/**
* @doc-module 上传
*/
class AttachmentController extends BaseAdminController
{
public function actionIndex(){
return ['node'=>['nodeName'=>'fileManage']];
return $this->render('index');
}
public function actionGroupList($type = null, $is_recycle = null)
{
$params = $this->requestValidate($this->get(),[
['is_recycle','integer'],
['type','string']
]);
$query = AttachmentGroup::find()->where([
'mall_id' => \Yii::$app->mallId,
'is_delete' => 0,
]);
isset($params->type) || $query->andWhere(['type' => $params->type === 'video' ? 1 : 0]);
return $query->all();
}
public function actionDo(){
$params = $this->requestValidate($this->get(),[
['type','in','range'=>['folder-create','files','folder-delete','folder']],
['keyword','string'],
['name','string'],
['id','integer'],
['page','integer']
]);
/**
* upload: props.uploadUrl,
list: props.fileUrl + '?type=files',
del: props.fileUrl + '?type=files-delete',
cateList: props.fileUrl + '?type=folder',
cateAdd: props.fileUrl + '?type=folder-create',
cateDel: props.fileUrl + '?type=folder-delete'
*/
switch ($params->type){
case 'folder':
$activeRecords = AttachmentGroup::find()->where(['mall_id' => \Yii::$app->mallId])->andWhere(['is_delete' => 0])->all();
return ArrayHelper::toArray($activeRecords,[
AttachmentGroup::class=>[
'dir_id'=>'id','name'
]
]);
break;
case 'folder-create';
$attachmentGroup = new AttachmentGroup();
$attachmentGroup->setAttributes([
'mall_id'=>\Yii::$app->mallId,
'name'=>$params->name,
]);
if($attachmentGroup->save()){
return ['id'=>$attachmentGroup->id,'name'=>$attachmentGroup->name];
}else{
return ['error'=>$attachmentGroup->getErrors()];
}
break;
case 'folder-delete';
Attachment::updateAll(['is_delete'=>1,'deleted_at'=>time()],['attachment_group_id'=>$params->id,'mall_id'=>\Yii::$app->mallId]);
AttachmentGroup::updateAll(['is_delete'=>1,'deleted_at'=>time()],['id'=>$params->id,'mall_id'=>\Yii::$app->mallId]);
return [];
break;
case 'files':
$query = Attachment::find()->where(['mall_id' => \Yii::$app->mallId])->andWhere(['is_delete' => 0])->orderBy('id desc');;
if(isset($params->id) && $params->id>0) {
$query->andWhere(['attachment_group_id'=>$params->id]);
}
$query->andFilterWhere(['like','name',$params->keyword]);
$this->field = [
Attachment::class=>[
'id',
'title'=>'name',
'url',
'ext'=>function($m){
$implode = explode('.', $m['name']);
return mb_strtolower(array_pop($implode));
},
'time'=>function($model){
return date("Y-m-d H:i:s",$model['created_at']);
},
'size'=>function($model){
return app_filesize($model['size']);
},
]
];
$post = array_merge(\Yii::$app->request->post(),\Yii::$app->request->get());
return $this->create($query,$post);
break;
}
}
public function actionUpload(){
$params = $this->requestValidate($this->post(),[
['id','integer'],
['id','default','value'=>0],
['file','file']
]);
$form = new AttachmentUploadForm();
$form->file = UploadedFile::getInstanceByName('file');
$form->attachment_group_id = $params->id;
return $form->save();
}
/**
* @doc-name oss直传-保存上传信息
* @doc-param int attachment_group_id 上传的分组
* @doc-param string name 上传的文件名称
* @doc-param int size 上传的文件大小
* @doc-param string url 上传的文件链接
*/
public function actionSave()
{
$post = \Yii::$app->request->post();
$this->requestValidate($post,[
[['attachment_group_id','name','size','url'],'required']
]);
$ext = pathinfo($post['url'])['extension'];
$type = (new AttachmentUploadForm())->checkExt($ext);
$attachment = new Attachment();
$attachment->attributes = $post;
$attachment->storage_id = 0;
$attachment->user_id = 0;
$attachment->type = $type;
$attachment->mall_id = \Yii::$app->mallId;
$attachment->thumb_url = $post['url'];
$attachment->saveOrFail();
return $attachment;
}
/**
* @doc-name oss直传-获取上传参数
*/
public function actionParams()
{
$callbackUrl = \Yii::$app->request->hostInfo.'/api/v1/callback/upload-notify';
$dir = \Yii::$app->mallId ? 'uploads/' . \Yii::$app->mallId.'/' . date('Ymd').'/' : 'uploads/' . date('Ymd').'/';
$data = [
'callbackUrl' => $callbackUrl,
'dir' => $dir
];
return (new UploadService())->redirectFile($data);
}
}