diff --git a/control/index.php b/control/index.php index faa9132..b8851d2 100644 --- a/control/index.php +++ b/control/index.php @@ -75,73 +75,42 @@ class index extends publicBase { public function ajax_save_user_case() { $uid = $this->post('uid'); $token = $this->post('token'); - 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, '请登录后操作'); - - $mUserCase = new mUserCase(); - - $name = $this->post('name'); - if(empty($name)) $this->ajax_json(false, '药方名称不能为空'); + $case_id = $this->post('case_id')+0; + if(empty($uid) || empty($token) || empty($case_id))$this->ajax_json(false, '非法请求'); - $id = $this->post('id'); - if($id){ - $data = array( - 'uid' => $uid, - 'name' => $_POST['name'], - 'feedback' => $_POST['feedback'], - ); + $data = array( + 'name' => trim($this->post('name')), + 'patient_name' => trim($this->post('patient_name')), + 'patient_age' => $this->post('patient_age')+0, + 'patient_sex' => $this->post('patient_sex')+0, + 'patient_say' => trim($this->post('patient_say')), + 'first_diagnosis' => trim($this->post('first_diagnosis')), + 'diagnosis' => trim($this->post('diagnosis')), + 'prescribe_num' => $this->post('prescribe_num')+0, + 'prescribe_herb' => $this->post('prescribe_herb'), + ); - $res = $mUserCase->updateUserCase($id,$data); - if(!$res)$this->ajax_json(false, $mUserCase->getError()); + $m_user_case = new mUserCase(); + $id = $m_user_case->createUserCase($uid, $token, $case_id, $data); + if(!$id)$this->ajax_json(false, $m_user_case->getError()); - $this->ajax_json(true, '保存成功'); - } + $this->ajax_json(true, '保存成功',array('id' => $id)); + } - $case_id = $this->post('case_id'); - $patient_name = $this->post('patient_name'); - $patient_age = $this->post('patient_age')+0; - $patient_sex = $this->post('patient_sex')+0; - $prescribe_num = $this->post('prescribe_num')+0; - $prescribe_herb = $this->post('prescribe_herb'); - $patient_say = $this->post('patient_say'); - $first_diagnosis = $this->post('first_diagnosis'); - $diagnosis = $this->post('diagnosis'); - - if(empty($case_id)) $this->ajax_json(false, '找不到相关药方'); - if(empty($patient_name)) $this->ajax_json(false, '患者姓名不能为空'); - if(empty($patient_age)) $this->ajax_json(false, '患者年龄不能为空'); - if(empty($prescribe_num)) $this->ajax_json(false, '开药数量不能为空'); - if(empty($prescribe_herb)) $this->ajax_json(false, '开药详情不能为空'); - if(empty($patient_say)) $this->ajax_json(false, '主诉不能为空'); - if(empty($first_diagnosis)) $this->ajax_json(false, '舌诊脉诊不能为空'); - if(empty($diagnosis)) $this->ajax_json(false, '诊断不能为空'); - - $prescribe_herb = $mUserCase->formatUserCaseHerbs($uid, $prescribe_herb); - if(!$prescribe_herb)$this->ajax_json(false, $mUserCase->getError()); + public function ajax_update_user_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, '非法请求'); $data = array( - 'name' => $name, - 'patient_name' => $patient_name, - 'patient_age' => $patient_age, - 'patient_sex' => $patient_sex, - 'patient_say' => $patient_say, - 'first_diagnosis' => $first_diagnosis, - 'diagnosis' => $diagnosis, - 'prescribe_num' => $prescribe_num, - 'prescribe_herb' => $prescribe_herb, - 'case_id' => $case_id, - 'uid' => $uid, + 'name' => trim($this->post('name')), + 'feedback' => trim($this->post('feedback')), ); - $id = $mUserCase->createUserCase($data); - if(!$id)$this->ajax_json(false, '保存失败'); - - //更新药方使用次数 这个后期会增加定时脚本去修正 - $mCase = new mCase(); - $mCase->updateCaseSearchNum($id); + $m_user_case = new mUserCase(); + $res = $m_user_case->updateUserCase($uid, $token, $id, $data); + if(!$res)$this->ajax_json(false, $m_user_case->getError()); $this->ajax_json(true, '保存成功',array('id' => $id)); } diff --git a/model/mUserCase.php b/model/mUserCase.php index 27082bc..0c3cc95 100644 --- a/model/mUserCase.php +++ b/model/mUserCase.php @@ -15,21 +15,16 @@ class mUserCase extends mBase { $this->user_herb_tbl = 'tcm_user_herb'; } - public function updateUserCase($id,$data){ - if(empty($data['name'])){ - $this->setError('药方名称不能为空'); - return false; - } - if(empty($data['feedback'])){ - $this->setError('用药反馈不能为空'); - return false; - } + public function updateUserCase($uid, $token, $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, $data['uid']))); - if(!$res){ - $this->setError('更新失败'); - return false; - } + $m_user = new mUser(); + $is_login = $m_user->validateToken($uid,$token); + if(!$is_login){$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;} return true; } @@ -38,10 +33,7 @@ class mUserCase extends mBase { $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){ @@ -68,10 +60,7 @@ class mUserCase extends mBase { } $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; @@ -80,8 +69,39 @@ class mUserCase extends mBase { return json_encode($data); } - public function createUserCase($info){ - return $this->obj->insert($this->tbl, $info); + public function createUserCase($uid, $token, $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_user = new mUser(); + $is_login = $m_user->validateToken($uid,$token); + if(!$is_login){$this->setError('请登录后操作');return false;} + + $m_case = new mCase(); + $case = $m_case->getCaseById($case_id); + if(!$case){$this->setError('找不到相关药方');return false;} + + //格式化医案开药详情 + $prescribe_herb = $this->formatUserCaseHerbs($uid, $data['prescribe_herb']); + + $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;} + + //更新药方使用次数 这个后期会增加定时脚本去修正 + $m_case->updateCaseSearchNum($case_id); + + return $id; } public function getUserCaseInfo($id, $uid){