export order
This commit is contained in:
parent
ea69096b34
commit
2ed93cd88a
|
|
@ -4,6 +4,7 @@ namespace Beike\Admin\Http\Controllers;
|
|||
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use App\Http\Controllers\Controller as BaseController;
|
||||
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
||||
|
||||
abstract class Controller extends BaseController
|
||||
{
|
||||
|
|
@ -21,6 +22,7 @@ abstract class Controller extends BaseController
|
|||
return request('_redirect') ?? request()->header('referer', admin_route($this->defaultRoute));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取当前管理界面列表页路由
|
||||
* @return string
|
||||
|
|
@ -32,4 +34,56 @@ abstract class Controller extends BaseController
|
|||
$name = $names[1] ?? '';
|
||||
return "{$name}.index";
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 导出CSV
|
||||
*
|
||||
* @param $fileName
|
||||
* @param $items
|
||||
* @param string $module
|
||||
* @return BinaryFileResponse
|
||||
* @throws \Exception
|
||||
*/
|
||||
protected function downloadCsv($fileName, $items, string $module = ''): BinaryFileResponse
|
||||
{
|
||||
$module = $module ?: $fileName;
|
||||
$charset = app()->getLocale() == 'zh-hk' ? 'BIG5' : 'GBK';
|
||||
|
||||
if (empty($items)) {
|
||||
throw new \Exception(trans('empty_items'));
|
||||
}
|
||||
if (!str_contains($fileName, '.csv')) {
|
||||
$fileName = $fileName . '-' . date('YmdHis') . '.csv';
|
||||
}
|
||||
$headers = [
|
||||
'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0',
|
||||
'Content-type' => 'application/octet-stream',
|
||||
// 'Content-type' => 'text/csv',
|
||||
'Content-Disposition' => "attachment; filename={$fileName}",
|
||||
'Content-Transfer-Encoding' => 'binary',
|
||||
'Expires' => '0',
|
||||
'Pragma' => 'public'
|
||||
];
|
||||
|
||||
$columns = array_keys($items[0]);
|
||||
foreach ($columns as $index => $column) {
|
||||
$columns[$index] = iconv("UTF-8", "{$charset}//IGNORE", trans("$module.{$column}"));
|
||||
}
|
||||
foreach ($items as $index => $item) {
|
||||
foreach ($item as $field => $value) {
|
||||
$items[$index][$field] = iconv("UTF-8", "{$charset}//IGNORE", $value);
|
||||
}
|
||||
}
|
||||
|
||||
$filePath = storage_path('app/' . $fileName);
|
||||
$file = fopen($filePath, 'w');
|
||||
fputcsv($file, $columns);
|
||||
foreach ($items as $item) {
|
||||
fputcsv($file, $item);
|
||||
}
|
||||
fclose($file);
|
||||
|
||||
return response()->download($filePath, $fileName, $headers);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@
|
|||
|
||||
namespace Beike\Admin\Http\Controllers;
|
||||
|
||||
use Beike\Admin\Http\Resources\OrderSimple;
|
||||
use Beike\Models\Order;
|
||||
use Beike\Repositories\OrderRepo;
|
||||
use Beike\Services\StateMachineService;
|
||||
|
|
@ -40,14 +41,13 @@ class OrderController extends Controller
|
|||
*
|
||||
* @param Request $request
|
||||
* @return mixed
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function export(Request $request)
|
||||
{
|
||||
$orders = OrderRepo::filterOrders($request->all());
|
||||
$data = [
|
||||
'orders' => OrderList::collection($orders),
|
||||
];
|
||||
return view('admin::pages.orders.index', $data);
|
||||
$items = OrderSimple::collection($orders)->jsonSerialize();
|
||||
return $this->downloadCsv('orders', $items, 'order');
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
/**
|
||||
* OrderSimple.php
|
||||
*
|
||||
* @copyright 2022 opencart.cn - All Rights Reserved
|
||||
* @link http://www.guangdawangluo.com
|
||||
* @author Edward Yang <yangjin@opencart.cn>
|
||||
* @created 2022-08-24 10:51:53
|
||||
* @modified 2022-08-24 10:51:53
|
||||
*/
|
||||
|
||||
namespace Beike\Admin\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class OrderSimple extends JsonResource
|
||||
{
|
||||
public function toArray($request): array
|
||||
{
|
||||
$data = [
|
||||
'id' => $this->id,
|
||||
'number' => $this->number,
|
||||
'customer_name' => $this->customer_name,
|
||||
'email' => $this->email,
|
||||
'telephone' => $this->telephone,
|
||||
'created_at' => $this->created_at,
|
||||
'updated_at' => $this->updated_at,
|
||||
'status' => trans("admin/order.{$this->status}"),
|
||||
];
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
|
@ -8,6 +8,7 @@
|
|||
"php": "^8.0.2",
|
||||
"ext-json": "*",
|
||||
"ext-zip": "*",
|
||||
"ext-iconv": "*",
|
||||
"doctrine/dbal": "^3.3",
|
||||
"fruitcake/laravel-cors": "^2.0",
|
||||
"guzzlehttp/guzzle": "^7.2",
|
||||
|
|
|
|||
|
|
@ -0,0 +1,21 @@
|
|||
<?php
|
||||
/**
|
||||
* order.php
|
||||
*
|
||||
* @copyright 2022 opencart.cn - All Rights Reserved
|
||||
* @link http://www.guangdawangluo.com
|
||||
* @author Edward Yang <yangjin@opencart.cn>
|
||||
* @created 2022-08-02 14:22:41
|
||||
* @modified 2022-08-02 14:22:41
|
||||
*/
|
||||
|
||||
return [
|
||||
'id' => 'ID',
|
||||
'number' => 'Number',
|
||||
'customer_name' => 'Customer Name',
|
||||
'email' => 'Email',
|
||||
'telephone' => 'Telephone',
|
||||
'created_at' => 'Created',
|
||||
'updated_at' => 'Updated',
|
||||
'status' => 'Status',
|
||||
];
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
<?php
|
||||
/**
|
||||
* order.php
|
||||
*
|
||||
* @copyright 2022 opencart.cn - All Rights Reserved
|
||||
* @link http://www.guangdawangluo.com
|
||||
* @author Edward Yang <yangjin@opencart.cn>
|
||||
* @created 2022-08-02 14:22:41
|
||||
* @modified 2022-08-02 14:22:41
|
||||
*/
|
||||
|
||||
return [
|
||||
'id' => 'ID',
|
||||
'number' => '订单号',
|
||||
'customer_name' => '客户姓名',
|
||||
'email' => 'Email',
|
||||
'telephone' => '联系电话',
|
||||
'created_at' => '创建时间',
|
||||
'updated_at' => '更新时间',
|
||||
'status' => '状态',
|
||||
];
|
||||
Loading…
Reference in New Issue