UploadAttaController.class.php 2.93 KB
<?php
/**
 * 附件上传
 * Created by PhpStorm.
 * User: mr.song
 * Date: 2016/7/22
 * Time: 16:49
 */

namespace Apicp\Controller\Attachment;

use Common\Common\Constant;
use VcySDK\Service;
use VcySDK\Attach;

class UploadAttaController extends AbstractController
{
    /**
     * UploadAtta
     *
     * @author heyuelong
     * @desc 文件上传接口
     *
     * @param String atMediatype 附件类型
     *
     * @return array
     */
    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
        );

        $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;
        }

        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);

        $data = [
            'title' => $this->dealPrefix($result['atFilename']),
            'source_type' => Constant::SOURCE_TYPE_FILE,
            'source_status' => Constant::SOURCE_STATUS_CONVERT,
            'at_id' => $result['atId'],
            'at_type' => Constant::ATTACH_TYPE_FILE,
            'at_url' => $result['atAttachment'],
            'at_name' => $result['atFilename'],
        ];

        $source_id = $this->saveAtt($data);

        $this->_result = [
            'source_id' => $source_id
        ];

        return true;
    }

    //去除标题末尾后缀
    public function dealPrefix($title)
    {
        $deal_title = explode('.', $title);
        array_pop($a);
        $title=implode('.',$deal_title);

        return $title;
    }
}