From 594ff99cf446659674a524b6b63b0a4787045951 Mon Sep 17 00:00:00 2001
From: pengda <10266652509@qq.com>
Date: Fri, 18 Oct 2024 13:55:26 +0800
Subject: [PATCH] =?UTF-8?q?=E8=8D=AF=E6=96=B9=E8=AF=86=E5=88=AB?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 config/define.php                      | 30 ++++++--------
 control/admin.php                      |  9 +++++
 model/mFormula.php                     | 63 ++++++++++++++++++++++++++++-
 view/templates/admin/formula_add.html  | 72 +++++++++++++++++++++++++++++++---
 view/templates/admin/formula_list.html | 30 +++++++++-----
 5 files changed, 169 insertions(+), 35 deletions(-)

diff --git a/config/define.php b/config/define.php
index 3966b90..0414d1e 100644
--- a/config/define.php
+++ b/config/define.php
@@ -57,24 +57,17 @@
     );
 
     $GLOBALS['num_list'] = array(
-        'num' => array(
-            '半'=>0.5,
-            '一'=>1,
-            '二'=>2,
-            '三'=>3,
-            '四'=>4,
-            '五'=>5,
-            '六'=>6,
-            '七'=>7,
-            '八'=>8,
-            '九'=>9,
-        ),
-        'unit' => array(
-            '十'=>10,
-            '百'=>100,
-            '千'=>1000,
-            '万'=>10000,
-        )
+        '半'=>0.5,
+        '一'=>1,
+        '二'=>2,
+        '三'=>3,
+        '四'=>4,
+        '五'=>5,
+        '六'=>6,
+        '七'=>7,
+        '八'=>8,
+        '九'=>9,
+        '十'=>10,
     );
 
     define('WEIGHT_LIANG', 1);
@@ -84,6 +77,7 @@
         WEIGHT_SHENG => '升',
     );
 
+    define('WEIGHT_UNIT_FROM_HAN','汉');
     $GLOBALS['weight_list'] = array(
         "汉" => array(
             '少许'=>'',
diff --git a/control/admin.php b/control/admin.php
index 4487a51..39c5315 100644
--- a/control/admin.php
+++ b/control/admin.php
@@ -161,6 +161,15 @@ class admin extends publicBase {
         $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() {
         $id = $this->post('id') + 0;
         $is_delete = $this->post('is_delete') + 0;
diff --git a/model/mFormula.php b/model/mFormula.php
index 4d5306a..b7d7e6e 100644
--- a/model/mFormula.php
+++ b/model/mFormula.php
@@ -323,6 +323,67 @@ class mFormula extends mBase {
         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 = '汉') {
         $num_list = $GLOBALS['num_list'];
         $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;
             }
 
diff --git a/view/templates/admin/formula_add.html b/view/templates/admin/formula_add.html
index a7111f6..42aec48 100644
--- a/view/templates/admin/formula_add.html
+++ b/view/templates/admin/formula_add.html
@@ -42,7 +42,7 @@
                             <div class="wizard-container"><div>
                             <div class="inputs">
                                 <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}">
 
                                         <div class="row">&nbsp;</div>
@@ -68,6 +68,15 @@
 
                                         <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;">
                                             <label>药材<font color='red'>*</font>:</label>
                                             <button class="add-button" type="button" onclick="addIngredient()">+</button>
@@ -97,7 +106,7 @@
                                         <div class="row" id="" style="position:relative;">
                                             <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 class="row">&nbsp;</div>
@@ -157,12 +166,23 @@
     }
 
     // 动态添加药材输入字段
-    function addIngredient() {
+    function addIngredient(herb_name,herb_desc,herb_num) {
         const ingredientList = document.getElementById('ingredient-list');
         const ingredientDiv = document.createElement('div');
         ingredientDiv.classList.add('row');
 
-        ingredientDiv.innerHTML = `
+        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 = `
                 <label>&nbsp;</label>
                 <div class="herb_input">
                     <input type="text" name="herb_name[]" placeholder="药材名" required />
@@ -171,6 +191,8 @@
                     <button class="" type="button" onclick="removeIngredient(this)">X</button>
                 </div>
             `;
+        }
+
         ingredientList.appendChild(ingredientDiv);
     }
 
@@ -179,8 +201,8 @@
         button.parentElement.parentElement.remove();
     }
 
-    function submitForm(e) {
-        e.preventDefault(); // 阻止默认表单提交
+    function submitForm(obj) {
+        obj.disabled = true;
 
         const form = document.getElementById('prescription-form');
         const formData = new FormData(form);
@@ -230,6 +252,44 @@
                     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)
             },
             error: function (xhr, status, error) {
diff --git a/view/templates/admin/formula_list.html b/view/templates/admin/formula_list.html
index fa8cb24..2560127 100644
--- a/view/templates/admin/formula_list.html
+++ b/view/templates/admin/formula_list.html
@@ -107,17 +107,17 @@
                                 </div>
                                 <div style="line-height: 20px;padding-top: 10px">{$item.method}</div>
                             </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.is_delete == 0} 审核通过 {/if}
-                                    {if $item.is_delete == 2} 待审核 {/if}
-                                    {if $item.is_delete == 3} 审核不通过 {/if}
+                                    {if $item.is_delete == 0} 通过{/if}
+                                    {if $item.is_delete == 2} 待审核{/if}
+                                    {if $item.is_delete == 3} 不通过 {/if}
                                 {/if}
                             </td>
                             <td>
                                 {if $item.uid == 0}
-                                <a href="javascript:;" onclick="to_check({$item.id},{$item.is_delete})">审核</a>
-                                <a href="/admin/formula_add/id/{$item.id}">编辑</a>
+                                <a href="javascript:;" onclick="to_check({$item.id})">审核</a>
+                                <a target="_blank" href="/admin/formula_add/id/{$item.id}">编辑</a>
                                 {/if}
                             </td>
                         </tr>
@@ -139,8 +139,8 @@
                         </div>
                         <div class="modal-body inputs" style="padding: 30px;">
                             <input type="hidden" name="id" id="check_id">
-                            <input type="radio" checked name="status" value="0">审核通过
-                            <input type="radio" name="status" value="3">审核不通过
+                            <input type="radio" checked name="status" value="0">通过
+                            <input type="radio" name="status" value="3">不通过
                             <button class="button primary" style="margin-left: 50px" onclick="to_status()">确定</button>
                         </div>
                     </div>
@@ -171,8 +171,9 @@
     function to_add(){
         location.href = '/admin/formula_add';
     }
-    function to_check(id,status){
+    function to_check(id){
         $('#check_id').val(id);
+        let status = $('.status_'+id).data('status')
         if(status == 3){
             $('input[name="status"][value="3"]').prop("checked", true);
         }else{
@@ -202,7 +203,16 @@
             dataType: 'json',
             success: function (response) {
                 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;
                 }
 
-- 
1.8.3.1