Browse Source

药方录入

药方查询接口
pull/1/head
pengda 9 months ago
parent
commit
b86f0611fa
  1. 77
      control/index.php
  2. 98
      model/mCase.php
  3. 2
      view/js/jquery-1.8.1.min.js
  4. 1147
      view/js/jquery.form.js
  5. 184
      view/templates/index/home.html

77
control/index.php

@ -7,7 +7,73 @@ include_once(dirname(dirname(__FILE__))."/library/publicBase.php");
class index extends publicBase { class index extends publicBase {
public function home() { public function home() {
$this->ajax_json(true, 'hello world'); $id = $this->get('id');
$mCase = new mCase();
$data = $mCase->getCaseInfo($id);
$this->view['data'] = $data;
$this->setViewFormat('html');
$this->setViewTpl('index/home.html');
}
public function ajax_save_case() {
$id = $this->post('id');
$name = $this->post('name');
$source = $this->post('source');
$method = $this->post('method');
$herbs = $this->post('herbs');
if(empty($name)||empty($source)||empty($method)||empty($herbs))$this->ajax_json(false, '参数错误');
$mCase = new mCase();
$case_data = array(
'name'=>$name,
'source'=>$source,
'method'=>$method,
);
if($id){
$case_id = $id;
$res = $mCase->updateCase($id, $case_data);
if(empty($res))$this->ajax_json(false, '保存失败');
}else{
$case_id = $mCase->createCase($case_data);
if(empty($case_id))$this->ajax_json(false, '添加失败');
}
$case_herb = array();
foreach ($herbs as $key=>$item) {
$temp = array();
$temp['case_id']=$case_id;
$temp['num']=$item['num'];
$temp['sort']=$key;
$herb = $mCase->getHerbByName($item['name']);
if($herb){
$temp['herb_id']=$herb['id'];
}else{
$temp['herb_id'] = $mCase->createHerb(array('name'=>$item['name']));
}
if(empty($temp['herb_id'])){
continue;
}
$case_herb[] = $temp;
}
if($id){
$odata = $mCase->getCaseHerbByCaseId($id);
$mCase->compareCaseHerb($odata,$case_herb);
$this->ajax_json(true, '保存成功');
}
$res = $mCase->createCaseHerb($case_herb);
if(empty($res))$this->ajax_json(false, '添加失败');
$this->ajax_json(true, '添加成功');
} }
public function ajax_search() { public function ajax_search() {
@ -25,9 +91,17 @@ class index extends publicBase {
public function ajax_case_detail() { public function ajax_case_detail() {
$id = $this->get('id'); $id = $this->get('id');
$is_search = $this->get('is_search');
if(empty($id))$this->ajax_json(false, '非法请求'); if(empty($id))$this->ajax_json(false, '非法请求');
$mCase = new mCase(); $mCase = new mCase();
if($is_search){
//先更新搜索次数
$mCase->updateCaseSearchNum($id);
}
//查询药方信息
$data = $mCase->getCaseInfo($id); $data = $mCase->getCaseInfo($id);
$this->ajax_json(true, '获取成功', $data); $this->ajax_json(true, '获取成功', $data);
@ -40,6 +114,7 @@ class index extends publicBase {
$this->ajax_json(true, '保存成功'); $this->ajax_json(true, '保存成功');
} }
public function ajax_user_case_list() { public function ajax_user_case_list() {
$uid = $this->post('uid'); $uid = $this->post('uid');
$token = $this->post('token'); $token = $this->post('token');

98
model/mCase.php

@ -20,4 +20,102 @@ class mCase extends mBase {
$this->collect_log_tbl = 'tcm_collect_log'; $this->collect_log_tbl = 'tcm_collect_log';
} }
public function createCase($info){
return $this->obj->insert($this->tbl, $info);
}
public function updateCase($id,$data){
return $this->obj->update($this->tbl, $data, array('sql'=>'`id`=?', 'vals'=>array($id)));
}
public function updateCaseSearchNum($id){
return $this->obj->increase($this->tbl, array('sql'=>'`id`=?', 'vals'=>array($id)),'search_num');
}
public function getCaseByName($name,$start,$pagesize) {
$sql = " `name` like '%{$name}%'";
$res = $this->obj->selectAll($this->tbl, array('sql'=>$sql, 'vals'=>array()), 'search_num desc,sort asc ', array($start, $pagesize));
if(empty($res)) return array();
return $res;
}
public function getCaseById($id){
return $this->obj->select($this->tbl, array('sql'=>'`id`=?','vals'=>array($id)));
}
public function getCaseInfo($id) {
//药方信息
$case = $this->getCaseById($id);
//药方药材信息
$case_herb = $this->getCaseHerbByCaseId($case['id']);
//药材名称
$herb_ids = array_column($case_herb,'herb_id');
$herb = $this->getHerbByIds($herb_ids);
$data = array(
'case' => $case,
'case_herb' => $case_herb,
'herb' => array_column($herb,null,'id'),
);
return $data;
}
public function getCaseByIds($ids){
return $this->obj->selectIn($this->tbl, array('id'=>$ids));
}
public function createCaseHerb($info){
return $this->obj->mutiInsert($this->case_herb_tbl, $info);
}
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 compareCaseHerb($old_data,$new_data){
$old_num = count($old_data);
$new_num = count($new_data);
//需要删除
if($old_num>=$new_num){
foreach ($old_data as $key => $value) {
if(!isset($new_data[$key])){
$this->obj->delete($this->case_herb_tbl, array('sql'=>'`id`=?', 'vals'=>array($value['id'])));
continue;
}
$this->obj->update($this->case_herb_tbl, $new_data[$key], array('sql'=>'`id`=?', 'vals'=>array($value['id'])));
}
}else{
foreach ($new_data as $key => $value) {
if(!isset($old_data[$key])){
$this->obj->insert($this->case_herb_tbl, $value);
continue;
}
$this->obj->update($this->case_herb_tbl, $value, array('sql'=>'`id`=?', 'vals'=>array($old_data[$key]['id'])));
}
}
return true;
}
public function createHerb($info){
return $this->obj->insert($this->herb_tbl, $info);
}
public function getHerbByName($name){
return $this->obj->select($this->herb_tbl, array('sql'=>'`name`=?', 'vals'=>array($name)));
}
public function getHerbByIds($ids){
return $this->obj->selectIn($this->herb_tbl, array('id'=>$ids));
}
public function getCollectLog($start,$pagesize) {
return $this->obj->selectAll($this->collect_log_tbl, array(), 'collect_time desc ', array($start, $pagesize));
}
} }

2
view/js/jquery-1.8.1.min.js

File diff suppressed because one or more lines are too long

1147
view/js/jquery.form.js

File diff suppressed because it is too large

184
view/templates/index/home.html

@ -0,0 +1,184 @@
<!DOCTYPE html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="{$smarty.const.CSS_URL}/js/jquery-1.8.1.min.js"></script>
<script type="text/javascript" src="{$smarty.const.CSS_URL}/js/jquery.form.js"></script>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>首页</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
{literal}
<style>
#prescription-form{
width: 1000px;
margin: 0 auto;
}
.form-group {
margin-bottom: 15px;
}
form-group label {
display: block;
margin-bottom: 5px;
}
.form-group input {
width: 100%;
padding: 8px;
box-sizing: border-box;
}
.form-group textarea {
width: 100%;
padding: 8px;
box-sizing: border-box;
}
.ingredient-list {
margin-top: 20px;
}
.add-button{
width: 20px;
height: 20px;
background-color: #00CC00;
border: none;
border-radius: 50px;
color: #fff;
}
.submit-btn{
margin-top: 50px;
width: 150px;
height: 30px;
background-color: #00CC00;
border: none;
border-radius:5px;
color: #fff;
}
.herb_item{
width: 500px;
position: relative;
display: flex;
line-height: 50px;
margin-top: 15px;
}
.herb_item button{
position: absolute;
left:420px;
top:15px;
width: 20px;
height: 20px;
background-color: #DD4B38;
border: none;
border-radius: 30px;
color: #fff;
}
.herb_input{
width: 450px;
}
.herb_input input{
height: 50px;
margin-right: 30px;
}
</style>
<script>
// 动态添加药材输入字段
function addIngredient() {
const ingredientList = document.getElementById('ingredient-list');
const ingredientDiv = document.createElement('div');
ingredientDiv.classList.add('herb_item');
ingredientDiv.innerHTML = `
<div class="herb_input">
<input type="text" name="herb_name[]" placeholder="药材名" required />
<input type="number" name="herb_num[]" placeholder="药材重量 (克)" required min="0" step="0.1" />
</div>
<button class="" type="button" onclick="removeIngredient(this)">X</button>
`;
ingredientList.appendChild(ingredientDiv);
}
// 移除药材输入字段
function removeIngredient(button) {
button.parentElement.remove();
}
function submitForm(e) {
e.preventDefault(); // 阻止默认表单提交
const form = document.getElementById('prescription-form');
const formData = new FormData(form);
const data = {
id: formData.get('id'),
name: formData.get('name'),
source: formData.get('source'),
method: formData.get('method'),
herbs: []
};
// 收集所有药材名称和重量
const herb_name = formData.getAll('herb_name[]');
const herb_num = formData.getAll('herb_num[]');
herb_name.forEach((name, index) => {
data.herbs.push({
name: name,
num: herb_num[index]
});
});
$.ajax({
url: 'ajax_save_case', // 替换为你的服务器端处理文件
type: 'POST',
data: data,
dataType: 'json',
success: function(response) {
alert(response.info);
if (response.status==true) {
window.location.reload();
}
},
error: function(xhr, status, error) {
console.error('错误:', error);
alert('提交失败,请重试。');
}
});
}
</script>
{/literal}
</head>
<body>
<h2 style="text-align: center">添加药方</h2>
<form id="prescription-form" onsubmit="submitForm(event)">
<input id="id" name="id" type="hidden" value="{$data.case.id}">
<div class="form-group">
<label for="name">药方名称:</label>
<input type="text" id="name" name="name" value="{$data.case.name}" required>
</div>
<div class="form-group">
<label for="source">药方来源:</label>
<textarea id="source" name="source" rows="4" required>{$data.case.source}</textarea>
</div>
<div class="form-group">
<label for="method">用法:</label>
<textarea id="method" name="method" rows="4" required>{$data.case.method}</textarea>
</div>
<h3>药材 <button class="add-button" type="button" onclick="addIngredient()">+</button></h3>
<div id="ingredient-list" class="ingredient-list">
<!-- 动态添加药材输入字段 -->
{foreach from=$data.case_herb key=key item=item}
<div class="herb_item">
<div class="herb_input">
<input type="text" name="herb_name[]" value="{if $data.herb[$item.herb_id]}{$data.herb[$item.herb_id].name}{/if}" placeholder="药材名" required />
<input type="number" name="herb_num[]" value="{$item.num}" placeholder="药材重量 (克)" required min="0" step="0.1" />
</div>
<button class="" type="button" onclick="removeIngredient(this)">X</button>
</div>
{/foreach}
</div>
<div class="form-group" style="text-align: center">
<button class="submit-btn" type="submit">提交药方</button>
</div>
</form>
</body>
</html>
Loading…
Cancel
Save