Browse Source

导出医案接口优化

pull/1/head
pengda 8 months ago
parent
commit
fe7cf2b840
  1. 41
      control/index.php
  2. 57
      model/mUserCase.php

41
control/index.php

@ -342,50 +342,29 @@ class index extends publicBase {
public function export_user_case() {
$uinfo = $this->_check_login();
//导出单个医案
$id = $this->post('id') + 0;
if ($id > 0) {
$post_url = USER_CASE_HTML_URL;
$data = array(
"uid" => $uinfo['uid'],
"id" => $id,
);
}
//导出全部医案
if ($id == 0) {
$start_date = $this->post('start_date');
//if (empty($start_date)) $this->ajax_json(false, '请选择导出开始时间');
$end_date = $this->post('end_date');
//if (empty($end_date)) $this->ajax_json(false, '请选择导出结束时间');
$post_url = USER_CASE_LIST_HTML_URL;
$data = array(
"uid" => $uinfo['uid'],
"start_date" => $start_date,
"end_date" => $end_date,
);
}
$m_user_case = new mUserCase();
$return = $m_user_case->postCUrl($post_url, $data);
$res = json_decode($return, 1);
if (isset($res['info'])) $this->ajax_json(false, '未查询到可导出的医案记录');
$pdf_name = md5($uinfo['uid'] . $id) . ".pdf";
$m_user_case->createPdf($uinfo['uid'], $pdf_name, $return);
$pdf_info = $m_user_case->getPdf($uinfo['uid'], $id, $start_date, $end_date);
$email = $this->post('email');
if (!empty($email)) {
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) $this->ajax_json(false, '邮箱地址无效');
$res = $m_user_case->sendMail(array($email), date('Y年m月d日', time()) . '-医案导出', '', sprintf(USER_CASE_PDF_PATH, $uinfo['uid']) . $pdf_name);
if (!$res) $this->ajax_json(true, '发送失败');
//生成唯一id 防止重复请求
$request_id = md5($email . $pdf_info['pdf_path']);
$request_times = $m_user_case->requestLimit(sprintf(_QR_REQUEST_LIMIT, $request_id), 1, 7200);
if (!$request_times) $this->ajax_json(false, "医案记录已发送至您的邮箱,请查收邮件");
$res = $m_user_case->sendPdf($email, $pdf_info['pdf_path']);
if (!$res) $this->ajax_json(false, $m_user_case->getError());
$this->ajax_json(true, '发送成功');
}
$this->ajax_json(true, '获取成功', array('pdf_url' => sprintf(USER_CASE_PDF_URL, $uinfo['uid']) . $pdf_name));
$this->ajax_json(true, '获取成功', array('pdf_url' => $pdf_info['pdf_url']));
}
public function user_case_list() {

57
model/mUserCase.php

@ -355,8 +355,50 @@ class mUserCase extends mBase {
return $data;
}
public function createPdf($uid, $pdf_name, $pdf_html) {
public function getPdf($uid, $id, $start_date = '', $end_date = '') {
$pdf_name = md5($uid . $id . $start_date . $end_date) . ".pdf";
$temp_dir = sprintf(USER_CASE_PDF_PATH, $uid);
$temp_url = sprintf(USER_CASE_PDF_URL, $uid);
if (file_exists($temp_dir . $pdf_name)) {
return array(
'pdf_path' => $temp_dir . $pdf_name,
'pdf_url' => $temp_url . $pdf_name,
);
}
//导出单个医案
if ($id > 0) {
$post_url = USER_CASE_HTML_URL;
$data = array(
"uid" => $uid,
"id" => $id,
);
} else {
$post_url = USER_CASE_LIST_HTML_URL;
$data = array(
"uid" => $uid,
"start_date" => $start_date,
"end_date" => $end_date,
);
}
$return = $this->postCUrl($post_url, $data);
$res = json_decode($return, 1);
if (isset($res['info'])) {
$this->setError('抱歉,未找到可导出的医案记录');
return false;
}
$this->createPdf($temp_dir, $pdf_name, $return);
return array(
'pdf_path' => $temp_dir . $pdf_name,
'pdf_url' => $temp_url . $pdf_name,
);
}
public function createPdf($temp_dir, $pdf_name, $pdf_html) {
if (!is_dir($temp_dir)) {
mkdir($temp_dir, 0755, true);
chown($temp_dir, 'nobody');
@ -369,4 +411,17 @@ class mUserCase extends mBase {
return $mpdf->Output($temp_dir . $pdf_name, 'F'); // D表示下载,I表示在浏览器中查看
}
function sendPdf($email, $pdf_path) {
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$this->setError('请填写合法的邮箱地址');
return false;
}
$res = $this->sendMail(array($email), date('Y年m月d日', time()) . '-医案导出', '', $pdf_path);
if (!$res) {
$this->setError('发送失败');
return false;
}
return true;
}
}
Loading…
Cancel
Save