ViewService.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace App\Services;
  3. use Illuminate\Support\Facades\DB;
  4. use Illuminate\Support\Facades\Log;
  5. /**
  6. * 视图管理
  7. */
  8. class ViewService
  9. {
  10. /**
  11. * 获取视图列表
  12. *
  13. * @param array $params 查询参数
  14. */
  15. public function viewList($viewName, array $whereParams, array $pageParams, array $sortParams = [])
  16. {
  17. try {
  18. $query = DB::table($viewName);
  19. // 获取分页数据
  20. // 条件筛选
  21. foreach ($whereParams as $key => $value) {
  22. if (is_string($value)) {
  23. $query->where($key, 'like', "%{$value}%");
  24. } else {
  25. $query->where($key, $value);
  26. }
  27. }
  28. // 排序
  29. ! empty($sortParams) && $query->orderBy($sortParams['orderBy'], $sortParams['orderDir']);
  30. $result = $query->paginate($pageParams['perPage'], ['*'], 'page', $pageParams['page']);
  31. return response()->json([
  32. 'code' => 200,
  33. 'message' => 'success',
  34. 'data' => $result,
  35. ]);
  36. } catch (\Exception $e) {
  37. Log::error('获取用户列表失败:'.$e->getMessage());
  38. return response()->json([
  39. 'code' => 500,
  40. 'message' => '获取用户列表失败',
  41. ]);
  42. }
  43. }
  44. }