Skip to content

Commit

Permalink
fix: export 对象参数处理
Browse files Browse the repository at this point in the history
  • Loading branch information
slowlyo committed Nov 16, 2024
1 parent c35a7ee commit f9e2027
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Traits/ElementTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ protected function exportAction($disableSelectedItem = false)
$buttons = [
// 导出全部
amis()->VanillaAction()->label(admin_trans('admin.export.all'))->onEvent(
$event("let data=event.data;let params=Object.keys(data).filter(key=>key!=='page' && key!=='__super').reduce((obj,key)=>{obj[key]=data[key];return obj;},{});let url=new URL('{$exportPath}',window.location.origin);Object.keys(params).forEach(key=>url.searchParams.append(key,params[key]));{$doAction}")
$event("let data=event.data;let params=Object.keys(data).filter(key=>key!=='page' && key!=='__super').reduce((obj,key)=>{obj[key]=data[key];return obj;},{});let url=new URL('{$exportPath}',window.location.origin);Object.keys(params).forEach(key=>url.searchParams.append(key,(typeof params[key] == 'string' ? params[key] : JSON.stringify(params[key]))));{$doAction}")
),
// 导出本页
amis()->VanillaAction()->label(admin_trans('admin.export.page'))->onEvent(
Expand Down
13 changes: 11 additions & 2 deletions src/Traits/ExportTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,24 @@ protected function export()
{
admin_abort_if(!class_exists('\Rap2hpoutre\FastExcel\FastExcel'), admin_trans('admin.export.please_install_laravel_excel'));

// 处理可能存在的对象参数
request()->replace(array_map(fn($i) => is_json($i) ? json_decode($i, true) : $i, request()->all()));

// 默认在 storage/app/ 下
$path = sprintf('%s-%s.xlsx', $this->exportFileName(), date('YmdHis'));

// 导出本页和导出选中项都是通过 _ids 查询
$ids = request()->input('_ids');

// listQuery() 为列表查询条件,与获取列表数据一致
$query = $this->service->listQuery()
->when($ids, fn($query) => $query->whereIn($this->service->getModel()->getTable() . '.' . $this->service->primaryKey(), explode(',', $ids)));
$query = $this->service
->listQuery()
->when($ids, function ($query) use ($ids) {
return $query->whereIn(
$this->service->getModel()->getTable() . '.' . $this->service->primaryKey(),
explode(',', $ids)
);
});

try {
fastexcel($query->get())->export(storage_path('app/' . $path), fn($row) => $this->exportMap($row));
Expand Down

0 comments on commit f9e2027

Please sign in to comment.