From 102c0e990b4b7cc8eb8d06664ae235aba4904ea5 Mon Sep 17 00:00:00 2001 From: pengda <10266652509@qq.com> Date: Thu, 5 Sep 2024 17:30:25 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=BC=E5=87=BApdf=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- control/index.php | 73 ++++++++++++++----- model/mUserCase.php | 134 ++++++++++++++++------------------- view/templates/index/export_pdf.html | 2 +- 3 files changed, 118 insertions(+), 91 deletions(-) diff --git a/control/index.php b/control/index.php index 37f4838..48360c6 100644 --- a/control/index.php +++ b/control/index.php @@ -151,7 +151,7 @@ class index extends publicBase { 'per_page' => $page_size, 'last_page' => ceil($total / $page_size), 'current_page' => $page_num, - 'data' => $data['list'], + 'data' => $data['user_case'], 'case_data' => $data['case_data'] ); @@ -239,17 +239,21 @@ class index extends publicBase { $page_num = $this->post('page_num') ? $this->post('page_num') : 1; $page_size = $this->post('page_size') ? $this->post('page_size') : 100; + $data = array( + "uid" => $uid, + "token" => $token, + "page_num" => $page_num, + "page_size" => $page_size, + ); $m_user_case = new mUserCase(); - $data = $m_user_case->getUserCaseList($uid, $page_num, $page_size, true, true); - if (!$data) $this->ajax_json(false, $m_user_case->getError()); - $total = $m_user_case->getUserCaseListCount($uid); + $return = $m_user_case->postCUrl(USER_CASE_LIST_HTML_URL, $data); - $data_key = sprintf(RQ_USER_CASE_DATA, $uid, 0); + $res = json_decode($return, 1); + if (isset($res['info'])) $this->ajax_json(false, $res['info']); - $robj = $m_user_case->initRedis(); - $robj->setex($data_key, 60, json_encode($data)); - $pdf_url = $m_user_case->createPdf($uid, $data_key); + $pdf_url = $m_user_case->createPdf($uid, $return); + $total = $m_user_case->getUserCaseListCount($uid); $rdata = array( 'total' => $total, 'per_page' => $page_size, @@ -271,27 +275,58 @@ class index extends publicBase { $is_login = $m_user->validateToken($uid, $token); if (!$is_login) $this->ajax_json(false, '请登录后操作'); + $data = array( + "uid" => $uid, + "token" => $token, + "id" => $id, + ); $m_user_case = new mUserCase(); - $data = $m_user_case->getUserCaseInfo($uid, $id, true); - if (!$data) $this->ajax_json(false, $m_user_case->getError()); + $return = $m_user_case->postCUrl(USER_CASE_HTML_URL, $data); - $data_key = sprintf(RQ_USER_CASE_DATA, $uid, $id); + $res = json_decode($return, 1); + if (isset($res['info'])) $this->ajax_json(false, $res['info']); - $robj = $m_user_case->initRedis(); - $robj->setex($data_key, 60, json_encode($data)); - $pdf_url = $m_user_case->createPdf($uid, $data_key); + $pdf_url = $m_user_case->createPdf($uid, $return); $this->ajax_json(true, '获取成功', array('pdf_url' => $pdf_url)); } - public function export_pdf() { - $key = $this->get('key'); + public function export_case_list() { + $uid = $this->post('uid'); + $token = $this->post('token'); + if (empty($uid) || empty($token)) $this->ajax_json(false, '非法请求'); + + $m_user = new mUser(); + $is_login = $m_user->validateToken($uid, $token); + if (!$is_login) $this->ajax_json(false, '您还没有登录'); + + $page_num = $this->post('page_num') ? $this->post('page_num') : 1; + $page_size = $this->post('page_size') ? $this->post('page_size') : 100; $m_user_case = new mUserCase(); - $robj = $m_user_case->initRedis(); - $data = $robj->get($key); + $data = $m_user_case->getUserCaseListPdfInfo($uid, $page_num, $page_size); + if (!$data) $this->ajax_json(false, $m_user_case->getError()); + + $this->view['data'] = $data['user_case']; + $this->setViewTpl('index/export_pdf.html'); + } + + public function export_case() { + $uid = $this->post('uid'); + $token = $this->post('token'); + $id = $this->post('id') + 0; + if (empty($uid) || empty($token) || empty($id)) $this->ajax_json(false, '非法请求'); + + $m_user = new mUser(); + $is_login = $m_user->validateToken($uid, $token); + if (!$is_login) $this->ajax_json(false, '您还没有登录'); + + $m_user_case = new mUserCase(); + $data = $m_user_case->getUserCasePdfInfo($uid, $id); + if (!$data) $this->ajax_json(false, $m_user_case->getError()); - $this->view['data'] = json_decode($data, true); + $this->view['data'] = $data['user_case']; + $this->setViewTpl('index/export_pdf.html'); } public function ajax_mail() { diff --git a/model/mUserCase.php b/model/mUserCase.php index 451caff..910a084 100644 --- a/model/mUserCase.php +++ b/model/mUserCase.php @@ -149,56 +149,33 @@ class mUserCase extends mBase { return $id; } - public function getUserCaseInfo($uid, $id, $is_format_data = false) { + public function getUserCaseInfo($uid, $id) { $user_case = $this->obj->select($this->tbl, array('sql' => '`id`=? and `uid`=?', 'vals' => array($id, $uid))); if (empty($user_case)) { $this->setError('找不到相关医案'); return false; } - $prescribe_herb = json_decode($user_case['prescribe_herb'], true); - $user_case['prescribe_herb'] = $prescribe_herb; - $mCase = new mCase(); - $case = $mCase->getCaseById($user_case['case_id']); + $case = $mCase->getCaseInfo($user_case['case_id']); if (empty($case)) { $this->setError('找不到相关药方'); return false; } - $herb_ids = $user_herb_ids = array(); - foreach ($prescribe_herb as $item) { - if (strpos($item['herb_id'], 'u_') !== false) { - $user_herb_ids[] = str_replace('u_', '', $item['herb_id']); - continue; - } - $herb_ids[] = $item['herb_id']; - } - if (empty($herb_ids) && empty($user_herb_ids)) { - $this->setError('医案开药详情不正确'); - return false; - } + $prescribe_herb = json_decode($user_case['prescribe_herb'], true); + $user_case['prescribe_herb'] = $prescribe_herb; - $mCase = new mCase(); - $herb = $mCase->getHerbByIds($herb_ids); - $user_herb = $this->getUserHerbByIds($user_herb_ids); - if (empty($herb) && empty($user_herb)) { - $this->setError('找不到相关药材'); - return false; - } + $user_case_herb = $this->getUserCaseHerb(array($prescribe_herb)); - $data = array( - 'data' => $user_case, + return array( + 'user_case' => $user_case, + 'user_case_herb' => $user_case_herb, 'case_data' => $case, - 'herb_data' => array_column($herb,null,'id'), - 'user_herb_data' => array_column($user_herb,null,'id'), ); - - if ($is_format_data) return $this->formatUserCaseData($data); - return $data; } - public function getUserCaseList($uid, $page_num, $page_size, $get_case_herb = false, $is_format_data = false) { + public function getUserCaseList($uid, $page_num, $page_size) { $offset = ($page_num - 1) * $page_size; $user_case = $this->obj->selectAll($this->tbl, array('sql' => '`uid`=?', 'vals' => array($uid)), 'case_time desc ', array($offset, $page_size)); if (empty($user_case)) { @@ -215,16 +192,15 @@ class mUserCase extends mBase { return false; } - $case = array_column($case, null, 'id'); - - if (!$get_case_herb) return array('list' => $user_case, 'case_data' => $case); + return array( + 'user_case' => $user_case, + 'case_data' => array_column($case, null, 'id') + ); + } - //获取医案开药详情 + public function getUserCaseHerb($prescribe_herb_arr) { $herb_ids = $user_herb_ids = array(); - foreach ($user_case as &$item) { - $prescribe_herb = json_decode($item['prescribe_herb'], true); - $item['prescribe_herb'] = $prescribe_herb; - + foreach ($prescribe_herb_arr as $prescribe_herb) { foreach ($prescribe_herb as $herb_item) { if (strpos($herb_item['herb_id'], 'u_') !== false) { $user_herb_ids[] = str_replace('u_', '', $herb_item['herb_id']); @@ -233,27 +209,29 @@ class mUserCase extends mBase { $herb_ids[] = $herb_item['herb_id']; } } + if (empty($herb_ids) && empty($user_herb_ids)) { $this->setError('医案开药详情不正确'); return false; } + $m_case = new mCase(); $herb = $m_case->getHerbByIds($herb_ids); - $user_herb = $this->getUserHerbByIds($user_herb_ids); - if (empty($herb) && empty($user_herb)) { - $this->setError('找不到相关药材'); - return false; - } + if (!empty($user_herb_ids)) { + $user_herb = $this->getUserHerbByIds($user_herb_ids); + if (empty($user_herb)) { + $this->setError('找不到相关自定义药材'); + return false; + } - $data = array( - 'list' => $user_case, - 'case_data' => $case, - 'herb_data' => array_column($herb,null,'id'), - 'user_herb_data' => array_column($user_herb,null,'id'), - ); + foreach ($user_herb as &$item) { + $item['id'] = "u_" . $item['id']; + } - if ($is_format_data) return $this->formatUserCaseData($data); - return $data; + $herb = array_merge($herb, $user_herb); + } + + return array_column($herb, null, 'id'); } public function getUserCaseListCount($uid) { @@ -264,15 +242,34 @@ class mUserCase extends mBase { return $this->obj->selectIn($this->user_herb_tbl, array('id' => $ids)); } + public function getUserCaseListPdfInfo($uid, $page_num, $page_size) { + $data = $this->getUserCaseList($uid, $page_num, $page_size); + + $prescribe_herb_arr = array(); + foreach ($data['user_case'] as &$value) { + $value['prescribe_herb'] = json_decode($value['prescribe_herb'], true); + $prescribe_herb_arr[] = $value['prescribe_herb']; + } + + $data['user_case_herb'] = $this->getUserCaseHerb($prescribe_herb_arr); + + return $this->formatUserCaseData($data); + } + + public function getUserCasePdfInfo($uid, $id) { + $data = $this->getUserCaseInfo($uid, $id); + + return $this->formatUserCaseData($data); + } + public function formatUserCaseData($data) { //兼容单个医案导出pdf - if (isset($data['data'])) { - $data['list'][0] = $data['data']; - unset($data['data']); - $data['case_data'] = array_column(array($data['case_data']), null, 'id'); + if (isset($data['user_case']['id'])) { + $data['user_case'] = array($data['user_case']); + $data['case_data'] = array($data['case_data']['id'] => $data['case_data']); } - foreach ($data['list'] as &$item) { + foreach ($data['user_case'] as &$item) { $item['patient_sex'] = $item['patient_sex'] == 0 ? '男' : '女'; $item['case_time'] = date('Y年m月d日', strtotime($item['case_time'])); @@ -281,12 +278,8 @@ class mUserCase extends mBase { $herb_arr = array(); foreach ($item['prescribe_herb'] as $herb) { - if (strpos($herb['herb_id'], 'u_') !== false) { - $herb['herb_id'] = str_replace('u_', '', $herb['herb_id']); - $herb_name = $data['user_herb_data'][$herb['herb_id']]['name']; - } else { - $herb_name = $data['herb_data'][$herb['herb_id']]['name']; - } + $herb_name = $data['user_case_herb'][$herb['herb_id']]['name']; + $herb_arr[] = $herb_name . $herb['num'] . "克"; } $herb_str = implode('、', $herb_arr); @@ -297,13 +290,8 @@ class mUserCase extends mBase { return $data; } - public function createPdf($uid, $data_key) { - $mpdf = new mPDF(); - $htmlContent = file_get_contents(sprintf(RQ_USER_CASE_HTML_URL, $data_key)); - - $mpdf->WriteHTML($htmlContent); - - $pdf_name = md5($data_key); + public function createPdf($uid, $pdf_html) { + $pdf_name = md5($uid); $temp_dir = sprintf(USER_CASE_PDF_PATH, $uid); if (!is_dir($temp_dir)) { @@ -312,6 +300,10 @@ class mUserCase extends mBase { chgrp($temp_dir, 'nobody'); } + $mpdf = new mPDF(); + + $mpdf->WriteHTML($pdf_html); + $mpdf->Output($temp_dir . $pdf_name . ".pdf", 'F'); // D表示下载,I表示在浏览器中查看 return sprintf(USER_CASE_PDF_URL, $uid, $pdf_name); diff --git a/view/templates/index/export_pdf.html b/view/templates/index/export_pdf.html index 5cef995..ebe47f0 100644 --- a/view/templates/index/export_pdf.html +++ b/view/templates/index/export_pdf.html @@ -46,7 +46,7 @@