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.

92 lines
3.0 KiB

7 years ago
7 years ago
4 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Http\Controllers\Controller;
  4. use Illuminate\Support\Facades\DB;
  5. class CommonController extends Controller
  6. {
  7. public $admin_info;
  8. public function __construct()
  9. {
  10. parent::__construct();
  11. // 添加管理员操作记录
  12. $this->operation_log_add();
  13. }
  14. /**
  15. * 获取分页数据及分页导航
  16. * @param string $modelname 模块名与数据库表名对应
  17. * @param array $where 查询条件
  18. * @param string $orderby 查询排序
  19. * @param string $field 要返回数据的字段
  20. * @param int $listRows 每页数量,默认30条
  21. *
  22. * @return 格式化后输出的数据。内容格式为:
  23. * - "code" (string):代码
  24. * - "info" (string):信息提示
  25. *
  26. * - "result" array
  27. *
  28. * - "img_list" (array) :图片队列,默认8张
  29. * - "img_title" (string):车图名称
  30. * - "img_url" (string):车图片url地址
  31. * - "car_name" (string):车名称
  32. */
  33. public function pageList($modelname, $where = '', $orderby = '', $field = '*', $listRows = 30)
  34. {
  35. $model = \DB::table($modelname);
  36. //查询条件
  37. if (!empty($where)) {
  38. $model = $model->where($where);
  39. }
  40. //排序
  41. if ($orderby != '') {
  42. if ($orderby == 'rand()') {
  43. $model = $model->orderBy(\DB::raw('rand()'));
  44. } else {
  45. if (count($orderby) == count($orderby, 1)) {
  46. $model = $model->orderBy($orderby[0], $orderby[1]);
  47. } else {
  48. foreach ($orderby as $row) {
  49. $model = $model->orderBy($row[0], $row[1]);
  50. }
  51. }
  52. }
  53. } else {
  54. $model = $model->orderBy('id', 'desc');
  55. }
  56. //要返回的字段
  57. if ($field != '*') {
  58. $model = $model->select(\DB::raw($field));
  59. }
  60. return $model->paginate($listRows);
  61. }
  62. // 添加管理员操作记录
  63. public function operation_log_add()
  64. {
  65. $time = time();
  66. // 记录操作
  67. if ($this->admin_info) {
  68. $data['login_id'] = $this->admin_info['id'];
  69. $data['login_name'] = $this->admin_info['name'];
  70. }
  71. $data['type'] = 1;
  72. $data['ip'] = request()->ip();
  73. $data['url'] = mb_strcut(request()->url(), 0, 255, 'UTF-8');
  74. $data['http_method'] = request()->method();
  75. $data['domain_name'] = mb_strcut($_SERVER['SERVER_NAME'], 0, 60, 'UTF-8');
  76. if ($data['http_method'] != 'GET') { $data['content'] = mb_strcut(json_encode(request()->toArray(), JSON_UNESCAPED_SLASHES), 0, 255, 'UTF-8'); }
  77. if (!empty($_SERVER['HTTP_REFERER'])) { $data['http_referer'] = mb_strcut($_SERVER['HTTP_REFERER'], 0, 255, 'UTF-8'); }
  78. $data['add_time'] = $time;
  79. logic('Log')->add($data);
  80. }
  81. }