Skip to content

Commit 0dcecd2

Browse files
committed
fix(repo): 具体处理下载失败的任务逻辑代码
1 parent 10be81f commit 0dcecd2

File tree

1 file changed

+45
-4
lines changed

1 file changed

+45
-4
lines changed

laravel/app/Http/Repository/ExcelRepository.php

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
namespace App\Http\Repository;
1010

1111

12+
use App\Models\ApiExcel;
13+
use App\Models\ApiExcelLogs;
1214
use Illuminate\Support\Facades\Log;
1315
use PhpOffice\PhpSpreadsheet\IOFactory;
1416
use PhpOffice\PhpSpreadsheet\Reader\Exception;
@@ -123,10 +125,11 @@ public function newLoadExcel()
123125
*
124126
* @param $param
125127
* @param $result
128+
* @param $file
126129
*
127-
* @return bool|string
130+
* @return string
128131
*/
129-
public function saveExcel($param, $result)
132+
public function saveExcel($param, $result, $file = '')
130133
{
131134
/************************* 2. 写入 Excel 文件 ******************************/
132135
// 首先创建一个新的对象 PHPExcel object
@@ -239,17 +242,55 @@ public function saveExcel($param, $result)
239242
// is_dir($path) || mkdir($path, 777, true);
240243
$did = explode('/', $param['website']);
241244
$did = is_numeric(end($did)) ? end($did) : '999';
242-
$fileName = '/out-'.$did.'-'.date('YmdHis').uniqid().'.xlsx';
245+
$fileName = '/out-'.$file.$did.'-'.date('YmdHis').uniqid().'.xlsx';
243246
$objWriter->save($path.$fileName);
244247

245248
return '/storage'.$fileName;
246249
} catch (\PhpOffice\PhpSpreadsheet\Exception|\PhpOffice\PhpSpreadsheet\Writer\Exception $exception) {
247250
// 记录任务失败的错误日志
248251
Log::error('Api_Excel 任务执行失败: ', ['error' => $exception]);
249-
return false;
252+
return '';
250253
}
251254
}
252255

256+
257+
/**
258+
* @param $api_excel_id
259+
* @param $user_id
260+
*
261+
* @return bool|string
262+
*/
263+
public function exportExcelLogs($api_excel_id, $user_id)
264+
{
265+
$api_excel = ApiExcel::find($api_excel_id);
266+
if ($api_excel->finish_url) {
267+
return $api_excel->finish_url;
268+
}
269+
270+
if ($api_excel && ($user_id == 1 || $user_id == $api_excel['uid'])) {
271+
// 有权限查询
272+
$param = $api_excel->apiParam;
273+
274+
$excel_logs = ApiExcelLogs::where('api_excel_id', $api_excel_id)->orderBy('sort_index')->get();
275+
276+
$result = [];
277+
foreach ($excel_logs as $key => $excel_log) {
278+
$reqParam = explode('|', $excel_log->param);
279+
$result[] = ['param' => $reqParam, 'result' => $excel_log->result];
280+
}
281+
if (!$result) {
282+
return '';
283+
}
284+
$failed_done_file = $this->saveExcel($param, $result, 'failed-done-');
285+
$api_excel->finish_url = $failed_done_file;
286+
$api_excel->save();
287+
288+
return $failed_done_file;
289+
}
290+
291+
return false;
292+
}
293+
253294
/**
254295
* BusRepository constructor.
255296
*/

0 commit comments

Comments
 (0)