UploadController.class.php 2.57 KB
<?php
/**
 * 上传文档
 * Created by PhpStorm.
 * User: mr.song
 * Date: 2016/7/22
 * Time: 16:49
 */

namespace Apicp\Controller\att;

use Common\Common\Vhall;
use VcySDK\Attach;
use VcySDK\Service;


class UploadController extends \Apicp\Controller\AbstractController
{

    public function Index()
    {
        /*
      * 文件类型
      * 99=普通文件(最大5M),
      * 1=图片(2M),
      * 2=音频(2M),
      * 3=视频(10M),
      * 总文件大小最大30M,
      * 支持类型 (xls,xlsx,jpg,jpeg,png,bmp,gif,mp3,amr,avi,mp4) (必填)
      */
        $types = array(
            Attach::TYPE_AUDIO,
            Attach::TYPE_IMG,
            Attach::TYPE_NORMAL,
            Attach::TYPE_VIDEO
        );

        $liveId = I('post.liveId');

        $atMediatype = I('post.atMediatype', 0, 'intval');
        // 2018-03-22 14:42:37 员圈的应用标示符只有 yuanquan 这里不用接前端传的值 现在出现有些地方前端传的不对 导致报错
        //        $plIdentifier = I('post._identifier', '', 'trim');
        $res_auth = I('post.res_auth', '', 'trim');

        // 检查参数
        if (!in_array($atMediatype, $types) || empty($_FILES)) {
            $this->_set_error('_ERR_PARAMS_UNALLOWED');
            return false;
        }

        $img_type = ['jpeg', 'jpg', 'png', 'bmp'];

        // 获取文件后缀
        $file_extension = end(explode('.', $_FILES['file']['name']));
        if (in_array($file_extension, $img_type)) {
            $atMediatype = Attach::TYPE_IMG;
        }

        if ($_FILES['file']['size'] == 0) {
            E('_ERR_UPLOAD_FILE_IS_EMPTY');
        }

        $params = [
            'plIdentifier' => APP_IDENTIFIER,
            'atMediatype' => $atMediatype,
            // 插入水印
            'atInsertWaterMark' => cfg('FILE_UPLOAD_WATERMARK', null, true) ? Attach::INSERT_WATERMARK_TRUE : Attach::INSERT_WATERMARK_FALSE,
        ];

        // 获取资源鉴权配置
        if (!empty($res_auth)) {
            $config = getResAuthConfig($res_auth);

            // 合并资源鉴权参数
            if (!empty($config)) {
                $params = array_merge($params, $config);
            }
        }

        $attach = new Attach(Service::instance());
        $result = $attach->upload($params, $_FILES);

        //如果是直播
        if($liveId){
            $res = Vhall::instance()->fileUpload(['liveId' => $liveId, 'atId' => $result['atId']]);
            if(!empty($result['data'])){
                $result = $res;
            }
        }

        $this->_result = $result;
        return true;
    }
}