2
0

DictPreferenceController.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Api\DictApi;
  4. use App\Http\Resources\DictPreferenceResource;
  5. use App\Models\UserDict;
  6. use App\Models\WordIndex;
  7. use App\Services\AuthService;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Http\Response;
  10. class DictPreferenceController extends Controller
  11. {
  12. /**
  13. * Display a listing of the resource.
  14. *
  15. * @return Response
  16. */
  17. public function index(Request $request)
  18. {
  19. //
  20. $dict_id = DictApi::getSysDict('system_preference');
  21. if (! $dict_id) {
  22. return $this->error('没有找到 system_preference 字典', 200, 200);
  23. }
  24. $table = WordIndex::where('user_dicts.dict_id', $dict_id)
  25. ->leftJoin('user_dicts', 'word_indices.word', '=', 'user_dicts.word')
  26. ->select([
  27. 'user_dicts.id',
  28. 'word_indices.word',
  29. 'word_indices.count',
  30. 'user_dicts.factors',
  31. 'user_dicts.parent',
  32. 'user_dicts.note',
  33. 'user_dicts.confidence',
  34. 'user_dicts.editor_id',
  35. ]);
  36. // 处理搜索
  37. if (! empty($request->input('keyword'))) {
  38. $table = $table->where('word_indices.word', 'like', '%'.$request->input('keyword').'%');
  39. }
  40. // 获取记录总条数
  41. $count = $table->count();
  42. // 处理排序
  43. $table = $table->orderBy(
  44. $request->input('order', 'word_indices.count'),
  45. $request->input('dir', 'desc')
  46. );
  47. // 处理分页
  48. $table = $table->skip($request->input('offset', 0))
  49. ->take($request->input('limit', 200));
  50. // 获取数据
  51. $result = $table->get();
  52. return $this->ok([
  53. 'rows' => DictPreferenceResource::collection($result),
  54. 'count' => $count,
  55. ]);
  56. }
  57. /**
  58. * Store a newly created resource in storage.
  59. *
  60. * @return Response
  61. */
  62. public function store(Request $request)
  63. {
  64. //
  65. }
  66. /**
  67. * Display the specified resource.
  68. *
  69. * @return Response
  70. */
  71. public function show(UserDict $userDict)
  72. {
  73. //
  74. }
  75. /**
  76. * Update the specified resource in storage.
  77. *
  78. * @param string $id
  79. * @return Response
  80. */
  81. public function update(Request $request, $id)
  82. {
  83. $user = AuthService::current($request);
  84. if (! $user) {
  85. return $this->error(__('auth.failed'), [], 401);
  86. }
  87. $newData = $request->all();
  88. $word = UserDict::findOrFail($id);
  89. if (isset($newData['factors'])) {
  90. $word->factors = $newData['factors'];
  91. }
  92. if (isset($newData['parent'])) {
  93. $word->parent = $newData['parent'];
  94. }
  95. if (isset($newData['confidence'])) {
  96. $word->confidence = $newData['confidence'];
  97. }
  98. $word->editor_id = $user['user_uid'];
  99. $word->save();
  100. return $this->ok(new DictPreferenceResource($word));
  101. }
  102. /**
  103. * Remove the specified resource from storage.
  104. *
  105. * @return Response
  106. */
  107. public function destroy(UserDict $userDict)
  108. {
  109. //
  110. }
  111. }