commit | author | age
|
2207d6
|
1 |
<?php |
W |
2 |
/** |
|
3 |
* @link:http://www.zjhejiang.com/ |
|
4 |
* @copyright: Copyright (c) 2018 浙江禾匠信息科技有限公司 |
|
5 |
* |
|
6 |
* Created by PhpStorm. |
|
7 |
* User: 风哀伤 |
|
8 |
* Date: 2018/12/4 |
|
9 |
* Time: 9:33 |
|
10 |
*/ |
|
11 |
|
|
12 |
namespace app\models; |
|
13 |
|
|
14 |
use yii\helpers\Json; |
|
15 |
|
|
16 |
class DistrictArr |
|
17 |
{ |
|
18 |
public static function getArr() |
|
19 |
{ |
|
20 |
return Json::decode(file_get_contents(__DIR__ . '/district.json'), true); |
|
21 |
} |
|
22 |
|
|
23 |
public static function getDiffCityDistrict($city_name) |
|
24 |
{ |
|
25 |
$list = []; |
|
26 |
return isset($list[$city_name]) ? $list[$city_name] : null; |
|
27 |
} |
|
28 |
|
|
29 |
// 数组排序 先按parent_id正序再按id正序进行排列 |
|
30 |
public static function getSort($arr) |
|
31 |
{ |
|
32 |
uasort($arr, function ($a, $b) { |
|
33 |
$a_p = intval($a['parent_id']); |
|
34 |
$b_p = intval($b['parent_id']); |
|
35 |
if ($a_p == $b_p) { |
|
36 |
$a_id = intval($a['id']); |
|
37 |
$b_id = intval($b['id']); |
|
38 |
if ($a_id == $b_id) { |
|
39 |
return 0; |
|
40 |
} |
|
41 |
return ($a_id < $b_id) ? -1 : 1; |
|
42 |
} |
|
43 |
return ($a_p < $b_p) ? -1 : 1; |
|
44 |
}); |
|
45 |
echo "<pre>"; |
|
46 |
var_export($arr); |
|
47 |
echo "</pre>"; |
|
48 |
|
|
49 |
exit(); |
|
50 |
} |
|
51 |
|
|
52 |
/** |
|
53 |
* 获取已父级id为$parent_id为根节点的树型结构数组 |
|
54 |
* @param array $arr 省市区数据 |
|
55 |
* @param string $level 不需要的数据的level,当前等级且包含其下级都排除 |
|
56 |
* @return array |
|
57 |
*/ |
|
58 |
public static function getList(&$arr, $level = null) |
|
59 |
{ |
|
60 |
$treeData = [];// 保存结果 |
|
61 |
$catList = $arr; |
|
62 |
foreach ($catList as &$item) { |
|
63 |
if ($level && $item['level'] == $level) { |
|
64 |
continue; |
|
65 |
} |
|
66 |
$parent_id = $item['parent_id']; |
|
67 |
if (isset($catList[$parent_id]) && !empty($catList[$parent_id])) {// 肯定是子分类 |
|
68 |
$catList[$parent_id]['list'][] = &$catList[$item['id']]; |
|
69 |
} else {// 肯定是一级分类 |
|
70 |
$treeData[] = &$catList[$item['id']]; |
|
71 |
} |
|
72 |
} |
|
73 |
unset($item); |
|
74 |
return $treeData[0]['list']; |
|
75 |
} |
|
76 |
|
|
77 |
// 根据id获取信息 |
|
78 |
public static function getDistrict($param) |
|
79 |
{ |
|
80 |
if (is_array($param)) { |
|
81 |
$id = $param['id']; |
|
82 |
} else { |
|
83 |
$id = $param; |
|
84 |
} |
|
85 |
$arr = self::getArr(); |
|
86 |
if (!isset($arr[$id])) { |
|
87 |
throw new \Exception('未找到省市区,请重新选择'); |
|
88 |
} |
|
89 |
$list = $arr[$id]; |
|
90 |
$str = \Yii::$app->serializer->encode($list); |
|
91 |
return \Yii::$app->serializer->decode($str); |
|
92 |
} |
|
93 |
|
|
94 |
// 根据指定的key=>value查找需要的数组 |
|
95 |
public static function getInfo($param) |
|
96 |
{ |
|
97 |
$newParam = []; |
|
98 |
foreach ($param as $key => $value) { |
|
99 |
$newParam[0] = $key; |
|
100 |
$newParam[1] = $value; |
|
101 |
} |
|
102 |
$arr = self::getArr(); |
|
103 |
$list = array_filter($arr, function ($v) use ($newParam) { |
|
104 |
return $v[$newParam[0]] == $newParam[1]; |
|
105 |
}); |
|
106 |
$str = \Yii::$app->serializer->encode($list); |
|
107 |
return \Yii::$app->serializer->decode($str); |
|
108 |
} |
|
109 |
|
|
110 |
// 运费规则、起送规则、包邮规则 |
|
111 |
public static function getRules() |
|
112 |
{ |
|
113 |
$arr = self::getArr(); |
|
114 |
$empty = []; |
|
115 |
$emptyPointer = &$empty; |
|
116 |
$ok = false; |
|
117 |
foreach ($arr as $index => &$item) { |
|
118 |
if ($item['parent_id'] == 1) { |
|
119 |
$okCity = false; |
|
120 |
$data = [ |
|
121 |
'id' => $item['id'], |
|
122 |
'name' => $item['name'] |
|
123 |
]; |
|
124 |
$data['show'] = false; |
|
125 |
$data['city'] = []; |
|
126 |
$dataPointer = &$data['city']; |
|
127 |
foreach ($arr as $key => $value) { |
|
128 |
if ($value['parent_id'] == $index) { |
|
129 |
$okCity = true; |
|
130 |
$dataPointer[] = [ |
|
131 |
'id' => $value['id'], |
|
132 |
'name' => $value['name'], |
|
133 |
'show' => false |
|
134 |
]; |
|
135 |
} |
|
136 |
if ($okCity && $value['parent_id'] != $index) { |
|
137 |
break; |
|
138 |
} |
|
139 |
} |
|
140 |
array_push($emptyPointer, $data); |
|
141 |
$ok = true; |
|
142 |
} |
|
143 |
if ($ok && $item['parent_id'] != 1) { |
|
144 |
break; |
|
145 |
} |
|
146 |
} |
|
147 |
|
|
148 |
return $empty; |
|
149 |
} |
|
150 |
|
|
151 |
// 微信获取地址 |
|
152 |
public static function getWechatDistrict($province_name, $city_name, $county_name) |
|
153 |
{ |
|
154 |
$arr = self::getArr(); |
|
155 |
$ok = false; |
|
156 |
$res = [ |
|
157 |
'code' => 0, |
|
158 |
'msg' => '', |
|
159 |
'data' => [ |
|
160 |
'district' => [ |
|
161 |
|
|
162 |
] |
|
163 |
] |
|
164 |
]; |
|
165 |
$county = []; |
|
166 |
foreach ($arr as $item) { |
|
167 |
if ($item['name'] == $county_name && $item['level'] == 'district') { |
|
168 |
$county = $item; |
|
169 |
$city = $arr[$item['parent_id']]; |
|
170 |
if (isset($arr[$county['parent_id']]) && $city['name'] == $city_name) { |
|
171 |
$province = $arr[$city['parent_id']]; |
|
172 |
if (isset($arr[$city['parent_id']]) && $province['name'] = $province_name) { |
|
173 |
$ok = true; |
|
174 |
break; |
|
175 |
} |
|
176 |
} |
|
177 |
} |
|
178 |
} |
|
179 |
if (!$ok) { |
|
180 |
$diff_district = self::getDiffCityDistrict($city_name); |
|
181 |
$res['data']['district'] = [ |
|
182 |
'province' => [ |
|
183 |
'id' => 3268, |
|
184 |
'name' => '其他', |
|
185 |
], |
|
186 |
'city' => [ |
|
187 |
'id' => 3269, |
|
188 |
'name' => '其他', |
|
189 |
], |
|
190 |
'district' => [ |
|
191 |
'id' => 3270, |
|
192 |
'name' => '其他', |
|
193 |
], |
|
194 |
]; |
|
195 |
if ($diff_district) { |
|
196 |
$res['data']['district'] = $diff_district; |
|
197 |
} |
|
198 |
return $res; |
|
199 |
} |
|
200 |
|
|
201 |
$res['data']['district'] = [ |
|
202 |
'province' => [ |
|
203 |
'id' => $province['id'], |
|
204 |
'name' => $province['name'] |
|
205 |
], |
|
206 |
'city' => [ |
|
207 |
'id' => $city['id'], |
|
208 |
'name' => $city['name'] |
|
209 |
], |
|
210 |
'district' => [ |
|
211 |
'id' => $county['id'], |
|
212 |
'name' => $county['name'] |
|
213 |
] |
|
214 |
]; |
|
215 |
|
|
216 |
return $res; |
|
217 |
} |
|
218 |
|
|
219 |
public static function getTerritorial() |
|
220 |
{ |
|
221 |
$data = \Yii::$app->cache->get('territorial_list'); |
|
222 |
if ($data) { |
|
223 |
return $data; |
|
224 |
} |
|
225 |
$arr = self::getArr(); |
|
226 |
$treeData = [];// 保存结果 |
|
227 |
$catList = &$arr; |
|
228 |
foreach ($catList as &$item) { |
|
229 |
$item['selected'] = false; |
|
230 |
$item['show'] = false; |
|
231 |
$parent_id = $item['parent_id']; |
|
232 |
if (isset($catList[$parent_id]) && !empty($catList[$parent_id])) {// 肯定是子分类 |
|
233 |
$catList[$parent_id]['list'][] = &$catList[$item['id']]; |
|
234 |
} else {// 肯定是一级分类 |
|
235 |
$treeData[] = &$catList[$item['id']]; |
|
236 |
} |
|
237 |
} |
|
238 |
unset($item); |
|
239 |
$data = $treeData[0]['list']; |
|
240 |
\Yii::$app->cache->set('territorial_list', $data); |
|
241 |
return $data; |
|
242 |
} |
|
243 |
} |