导出数据

This commit is contained in:
hant 2025-07-11 00:08:29 +08:00
parent 5992ff229c
commit 8be522f110
4 changed files with 136 additions and 26 deletions

View File

@ -2,26 +2,13 @@
namespace app\admin\command; namespace app\admin\command;
use app\admin\addresmart\Address;
use app\admin\controller\AutoDispatchLogic;
use app\admin\controller\orders\DispatchLogic;
use app\admin\controller\SendMailLogic;
use app\admin\model\Order;
use app\admin\model\OrderDispatch;
use app\admin\model\OrderReview;
use app\admin\model\Worker;
use app\admin\model\WorkerItem;
use app\admin\controller\AmapTrait; use app\admin\controller\AmapTrait;
use think\Collection; use app\admin\controller\AutoDispatchLogic;
use app\admin\model\Order;
use think\console\Command; use think\console\Command;
use think\console\Input; use think\console\Input;
use think\console\Output; use think\console\Output;
use think\Db; use think\Exception;
use think\exception\DbException;
use think\Hook;
use think\Lang;
use think\Model;
use function Symfony\Component\Clock\now;
class Test extends Command class Test extends Command
{ {
@ -35,8 +22,18 @@ class Test extends Command
protected function execute(Input $input, Output $output) protected function execute(Input $input, Output $output)
{ {
$order = Order::where('id',221)->select()[0]; // $order = Order::where('id','>=',694)->select();
AutoDispatchLogic::autoDispatch($order); try {
$order = Order::get(5126);
AutoDispatchLogic::autoDispatch($order,null,true);
}catch (Exception $e){
$output->writeln('<error>' . $e->getMessage() . '</error>');
$output->writeln('<comment>' . $e->getFile() . ':' . $e->getLine() . '</comment>');
$output->writeln($e->getTraceAsString());
}
dd(1);
} }

View File

@ -19,6 +19,7 @@ use app\common\Logic\OrderLogic;
use Carbon\Carbon; use Carbon\Carbon;
use Carbon\Traits\Creator; use Carbon\Traits\Creator;
use fast\Tree; use fast\Tree;
use PhpOffice\PhpSpreadsheet\Settings;
use think\Db; use think\Db;
use think\Exception; use think\Exception;
use think\exception\DbException; use think\exception\DbException;
@ -28,6 +29,8 @@ use think\Hook;
use think\Loader; use think\Loader;
use think\Model; use think\Model;
use function Symfony\Component\Clock\now; use function Symfony\Component\Clock\now;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
/** /**
* 订单列管理 * 订单列管理
@ -127,12 +130,11 @@ class Order extends Backend
*/ */
public function index() public function index($getArray = false,$page = 0,$input_limit = 0)
{ {
$this->request->filter(['strip_tags', 'trim']); $this->request->filter(['strip_tags', 'trim']);
$group = \model('auth_group_access')->where('uid', $this->auth->id)->find()->group_id ?? 0; $group = \model('auth_group_access')->where('uid', $this->auth->id)->find()->group_id ?? 0;
$user = \model('admin')->find($this->auth->id); $user = \model('admin')->find($this->auth->id);
$this->assignconfig('permissions', [ $this->assignconfig('permissions', [
'add' => $this->auth->check('order/add'), 'add' => $this->auth->check('order/add'),
'edit' => $this->auth->check('order/edit'), 'edit' => $this->auth->check('order/edit'),
@ -149,8 +151,7 @@ class Order extends Backend
]); ]);
if (false === $this->request->isAjax()) { if (false === $this->request->isAjax() && $getArray == false) {
return $this->view->fetch(); return $this->view->fetch();
} }
//如果发送的来源是 Selectpage则转发到 Selectpage //如果发送的来源是 Selectpage则转发到 Selectpage
@ -216,7 +217,7 @@ class Order extends Backend
}); });
} }
$list = $build $build
->with(['user' => function ($q) { ->with(['user' => function ($q) {
$q->field('id,nickname'); $q->field('id,nickname');
}, 'area' => function ($q) { }, 'area' => function ($q) {
@ -231,8 +232,15 @@ class Order extends Backend
} }
]] ]]
) )
->order($sort, $order) ->order($sort, $order);
->paginate($limit); if ($getArray){
$list = $build->paginate([
'list_rows' => $input_limit,
'page' => $page
]);
}else{
$list = $build->paginate($limit);
}
foreach ($list as $item) { foreach ($list as $item) {
$item->aftersale_btn = false; $item->aftersale_btn = false;
@ -246,7 +254,6 @@ class Order extends Backend
} }
unset($item->source); unset($item->source);
} }
$result = ['total' => $list->total(), 'rows' => $list->items()]; $result = ['total' => $list->total(), 'rows' => $list->items()];
return json($result); return json($result);
} }
@ -1076,4 +1083,80 @@ class Order extends Backend
return json($result); return json($result);
} }
public function export()
{
// 默认分页配置
$limit = 1000; // 每页导出 500 条
$page = 1;
$allRows = [];
do {
// 调用 index 方法(返回 JSON 格式数据)
$result = $this->index(true,$page,$limit); // index(true) 表示返回原始数据数组而不是 view
$result = $result->getData();
if (!isset($result['rows'])) {
break;
}
$rows = $result['rows'];
$count = count($rows);
if ($count == 0 || count($allRows) > 3000) {
break;
}
$allRows = array_merge($allRows, $rows);
$page++;
} while ($count == $limit); // 如果最后一页不足 limit说明结束了
if (empty($allRows)) {
return json(['code' => 0, 'msg' => '无数据可导出']);
}
// 开启磁盘缓存,防止内存爆掉
Settings::setLocale('fr');
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
// ===== 表头处理 =====
$columns = json_decode($this->request->get('columns', ''), true);
if (!$columns && isset($allRows[0])) {
$columns = [];
foreach ($allRows[0] as $key => $value) {
$columns[] = ['field' => $key, 'title' => $key];
}
}
$titles = array_column($columns, 'title');
$fields = array_column($columns, 'field');
// 写入表头(从 A1 开始)
foreach ($titles as $col => $title) {
$sheet->setCellValueByColumnAndRow($col + 1, 1, $title);
}
// ===== 分批写入数据行 =====
$rowIndex = 2;
foreach ($allRows as $row) {
foreach ($fields as $colIndex => $field) {
$value = isset($row[$field]) ? $row[$field] : '';
$sheet->setCellValueByColumnAndRow($colIndex + 1, $rowIndex, $value);
}
$rowIndex++;
}
// 保存文件
$filename = '导出_' . date('Ymd_His') . '.xlsx';
$filepath = __DIR__ .'/' . $filename;
(new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet))->save($filepath);
return json([
'code' => 1,
'msg' => '导出成功',
'url' => request()->domain() . '/' . $filename
]);
}
} }

View File

@ -24,6 +24,9 @@
<div id="toolbar" class="toolbar"> <div id="toolbar" class="toolbar">
<a href="javascript:;" class="btn btn-primary btn-refresh" title="{:__('Refresh')}" ><i class="fa fa-refresh"></i> </a> <a href="javascript:;" class="btn btn-primary btn-refresh" title="{:__('Refresh')}" ><i class="fa fa-refresh"></i> </a>
<a href="javascript:;" class="btn btn-success btn-edit {:$auth->check('order/edit')?'':'hide'}" title="{:__('Edit')}" ><i class="fa fa-edit"></i> {:__('Edit')}</a> <a href="javascript:;" class="btn btn-success btn-edit {:$auth->check('order/edit')?'':'hide'}" title="{:__('Edit')}" ><i class="fa fa-edit"></i> {:__('Edit')}</a>
<a href="javascript:;" class="btn btn-success btn-export" id="btn-export">
<i class="fa fa-download"></i> 导出
</a>
<!-- <div class="dropdown btn-group {:$auth->check('order/multi')?'':'hide'}">--> <!-- <div class="dropdown btn-group {:$auth->check('order/multi')?'':'hide'}">-->
<!-- <a class="btn btn-primary btn-more dropdown-toggle btn-disabled disabled" data-toggle="dropdown"><i class="fa fa-cog"></i> {:__('More')}</a>--> <!-- <a class="btn btn-primary btn-more dropdown-toggle btn-disabled disabled" data-toggle="dropdown"><i class="fa fa-cog"></i> {:__('More')}</a>-->
<!-- <ul class="dropdown-menu text-left" role="menu">--> <!-- <ul class="dropdown-menu text-left" role="menu">-->

View File

@ -95,6 +95,7 @@ ${data.receive_type == 1 ? '已收定金' : '已收全款'}
renderDefault: true, renderDefault: true,
searchFormVisible: true, searchFormVisible: true,
search: false, search: false,
showExport: false,
columns: [ columns: [
[ [
{checkbox: true}, {checkbox: true},
@ -530,6 +531,32 @@ ${data.receive_type == 1 ? '已收定金' : '已收全款'}
clickParent: true clickParent: true
}); });
$('#btn-export').on('click', function () {
var options = $("#table").bootstrapTable('getOptions');
// 提取列信息(不含 checkbox
var columns = [];
$.each(options.columns[0], function (i, item) {
if (item.field && !item.checkbox && !item.visible === false) {
columns.push({
field: item.field,
title: item.title
});
}
});
var params = {
columns: JSON.stringify(columns),
filter: options.queryParams({}).filter || {},
op: options.queryParams({}).op || {},
sort: options.sortName,
order: options.sortOrder
};
// console.log($.param(params))
var url = '/admin/order/export?' + $.param(params);
window.open(url); // 发起文件下载
});
}, },
add: function () { add: function () {
$("#mybuttom").on("click", function () { $("#mybuttom").on("click", function () {