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.

90 lines
2.9 KiB

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