DictMeaningController.php 3.0 KB

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