RegionService.php
1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
namespace App\Http\Services;
//后台用户相关信息服务
use App\Model\RedisModel;
use App\Model\RegionModel;
use Illuminate\Support\Facades\DB;
//用于判断是否已经查看的服务
class RegionService
{
public function getCityRegionData()
{
$regionModel = new RegionModel();
$data = $regionModel->whereIn('region_type', [1,2])->get()->toArray();
$topRegions = [];
foreach ($data as $region) {
if ($region['parent_id'] == 1) {
$topRegions[] = [
'id' => $region['region_id'],
'name' => $region['region_name'],
'children'=>[],
];
}
}
foreach ($topRegions as &$topRegion) {
foreach ($data as $region) {
if ($region['parent_id'] == 1) {
continue;
}
if ($region['parent_id'] == $topRegion['id']) {
$topRegion['children'][] = [
'id' => $region['region_id'],
'name' => $region['region_name'],
];
}
}
}
unset($topRegion);
$regionData = $topRegions;
return $regionData;
}
public function getRegionNameByIds($ids)
{
$ids = array_filter($ids, function ($id) {
return !empty($id);
});
if (empty($ids)) {
return [];
}
$model = new RegionModel();
return $model->whereIn('region_id', $ids)->pluck('region_name')->toArray();
}
}