Browse Source

计量单位换算

pull/1/head
pengda 9 months ago
parent
commit
6e7056c0ed
  1. 80
      config/define.php
  2. 3
      control/index.php
  3. 53
      model/mCase.php
  4. 55
      view/templates/index/home.html

80
config/define.php

@ -80,19 +80,77 @@
'斤'=>249.6,
'担'=>24960,
'合'=>array(
'水'=>'',
'米'=>'',
'蜂蜜'=>'',
'酒'=>'',
'粳米'=>0,
'白蜜'=>0,
'人尿'=>0,
'猪胆汁'=>0,
'半夏'=>0,
'芒硝'=>0,
'蜀椒'=>0,
'食蜜'=>0,
'五味子'=>0,
'香豉'=>0,
),
'升'=>array(
'半夏'=>134,
'芒硝'=>154,
'吴茱萸'=>104,
'五味子'=>88,
'麦门冬'=>108,
'赤小豆'=>150,
'小麦'=>0,
'杏仁'=>0,
'竹茹'=>0,
'白酒'=>0,
'麻仁'=>0,
'酸枣仁'=>0,
'粳米'=>0,
'饴糖'=>0,
'豉'=>0,
'地黄汁'=>0,
'虻虫'=>0,
'蛴螬'=>0,
'虫'=>0,
'胶饴'=>0,
'葶苈'=>0,
'大黄'=>0,
),
'斗'=>array(
'白酒'=>0,
),
'个'=>array(
'水蛭'=>3.6,
'附子'=>12,
'虻虫'=>0.16,
'杏仁'=>0.33,
'桃仁'=>0.34,
'大枣'=>4,
'栀子'=>0,
'瓜蒂'=>0,
),
'片'=>array(
'鳖甲'=>0,
),
'升'=>'',
'斗'=>'',
'个'=>'',
'片'=>'',
'味'=>'',
'把'=>'',
'枚'=>'',
'茎'=>'',
'把'=>array(
'艾叶'=>0,
),
'枚'=>array(
'瓜蒌实'=>30,
'乌头'=>0,
'乌头大者'=>0,
'枳实'=>0,
'栀子'=>0,
'蜘蛛'=>0,
'大猪胆'=>0,
'百合'=>0,
'水蛭'=>0,
'附子'=>0,
'附子大者'=>0,
),
'茎'=>array(
'葱白'=>0,
),
'少许'=>'',
),
);

3
control/index.php

@ -36,13 +36,12 @@ class index extends publicBase {
public function ajax_save_case() {
$name = trim($this->post('name'));
$source = trim($this->post('source'));
$original = trim($this->post('original'));
$method = trim($this->post('method'));
$herbs = $this->post('herbs');
//新增药方
$m_case = new mCase();
$id = $m_case->createCase($name, $source, $original, $method, $herbs);
$id = $m_case->createCase($name, $source, $method, $herbs);
if (!$id) $this->ajax_json(false, $m_case->getError());
$this->ajax_json(true, '添加成功');

53
model/mCase.php

@ -22,7 +22,7 @@ class mCase extends mBase {
$this->case_use_log_tbl = 'tcm_case_use_log';
}
public function createCase($name, $source, $original, $method, $herbs) {
public function createCase($name, $source, $method, $herbs) {
if (empty($name)) {
$this->setError('药方名称不能为空');
return false;
@ -31,10 +31,6 @@ class mCase extends mBase {
$this->setError('药方来源不能为空');
return false;
}
if (empty($original)) {
$this->setError('药方原方不能为空');
return false;
}
if (empty($method)) {
$this->setError('药方用法不能为空');
return false;
@ -44,6 +40,9 @@ class mCase extends mBase {
return false;
}
//保存原方
$original = json_encode($herbs, JSON_UNESCAPED_UNICODE);
$id = $this->obj->insert($this->tbl, array('name' => $name, 'source' => $source, 'original' => $original, 'method' => $method));
if (!$id) {
$this->setError('添加失败');
@ -88,21 +87,55 @@ class mCase extends mBase {
$case_herb = array();
foreach ($herbs as $key => $item) {
$temp = array();
$temp['name'] = $item['name'];
$temp['case_id'] = $case_id;
$temp['num'] = $item['num'];
$temp['num'] = $this->convertToNum('汉', $item['num'], $item['name']);
$temp['sort'] = $key;
$herb = $this->getHerbByName($item['name']);
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'], 'desc' => $item['desc']));
}
$case_herb[] = $temp;
}
return $case_herb;
}
public function convertToNum($from, $str, $herb_name) {
$num_list = $GLOBALS['num_list'];
$num_list_arr = array_merge(array_keys($GLOBALS['num_list']['num']), array_keys($GLOBALS['num_list']['unit']));
$weight_list = $GLOBALS['weight_list'][$from];
$weight_list_arr = array_keys($GLOBALS['weight_list'][$from]);
$unit = str_replace($num_list_arr, '', $str);
$weight = isset($weight_list[$unit]) ? $weight_list[$unit] : 0;
if (isset($weight[$herb_name])) $weight = $weight[$herb_name];
$num = 0;
$temp = 0;
$num_str = str_replace($weight_list_arr, '', $str);
$length = mb_strlen($num_str);
for ($i = 0; $i < $length; $i++) {
$char = mb_substr($num_str, $i, 1);
if (isset($num_list['num'][$char])) {
$temp = $num_list['num'][$char];
} elseif (isset($num_list['unit'][$char])) {
$temp = ($temp == 0 ? 1 : $temp) * $num_list['unit'][$char];
$num += $temp; // 将临时结果累加到最终结果中
$temp = 0; // 重置临时结果
}
}
$num += $temp;
return $num * $weight;
}
public function getHerbByName($name) {
return $this->obj->select($this->herb_tbl, array('sql' => '`name`=?', 'vals' => array($name)));
}
@ -213,11 +246,7 @@ class mCase extends mBase {
return false;
}
return array(
'case' => $case,
'case_herb' => $case_herb,
'herb' => array_column($herb,null,'id'),
);
return array('case' => $case, 'case_herb' => $case_herb, 'herb' => array_column($herb, null, 'id'),);
}
public function getCaseById($id) {

55
view/templates/index/home.html

@ -10,7 +10,7 @@
{literal}
<style>
#prescription-form{
width: 500px;
width: 800px;
margin-left: 100px;
}
.form-group {
@ -52,7 +52,7 @@
color: #fff;
}
.herb_item{
width: 500px;
width: 800px;
position: relative;
display: flex;
line-height: 50px;
@ -60,7 +60,7 @@
}
.herb_item button{
position: absolute;
left:420px;
left:620px;
top:15px;
width: 20px;
height: 20px;
@ -70,13 +70,18 @@
color: #fff;
}
.herb_input{
width: 450px;
/*width: 800px;*/
}
.herb_input input{
height: 50px;
margin-right: 30px;
text-indent: 10px;
}
#change{
position: absolute;
left: 1000px;
top:500px
}
</style>
<script>
// 动态添加药材输入字段
@ -88,7 +93,8 @@
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" />
<input type="text" name="herb_num[]" placeholder="药量" required />
<input type="text" name="herb_desc[]" placeholder="说明"/>
</div>
<button class="" type="button" onclick="removeIngredient(this)">X</button>
`;
@ -110,7 +116,6 @@
id: formData.get('id'),
name: formData.get('name'),
source: formData.get('source'),
original: formData.get('original'),
method: formData.get('method'),
herbs: []
};
@ -118,11 +123,13 @@
// 收集所有药材名称和重量
const herb_name = formData.getAll('herb_name[]');
const herb_num = formData.getAll('herb_num[]');
const herb_desc = formData.getAll('herb_desc[]');
herb_name.forEach((name, index) => {
data.herbs.push({
name: name,
num: herb_num[index]
num: herb_num[index],
desc: herb_desc[index]
});
});
@ -139,7 +146,7 @@
}
},
error: function(xhr, status, error) {
console.error('错误:', error);
console.error('错误:', response);
alert('提交失败,请重试。');
}
});
@ -156,7 +163,7 @@
}
},
error: function(xhr, status, error) {
console.error('错误:', error);
console.error('错误:', response);
alert('提交失败,请重试。');
}
});
@ -176,31 +183,45 @@
</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="original">原方:</label>
<textarea id="original" name="original" rows="4" required>{$data.case.original}</textarea>
<textarea id="source" name="source" rows="4">{$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>
{if $data.case.original}
<h3>原方 <button class="add-button" type="button" onclick="addIngredient()">+</button></h3>
{else}
<h3>药材 <button class="add-button" type="button" onclick="addIngredient()">+</button></h3>
{/if}
<div id="ingredient-list" class="ingredient-list">
<!-- 动态添加药材输入字段 -->
{foreach from=$data.case_herb key=key item=item}
{foreach from=$data.case.original 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" />
<input type="text" name="herb_name[]" value="{$item.name}" placeholder="药材名" required />
<input type="text" name="herb_num[]" value="{$item.num}" placeholder="用量" required/>
<input type="text" name="herb_desc[]" value="{$item.desc}" placeholder="说明" required />
</div>
<button class="" type="button" onclick="removeIngredient(this)">X</button>
</div>
{/foreach}
</div>
<div id="change" class="form-group">
<label for="method">转换后:</label>
{foreach from=$data.case_herb key=key item=item}
<div style="display: flex;line-height: 50px">
<div style="margin-right: 20px">{if $data.herb[$item.herb_id]}{$data.herb[$item.herb_id].name}{/if}</div>
<div style="margin-right: 20px">{$item.num} 克</div>
<div>{if $data.herb[$item.herb_id]}{$data.herb[$item.herb_id].desc}{/if}</div>
</div>
{/foreach}
</div>
<div class="form-group" style="text-align: center">
<button class="submit-btn" type="submit">提交药方</button>
</div>

Loading…
Cancel
Save