From bd7f62b9eddd0b79ad26166c860cfe2052d2ecd2 Mon Sep 17 00:00:00 2001 From: JaguarJack Date: Tue, 8 Sep 2020 14:12:42 +0800 Subject: [PATCH] =?UTF-8?q?add:=E6=96=B0=E5=A2=9E=E5=AF=BC=E5=87=BA?= =?UTF-8?q?=E7=94=A8=E6=88=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- catch/permissions/controller/User.php | 16 ++++++ catch/permissions/excel/UserExport.php | 68 ++++++++++++++++++++++++++ catch/permissions/route.php | 1 + 3 files changed, 85 insertions(+) diff --git a/catch/permissions/controller/User.php b/catch/permissions/controller/User.php index cf0d1a2..b1710e0 100644 --- a/catch/permissions/controller/User.php +++ b/catch/permissions/controller/User.php @@ -1,6 +1,7 @@ $roleIds, ]); } + + /** + * 导出 + * + * @time 2020年09月08日 + * @param Excel $excel + * @param UserExport $userExport + * @throws \PhpOffice\PhpSpreadsheet\Exception + * @return \think\response\Json + */ + public function export(Excel $excel, UserExport $userExport) + { + return CatchResponse::success($excel->save($userExport, Utils::publicPath('export/users'))); + } } diff --git a/catch/permissions/excel/UserExport.php b/catch/permissions/excel/UserExport.php index ef90e0a..f60f6c5 100644 --- a/catch/permissions/excel/UserExport.php +++ b/catch/permissions/excel/UserExport.php @@ -8,3 +8,71 @@ // +---------------------------------------------------------------------- // | Author: JaguarJack [ njphper@gmail.com ] // +---------------------------------------------------------------------- +namespace catchAdmin\permissions\excel; + +use catchAdmin\permissions\model\Users; +use catcher\library\excel\ExcelContract; +use PhpOffice\PhpSpreadsheet\Style\Alignment; + +class UserExport implements ExcelContract +{ + + /** + * 设置头部 + * + * @time 2020年09月08日 + * @return string[] + */ + public function headers(): array + { + // TODO: Implement headers() method. + return [ + 'id', '用户名', '邮箱', '状态', '创建日期' + ]; + } + + /** + * 处理数据 + * + * @time 2020年09月08日 + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + * @return \think\Collection + */ + public function sheets() + { + // TODO: Implement sheets() method. + $users = Users::field(['id', 'username', 'email', 'status', 'created_at'])->select(); + + foreach ($users as &$user) { + $user->status = $user->status == Users::ENABLE ? '启用' : '停用'; + } + + return $users; + } + + /** + * 设置开始行 + * + * @time 2020年09月08日 + * @return int + */ + public function setRow() + { + return 2; + } + + /** + * 设置标题 + * + * @time 2020年09月08日 + * @return array + */ + public function setTitle() + { + return [ + 'A1:G1', '导出用户', Alignment::HORIZONTAL_CENTER + ]; + } +} \ No newline at end of file diff --git a/catch/permissions/route.php b/catch/permissions/route.php index d8ceab8..a3c1c4e 100644 --- a/catch/permissions/route.php +++ b/catch/permissions/route.php @@ -23,4 +23,5 @@ $router->group(function () use ($router){ $router->put('users/recover/', '\catchAdmin\permissions\controller\User@recover'); $router->get('users/get/roles', '\catchAdmin\permissions\controller\User@getRoles'); $router->get('user/info', '\catchAdmin\permissions\controller\User@info'); + $router->get('user/export', '\catchAdmin\permissions\controller\User@export'); })->middleware('auth'); \ No newline at end of file