Просмотр исходного кода

:sparkles: 字典检索支持结果排序

visuddhinanda 3 лет назад
Родитель
Сommit
eb3f66b320
2 измененных файлов с 122 добавлено и 6 удалено
  1. 65 0
      app/Http/Api/DictApi.php
  2. 57 6
      app/Http/Controllers/DictController.php

+ 65 - 0
app/Http/Api/DictApi.php

@@ -3,6 +3,71 @@ namespace App\Http\Api;
 use App\Models\DictInfo;
 
 class DictApi{
+    public static function langOrder(string $lang){
+        switch ($lang) {
+            case 'zh':
+                $output = ["zh","jp","en","my"];
+                break;
+            case 'en':
+                $output = ["en","my"];
+                break;
+            case 'my':
+                $output = ["my","en"];
+                break;
+            default:
+                $output = [$lang,"en","my"];
+                break;
+        }
+        $output[] = "others";
+        return $output;
+    }
+
+    public static function dictOrder($lang){
+        $output = [];
+        switch ($lang) {
+            case 'zh':
+                $output = [
+                "0d79e8e8-1430-4c99-a0f1-b74f2b4b26d8",	/*《巴汉词典》增订*/
+                "f364d3dc-b611-471b-9a4f-531286b8c2c3",	/*《巴汉词典》Mahāñāṇo Bhikkhu编著*/
+                "0e4dc5c8-a228-4693-92ba-7d42918d8a91",	/*汉译パーリ语辞典-黃秉榮*/
+                "6aa9ec8b-bba4-4bcd-abd2-9eae015bad2b",	/*汉译パーリ语辞典-李瑩*/
+                "eb99f8b4-c3e5-43af-9102-6a93fcb97db6",	/*パーリ语辞典--勘误表*/
+                ];
+                break;
+            case 'jp':
+                $output = [
+                "91d3ec93-3811-4973-8d84-ced99179a0aa",	/*パーリ语辞典*/
+                "6d6c6812-75e7-457d-874f-5b049ad4b6de",	/*パーリ语辞典-增补*/
+                ];
+                break;
+            case 'en':
+                $output = [
+                "c6e70507-4a14-4687-8b70-2d0c7eb0cf21",	/*	Concise P-E Dict*/
+                "6afb8c05-5cbe-422e-b691-0d4507450cb7",	/*	PTS P-E dictionary*/
+                ];
+                break;
+            case 'my':
+                $output =[
+                "e740ef40-26d7-416e-96c2-925d6650ac6b",	/*	Tipiṭaka Pāḷi-Myanmar*/
+                "beb45062-7c20-4047-bcd4-1f636ba443d1",	/*	U Hau Sein’s Pāḷi-Myanmar Dictionary*/
+                "1e299ccb-4fc4-487d-8d72-08f63d84c809",	/*	Pali Roots Dictionary*/
+                "6f9caea1-17fa-41f1-92e5-bd8e6e70e1d7",	/*	U Hau Sein’s Pāḷi-Myanmar*/
+                ];
+                break;
+            case 'vi':
+                $output = [
+                "23f67523-fa03-48d9-9dda-ede80d578dd2",	/*	Pali Viet Dictionary*/
+                "4ac8a0d5-9c6f-4b9f-983d-84288d47f993",	/*	Pali Viet Abhi-Terms*/
+                "7c7ee287-35ba-4cf3-b87b-30f1fa6e57c9",	/*	Pali Viet Vinaya Terms*/
+                ];
+                break;
+            default:
+                $output = [];
+                break;
+        };
+        $output[] = "others";
+        return $output;
+    }
     public static function getSysDict($name){
         $dict_info=  DictInfo::where('name',$name)
                     ->where('owner_id',config("app.admin.root_uuid"))

+ 57 - 6
app/Http/Controllers/DictController.php

@@ -7,6 +7,7 @@ use App\Models\DictInfo;
 use Illuminate\Http\Request;
 use App\Tools\CaseMan;
 use Illuminate\Support\Facades\Log;
+use App\Http\Api\DictApi;
 
 require_once __DIR__."/../../../public/app/dict/grm_abbr.php";
 
@@ -30,6 +31,8 @@ class DictController extends Controller
         $word_base = [];
         $searched = [];
         $words[$request->get('word')] = [];
+        $userLang = $request->get('lang',"zh");
+
         for ($i=0; $i < 2; $i++) {
             # code...
             $word_base = [];
@@ -47,6 +50,27 @@ class DictController extends Controller
                     'anchor'=> $anchor,
                     'dict' => [],
                 ];
+                /**
+                 * 按照语言调整词典顺序
+                 * 算法:准备理想的词典顺序容器。
+                 * 将查询的结果放置在对应的容器中。
+                 * 最后将结果扁平化
+                 * 准备字典容器
+                * $wordDict = [
+                *    "zh"=>[
+                *        "0d79e8e8-1430-4c99-a0f1-b74f2b4b26d8"=>[];
+                *    ]
+                * ]
+                 */
+
+                foreach (DictApi::langOrder($userLang) as  $langId) {
+                    # code...
+                    $dictContainer = [];
+                    foreach (DictApi::dictOrder($langId) as $dictId) {
+                        $dictContainer[$dictId] = [];
+                    }
+                    $wordDict[$langId] = $dictContainer;
+                }
                 $dictList=[
                     'href'=> '#'.$anchor,
                     'title'=> "{$word}",
@@ -54,19 +78,46 @@ class DictController extends Controller
                 foreach ($result as $key => $value) {
                     # code...
                     $dictInfo= DictInfo::find($value->dict_id);
-
+                    $dict_lang = explode('-',$dictInfo->dest_lang);
                     $anchor = "{$word}-{$dictInfo->shortname}";
-                    $wordData['dict'][] = [
-                        'dictname'=> $dictInfo->name,
-                        'word'=> $word,
-                        'note'=> $this->GrmAbbr($value->note,0),
-                        'anchor'=> $anchor,
+                    $currData = [
+                            'dictname'=> $dictInfo->name,
+                            'shortname'=> $dictInfo->shortname,
+                            'dict_id' => $value->dict_id,
+                            'lang' => $dict_lang[0],
+                            'word'=> $word,
+                            'note'=> $this->GrmAbbr($value->note,0),
+                            'anchor'=> $anchor,
                     ];
+                    if(isset($wordDict[$dict_lang[0]])){
+                        if(isset($wordDict[$dict_lang[0]][$value->dict_id])){
+                            array_push($wordDict[$dict_lang[0]][$value->dict_id],$currData);
+                        }else{
+                            array_push($wordDict[$dict_lang[0]]["others"],$currData);
+                        }
+                    }else{
+                        array_push($wordDict['others']['others'],$currData);
+                    }
+
                     $dictList['children'][] = [
                         'href'=> '#'.$anchor,
                         'title'=> "{$dictInfo->shortname}",
                     ];
                 }
+                /**
+                 * 把树状数据变为扁平数据
+                 */
+                foreach ($wordDict as $oneLang) {
+                    # code...
+                    foreach ($oneLang as $langId => $dictId) {
+                        # code...
+                        foreach ($dictId as $oneData) {
+                            # code...
+                            $wordData['dict'][] = $oneData;
+                        }
+                    }
+                }
+
                 $wordDataOutput[]=$wordData;
                 $dictListOutput[]=$dictList;