AprilActivity.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace backend\models;
  3. use Yii;
  4. /**
  5. * @inheritdoc
  6. */
  7. class AprilActivity extends \common\models\AprilActivity
  8. {
  9. /**
  10. * 四月活动后台列表数据
  11. * @param array $post
  12. * @return array
  13. */
  14. public function getAprilList($post)
  15. {
  16. $result = ['code' => 0, 'data' => [], 'message' => ''];
  17. $order = isset($post['order']) ? strtolower($post['order']) : '';
  18. $orderBy = isset($post['orderBy']) ? strtolower($post['orderBy']) : 'desc';
  19. $search = isset($post['search']) ? $post['search'] : '';
  20. $start = isset($post['start']) ? (int) $post['start'] : 0;
  21. $length = isset($post['length']) ? (int) $post['length'] : 20;
  22. $draw = isset($post['draw']) ? $post['draw'] : 1;
  23. $where = ['and'];
  24. // 搜索
  25. if ($search) {
  26. if (filter_var($search, FILTER_VALIDATE_IP) !== false) {
  27. $where[] = ['=', 'ip', $search];
  28. } elseif (is_numeric($search)) {
  29. // 用户名也可能是数字
  30. $where[] = [
  31. 'or',
  32. ['=', 'phone', $search],
  33. ];
  34. } else {
  35. $where[] = [
  36. 'or',
  37. ['like', 'name', $search],
  38. ];
  39. }
  40. }
  41. // 排序
  42. $allowOrderColumn = ['id', 'name', 'phone', 'ip', 'create_time'];
  43. if (in_array($order, $allowOrderColumn) && in_array($orderBy, ['asc', 'desc'])) {
  44. if ($orderBy == 'asc') {
  45. $orderCondition = [$order => SORT_ASC];
  46. } else {
  47. $orderCondition = [$order => SORT_DESC];
  48. }
  49. } else {
  50. $orderCondition = ['id' => SORT_DESC];
  51. }
  52. $query = static::find();
  53. $query->where($where)
  54. ->orderBy($orderCondition);
  55. $count = $query->count();
  56. $query->offset($start)->limit($length);
  57. $list = $query->asArray()->all();
  58. foreach ($list as $k => $v) {
  59. $list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
  60. }
  61. $data['data'] = $list;
  62. $data['draw'] = $draw;
  63. $data['recordsFiltered'] = $count;
  64. $data['recordsTotal'] = $count;
  65. $result['data'] = $data;
  66. $result['code'] = 1;
  67. return $result;
  68. }
  69. }