UploadImage.php 2.48 KB
<?php
/**
 * 图片相关功能区块视图
 * @author wuxiao
 * @date 2016-8-12
 */
namespace backend\widgets;

use Yii;
use yii\base\Widget;
use yii\web\View;

class UploadImage extends Widget
{
    const appName = 'Image';
    
    /**
     * 实例化自身
     * @return \YHGImage\Upload\Client
     */
	public static function self()
	{
		return new self();
	}
    
    /**
     * 单个头像上传功能区块,整个页面只能有一个此区块
     * @param string $name 文件表单name, 即在form中的表单名
     * @param string $value 文件表单默认值
     */
    public function logo($name, $value = '')
    {        
        $view = $this->getView();
        $view->registerCssFile('/js/webuploader/webuploader.css');
        $view->registerCssFile('/js/webuploader/upload.css');
        $view->registerJsFile('/js/webuploader/webuploader.min.js');
        $view->registerJsFile('/js/uploadlogo.js');
        
        return $this->render('upload/logo',[
            'name'=>$name,
            'value'=>$value,
        ]);
    }
    
    /**
     * 单个图片上传功能区块,页面可以有多个此区块
     * @param string $name 文件表单name, 即在form中的表单名
     * @param string $value 文件表单默认值
     */
    public function single($name, $value = '')
    {
        $id = sprintf('%u',crc32(uniqid(true)));
        
        $view = $this->getView();
        $view->registerCssFile('/js/webuploader/webuploader.css');
        $view->registerCssFile('/js/webuploader/upload.css');
        
                $pre = <<<EOT
<script>
    var uploaders = [], uidx = 0;
</script>
EOT;
        
        return $pre . $this->render('upload/single',[
            'id'=>$id,
            'name'=>$name,
            'value'=>$value,
        ]);
    }
    
    /**
     * 多图上传功能区块
     * @param string $name 文件表单name, 即在form中的表单名
     * @param array $value 文件表单多图默认值
     */
    public static function mutil($name,$values = array())
    {
        $appConfig = Registry::get('appConfig');
        $url = '/static';
        $appName = self::appName;
        
        $values = is_array($values) ? $values : array($values);
        
        $view = new ViewSimple(APPLICATION_PATH."/application/modules/{$appName}/views/tpl");
        return  $view
            ->assign('name',$name)
            ->assign('values',$values)
            ->assign('url',$url)
            ->assign('appName',$appName)
            ->render('mutil.phtml');
    }
}