|
11 | 11 | use App\Http\Repository\BusRepository;
|
12 | 12 | use App\Http\Repository\NewBusRepository;
|
13 | 13 | use Illuminate\Http\Request;
|
| 14 | +use Illuminate\Support\Facades\Cache; |
14 | 15 |
|
15 | 16 | class IndexController extends CommonController
|
16 | 17 | {
|
@@ -41,24 +42,74 @@ public function getList(Request $request)
|
41 | 42 | {
|
42 | 43 | $line = $request['linename'];
|
43 | 44 | $line = preg_replace('/快\b(\d)/', '快线$1号', $line);
|
44 |
| - switch ($request['type']) { |
45 |
| - case '1': |
46 |
| - case 'new': |
47 |
| - $list = NewBusRepository::getInstent()->getLine($line); |
48 |
| - break; |
49 |
| - |
50 |
| - default: |
51 |
| - $list = BusRepository::getInstent()->getList($line); |
52 |
| - // $list = []; |
53 |
| - if (empty($list)) { |
54 |
| - $list = NewBusRepository::getInstent()->getLine($line); |
55 |
| - } |
| 45 | + // 1. 前端直接传递 type 参数时先判断 |
| 46 | + if (isset($request['type'])) { |
| 47 | + switch ($request['type']) { |
| 48 | + case '1': |
| 49 | + case 'new': |
| 50 | + $list = $this->getNewList($line); |
| 51 | + break; |
| 52 | + default: |
| 53 | + $list = $this->getOldList($line); |
| 54 | + // $list = []; |
| 55 | + if (empty($list)) { |
| 56 | + $list = $this->getNewList($line); |
| 57 | + } |
| 58 | + } |
| 59 | + } else { |
| 60 | + // 2. 根据后台配置查询不同的结果 |
| 61 | + $default_open = Cache::get('default_open'); |
| 62 | + switch ($default_open) { |
| 63 | + case '2': // 查询新版 |
| 64 | + $list = $this->getNewList($line); |
| 65 | + break; |
| 66 | + case '3': // 先查老版再查新版 |
| 67 | + $list = $this->getOldList($line); |
| 68 | + // $list = []; |
| 69 | + if (empty($list)) { |
| 70 | + $list = $this->getNewList($line); |
| 71 | + } |
| 72 | + break; |
| 73 | + case '4': // 先查新版再查老版 |
| 74 | + $list = $this->getNewList($line); |
| 75 | + // $list = []; |
| 76 | + if (empty($list)) { |
| 77 | + $list = $this->getOldList($line); |
| 78 | + } |
| 79 | + break; |
| 80 | + default: // 查询老版 |
| 81 | + $list = $this->getOldList($line); |
| 82 | + } |
56 | 83 | }
|
57 | 84 |
|
58 | 85 | // return $this->exportData($list);
|
59 | 86 | return $this->out(200, $list);
|
60 | 87 | }
|
61 | 88 |
|
| 89 | + /** |
| 90 | + * 获取老线路列表 |
| 91 | + * |
| 92 | + * @param $line |
| 93 | + * |
| 94 | + * @return array |
| 95 | + */ |
| 96 | + private function getNewList($line) |
| 97 | + { |
| 98 | + return NewBusRepository::getInstent()->getLine($line); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * 获取老线路列表 |
| 103 | + * |
| 104 | + * @param $line |
| 105 | + * |
| 106 | + * @return array |
| 107 | + */ |
| 108 | + private function getOldList($line) |
| 109 | + { |
| 110 | + return BusRepository::getInstent()->getList($line); |
| 111 | + } |
| 112 | + |
62 | 113 | /**
|
63 | 114 | * 获取实时公交数据table列表 --- 根据data-href 地址,请求 szjt.gov.cn 查询实时公交数据,不能缓存
|
64 | 115 | * 获取实时公交数据table列表,数据来自 szjt.gov.cn,需要限制访问频率,1秒一次请求
|
|
0 commit comments