/**
* 文件上传接口
*
*/
public function uploadFile()
{
//接收上传文件
$data = $this->request->param();
$file = $this->request->file('file');
if($_FILES['file']['size']> sysconfig('upload', 'upload_allow_size')) return json(['code' => 1, 'msg' => '上传文件不能超过500M', 'data' => '']);
$file_info = pathinfo($_FILES['file']['name']);
//拼接文件名
$file_name = $data['openid'] . sha1(date('YmdHis', time()) . uniqid()) . '.' . $file_info['extension'];
//调用阿里云OSS上传
$res = self::AliuploadFile($file_name, $file, $data['type']);
if ($res != '') {
return json(['code' => 1, 'msg' => 'succeed', 'data' => $res]);
}
}
/**
* 存储文件
*
* @param $srcFile
* @param $desFile
* @throws Exception
*/
public function AliuploadFile($module, $file, $type)
{
//配置
$accessKeyId = sysconfig('upload', 'alioss_access_key_id');
$accessKeySecret = sysconfig('upload', 'alioss_access_key_secret');
$endpoint = sysconfig('upload', 'alioss_endpoint');
$bucket = sysconfig('upload', 'alioss_bucket');
if ($type == 1) {
$object = "upload/images/" . $module;
} else {
$object = "upload/video/" . $module;
}
$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
$updata = $ossClient->uploadFile($bucket, $object, $file);
if (isset($updata['info']['url']) != '') {
return contentFormat($updata['info']['url']);
}
}
|