DictMeaningController.php 2.8 KB

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