Browse Source

Merge pull request '药方识别' (#24) from wpd_shibie into master

Reviewed-on: #24
pull/25/head
pengda 7 months ago
parent
commit
755bb157ba
  1. 8
      config/define.php
  2. 9
      control/admin.php
  3. 63
      model/mFormula.php
  4. 70
      view/templates/admin/formula_add.html
  5. 28
      view/templates/admin/formula_list.html

8
config/define.php

@ -57,7 +57,6 @@
); );
$GLOBALS['num_list'] = array( $GLOBALS['num_list'] = array(
'num' => array(
'半'=>0.5, '半'=>0.5,
'一'=>1, '一'=>1,
'二'=>2, '二'=>2,
@ -68,13 +67,7 @@
'七'=>7, '七'=>7,
'八'=>8, '八'=>8,
'九'=>9, '九'=>9,
),
'unit' => array(
'十'=>10, '十'=>10,
'百'=>100,
'千'=>1000,
'万'=>10000,
)
); );
define('WEIGHT_LIANG', 1); define('WEIGHT_LIANG', 1);
@ -84,6 +77,7 @@
WEIGHT_SHENG => '升', WEIGHT_SHENG => '升',
); );
define('WEIGHT_UNIT_FROM_HAN','汉');
$GLOBALS['weight_list'] = array( $GLOBALS['weight_list'] = array(
"汉" => array( "汉" => array(
'少许'=>'', '少许'=>'',

9
control/admin.php

@ -161,6 +161,15 @@ class admin extends publicBase {
$this->ajax_json(true, '操作成功', array('id' => $id)); $this->ajax_json(true, '操作成功', array('id' => $id));
} }
public function ajax_shibie_original() {
$original = trim($this->post('original'));
$mformula = new mFormula();
$data = $mformula->convOriginal($original);
$this->ajax_json(true, '识别成功', $data);
}
public function ajax_delete_formula() { public function ajax_delete_formula() {
$id = $this->post('id') + 0; $id = $this->post('id') + 0;
$is_delete = $this->post('is_delete') + 0; $is_delete = $this->post('is_delete') + 0;

63
model/mFormula.php

@ -323,6 +323,67 @@ class mFormula extends mBase {
return $formula; return $formula;
} }
public function convOriginal($original) {
$herbString_arr = explode(' ', $original);
$num_map = $GLOBALS['num_list'];
$num_list = implode('', array_keys($num_map));
foreach ($herbString_arr as $herbString) {
$pattern = '/^(..)(.*?)([' . $num_list . ']+)([^(]*)?((.*?))?/u';
preg_match($pattern, $herbString, $matches);
if ($matches) {
$herb_arr[] = array(
'herb_name' => trim($matches[1] . $matches[2]),
'herb_num' => trim($matches[3] . $matches[4]),
'herb_desc' => isset($matches[5]) ? str_replace(array('(', ')'), '', trim($matches[5])) : "",
);
} else {
$herb_arr[] = array(
'herb_name' => $herbString,
'herb_num' => "",
'herb_desc' => "",
);
}
}
return $herb_arr;
}
public function convToNumber($str) {
$num_map = $GLOBALS['num_list'];
$num_list = implode('', array_keys($num_map));
preg_match_all('/[' . $num_list . ']+/u', $str, $matches);
foreach ($matches[0] as $chinese) {
$result = 0;
$current = 0; // 当前的数字
for ($i = 0; $i < mb_strlen($chinese); $i++) {
$char = mb_substr($chinese, $i, 1);
if (isset($num_map[$char])) {
$num = $num_map[$char];
if ($num == 10) {
// 如果是“十”
if ($current == 0) {
$current = 1; // 处理“十”前没有数字的情况
}
$result += $current * 10; // 将当前数字乘以十
$current = 0; // 重置当前数字
} else {
$current += $num; // 累加当前数字
}
}
}
$result = $result + $current;
$str = str_replace($chinese, $result, $str);
}
return $str;
}
public function convertToNum($herb_name, $str, $onlyconertNum = true, $from = '汉') { public function convertToNum($herb_name, $str, $onlyconertNum = true, $from = '汉') {
$num_list = $GLOBALS['num_list']; $num_list = $GLOBALS['num_list'];
$num_list_arr = array_merge(array_keys($GLOBALS['num_list']['num']), array_keys($GLOBALS['num_list']['unit'])); $num_list_arr = array_merge(array_keys($GLOBALS['num_list']['num']), array_keys($GLOBALS['num_list']['unit']));
@ -556,7 +617,7 @@ class mFormula extends mBase {
); );
//如果转义成功 则保存转义后的数据 //如果转义成功 则保存转义后的数据
$num = $this->convertToNum(trim($herb['name']), trim($herb['num'])); $num = $this->convToNumber(trim($herb['num']));
if ($num != 0) $org_herb[$key]['num'] = $num; if ($num != 0) $org_herb[$key]['num'] = $num;
} }

70
view/templates/admin/formula_add.html

@ -42,7 +42,7 @@
<div class="wizard-container"><div> <div class="wizard-container"><div>
<div class="inputs"> <div class="inputs">
<div id="form_area"> <div id="form_area">
<form id="prescription-form" onsubmit="submitForm(event)"> <form id="prescription-form" onsubmit="return false">
<input id="id" name="id" type="hidden" value="{$data.id}"> <input id="id" name="id" type="hidden" value="{$data.id}">
<div class="row">&nbsp;</div> <div class="row">&nbsp;</div>
@ -68,6 +68,15 @@
<div class="row">&nbsp;</div> <div class="row">&nbsp;</div>
<div class="row">
<label for="method">古方识别<font color='red'></font></label>
<textarea rows="5" cols="70" id="shibie_text"></textarea>
<button class="button info next" id="shibie_btn"><span class="tdesc_text" onclick="submitShibie(this)" style="color:white;">识别</span></button>
</div>
<div class="row">&nbsp;</div>
<div class="row" style="position:relative;"> <div class="row" style="position:relative;">
<label>药材<font color='red'>*</font></label> <label>药材<font color='red'>*</font></label>
<button class="add-button" type="button" onclick="addIngredient()">+</button> <button class="add-button" type="button" onclick="addIngredient()">+</button>
@ -97,7 +106,7 @@
<div class="row" id="" style="position:relative;"> <div class="row" id="" style="position:relative;">
<label>&nbsp;</label> <label>&nbsp;</label>
<button class="button primary next submitlock" id="submitbtn" onclick="submitForm()"><span class="tdesc_text" style="color:white;">提交</span></button> <button class="button primary next submitlock" id="submitbtn" onclick="submitForm(this)"><span class="tdesc_text" style="color:white;">提交</span></button>
</div> </div>
<div class="row">&nbsp;</div> <div class="row">&nbsp;</div>
@ -157,11 +166,22 @@
} }
// 动态添加药材输入字段 // 动态添加药材输入字段
function addIngredient() { function addIngredient(herb_name,herb_desc,herb_num) {
const ingredientList = document.getElementById('ingredient-list'); const ingredientList = document.getElementById('ingredient-list');
const ingredientDiv = document.createElement('div'); const ingredientDiv = document.createElement('div');
ingredientDiv.classList.add('row'); ingredientDiv.classList.add('row');
if(herb_name){
ingredientDiv.innerHTML = `
<label>&nbsp;</label>
<div class="herb_input">
<input type="text" name="herb_name[]" value="`+herb_name+`" placeholder="药材名" required />
<input type="text" name="herb_desc[]" value="`+herb_desc+`" placeholder="炮制方法"/>
<input type="text" name="herb_num[]" value="`+herb_num+`" placeholder="药量" required />
<button class="" type="button" onclick="removeIngredient(this)">X</button>
</div>
`;
}else{
ingredientDiv.innerHTML = ` ingredientDiv.innerHTML = `
<label>&nbsp;</label> <label>&nbsp;</label>
<div class="herb_input"> <div class="herb_input">
@ -171,6 +191,8 @@
<button class="" type="button" onclick="removeIngredient(this)">X</button> <button class="" type="button" onclick="removeIngredient(this)">X</button>
</div> </div>
`; `;
}
ingredientList.appendChild(ingredientDiv); ingredientList.appendChild(ingredientDiv);
} }
@ -179,8 +201,8 @@
button.parentElement.parentElement.remove(); button.parentElement.parentElement.remove();
} }
function submitForm(e) { function submitForm(obj) {
e.preventDefault(); // 阻止默认表单提交 obj.disabled = true;
const form = document.getElementById('prescription-form'); const form = document.getElementById('prescription-form');
const formData = new FormData(form); const formData = new FormData(form);
@ -230,6 +252,44 @@
return true; return true;
} }
obj.disabled = false;
showAlert(response.info)
},
error: function (xhr, status, error) {
console.error('错误:', response);
alert('提交失败,请重试。');
}
});
}
function submitShibie(obj) {
obj.disabled = true;
const original = document.getElementById('shibie_text').value;
$.ajax({
url: '/admin/ajax_shibie_original', // 替换为你的服务器端处理文件
type: 'POST',
data: {original:original},
dataType: 'json',
success: function (response) {
obj.disabled = false;
if (response.status == true) {
if (Array.isArray(response.data)) {
document.getElementById('ingredient-list').innerHTML = '';
response.data.forEach((row, index) => {
addIngredient(row.herb_name,row.herb_desc,row.herb_num)
});
}else{
console.log('识别失败')
}
return true;
}
if(response.data.code == 40002){
showAlert(response.info,"/admin/login")
return true;
}
showAlert(response.info) showAlert(response.info)
}, },
error: function (xhr, status, error) { error: function (xhr, status, error) {

28
view/templates/admin/formula_list.html

@ -107,17 +107,17 @@
</div> </div>
<div style="line-height: 20px;padding-top: 10px">{$item.method}</div> <div style="line-height: 20px;padding-top: 10px">{$item.method}</div>
</td> </td>
<td> <td class="status_{$item.id}" data-status="{$item.is_delete}" style="{if $item.is_delete == 3}color:red{/if}">
{if $item.uid==0} {if $item.uid==0}
{if $item.is_delete == 0} 审核通过 {/if} {if $item.is_delete == 0} 通过{/if}
{if $item.is_delete == 2} 待审核{/if} {if $item.is_delete == 2} 待审核{/if}
{if $item.is_delete == 3} 审核不通过 {/if} {if $item.is_delete == 3} 不通过 {/if}
{/if} {/if}
</td> </td>
<td> <td>
{if $item.uid == 0} {if $item.uid == 0}
<a href="javascript:;" onclick="to_check({$item.id},{$item.is_delete})">审核</a> <a href="javascript:;" onclick="to_check({$item.id})">审核</a>
<a href="/admin/formula_add/id/{$item.id}">编辑</a> <a target="_blank" href="/admin/formula_add/id/{$item.id}">编辑</a>
{/if} {/if}
</td> </td>
</tr> </tr>
@ -139,8 +139,8 @@
</div> </div>
<div class="modal-body inputs" style="padding: 30px;"> <div class="modal-body inputs" style="padding: 30px;">
<input type="hidden" name="id" id="check_id"> <input type="hidden" name="id" id="check_id">
<input type="radio" checked name="status" value="0">审核通过 <input type="radio" checked name="status" value="0">通过
<input type="radio" name="status" value="3">审核不通过 <input type="radio" name="status" value="3">不通过
<button class="button primary" style="margin-left: 50px" onclick="to_status()">确定</button> <button class="button primary" style="margin-left: 50px" onclick="to_status()">确定</button>
</div> </div>
</div> </div>
@ -171,8 +171,9 @@
function to_add(){ function to_add(){
location.href = '/admin/formula_add'; location.href = '/admin/formula_add';
} }
function to_check(id,status){ function to_check(id){
$('#check_id').val(id); $('#check_id').val(id);
let status = $('.status_'+id).data('status')
if(status == 3){ if(status == 3){
$('input[name="status"][value="3"]').prop("checked", true); $('input[name="status"][value="3"]').prop("checked", true);
}else{ }else{
@ -202,7 +203,16 @@
dataType: 'json', dataType: 'json',
success: function (response) { success: function (response) {
if (response.status == true) { if (response.status == true) {
showAlert('操作成功', window.location.href) showAlert('操作成功')
$('.status_'+id).data('status',status);
if(status == 0){
$('.status_'+id).html('通过');
$('.status_'+id).css('color','#000');
}
if(status == 3){
$('.status_'+id).html('不通过');
$('.status_'+id).css('color','red');
}
return true; return true;
} }

Loading…
Cancel
Save