From 8be522f110a3b300ad912353e826275d45dc6517 Mon Sep 17 00:00:00 2001 From: hant Date: Fri, 11 Jul 2025 00:08:29 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=BC=E5=87=BA=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/admin/command/Test.php | 33 ++++----- application/admin/controller/Order.php | 99 +++++++++++++++++++++++-- application/admin/view/order/index.html | 3 + public/assets/js/backend/order.js | 27 +++++++ 4 files changed, 136 insertions(+), 26 deletions(-) diff --git a/application/admin/command/Test.php b/application/admin/command/Test.php index a9190af..4ad4148 100644 --- a/application/admin/command/Test.php +++ b/application/admin/command/Test.php @@ -2,26 +2,13 @@ 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 think\Collection; +use app\admin\controller\AutoDispatchLogic; +use app\admin\model\Order; use think\console\Command; use think\console\Input; use think\console\Output; -use think\Db; -use think\exception\DbException; -use think\Hook; -use think\Lang; -use think\Model; -use function Symfony\Component\Clock\now; +use think\Exception; class Test extends Command { @@ -35,8 +22,18 @@ class Test extends Command protected function execute(Input $input, Output $output) { - $order = Order::where('id',221)->select()[0]; - AutoDispatchLogic::autoDispatch($order); +// $order = Order::where('id','>=',694)->select(); + try { + $order = Order::get(5126); + AutoDispatchLogic::autoDispatch($order,null,true); + }catch (Exception $e){ + $output->writeln('' . $e->getMessage() . ''); + $output->writeln('' . $e->getFile() . ':' . $e->getLine() . ''); + $output->writeln($e->getTraceAsString()); + } + + dd(1); + } diff --git a/application/admin/controller/Order.php b/application/admin/controller/Order.php index d395d67..63a35ab 100644 --- a/application/admin/controller/Order.php +++ b/application/admin/controller/Order.php @@ -19,6 +19,7 @@ use app\common\Logic\OrderLogic; use Carbon\Carbon; use Carbon\Traits\Creator; use fast\Tree; +use PhpOffice\PhpSpreadsheet\Settings; use think\Db; use think\Exception; use think\exception\DbException; @@ -28,6 +29,8 @@ use think\Hook; use think\Loader; use think\Model; 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']); $group = \model('auth_group_access')->where('uid', $this->auth->id)->find()->group_id ?? 0; $user = \model('admin')->find($this->auth->id); - $this->assignconfig('permissions', [ 'add' => $this->auth->check('order/add'), '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(); } //如果发送的来源是 Selectpage,则转发到 Selectpage @@ -216,7 +217,7 @@ class Order extends Backend }); } - $list = $build + $build ->with(['user' => function ($q) { $q->field('id,nickname'); }, 'area' => function ($q) { @@ -231,8 +232,15 @@ class Order extends Backend } ]] ) - ->order($sort, $order) - ->paginate($limit); + ->order($sort, $order); + if ($getArray){ + $list = $build->paginate([ + 'list_rows' => $input_limit, + 'page' => $page + ]); + }else{ + $list = $build->paginate($limit); + } foreach ($list as $item) { $item->aftersale_btn = false; @@ -246,7 +254,6 @@ class Order extends Backend } unset($item->source); } - $result = ['total' => $list->total(), 'rows' => $list->items()]; return json($result); } @@ -1076,4 +1083,80 @@ class Order extends Backend 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 + ]); + } + + } diff --git a/application/admin/view/order/index.html b/application/admin/view/order/index.html index 2b4db2e..1af2926 100644 --- a/application/admin/view/order/index.html +++ b/application/admin/view/order/index.html @@ -24,6 +24,9 @@
{:__('Edit')} + + 导出 + diff --git a/public/assets/js/backend/order.js b/public/assets/js/backend/order.js index 5ff7b6c..def402a 100644 --- a/public/assets/js/backend/order.js +++ b/public/assets/js/backend/order.js @@ -95,6 +95,7 @@ ${data.receive_type == 1 ? '已收定金' : '已收全款'} renderDefault: true, searchFormVisible: true, search: false, + showExport: false, columns: [ [ {checkbox: true}, @@ -530,6 +531,32 @@ ${data.receive_type == 1 ? '已收定金' : '已收全款'} 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 () { $("#mybuttom").on("click", function () {