DictController.php 3.5 KB

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