From 33350cfd47d8826f0fd75d4d8c5b6bae7d96e1fd Mon Sep 17 00:00:00 2001 From: callmeyan Date: Fri, 21 Jun 2019 13:21:08 +0800 Subject: [PATCH] todo download --- app/controller/Admin.php | 40 +++++++++++++++++++++++++++++--------- app/util/ListCountData.php | 10 ++++++++-- 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/app/controller/Admin.php b/app/controller/Admin.php index 64178ce..d4ddc1e 100644 --- a/app/controller/Admin.php +++ b/app/controller/Admin.php @@ -42,7 +42,7 @@ class Admin extends BaseController if (empty($user) || $user->isEmpty()) { return ErrorResponse::createError(ErrorCode::ERROR_ADMIN_LOGIN_PWD, '用户名或者密码错误(1)'); } - if ($user->password != md5($username.$password . $user->salt)) { + if ($user->password != md5($username . $password . $user->salt)) { return ErrorResponse::createError(ErrorCode::ERROR_ADMIN_LOGIN_PWD, '用户名或者密码错误(2)');; } $data = $user->getPartData(['id', 'username', 'email', 'avatar', 'last_login', 'sex']); @@ -100,34 +100,56 @@ class Admin extends BaseController { $resultType = $this->request->get('resultType'); if ($resultType == null) $resultType = EvaluationService::ResultAll; - list($page,$size) = $this->getPageParam(); + list($page, $size) = $this->getPageParam(); $data = EvaluationService::search( intval($resultType), $this->request->get('name'), $this->request->get('sort', EvaluationService::SortByTime), - $page,$size + $page, $size ); return SuccessResponse::create($data->toArray()); } - public function downloadEvaluation(){ + public function downloadEvaluation() + { $resultType = $this->request->get('resultType'); if ($resultType == null) $resultType = EvaluationService::ResultAll; - list($page,$size) = $this->getPageParam(); + list($page, $size) = $this->getPageParam(); $data = EvaluationService::search( intval($resultType), $this->request->get('name'), $this->request->get('sort', EvaluationService::SortByTime), - $page,$size); + $page, $size); $dataType = $this->request->post('type'); $excel = new Spreadsheet(); $sheet = $excel->getActiveSheet(); -// $rowNumber = - $sheet->setCellValue('A1', 'Hello World !'); + $rowNumber = 1; + $titles = [ + 'A' => ['text' => '自评时间', 'key' => 'create_time'], + 'B' => ['text' => '姓名', 'key' => 'realname'], + 'C' => ['text' => '头痛症状', 'key' => 'headache'], + 'D' => ['text' => '胃肠道症状', 'key' => 'gastrointestinal'], + 'E' => ['text' => '劳累或疲劳', 'key' => 'tired'], + 'F' => ['text' => '头晕或眩晕', 'key' => 'dizzy'], + 'G' => ['text' => '总分', 'key' => 'score'], + 'H' => ['text' => '评估结果', 'key' => 'resultType'], + 'I' => ['text' => '性别', 'key' => 'gender'], + 'J' => ['text' => '年龄', 'key' => 'age'] + ]; + $fields = explode(',', 'A,B,C,D,E,F,G.H'); + foreach ($fields as $f) { + $sheet->setCellValue($f . $rowNumber, $titles[$f]); + } + foreach ($data->dataList as $item) { + $rowNumber++; + foreach ($fields as $f) { + $sheet->setCellValue($f . $rowNumber, $item[$f]); + } + } header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); $filename = urlencode("评估记录_") . date('mdHi'); @@ -135,7 +157,7 @@ class Admin extends BaseController if ($dataType == 'xlsx') { $writer = new Xlsx($excel); $filename .= '.xlsx'; - }else { + } else { $filename .= '.csv'; } header('Content-Disposition: attachment;filename=' . $filename); diff --git a/app/util/ListCountData.php b/app/util/ListCountData.php index df2f8a1..e5c65b7 100644 --- a/app/util/ListCountData.php +++ b/app/util/ListCountData.php @@ -11,16 +11,22 @@ namespace app\util; class ListCountData { + /** + * @var int + */ public $totalCount; + /** + * @var array + */ public $dataList; - public function __construct(int $count, $data) + public function __construct(int $count,array $data) { $this->totalCount = $count; $this->dataList = $data; } - public static function Create(int $count, $data) + public static function Create(int $count, array $data) { return new ListCountData($count, $data); }