130 lines
3.2 KiB
PHP
130 lines
3.2 KiB
PHP
<?php
|
||
|
||
/**
|
||
* @author Any
|
||
* @description KISS
|
||
* @date 2020-12-10
|
||
* @version 1.0.0
|
||
*
|
||
* _____LOG_____
|
||
*
|
||
*/
|
||
namespace app\widgets;
|
||
|
||
use yii\base\Widget;
|
||
|
||
|
||
class UnionFile extends Widget
|
||
{
|
||
/**
|
||
* 单图片或单文件直接通过表单获取
|
||
* 多图文件通过JS变量_gallery_$id获取
|
||
* 多文件通过JS变量_filelist_$id获取
|
||
*
|
||
* example:
|
||
* ###引入
|
||
* use app\widgets\PickFile;
|
||
* ###在View使用
|
||
* <?=PickFile::widget([
|
||
* 'multiple' => false,
|
||
* 'accept' => 'file',
|
||
* 'id' => 'id',
|
||
* 'name' => 'name',
|
||
* 'label' => 'label',
|
||
* 'value' => 'value',
|
||
* 'tip' => 'tip',
|
||
* ])?>
|
||
*/
|
||
|
||
/**
|
||
* string 允许文件类型 images(图片)、file(所有文件)、video(视频)、audio(音频)
|
||
*/
|
||
public $accept;
|
||
|
||
/**
|
||
* bool 是否多文件上传
|
||
*/
|
||
public $multiple;
|
||
|
||
/**
|
||
* string 表单ID,默认$name
|
||
*/
|
||
public $id;
|
||
|
||
/**
|
||
* string 表单字段名称
|
||
*/
|
||
public $name;
|
||
|
||
/**
|
||
* string 表单标签
|
||
*/
|
||
public $label;
|
||
|
||
/**
|
||
* string|array|string(json) 表单字段值
|
||
*/
|
||
public $value;
|
||
|
||
/**
|
||
* string 表单字段输入提示信息
|
||
*/
|
||
public $tip;
|
||
|
||
/**
|
||
* string 上传地址,默认\Yii::$app->urlManager->createUrl(['file/upload/file'])
|
||
*/
|
||
public $url;
|
||
|
||
|
||
/**
|
||
* bool required 是否必须
|
||
*/
|
||
public $required;
|
||
|
||
/**
|
||
* integer 是否开启图片压缩,0=否,1=是
|
||
*/
|
||
public $imageCompressEnable;
|
||
|
||
/**
|
||
* integer 图片压缩最长边限制,默认800
|
||
*/
|
||
public $imageCompressBorder;
|
||
|
||
|
||
|
||
|
||
|
||
public function init() {
|
||
parent::init();
|
||
$this->imageCompressEnable = $this->imageCompressEnable ? 1 : 0;
|
||
$this->imageCompressBorder = $this->imageCompressBorder ? $this->imageCompressBorder : 800;
|
||
}
|
||
|
||
public function run()
|
||
{
|
||
$this->id = $this->id != null ? $this->id : $this->name;
|
||
$this->url = $this->url != null ? $this->url : ($this->accept == 'images'
|
||
? \Yii::$app->urlManager->createUrl(['file/upload/union-image', 'imageCompressEnable' => $this->imageCompressEnable, 'imageCompressBorder' => $this->imageCompressBorder])
|
||
: \Yii::$app->urlManager->createUrl(['file/upload/union-file'])
|
||
);
|
||
$this->required = $this->required ? 'required' : '';
|
||
$this->multiple = $this->multiple ? true : false;
|
||
if($this->multiple){
|
||
$this->value = is_array($this->value) ? json_encode($this->value) : $this->value;
|
||
$this->value = empty($this->value) ? json_encode([]) : $this->value;
|
||
}
|
||
return $this->render('pick-file',[
|
||
'accept' => $this->accept,
|
||
'multiple' => $this->multiple,
|
||
'id' => $this->id,
|
||
'name' => $this->name,
|
||
'label' => $this->label,
|
||
'value' => $this->value,
|
||
'tip' => $this->tip,
|
||
'required' => $this->required,
|
||
'url' => $this->url
|
||
]);
|
||
}
|
||
} |