367 lines
14 KiB
PHP
367 lines
14 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Controllers;
|
||
|
||
use App\Models\ApproveHistory;
|
||
use App\Models\User;
|
||
use Illuminate\Http\Request;
|
||
use Illuminate\Support\Str;
|
||
use Log;
|
||
use OSS\OssClient;
|
||
use App\Contracts\UserContract;
|
||
use App\Http\Response\ResponseJson;
|
||
use AlibabaCloud\Client\AlibabaCloud;
|
||
use AlibabaCloud\Client\Exception\ClientException;
|
||
use AlibabaCloud\Client\Exception\ServerException;
|
||
|
||
class UploadController extends Controller
|
||
{
|
||
use ResponseJson;
|
||
protected $userCon;
|
||
public function __construct(UserContract $userCon)
|
||
{
|
||
$this->userCon = $userCon;
|
||
}
|
||
|
||
public static function getOssClient()
|
||
{
|
||
try {
|
||
$ossClient = new OssClient(config('alioss.id'), config('alioss.secret'), config('alioss.host'));
|
||
} catch (\OSS\Core\OssException $e) {
|
||
return false;
|
||
}
|
||
return $ossClient;
|
||
}
|
||
//
|
||
public function upload(Request $request)
|
||
{
|
||
$file = $_FILES['fileData'];
|
||
//延时0.5秒,避免出现上传头像找不到图片问题
|
||
usleep(500000);
|
||
return $this->uploadFile($file);
|
||
}
|
||
|
||
|
||
|
||
/**
|
||
* 不需要登录就可以上传头像
|
||
* 上传完图片 进行图片检测
|
||
*/
|
||
public function uploadNotLogin(Request $request)
|
||
{
|
||
$file = $_FILES['fileData'];
|
||
//延时0.5秒,避免出现上传头像找不到图片问题
|
||
usleep(500000);
|
||
$url = $this->uploadFile($file);
|
||
$result = \CommonUtilsService::imageContentCecurity([$url]);
|
||
if ($result && isset($result['result']) && $result['result']) {
|
||
return $this->failure('图片异常',$result);
|
||
}else{
|
||
return $this->success('upload_ok',$url);
|
||
}
|
||
}
|
||
|
||
public function officialUpload(Request $request)
|
||
{
|
||
$param = array_filter($request->input());
|
||
$arr['zheng'] = $param['file_positive'];
|
||
$arr['fan'] = $param['file_side'];
|
||
$user = auth()->user();
|
||
$Afiles = [];
|
||
foreach ($arr as $k => $v) {
|
||
$Afiles[] = $v;
|
||
}
|
||
$user_id = $user->id;
|
||
$has_history = ApproveHistory::where('user_id', $user_id)->where('status', 0)->count();
|
||
if ($has_history) {
|
||
return $this->failure('已有待审核的认证');
|
||
}
|
||
$Ainfo = ApproveHistory::where('user_id', $user_id)->get()->toArray();
|
||
if (!empty($Ainfo)) {
|
||
if ($Ainfo[0]['status'] == 1) return $this->failure('已经认证,请不要重复提交!');
|
||
}
|
||
User::where('id', $user_id)->update(['identification_photos' => json_encode($Afiles, true)]);
|
||
$Acreate = [];
|
||
$Acreate['user_id'] = $user_id;
|
||
$Acreate['type'] = 'name';
|
||
$Acreate['status'] = 0;
|
||
if (!empty($Ainfo)) {
|
||
ApproveHistory::where('user_id', $user_id)->update(['status' => 0]);
|
||
} else {
|
||
ApproveHistory::create($Acreate);
|
||
}
|
||
return $this->success('upload_ok', array_values($Afiles));
|
||
}
|
||
|
||
public function saveIdCardPhoto($file)
|
||
{
|
||
//生成新二维码云端全URI
|
||
$object = date('Y') . date('m') . "/" . date('d') . "/" . $file['name'];
|
||
$file_url = 'https://' . config('alioss.picture_domain') . '/' . $object;
|
||
require_once base_path('vendor/aliyuncs/oss-sdk-php') . '/autoload.php';
|
||
|
||
//连接aliyun oss server
|
||
try {
|
||
$ossClient = new \OSS\OssClient(config('alioss.id'), config('alioss.secret'), config('alioss.host'));
|
||
} catch (\OSS\Core\OssException $e) {
|
||
return $this->failure('oss_connect_failure', $e->getMessage());
|
||
}
|
||
|
||
|
||
//上传图片到aliyun oss
|
||
try {
|
||
$result = $ossClient->uploadFile(config('alioss.buckets.picture'), $object, $file['tmp_name']);
|
||
} catch (\OSS\Core\OssException $e) {
|
||
return $this->failure('oss_put_failure', $e->getMessage());
|
||
}
|
||
return $file_url;
|
||
}
|
||
|
||
//上传文件
|
||
public function uploadFile($file)
|
||
{
|
||
$type_arr = ['text/html', 'text/htm'];
|
||
if (in_array($file['type'], $type_arr)) return $this->failure('后缀名错误');
|
||
|
||
//生成新二维码云端全URI
|
||
$path = Request()->path ? Request()->path : date('Y') . date('m') . "/" . date('d');
|
||
$object = $path . "/" . $file['name'];
|
||
$file_url = 'https://' . config('alioss.picture_domain') . '/' . $object;
|
||
require_once base_path('vendor/aliyuncs/oss-sdk-php') . '/autoload.php';
|
||
|
||
//连接aliyun oss server
|
||
try {
|
||
$ossClient = new \OSS\OssClient(config('alioss.id'), config('alioss.secret'), config('alioss.host'));
|
||
} catch (\OSS\Core\OssException $e) {
|
||
return $this->failure('oss_connect_failure', $e->getMessage());
|
||
}
|
||
|
||
//上传图片到aliyun oss
|
||
try {
|
||
$result = $ossClient->uploadFile(config('alioss.buckets.picture'), $object, $file['tmp_name']);
|
||
} catch (\OSS\Core\OssException $e) {
|
||
return $this->failure('oss_put_failure', $e->getMessage());
|
||
}
|
||
|
||
//内容安全-图片
|
||
// $result = \CommonUtilsService::imageContentCecurity([$file_url]);
|
||
// if ($result && isset($result['result']) && $result['result']) {
|
||
// return $this->failure('图片' . $result['result'] . ',请换一张照片');
|
||
// }
|
||
return $this->success('upload_ok', $file_url);
|
||
}
|
||
|
||
public function uploadFiles(Request $request)
|
||
{
|
||
Log::info("上传文件");
|
||
$file_count = $request->input('file_count');
|
||
$files = [];
|
||
for ($i = 0; $i < $file_count; $i++) {
|
||
$files[] = $_FILES['file_' . $i];
|
||
}
|
||
try {
|
||
$ossClient = new \OSS\OssClient(config('alioss.id'), config('alioss.secret'), config('alioss.host'));
|
||
$index = 0;
|
||
$file_arr = [];
|
||
foreach ($files as $file) {
|
||
$object = date('Y') . date('m') . "/" . date('d') . "/" . $file['name'];
|
||
$file_url = 'https://' . config('alioss.picture_domain') . '/' . $object;
|
||
$file_arr[] = $file_url;
|
||
$result = $ossClient->uploadFile(config('alioss.buckets.picture'), $object, $file['tmp_name']);
|
||
$index++;
|
||
}
|
||
return $this->success('ok', $file_arr);
|
||
} catch (\Exception $e) {
|
||
$this->getError($e);
|
||
return $this->failure('上传文件失败');
|
||
}
|
||
}
|
||
|
||
//获取Web真传签名
|
||
public function aliyunSignature(Request $request)
|
||
{
|
||
|
||
try {
|
||
$id = config('alioss.id');
|
||
$key = config('alioss.secret');
|
||
$host = 'https://' . config('alioss.picture_domain');
|
||
// $host = 'https://ufutx-images.oss-accelerate.aliyuncs.com';
|
||
$now = time();
|
||
$expire = 60 * 30; //设置该policy超时时间是60s. 即这个policy过了这个有效时间,将不能访问
|
||
$end = $now + $expire;
|
||
$expiration = $this->gmt_iso8601($end);
|
||
|
||
$dir = date('Y') . date('m') . "/" . date('d') . "/";
|
||
|
||
//最大文件大小.用户可以自己设置
|
||
$condition = array(0 => 'content-length-range', 1 => 0, 2 => 1048576000);
|
||
$conditions[] = $condition;
|
||
|
||
//表示用户上传的数据,必须是以$dir开始, 不然上传会失败,这一步不是必须项,只是为了安全起见,防止用户通过policy上传到别人的目录
|
||
$start = array(0 => 'starts-with', 1 => '$key', 2 => $dir);
|
||
$conditions[] = $start;
|
||
|
||
|
||
//这里默认设置是2020年.注意了,可以根据自己的逻辑,设定expire 时间.达到让前端定时到后面取signature的逻辑
|
||
$arr = array('expiration' => $expiration, 'conditions' => $conditions);
|
||
|
||
$policy = json_encode($arr);
|
||
$base64_policy = base64_encode($policy);
|
||
$string_to_sign = $base64_policy;
|
||
$signature = base64_encode(hash_hmac('sha1', $string_to_sign, $key, true));
|
||
$callback_url = $request->root() . '/api/upload';
|
||
$callback_param = array(
|
||
'callbackUrl' => $callback_url,
|
||
'callbackBody' => 'filename=${object}&size=${size}&mimeType=${mimeType}&height=${imageInfo.height}&width=${imageInfo.width}',
|
||
'callbackBodyType' => "application/x-www-form-urlencoded"
|
||
);
|
||
$callback_string = json_encode($callback_param);
|
||
$base64_callback_body = base64_encode($callback_string);
|
||
|
||
|
||
$response = array();
|
||
$response['accessid'] = $id;
|
||
$response['host'] = $host;
|
||
$response['policy'] = $base64_policy;
|
||
$response['signature'] = $signature;
|
||
$response['expire'] = $end;
|
||
//这个参数是设置用户上传指定的前缀
|
||
$response['dir'] = $dir;
|
||
$response['picture_domain'] = config('alioss.picture_domain');
|
||
//$response['callback'] = $base64_callback_body;
|
||
|
||
return $this->success('aliyun_signature', $response);
|
||
} catch (\Exception $e) {
|
||
\Log::error($e->getMessage());
|
||
return $this->failure("上传失败");
|
||
}
|
||
}
|
||
|
||
//aliyun get signature using
|
||
private function gmt_iso8601($time)
|
||
{
|
||
$dtStr = date("c", $time);
|
||
$mydatetime = new \DateTime($dtStr);
|
||
$expiration = $mydatetime->format(\DateTime::ISO8601);
|
||
$pos = strpos($expiration, '+');
|
||
$expiration = substr($expiration, 0, $pos);
|
||
return $expiration . "Z";
|
||
}
|
||
|
||
public function createUploadVideo(Request $request)
|
||
{
|
||
\Log::info(env('ALIYUN_VOD_ACCESS_KEY_ID'));
|
||
try {
|
||
$mp3_arr = ['wma','wav','ogg'];
|
||
$mp4_arr = ['ma4','m4a','wmv','MOV','video/quicktime','mp4'];
|
||
$file_name = $request->input('file_name');
|
||
$cover_url = $request->input('cover_url');
|
||
if (empty($file_name)) throw new \Exception('缺少视频源文件名', 1);
|
||
$str = substr($file_name,strrpos($file_name,'.')+1);
|
||
$query = [
|
||
'query' => [
|
||
'RegionId' => "cn-hangzhou",
|
||
'Title' => $file_name,
|
||
'FileName' => $file_name,
|
||
'CoverURL' => $cover_url,
|
||
],
|
||
'headers' =>[
|
||
'referer' => 'http://love.ufutx.com'
|
||
]
|
||
];
|
||
if(in_array($str,$mp3_arr)){
|
||
$end = 'mp3';
|
||
$file_name = str_replace($str,$end,$file_name);
|
||
$query['query']['FileName'] = $file_name;
|
||
}
|
||
if(in_array($str,$mp4_arr)){
|
||
$end = 'mp4';
|
||
$file_name = str_replace($str,$end,$file_name);
|
||
$query['query']['FileName'] = $file_name;
|
||
//$query['query']['TemplateGroupId'] = '0945f5dd3a9372ce390cf41d6d35e96b';
|
||
}
|
||
AlibabaCloud::accessKeyClient(env('ALIYUN_VOD_ACCESS_KEY_ID'), env('ALIYUN_VOD_ACCESS_KEY_SECRET'))
|
||
->regionId('cn-hangzhou')
|
||
->asDefaultClient();
|
||
$result = AlibabaCloud::rpc()
|
||
->product('vod')
|
||
// ->scheme('https') // https | http
|
||
->version('2017-03-21')
|
||
->action('CreateUploadVideo')
|
||
->method('POST')
|
||
->host('vod.cn-shanghai.aliyuncs.com')
|
||
->options($query)
|
||
->request();
|
||
$result = $result->toArray();
|
||
return $this->success('ok', $result);
|
||
} catch (ClientException $e) {
|
||
$this->getError($e);
|
||
return $this->failure('视频上传失败,请稍后再试');
|
||
} catch (ServerException $e) {
|
||
$this->getError($e);
|
||
return $this->failure('视频上传失败,请稍后再试');
|
||
} catch (\Exception $e) {
|
||
$this->getError($e);
|
||
return $this->failure('视频上传失败,请稍后再试');
|
||
}
|
||
}
|
||
|
||
public function refreshUploadVideo(Request $request)
|
||
{
|
||
try {
|
||
$video_id = $request->input('video_id');
|
||
if (empty($video_id)) throw new \Exception("渠道视频id", 1);
|
||
AlibabaCloud::accessKeyClient(env('ALIYUN_VOD_ACCESS_KEY_ID'), env('ALIYUN_VOD_ACCESS_KEY_SECRET'))
|
||
->regionId('cn-hangzhou')
|
||
->asDefaultClient();
|
||
$result = AlibabaCloud::rpc()
|
||
->product('vod')
|
||
// ->scheme('https') // https | http
|
||
->version('2017-03-21')
|
||
->action('RefreshUploadVideo')
|
||
->method('POST')
|
||
->host('vod.cn-shanghai.aliyuncs.com')
|
||
->options([
|
||
'query' => [
|
||
'RegionId' => "cn-hangzhou",
|
||
'VideoId' => $video_id,
|
||
],
|
||
'headers' =>[
|
||
'referer' => 'http://lova.ufutx.com'
|
||
]
|
||
])
|
||
->request();
|
||
$result = $result->toArray();
|
||
return $this->success('ok', $result);
|
||
} catch (ClientException $e) {
|
||
$this->getError($e);
|
||
return $this->failure('视频上传失败,请稍后再试');
|
||
} catch (ServerException $e) {
|
||
$this->getError($e);
|
||
return $this->failure('视频上传失败,请稍后再试');
|
||
} catch (\Exception $e) {
|
||
$this->getError($e);
|
||
return $this->failure('视频上传失败,请稍后再试');
|
||
}
|
||
}
|
||
|
||
public function videoUrl(Request $request)
|
||
{
|
||
try {
|
||
$video_id = $request->input('aliyun_video_id');
|
||
if (empty($video_id)) return $this->failure('缺少视频id');
|
||
$result = \AliyunService::getPlayInfo($video_id, 'vod-mp.ufutx.com');
|
||
$play_url = $result['PlayInfoList']['PlayInfo'][0]['PlayURL'];
|
||
$cover_url = $result['VideoBase']['CoverURL'] ?? '';
|
||
return $this->success('ok', compact('play_url', 'cover_url'));
|
||
} catch (\Exception $e) {
|
||
//$this->getError($e);
|
||
if($e->getCode() == 403){
|
||
return $this->jsonResponse(0,"视频审核中,请稍后重试");
|
||
}else{
|
||
return $this->failure('获取播放地址失败');
|
||
}
|
||
}
|
||
}
|
||
}
|