DictController.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\UserDict;
  4. use App\Models\DictInfo;
  5. use Illuminate\Http\Request;
  6. require_once __DIR__."/../../../public/app/dict/grm_abbr.php";
  7. class DictController extends Controller
  8. {
  9. /**
  10. * Display a listing of the resource.
  11. *
  12. * @return \Illuminate\Http\Response
  13. */
  14. public function index(Request $request)
  15. {
  16. //
  17. $output = [];
  18. $wordDataOutput = [];
  19. $dictListOutput = [];
  20. $caseListOutput = [];
  21. $indexCol = ['word','note','dict_id'];
  22. $word = $request->get('word');
  23. $result = UserDict::select($indexCol)->where('word',$word)->where('source','_PAPER_')->get();
  24. $anchor = $word;
  25. $wordData=[
  26. 'word'=> $word,
  27. 'factors'=> "",
  28. 'parents'=> "",
  29. 'case'=> [],
  30. 'anchor'=> $anchor,
  31. 'dict' => [],
  32. ];
  33. $dictList=[
  34. 'href'=> '#'.$anchor,
  35. 'title'=> "{$word}",
  36. ];
  37. foreach ($result as $key => $value) {
  38. # code...
  39. $dictInfo= DictInfo::find($value->dict_id);
  40. $anchor = "{$word}-{$dictInfo->shortname}";
  41. $wordData['dict'][] = [
  42. 'dictname'=> $dictInfo->name,
  43. 'word'=> $word,
  44. 'note'=> $this->GrmAbbr($value->note,0),
  45. 'anchor'=> $anchor,
  46. ];
  47. $dictList['children'][] = [
  48. 'href'=> '#'.$anchor,
  49. 'title'=> "{$dictInfo->shortname}",
  50. ];
  51. }
  52. $wordDataOutput[]=$wordData;
  53. $dictListOutput[]=$dictList;
  54. $output['words'] = $wordDataOutput;
  55. $output['dictlist'] = $dictListOutput;
  56. $output['caselist'] = $caseListOutput;
  57. //TODO 加变格查询
  58. //$result = UserDict::select('word')->where('word','like',"{$word}%")->groupBy('word')->get();
  59. //$output['like'] = $result;
  60. return $this->ok($output);
  61. }
  62. /**
  63. * Store a newly created resource in storage.
  64. *
  65. * @param \Illuminate\Http\Request $request
  66. * @return \Illuminate\Http\Response
  67. */
  68. public function store(Request $request)
  69. {
  70. //
  71. }
  72. /**
  73. * Display the specified resource.
  74. *
  75. * @param \App\Models\UserDict $userDict
  76. * @return \Illuminate\Http\Response
  77. */
  78. public function show(UserDict $userDict)
  79. {
  80. //
  81. }
  82. /**
  83. * Update the specified resource in storage.
  84. *
  85. * @param \Illuminate\Http\Request $request
  86. * @param \App\Models\UserDict $userDict
  87. * @return \Illuminate\Http\Response
  88. */
  89. public function update(Request $request, UserDict $userDict)
  90. {
  91. //
  92. }
  93. /**
  94. * Remove the specified resource from storage.
  95. *
  96. * @param \App\Models\UserDict $userDict
  97. * @return \Illuminate\Http\Response
  98. */
  99. public function destroy(UserDict $userDict)
  100. {
  101. //
  102. }
  103. private function GrmAbbr($input,$dictid){
  104. $mean = $input;
  105. foreach (GRM_ABBR as $key => $value) {
  106. # code...
  107. if($dictid !== 0){
  108. if($value["dictid"]=== $dictid && strpos($input,$value["abbr"]."|") == false){
  109. $mean = str_ireplace($value["abbr"],"|@{$value["abbr"]}-grammar_{$value["replace"]}",$mean);
  110. }
  111. }else{
  112. if( strpos($mean,"|@".$value["abbr"]) == false){
  113. $mean = str_ireplace($value["abbr"],"|@{$value["abbr"]}-grammar_{$value["replace"]}|",$mean);
  114. }
  115. }
  116. }
  117. return $mean;
  118. }
  119. }