You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
2.1 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Http\Controllers\Controller;
  4. use DB;
  5. class CommonController extends Controller
  6. {
  7. public $user_info;
  8. public function __construct()
  9. {
  10. parent::__construct();
  11. if(isset($_SESSION['admin_user_info']))
  12. {
  13. $this->user_info = $_SESSION['admin_user_info'];
  14. }
  15. else
  16. {
  17. header("Location:".route('page404'));
  18. exit();
  19. }
  20. }
  21. /**
  22. * 获取分页数据及分页导航
  23. * @param string $modelname 模块名与数据库表名对应
  24. * @param array $where 查询条件
  25. * @param string $orderby 查询排序
  26. * @param string $field 要返回数据的字段
  27. * @param int $listRows 每页数量,默认30条
  28. *
  29. * @return 格式化后输出的数据。内容格式为:
  30. * - "code" (string):代码
  31. * - "info" (string):信息提示
  32. *
  33. * - "result" array
  34. *
  35. * - "img_list" (array) :图片队列,默认8张
  36. * - "img_title" (string):车图名称
  37. * - "img_url" (string):车图片url地址
  38. * - "car_name" (string):车名称
  39. */
  40. public function pageList($modelname, $where = '', $orderby = '', $field = '*', $listRows = 30)
  41. {
  42. $model = \DB::table($modelname);
  43. //查询条件
  44. if(!empty($where)){$model = $model->where($where);}
  45. //排序
  46. if($orderby!='')
  47. {
  48. if($orderby == 'rand()')
  49. {
  50. $model = $model->orderBy(\DB::raw('rand()'));
  51. }
  52. else
  53. {
  54. if(count($orderby) == count($orderby, 1))
  55. {
  56. $model = $model->orderBy($orderby[0], $orderby[1]);
  57. }
  58. else
  59. {
  60. foreach($orderby as $row)
  61. {
  62. $model = $model->orderBy($row[0], $row[1]);
  63. }
  64. }
  65. }
  66. }
  67. else
  68. {
  69. $model = $model->orderBy('id', 'desc');
  70. }
  71. //要返回的字段
  72. if($field!='*'){$model = $model->select(\DB::raw($field));}
  73. return $model->paginate($listRows);
  74. }
  75. }