DictMeaningController.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\UserDict;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Support\Facades\Cache;
  6. class DictMeaningController extends Controller
  7. {
  8. protected $langOrder = [
  9. "zh-Hans"=>[
  10. "zh-Hans","zh-Hant","jp","en","my","vi"
  11. ],
  12. "zh-Hant"=>[
  13. "zh-Hant","zh-Hans","jp","en","my","vi"
  14. ],
  15. "en"=>[
  16. "en","my","zh-Hant","zh-Hans","jp","vi"
  17. ],
  18. "jp"=>[
  19. "jp","en","my","zh-Hant","zh-Hans","vi"
  20. ],
  21. ];
  22. /**
  23. * Display a listing of the resource.
  24. *
  25. * @return \Illuminate\Http\Response
  26. */
  27. public function index(Request $request)
  28. {
  29. //
  30. $words = explode("-",$request->get('word'));
  31. $lang = $request->get('lang');
  32. $key = "dict_first_mean/";
  33. $meaning = [];
  34. foreach ($words as $key => $word) {
  35. # code...
  36. $meaning[] = ['word'=>$word,'meaning'=>$this->get($word,$lang)];
  37. }
  38. return $this->ok($meaning);
  39. }
  40. public function get(string $word,string $lang){
  41. $currMeaning = "";
  42. if(isset($this->langOrder[$lang])){
  43. foreach ($this->langOrder[$lang] as $key => $value) {
  44. # 遍历每种语言。找到返回
  45. $cacheKey = "dict_first_mean/{$value}/{$word}";
  46. $meaning = Cache::get($cacheKey);
  47. if(!empty($meaning)){
  48. $currMeaning = $meaning;
  49. break;
  50. }
  51. }
  52. }
  53. return $currMeaning;
  54. }
  55. /**
  56. * Show the form for creating a new resource.
  57. *
  58. * @return \Illuminate\Http\Response
  59. */
  60. public function create()
  61. {
  62. //
  63. }
  64. /**
  65. * Store a newly created resource in storage.
  66. *
  67. * @param \Illuminate\Http\Request $request
  68. * @return \Illuminate\Http\Response
  69. */
  70. public function store(Request $request)
  71. {
  72. //
  73. }
  74. /**
  75. * Display the specified resource.
  76. *
  77. * @param \App\Models\UserDict $userDict
  78. * @return \Illuminate\Http\Response
  79. */
  80. public function show(UserDict $userDict)
  81. {
  82. //
  83. }
  84. /**
  85. * Show the form for editing the specified resource.
  86. *
  87. * @param \App\Models\UserDict $userDict
  88. * @return \Illuminate\Http\Response
  89. */
  90. public function edit(UserDict $userDict)
  91. {
  92. //
  93. }
  94. /**
  95. * Update the specified resource in storage.
  96. *
  97. * @param \Illuminate\Http\Request $request
  98. * @param \App\Models\UserDict $userDict
  99. * @return \Illuminate\Http\Response
  100. */
  101. public function update(Request $request, UserDict $userDict)
  102. {
  103. //
  104. }
  105. /**
  106. * Remove the specified resource from storage.
  107. *
  108. * @param \App\Models\UserDict $userDict
  109. * @return \Illuminate\Http\Response
  110. */
  111. public function destroy(UserDict $userDict)
  112. {
  113. //
  114. }
  115. }