From 537896470feaa1599c494a25774effab552f31f7 Mon Sep 17 00:00:00 2001 From: pengda <10266652509@qq.com> Date: Tue, 3 Sep 2024 10:53:29 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E9=A3=8E=E6=A0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- control/index.php | 71 ++++++++++---------- index.php | 12 ++-- model/mCase.php | 173 ++++++++++++++++++++++++++++++++---------------- model/mUser.php | 12 ++-- model/mUserCase.php | 186 ++++++++++++++++++++++++++++++++++++---------------- 5 files changed, 293 insertions(+), 161 deletions(-) diff --git a/control/index.php b/control/index.php index 8099590..fe13886 100644 --- a/control/index.php +++ b/control/index.php @@ -47,7 +47,7 @@ class index extends publicBase { public function ajax_search() { $content = $this->post('content'); - if(empty($content))$this->ajax_json(false, '请输入方名'); + if (empty($content))$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; @@ -61,11 +61,11 @@ class index extends publicBase { public function ajax_case_detail() { $id = $this->get('id')+0; - if(empty($id))$this->ajax_json(false, '非法请求'); + if (empty($id)) $this->ajax_json(false, '非法请求'); $m_case = new mCase(); $data = $m_case->getCaseInfo($id); - if(!$data)$this->ajax_json(false, $m_case->getError()); + if (!$data) $this->ajax_json(false, $m_case->getError()); $this->ajax_json(true, '获取成功', $data); } @@ -74,11 +74,11 @@ class index extends publicBase { $uid = $this->post('uid'); $token = $this->post('token'); $case_id = $this->post('case_id')+0; - if(empty($uid) || empty($token) || empty($case_id)) $this->ajax_json(false, '非法请求'); + if (empty($uid) || empty($token) || empty($case_id)) $this->ajax_json(false, '非法请求'); $m_user = new mUser(); $is_login = $m_user->validateToken($uid,$token); - if(!$is_login) $this->ajax_json(false, '请登录后操作'); + if (!$is_login) $this->ajax_json(false, '请登录后操作'); $data = array( 'name' => trim($this->post('name')), @@ -94,7 +94,7 @@ class index extends publicBase { $m_user_case = new mUserCase(); $id = $m_user_case->createUserCase($uid, $case_id, $data); - if(!$id) $this->ajax_json(false, $m_user_case->getError()); + if (!$id) $this->ajax_json(false, $m_user_case->getError()); $this->ajax_json(true, '保存成功',array('id' => $id)); } @@ -103,11 +103,11 @@ class index extends publicBase { $uid = $this->post('uid'); $token = $this->post('token'); $id = $this->post('id')+0; - if(empty($uid) || empty($token) || empty($id))$this->ajax_json(false, '非法请求'); + 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, '请登录后操作'); + if (!$is_login) $this->ajax_json(false, '请登录后操作'); $data = array( 'name' => trim($this->post('name')), @@ -116,7 +116,7 @@ class index extends publicBase { $m_user_case = new mUserCase(); $res = $m_user_case->updateUserCase($uid, $id, $data); - if(!$res)$this->ajax_json(false, $m_user_case->getError()); + if (!$res) $this->ajax_json(false, $m_user_case->getError()); $this->ajax_json(true, '保存成功',array('id' => $id)); } @@ -124,18 +124,18 @@ class index extends publicBase { public function ajax_user_case_list() { $uid = $this->post('uid'); $token = $this->post('token'); - if(empty($uid) || empty($token))$this->ajax_json(false, '非法请求'); + 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, '请登录后操作'); + 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(); $data = $m_user_case->getUserCaseList($uid, $page_num, $page_size); - if(!$data)$this->ajax_json(false, $m_user_case->getError()); + if (!$data) $this->ajax_json(false, $m_user_case->getError()); $total = $m_user_case->getUserCaseListCount($uid); $return = array( @@ -152,15 +152,15 @@ class index extends publicBase { $uid = $this->post('uid'); $token = $this->post('token'); $id = $this->post('id')+0; - if(empty($uid) || empty($token) || empty($id)) $this->ajax_json(false, '非法请求'); + 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, '请登录后操作'); + if (!$is_login) $this->ajax_json(false, '请登录后操作'); $m_user_case = new mUserCase(); $data = $m_user_case->getUserCaseInfo($uid, $id); - if(!$data) $this->ajax_json(false, $m_user_case->getError()); + if (!$data) $this->ajax_json(false, $m_user_case->getError()); $this->ajax_json(true, '获取成功', $data); } @@ -178,14 +178,14 @@ class index extends publicBase { public function ajax_login(){ $code = $this->get('code'); - if(!$code)$this->ajax_json(false, '非法请求'); + if (!$code)$this->ajax_json(false, '非法请求'); $m_user = new mUser(); $openid = $m_user->getOpenid($code); - if(!$openid)$this->ajax_json(false, $m_user->getError()); + if (!$openid)$this->ajax_json(false, $m_user->getError()); $user = $m_user->getUserByOpenid($openid); - if($user){ + if ($user){ $token = $m_user->getToken($user['uid']); $this->ajax_json(true, '登录成功', array('uid' => $user['uid'], 'token'=> $token)); } @@ -193,10 +193,10 @@ class index extends publicBase { //获取唯一uid串 $uid = $m_user->createUniqueUid($openid); $user = $m_user->getUserByUid($uid); - if($user)$this->ajax_json(false, 'uid生成失败'); + if ($user)$this->ajax_json(false, 'uid生成失败'); $id = $m_user->createUser(array('uid'=>$uid,'openid'=>$openid)); - if(!$id)$this->ajax_json(false, '用户创建失败'); + if (!$id)$this->ajax_json(false, '用户创建失败'); $token = $m_user->getToken($user['uid']); $this->ajax_json(true, '登录成功', array('uid' => $user['uid'], 'token'=> $token)); @@ -209,18 +209,18 @@ class index extends publicBase { public function export_user_case_list(){ $uid = $this->post('uid'); $token = $this->post('token'); - if(empty($uid) || empty($token)) $this->ajax_json(false, '非法请求'); + 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, '请登录后操作'); + 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(); $data = $m_user_case->getUserCaseList($uid, $page_num, $page_size, true, true); - if(!$data) $this->ajax_json(false, $m_user_case->getError()); + if (!$data) $this->ajax_json(false, $m_user_case->getError()); $total = $m_user_case->getUserCaseListCount($uid); $data_key = sprintf(RQ_USER_CASE_DATA, $uid, 0); @@ -236,15 +236,15 @@ class index extends publicBase { $uid = $this->post('uid'); $token = $this->post('token'); $id = $this->post('id')+0; - if(empty($uid) || empty($token) || empty($id)) $this->ajax_json(false, '非法请求'); + 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, '请登录后操作'); + if (!$is_login) $this->ajax_json(false, '请登录后操作'); $m_user_case = new mUserCase(); $data = $m_user_case->getUserCaseInfo($uid, $id, true); - if(!$data) $this->ajax_json(false, $m_user_case->getError()); + if (!$data) $this->ajax_json(false, $m_user_case->getError()); $data_key = sprintf(RQ_USER_CASE_DATA, $uid, $id); @@ -255,8 +255,7 @@ class index extends publicBase { $this->ajax_json(true, '获取成功',array('pdf_url'=> $pdf_url)); } - public function export_pdf() - { + public function export_pdf(){ $key = $this->get('key'); $m_user_case = new mUserCase(); @@ -269,26 +268,26 @@ class index extends publicBase { public function ajax_mail(){ $uid = $this->post('uid'); $token = $this->post('token'); - if(empty($uid) || empty($token)) $this->ajax_json(false, '非法请求'); + if (empty($uid) || empty($token)) $this->ajax_json(false, '非法请求'); $mUser = new mUser(); $is_login = $mUser->validateToken($uid,$token); - if(!$is_login) $this->ajax_json(false, '请登录后操作'); + if (!$is_login) $this->ajax_json(false, '请登录后操作'); $email = $this->post('email'); - if(empty($email)) $this->ajax_json(false, '邮箱地址不能为空'); - if(!filter_var($email, FILTER_VALIDATE_EMAIL)) $this->ajax_json(false, '邮箱地址无效'); + if (empty($email)) $this->ajax_json(false, '邮箱地址不能为空'); + if (!filter_var($email, FILTER_VALIDATE_EMAIL)) $this->ajax_json(false, '邮箱地址无效'); $pdf_url = $this->post('pdf_url'); - if(empty($pdf_url)) $this->ajax_json(false, 'pdf地址不能为空'); - if(!filter_var($pdf_url, FILTER_VALIDATE_URL)) $this->ajax_json(false, 'pdf地址无效'); + if (empty($pdf_url)) $this->ajax_json(false, 'pdf地址不能为空'); + if (!filter_var($pdf_url, FILTER_VALIDATE_URL)) $this->ajax_json(false, 'pdf地址无效'); $directory_name = basename(dirname($pdf_url)); - if($directory_name != $uid) $this->ajax_json(false, '非法请求'); + if ($directory_name != $uid) $this->ajax_json(false, '非法请求'); $mUserCase = new mUserCase(); $res = $mUserCase->sendMail(array($email),date('Y年m月d日',time()).'-医案导出','',sprintf(USER_CASE_PDF_PATH, $uid) . basename($pdf_url)); - if(!$res) $this->ajax_json(true, '发送失败'); + if (!$res) $this->ajax_json(true, '发送失败'); $this->ajax_json(true, '发送成功'); } diff --git a/index.php b/index.php index f058e13..8fd8f15 100644 --- a/index.php +++ b/index.php @@ -17,7 +17,7 @@ private function checkpara() { $argv = trim(trim($_GET['argv']),'/'); - if(empty($argv)) { + if (empty($argv)) { $this->control_name = 'index'; $this->control_func = 'home'; } else { @@ -27,7 +27,7 @@ } // 还原GET - if(!empty($array)) { + if (!empty($array)) { unset($_GET['argv']); unset($array[0]); unset($array[1]); @@ -40,7 +40,7 @@ // 如果URI带有常规传参形式,并入$_GET $pos = strpos($_SERVER['REQUEST_URI'], '?'); - if($pos!==false) { + if ($pos!==false) { $new_uri = substr($_SERVER['REQUEST_URI'], $pos+1); $new_uri_arr = explode("&", $new_uri); foreach($new_uri_arr as $v) { @@ -55,7 +55,7 @@ // 判断控制层文件是否存在 $control_path = dirname(__FILE__).'/control/'.$this->control_name.'.php'; - if(!file_exists($control_path)) { + if (!file_exists($control_path)) { die('/'.$this->control_name.'.php not exist.'); } include_once ($control_path); @@ -76,7 +76,7 @@ die('method '.$this->control_func.' not exist.'); } - if($obj->getViewFormat()=='json' && $res===false) { + if ($obj->getViewFormat()=='json' && $res===false) { echo urldecode($obj->getError()); exit; } @@ -87,7 +87,7 @@ } private function display() { - if($this->viewFormat=='json') { + if ($this->viewFormat=='json') { $display = new DisplayJson(); } elseif($this->viewFormat=='string') { $display = new DisplayNone(); diff --git a/model/mCase.php b/model/mCase.php index 7c6caa3..acb5c64 100644 --- a/model/mCase.php +++ b/model/mCase.php @@ -20,15 +20,33 @@ class mCase extends mBase { $this->collect_log_tbl = 'tcm_collect_log'; } - public function createCase($name, $source, $original, $method, $herbs){ - if(empty($name)){$this->setError('药方名称不能为空');return false;} - if(empty($source)){$this->setError('药方来源不能为空');return false;} - if(empty($original)){$this->setError('药方原方不能为空');return false;} - if(empty($method)){$this->setError('药方用法不能为空');return false;} - if(empty($herbs)){$this->setError('药方药材不能为空');return false;} + public function createCase($name, $source, $original, $method, $herbs) { + if (empty($name)) { + $this->setError('药方名称不能为空'); + return false; + } + if (empty($source)) { + $this->setError('药方来源不能为空'); + return false; + } + if (empty($original)) { + $this->setError('药方原方不能为空'); + return false; + } + if (empty($method)) { + $this->setError('药方用法不能为空'); + return false; + } + if (empty($herbs)) { + $this->setError('药方药材不能为空'); + return false; + } $id = $this->obj->insert($this->tbl, array('name'=>$name, 'source'=>$source, 'original'=>$original, 'method'=>$method)); - if(!$id){$this->setError('添加失败');return false;} + if (!$id) { + $this->setError('添加失败'); + return false; + } //格式化药方对应的药材数据 $case_herb = $this->formatCaseHerb($id, $herbs); @@ -38,17 +56,31 @@ class mCase extends mBase { return $id; } - public function updateCase($id, $name, $source, $original, $method, $herbs){ - if(empty($herbs)){$this->setError('药方药材不能为空');return false;} + public function updateCase($id, $name, $source, $original, $method, $herbs) { + if (empty($herbs)) { + $this->setError('药方药材不能为空'); + return false; + } $data = array(); - if(!empty($name)){$data['name']=$name;} - if(!empty($source)){$data['source']=$source;} - if(!empty($original)){$data['original']=$original;} - if(!empty($method)){$data['method']=$method;} + if (!empty($name)) { + $data['name']=$name; + } + if (!empty($source)) { + $data['source']=$source; + } + if (!empty($original)) { + $data['original']=$original; + } + if (!empty($method)) { + $data['method']=$method; + } - $res = $this->obj->update($this->tbl, $data, array('sql'=>'`id`=?', 'vals'=>array($id))); - if(!$res){$this->setError('更新失败');return false;} + $res = $this->obj->update($this->tbl, $data, array('sql' => '`id`=?', 'vals' => array($id))); + if (!$res) { + $this->setError('更新失败'); + return false; + } //格式化药方对应的药材数据 $case_herb = $this->formatCaseHerb($id, $herbs); @@ -58,107 +90,134 @@ class mCase extends mBase { return true; } - public function formatCaseHerb($case_id,$herbs){ + public function formatCaseHerb($case_id,$herbs) { $case_herb = array(); - foreach ($herbs as $key=>$item) { + foreach ($herbs as $key => $item) { $temp = array(); - $temp['case_id']=$case_id; - $temp['num']=$item['num']; - $temp['sort']=$key; + $temp['case_id'] = $case_id; + $temp['num'] = $item['num']; + $temp['sort'] = $key; $herb = $this->getHerbByName($item['name']); - if($herb){ + if ($herb) { $temp['herb_id'] = $herb['id']; }else{ - $temp['herb_id'] = $this->createHerb(array('name'=>$item['name'])); + $temp['herb_id'] = $this->createHerb(array('name' => $item['name'])); } $case_herb[] = $temp; } return $case_herb; } - public function getHerbByName($name){ - return $this->obj->select($this->herb_tbl, array('sql'=>'`name`=?', 'vals'=>array($name))); + public function getHerbByName($name) { + return $this->obj->select($this->herb_tbl, array('sql' => '`name`=?', 'vals' => array($name))); } - public function createHerb($info){ + public function createHerb($info) { $herb_id = $this->obj->insert($this->herb_tbl, $info); - if(empty($herb_id)){$this->setError('添加药材失败');return false;} + if (empty($herb_id)) { + $this->setError('添加药材失败'); + return false; + } return $herb_id; } - public function createCaseHerb($info){ + public function createCaseHerb($info) { $res = $this->obj->mutiInsert($this->case_herb_tbl, $info); - if(!$res){$this->setError('添加药方药材失败');return false;} + if (!$res) { + $this->setError('添加药方药材失败'); + return false; + } return true; } - public function compareCaseHerb($id,$new_data){ + public function compareCaseHerb($id,$new_data) { $old_data = $this->getCaseHerbByCaseId($id); $old_num = count($old_data); $new_num = count($new_data); - if($old_num>=$new_num){ + if ($old_num>=$new_num) { //需要删除药方对应的药材 foreach ($old_data as $key => $value) { - if(!isset($new_data[$key])){ - $res = $this->obj->delete($this->case_herb_tbl, array('sql'=>'`id`=?', 'vals'=>array($value['id']))); - if(!$res){$this->setError('删除药方药材失败');return false;} + if (!isset($new_data[$key])) { + $res = $this->obj->delete($this->case_herb_tbl, array('sql' => '`id`=?', 'vals' => array($value['id']))); + if (!$res) { + $this->setError('删除药方药材失败'); + return false; + } + } + $res = $this->obj->update($this->case_herb_tbl, $new_data[$key], array('sql' => '`id`=?', 'vals' => array($value['id']))); + if (!$res) { + $this->setError('更新药方药材失败'); + return false; } - $res = $this->obj->update($this->case_herb_tbl, $new_data[$key], array('sql'=>'`id`=?', 'vals'=>array($value['id']))); - if(!$res){$this->setError('更新药方药材失败');return false;} } }else{ //需要增加药方对应的药材 foreach ($new_data as $key => $value) { - if(!isset($old_data[$key])){ + if (!isset($old_data[$key])) { $res = $this->obj->insert($this->case_herb_tbl, $value); - if(!$res){$this->setError('添加药方药材失败');return false;} + if (!$res) { + $this->setError('添加药方药材失败'); + return false; + } + } + $res = $this->obj->update($this->case_herb_tbl, $value, array('sql' => '`id`=?', 'vals' => array($old_data[$key]['id']))); + if (!$res) { + $this->setError('更新药方药材失败'); + return false; } - $res = $this->obj->update($this->case_herb_tbl, $value, array('sql'=>'`id`=?', 'vals'=>array($old_data[$key]['id']))); - if(!$res){$this->setError('更新药方药材失败');return false;} } } return true; } - public function getCaseHerbByCaseId($case_id){ - return $this->obj->selectAll($this->case_herb_tbl, array('sql'=>'`case_id`=?','vals'=>array($case_id)), 'sort asc '); + public function getCaseHerbByCaseId($case_id) { + return $this->obj->selectAll($this->case_herb_tbl, array('sql' => '`case_id`=?','vals' => array($case_id)), 'sort asc '); } public function getCaseByName($name, $page_num, $page_size) { $sql = " `name` like '%{$name}%'"; $offset = ($page_num - 1) * $page_size; - $res = $this->obj->selectAll($this->tbl, array('sql'=>$sql, 'vals'=>array()), 'use_num desc,sort asc ', array($offset, $page_size)); + $res = $this->obj->selectAll($this->tbl, array('sql' => $sql, 'vals'=>array()), 'use_num desc,sort asc ', array($offset, $page_size)); - if(empty($res)) return array(); + if (empty($res)) return array(); return $res; } public function getCaseByNameTotal($name) { $sql = " `name` like '%{$name}%'"; - return $this->obj->count($this->tbl, array('sql'=>$sql, 'vals'=>array())); + return $this->obj->count($this->tbl, array('sql' => $sql, 'vals'=>array())); } public function getCaseInfo($id) { //药方信息 $case = $this->getCaseById($id); - if(empty($case)){$this->setError('查询不到此药方');return false;} + if (empty($case)) { + $this->setError('查询不到此药方'); + return false; + } //药方药材信息 $case_herb = $this->getCaseHerbByCaseId($case['id']); - if(empty($case_herb)){$this->setError('药方药材查询失败');return false;} + if (empty($case_herb)) { + $this->setError('药方药材查询失败'); + return false; + } //药材名称 $herb_ids = array_column($case_herb,'herb_id'); $herb = $this->getHerbByIds($herb_ids); - if(empty($herb)){$this->setError('药材名称查询失败');return false;} + if (empty($herb)) { + $this->setError('药材名称查询失败'); + return false; + } return array( 'case' => $case, @@ -167,21 +226,21 @@ class mCase extends mBase { ); } - public function getCaseById($id){ - return $this->obj->select($this->tbl, array('sql'=>'`id`=?','vals'=>array($id))); + public function getCaseById($id) { + return $this->obj->select($this->tbl, array('sql' => '`id`=?','vals' => array($id))); } - public function getHerbByIds($ids){ - return $this->obj->selectIn($this->herb_tbl, array('id'=>$ids)); + public function getHerbByIds($ids) { + return $this->obj->selectIn($this->herb_tbl, array('id' => $ids)); } - public function getCaseByIds($ids){ - return $this->obj->selectIn($this->tbl, array('id'=>$ids)); + public function getCaseByIds($ids) { + return $this->obj->selectIn($this->tbl, array('id' => $ids)); } - public function updateCaseSearchNum($id){ - $res = $this->obj->increase($this->tbl, array('sql'=>'`id`=?', 'vals'=>array($id)),'use_num'); - if(!$res){ + public function updateCaseSearchNum($id) { + $res = $this->obj->increase($this->tbl, array('sql' => '`id`=?', 'vals' => array($id)),'use_num'); + if (!$res) { $tool_obj = new qTool(); $tool_obj->trackLog('tcm', $id, sprintf(LOG_TRACK_SAVE_PATH, date('Y-m-d'), 'tcm_case_use_num')); @@ -198,7 +257,7 @@ class mCase extends mBase { } public function getCollectLogTotal() { - return $this->obj->count($this->collect_log_tbl, array()); + return $this->obj->count($this->collect_log_tbl); } } \ No newline at end of file diff --git a/model/mUser.php b/model/mUser.php index a94be83..859f600 100644 --- a/model/mUser.php +++ b/model/mUser.php @@ -22,7 +22,7 @@ class mUser extends mBase { $jsonres = $this->getCUrl($get_openid_url); $get_openid_res = json_decode($jsonres, true); - if(empty($get_openid_res['openid'])){ + if (empty($get_openid_res['openid'])) { $this->setError('获取openid失败'); $tool_obj = new qTool(); $tool_obj->trackLog('tcm', $get_openid_url . "|" .$jsonres, sprintf(LOG_TRACK_SAVE_PATH, date('Y-m-d'), 'tcm_get_openid')); @@ -33,7 +33,7 @@ class mUser extends mBase { } public function getUserByOpenid($openid) { - return $this->obj->select($this->tbl, array('sql'=>'`openid`=?','vals'=>array($openid))); + return $this->obj->select($this->tbl, array('sql' => '`openid`=?','vals' => array($openid))); } function createUniqueUid($openid) { @@ -44,14 +44,14 @@ class mUser extends mBase { } public function getUserByUid($uid) { - return $this->obj->select($this->tbl, array('sql'=>'`uid`=?','vals'=>array($uid))); + return $this->obj->select($this->tbl, array('sql' => '`uid`=?','vals' => array($uid))); } - public function createUser($info){ + public function createUser($info) { return $this->obj->insert($this->tbl, $info); } - public function getToken($uid){ + public function getToken($uid) { $secretKey = JWT_KEY; $timestamp = time(); $data = $uid . '|' . $timestamp; @@ -59,7 +59,7 @@ class mUser extends mBase { return base64_encode($data . '|' . $token); } - public function validateToken($uid, $token){ + public function validateToken($uid, $token) { $secretKey = JWT_KEY; $decodedToken = base64_decode($token); list($valid_uid, $timestamp, $tokenHash) = explode('|', $decodedToken); diff --git a/model/mUserCase.php b/model/mUserCase.php index deae062..7873e9f 100644 --- a/model/mUserCase.php +++ b/model/mUserCase.php @@ -15,48 +15,63 @@ class mUserCase extends mBase { $this->user_herb_tbl = 'tcm_user_herb'; } - public function updateUserCase($uid, $id, $data){ - if(empty($data['name'])){$this->setError('药方名称不能为空');return false;} - if(empty($data['feedback'])){$this->setError('用药反馈不能为空');return false;} + public function updateUserCase($uid, $id, $data) { + if (empty($data['name'])) { + $this->setError('药方名称不能为空'); + return false; + } + if (empty($data['feedback'])) { + $this->setError('用药反馈不能为空'); + return false; + } $res = $this->obj->update($this->tbl, $data, array('sql'=>'`id`=? and `uid`=?', 'vals'=>array($id, $uid))); - if(!$res){$this->setError('更新失败');return false;} + if (!$res) { + $this->setError('更新失败'); + return false; + } return true; } - public function formatUserCaseHerbs($uid, $prescribe_herb){ + public function formatUserCaseHerbs($uid, $prescribe_herb) { $mCase = new mCase(); $prescribe_herb = json_decode($prescribe_herb,true); - if(empty($prescribe_herb)){$this->setError('开药详情为空');return false;} + if (empty($prescribe_herb)) { + $this->setError('开药详情为空'); + return false; + } $data = array(); - foreach ($prescribe_herb as $item){ + foreach ($prescribe_herb as $item) { $temp = array(); $temp['num'] = $item['num']; - if(isset($item['herb_id'])){ + if (isset($item['herb_id'])) { $temp['herb_id'] = $item['herb_id']; $data[] = $temp; continue; } $herb = $mCase->getHerbByName($item['name']); - if($herb){ + if ($herb) { $temp['herb_id'] = $herb['id']; $data[] = $temp; continue; } $userherb = $this->obj->select($this->user_herb_tbl, array('sql'=>'`name`=?', 'vals'=>array($item['name']))); - if($userherb){ + if ($userherb) { $temp['herb_id'] = 'u_'.$userherb['id']; $data[] = $temp; continue; } $user_herb_id = $this->obj->insert($this->user_herb_tbl, array('uid' => $uid,'name'=> $item['name'])); - if(!$user_herb_id){$this->setError('添加自定义药材失败');return false;} + if (!$user_herb_id) { + $this->setError('添加自定义药材失败'); + return false; + } $temp['herb_id'] = 'u_'.$user_herb_id; $data[] = $temp; @@ -65,31 +80,67 @@ class mUserCase extends mBase { return json_encode($data); } - public function createUserCase($uid, $case_id, $data){ - if($case_id<=0){$this->setError('找不到相关药方');return false;} - if(empty($data['patient_name'])){$this->setError('患者姓名不能为空');return false;} - if($data['patient_age']<=0){$this->setError('患者年龄不正确');return false;} - if($data['sex']<0){$this->setError('患者性别不能为空');return false;} - if($data['prescribe_num']<=0){$this->setError('开药数量不正确');return false;} - if(empty($data['prescribe_herb'])){$this->setError('开药详情不能为空');return false;} - if(empty($data['patient_say'])){$this->setError('主诉不能为空');return false;} - if(empty($data['first_diagnosis'])){$this->setError('舌诊脉诊不能为空');return false;} - if(empty($data['diagnosis'])){$this->setError('诊断不能为空');return false;} + public function createUserCase($uid, $case_id, $data) { + if ($case_id <= 0) { + $this->setError('找不到相关药方'); + return false; + } + if (empty($data['patient_name'])) { + $this->setError('患者姓名不能为空'); + return false; + } + if ($data['patient_age'] <= 0) { + $this->setError('患者年龄不正确'); + return false; + } + if ($data['sex']<0) { + $this->setError('患者性别不能为空'); + return false; + } + if ($data['prescribe_num'] <= 0) { + $this->setError('开药数量不正确'); + return false; + } + if (empty($data['prescribe_herb'])) { + $this->setError('开药详情不能为空'); + return false; + } + if (empty($data['patient_say'])) { + $this->setError('主诉不能为空'); + return false; + } + if (empty($data['first_diagnosis'])) { + $this->setError('舌诊脉诊不能为空'); + return false; + } + if (empty($data['diagnosis'])) { + $this->setError('诊断不能为空'); + return false; + } $m_case = new mCase(); $case = $m_case->getCaseById($case_id); - if(!$case){$this->setError('找不到相关药方');return false;} + if (!$case) { + $this->setError('找不到相关药方'); + return false; + } //格式化医案开药详情 $prescribe_herb = $this->formatUserCaseHerbs($uid, $data['prescribe_herb']); - if(!$prescribe_herb){$this->setError('医案开药详情不正确');return false;} + if (!$prescribe_herb) { + $this->setError('医案开药详情不正确'); + return false; + } $data['prescribe_herb'] = $prescribe_herb; $data['case_id'] = $case['id']; $data['uid'] = $uid; $id = $this->obj->insert($this->tbl, $data); - if(!$id){$this->setError('医案保存失败');return false;} + if (!$id) { + $this->setError('医案保存失败'); + return false; + } //更新药方使用次数 这个后期会增加定时脚本去修正 $m_case->updateCaseSearchNum($case_id); @@ -97,31 +148,43 @@ class mUserCase extends mBase { return $id; } - public function getUserCaseInfo($uid, $id, $is_format_data = false){ + public function getUserCaseInfo($uid, $id, $is_format_data = false) { $user_case = $this->obj->select($this->tbl, array('sql'=>'`id`=? and `uid`=?', 'vals'=>array($id, $uid))); - if(empty($user_case)){$this->setError('找不到相关医案');return false;} + 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']); - if(empty($case)){$this->setError('找不到相关药方');return false;} + 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){ + 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']; + $herb_ids[] = $item['herb_id']; + } + if (empty($herb_ids) && empty($user_herb_ids)) { + $this->setError('医案开药详情不正确'); + return false; } - if(empty($herb_ids) && empty($user_herb_ids)){$this->setError('医案开药详情不正确');return false;} $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;} + if (empty($herb) && empty($user_herb)) { + $this->setError('找不到相关药材'); + return false; + } $data = array( 'data' => $user_case, @@ -130,44 +193,56 @@ class mUserCase extends mBase { 'user_herb_data' => array_column($user_herb,null,'id'), ); - if($is_format_data)return $this->formatUserCaseData($data); + 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, $get_case_herb = false, $is_format_data = false) { $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)){$this->setError('找不到相关医案');return false;} + if (empty($user_case)) { + $this->setError('找不到相关医案'); + return false; + } $case_ids = array_column($user_case,'case_id'); $m_case = new mCase(); $case = $m_case->getCaseByIds($case_ids); - if(empty($case)){$this->setError('找不到相关药方');return false;} + if (empty($case)) { + $this->setError('找不到相关药方'); + return false; + } $case = array_column($case,null,'id'); - if(!$get_case_herb)return array('list' => $user_case, 'case_data' => $case); + if (!$get_case_herb)return array('list' => $user_case, 'case_data' => $case); //获取医案开药详情 $herb_ids = $user_herb_ids = array(); - foreach ($user_case as &$item){ + foreach ($user_case as &$item) { $prescribe_herb = json_decode($item['prescribe_herb'],true); $item['prescribe_herb'] = $prescribe_herb; - foreach ($prescribe_herb as $herb_item){ - if(strpos($herb_item['herb_id'],'u_')!==false){ + foreach ($prescribe_herb as $herb_item) { + if (strpos($herb_item['herb_id'],'u_') !== false) { $user_herb_ids[] = str_replace('u_','',$herb_item['herb_id']); continue; } $herb_ids[] = $herb_item['herb_id']; } } - if(empty($herb_ids) && empty($user_herb_ids)){$this->setError('医案开药详情不正确');return false;} + if (empty($herb_ids) && empty($user_herb_ids)) { + $this->setError('医案开药详情不正确'); + return false; + } $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($herb) && empty($user_herb)) { + $this->setError('找不到相关药材'); + return false; + } $data = array( 'list' => $user_case, @@ -176,36 +251,36 @@ class mUserCase extends mBase { 'user_herb_data' => array_column($user_herb,null,'id'), ); - if($is_format_data)return $this->formatUserCaseData($data); + if ($is_format_data)return $this->formatUserCaseData($data); return $data; } - public function getUserCaseListCount($uid){ - return $this->obj->count($this->tbl, array('sql'=>'`uid`=?', 'vals'=>array($uid))); + public function getUserCaseListCount($uid) { + return $this->obj->count($this->tbl, array('sql' => '`uid`=?', 'vals' => array($uid))); } - public function getUserHerbByIds($ids){ - return $this->obj->selectIn($this->user_herb_tbl, array('id'=>$ids)); + public function getUserHerbByIds($ids) { + return $this->obj->selectIn($this->user_herb_tbl, array('id' => $ids)); } - public function formatUserCaseData($data){ + public function formatUserCaseData($data) { //兼容单个医案导出pdf - if(isset($data['data'])){ + if (isset($data['data'])) { $data['list'][0] = $data['data']; unset($data['data']); $data['case_data'] =array_column(array($data['case_data']),null,'id'); } - foreach ($data['list'] as &$item){ - $item['patient_sex'] = $item['patient_sex']==0?'男':'女'; + foreach ($data['list'] as &$item) { + $item['patient_sex'] = $item['patient_sex'] == 0 ? '男' : '女'; $item['case_time'] = date('Y年m月d日',strtotime($item['case_time'])); $case = $data['case_data'][$item['case_id']]; $item['case_info'] = "选 ".$case['source']." ".$case['name'].":"; $herb_arr = array(); - foreach ($item['prescribe_herb'] as $herb){ - if(strpos($herb['herb_id'],'u_')!==false){ + 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{ @@ -221,8 +296,7 @@ class mUserCase extends mBase { return $data; } - public function createPdf($uid, $data_key) - { + public function createPdf($uid, $data_key) { $mpdf = new mPDF(); $htmlContent = file_get_contents(sprintf(RQ_USER_CASE_HTML_URL, $data_key)); @@ -231,7 +305,7 @@ class mUserCase extends mBase { $pdf_name = md5($data_key); $temp_dir = sprintf(USER_CASE_PDF_PATH, $uid); - if(!is_dir($temp_dir)) { + if (!is_dir($temp_dir)) { mkdir($temp_dir, 0755, true); chown($temp_dir, 'nobody'); chgrp($temp_dir, 'nobody');