DictMeaningController.php 3.2 KB

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