Commit 7ad565b4 by 杨昕

优化代码

parent a4f9f03f
Pipeline #14113 passed with stages
in 2 minutes 2 seconds
......@@ -25,6 +25,7 @@ class MediasExport implements WithHeadings,FromArray,WithColumnWidths
'B' => 55,
'C' => 45,
'D' => 45,
'E' => 55
];
}
......@@ -37,8 +38,9 @@ class MediasExport implements WithHeadings,FromArray,WithColumnWidths
return [
'媒资编号',
'视频名称',
"名称",
"永久播放地址",
"全屏预览地址"
"全屏预览地址",
];
}
......
......@@ -5,6 +5,7 @@ namespace App\Http\Controllers\Web;
use App\Exports\MediasExport;
use App\Exports\MubanExport;
use App\Imports\MediasImport;
use App\Model\DownloadRecord;
use App\Model\MediaCategoryModel;
use App\Model\MediaModel;
use App\Tool\SrsHookValidate;
......@@ -275,7 +276,7 @@ class MediaController extends Controller
* @throws \App\Exceptions\ControllerException
*/
public function import_batch_media(Request $request){
$type = $request->type??'media';
// $type = $request->type??'media';
$data = handler_drive(function () use($request){
......@@ -284,24 +285,67 @@ class MediaController extends Controller
#将文件内容转化为数组
$sheet = (new MediasImport())->toArray($file);
$temp = [];
foreach ($sheet[0] as $key => $item){
if (!isset($item["媒资编号"]) || !isset($item["名称"])){
throw new \Exception("文件格式有误");
}
$media_ids = collect($sheet[0])->map(function ($item,$key){
return array_first($item);
})->toArray();
$temp[$item['媒资编号']] = [
'media_id' => $item["媒资编号"],
'name' => $item['名称']
];
}
$data = [];
if (!empty($sheet[0])){
#批量获取永久播放链接地址
$mediaInfos = MediaModel::batchGetMediaForverM3u8($request,$media_ids);
$mediaInfos = MediaModel::batchGetMediaForverM3u8($request,$temp);
$data = new MediasExport(array_values($mediaInfos));
}
return $data;
$record = md5(json_encode($data));
DownloadRecord::addRecord($record,$data);
return $record;
});
return Excel::download($data,$type.'.xlsx');
return success($data);
}
/**
* @param Request $request
* @param $keyId
* @return \Symfony\Component\HttpFoundation\BinaryFileResponse
* @throws \App\Exceptions\ControllerException
*/
public function download(Request $request,$keyId){
$data = handler_drive(function () use($keyId){
$record = DownloadRecord::getRecord($keyId);
if (empty($record)){
throw new \Exception("数据不存在");
}
$content = json_decode($record['content'],true);
DownloadRecord::delRecord($keyId);
return new MediasExport($content);
});
return Excel::download($data,'media.xlsx');
}
}
......@@ -29,6 +29,8 @@ class MediaRoute extends AuthenRoute
$router->get('media_manager/export_batch_media','MediaController@export_batch_media');
$router->get("/media_manager/download/{keyId}",'MediaController@download');
});
......
<?php
namespace App\Model;
use App\Exceptions\DatabaseException;
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
use MongoDB\BSON\UTCDateTime;
class DownloadRecord extends Eloquent
{
protected $collection = "download_record";
protected $connection = 'mongodb'; //使用mongodb
protected $primaryKey = '_id';
/**
* @param $keyId
* @param $data
* @return mixed
* @throws DatabaseException
*/
public static function addRecord($keyId,$data){
try{
$record = self::getRecord($keyId);
if (empty($record)){
$create_time = new UTCDateTime(time() * 1000);;
$data = [
'key_id' => $keyId,
'content' => json_encode($data),
'create_time' => $create_time,
];
$id = DownloadRecord::insertGetId($data);
}else{
$id = $record['id'];
}
}catch (\Exception $exception){
throw new DatabaseException($exception->getMessage());
}
return $id;
}
/**
* @param $keyId
* @return mixed
* @throws DatabaseException
*/
public static function getRecord($keyId){
try{
$record = DownloadRecord::where("key_id",$keyId)->first();
}catch (\Exception $exception){
throw new DatabaseException($exception->getMessage());
}
return $record;
}
/**
* 删除记录
* @param $keyId
* @return mixed
* @throws DatabaseException
*/
public static function delRecord($keyId){
try{
$flag = DownloadRecord::where("key_id",$keyId)->delete();
}catch (\Exception $exception){
throw new DatabaseException($exception->getMessage());
}
return $flag;
}
}
......@@ -1009,13 +1009,14 @@ class MediaModel extends Eloquent
* @return array
* @throws DatabaseException
*/
public static function batchGetMediaForverM3u8($request,array $media_ids){
public static function batchGetMediaForverM3u8($request,array $orginal_data){
try{
$ids = implode(',',$media_ids);
$ids = implode(',',array_keys($orginal_data));
#生成批量处理地址
$url = getRbac($request,'video/infos',$ids ,$type='mul_play_m3u8');
$url = getRbac($request,'video/infos','' ,$type='mul_play_m3u8');
$res = http_request_code($url,null,'POST',array('mediaIds'=>$ids));
......@@ -1030,7 +1031,7 @@ class MediaModel extends Eloquent
throw new \Exception("获取数据失败");
}
return self::mediaExportFormat($result['data'],$media_ids);
return self::mediaExportFormat($result['data'],$orginal_data);
}catch (\Exception $exception){
throw new DatabaseException($exception->getMessage());
......@@ -1043,7 +1044,8 @@ class MediaModel extends Eloquent
* @param $media_ids
* @return mixed
*/
private static function mediaExportFormat(array $data){
private static function mediaExportFormat(array $data,$orginal_data){
foreach ($data as $key => &$item){
......@@ -1054,6 +1056,7 @@ class MediaModel extends Eloquent
$item['media_name'] = '未查到相关数据';
$item['forvery_url'] = '未查到相关数据';
$item['is_full_url'] = "未查到相关数据";
$item['name'] = "未查到相关数据";
}else{
......@@ -1064,6 +1067,8 @@ class MediaModel extends Eloquent
$item['_id'] = $item['id'];
$item['media_name'] = $media['media_name'];
$item['name'] = $orginal_data[$item['_id']]['name'];
$tmp = [
'media_num' => $media_num,
......@@ -1073,6 +1078,7 @@ class MediaModel extends Eloquent
$url = getenv('preview_domain')?getenv('preview_domain'):"https://xue.t.eoffcn.com/preview/temp/";
$item['forvery_url'] = $item['url'];
$item['is_full_url'] = $url."player".'/'.$media_num;
$item['name'] = $orginal_data[$media_id]['name'];
unset($item['id']);
unset($item['key']);
unset($item['status']);
......
......@@ -31,7 +31,7 @@
<!--***** CONTENT *****-->
<div class="row ml20 batch-list">
<form action="{{ asset('/web/media_manager/import_batch_media') }}" method="post" enctype="multipart/form-data">
<form>
{{ method_field('POST')}}
<input type="hidden" name="_token" value="{{csrf_token()}}"/>
......@@ -40,7 +40,7 @@
<div class="form-group">
<input type="file" class="btn btn-info" id="uploadFile" name="uploadFile" accept=".xls,.xlsx">
</div>
<button class="btn btn-info batch-media" type="submit" id="batch-media" disabled onclick="test()" >导出</button>
<button class="btn btn-info batch-media" type="button" id="batch-media" disabled onclick="test()" >导出</button>
</form>
......@@ -112,16 +112,56 @@
*/
function test() {
var formData = new FormData();
formData.append("uploadFile",$("#uploadFile")[0].files[0]);
$.ajax({
url:'/web/media_manager/import_batch_media', /*接口域名地址*/
type:'post',
data: formData,
processData: false,
contentType: false,
success:function(res){
if(res.code === 200){
$('body').toast({
position: 'fixed',
content: '导入成功,数据处理中......',
duration: 3000,
top: '50%'
});
setTimeout(function () {
$("#batch-media").attr("disabled",true);
$("#uploadFile").val("")
}, 2000)
window.location.href = "/web/media_manager/download/"+res.data
}else {
$('body').toast({
position: 'fixed',
content: res.msg,
duration: 3000,
top: '50%'
});
}
}
})
// $('body').toast({
// position: 'fixed',
// content: '导入成功,数据处理中......',
// duration: 3000,
// top: '50%'
// });
// setTimeout(function () {
// $("#batch-media").attr("disabled",true);
// $("#uploadFile").val("")
// }, 2000)
}
</script>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment