Api.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. <?php
  2. namespace Area\Src;
  3. use Dever;
  4. class Api
  5. {
  6. private $default = array
  7. (
  8. 'value' => -1,
  9. 'name' => '不限',
  10. );
  11. private $search_default = array
  12. (
  13. 'value' => -1,
  14. 'name' => '地区选择',
  15. );
  16. /**
  17. * 获取地区数据
  18. *
  19. * @return mixed
  20. */
  21. public function get()
  22. {
  23. # 联动总数,默认到县区
  24. $level_total = Dever::input('level_total', 3);
  25. # 当前联动级别
  26. $level_num = Dever::input('level_num');
  27. # 一般为id
  28. $level_id = Dever::input('level_id');
  29. # 是否是搜索列表页
  30. $level_search = Dever::input('level_search');
  31. if ($level_search) {
  32. $default = $this->search_default;
  33. if ($level_num == 1) {
  34. $default['name'] = '省份选择';
  35. } elseif ($level_num == 2) {
  36. $default['name'] = '城市选择';
  37. } elseif ($level_num == 3) {
  38. $default['name'] = '县区选择';
  39. } elseif ($level_num == 4) {
  40. $default['name'] = '街道选择';
  41. } else {
  42. $default['name'] = '社区选择';
  43. }
  44. } else {
  45. $default = $this->default;
  46. }
  47. # 四级联动
  48. if ($level_num == 1) {
  49. $data = Dever::db('area/province')->getAll();
  50. } elseif ($level_num == 2) {
  51. $data = Dever::db('area/city')->getAll(array('province_id' => $level_id));
  52. } elseif ($level_num == 3) {
  53. $data = Dever::db('area/county')->getAll(array('city_id' => $level_id));
  54. } elseif ($level_num == 4) {
  55. $data = Dever::db('area/town')->getAll(array('county_id' => $level_id));
  56. } else {
  57. $data = Dever::db('area/village')->getAll(array('town_id' => $level_id));
  58. }
  59. if (!$data) {
  60. Dever::alert('error');
  61. }
  62. if ($level_search || $level_num > 1) {
  63. array_unshift($data, $default);
  64. }
  65. $result['level_total'] = $level_total;
  66. $result['list'] = $data;
  67. return $result;
  68. }
  69. /**
  70. * 获取详细信息
  71. *
  72. * @return mixed
  73. */
  74. public function getInfo($area, $col = 'id')
  75. {
  76. if ($area) {
  77. $area = explode(',', $area);
  78. $result = array();
  79. foreach ($area as $k => $v) {
  80. if ($k == 0) {
  81. $result[$k] = $this->getName('province', $v, true, $col);
  82. } elseif ($k == 1) {
  83. $result[$k] = $this->getName('city', $v, true, $col);
  84. if ($col == 'id' && isset($result[1]['name']) && $result[0]['name'] == $result[1]['name']) {
  85. unset($result[1]);
  86. }
  87. } elseif ($k == 2) {
  88. $result[$k] = $this->getName('county', $v, true, $col);
  89. } elseif ($k == 3) {
  90. $result[$k] = $this->getName('town', $v, true, $col);
  91. } elseif ($k == 4) {
  92. $result[$k] = $this->getName('village', $v, true, $col);
  93. }
  94. }
  95. return $result;
  96. }
  97. return array();
  98. }
  99. /**
  100. * 根据地区id转成名称
  101. *
  102. * @return mixed
  103. */
  104. public function string($area, $im = ',', $unset = true, $name = '不限')
  105. {
  106. if ($area) {
  107. $area = explode(',', $area);
  108. $result = array();
  109. foreach ($area as $k => $v) {
  110. if ($k == 0) {
  111. $result[$k] = $this->getName('province', $v, false, 'id', $name);
  112. } elseif ($k == 1) {
  113. $result[$k] = $this->getName('city', $v);
  114. if ($result[0] == $result[1] && $unset) {
  115. unset($result[1]);
  116. }
  117. } elseif ($k == 2) {
  118. $result[$k] = $this->getName('county', $v, false, 'id', $name);
  119. } elseif ($k == 3) {
  120. $result[$k] = $this->getName('town', $v, false, 'id', $name);
  121. } elseif ($k == 4) {
  122. $result[$k] = $this->getName('village', $v, false, 'id', $name);
  123. } else {
  124. $result[$k] = '';
  125. }
  126. if (!$result[$k]) {
  127. unset($result[$k]);
  128. }
  129. }
  130. return implode($im, $result);
  131. }
  132. return '';
  133. }
  134. private function getName($table, $value, $state = false, $col = 'id', $name = '不限')
  135. {
  136. if (($col == 'id' && $value > 0) || ($col != 'id' && $value)) {
  137. $where[$col] = $value;
  138. $data = Dever::db('area/' . $table)->one($where);
  139. if ($state) {
  140. return $data;
  141. }
  142. if ($data) {
  143. $name = $data['name'];
  144. }
  145. }
  146. return $name;
  147. }
  148. # 获取6位地区编码
  149. public function code($id, $len = 6)
  150. {
  151. $id = substr($id, 0, $len);
  152. $id = str_pad($id, $len, '0', STR_PAD_RIGHT);
  153. return $id;
  154. }
  155. # 修改区域状态
  156. public function upStatus($area, $status = 2)
  157. {
  158. $table = '';
  159. $update['status'] = $status;
  160. $temp = explode(',', $area);
  161. $num = count($temp);
  162. if ($num == 4) {
  163. # 街道
  164. $update['where_id'] = $temp[3];
  165. $table = 'area/town';
  166. } elseif ($num == 3) {
  167. # 区县
  168. $update['where_id'] = $temp[2];
  169. $table = 'area/county';
  170. } elseif ($num == 2) {
  171. # 城市
  172. $update['where_id'] = $temp[1];
  173. $table = 'area/city';
  174. }
  175. $state = false;
  176. if ($table) {
  177. $state = Dever::db($table)->update($update);
  178. }
  179. return $state;
  180. }
  181. public function getProvince()
  182. {
  183. return Dever::db('area/province')->getAll();
  184. }
  185. public function getCity($province_id)
  186. {
  187. return Dever::db('area/city')->getAll(array('province_id' => $province_id));
  188. }
  189. public function getCounty($city_id)
  190. {
  191. return Dever::db('area/county')->getAll(array('city_id' => $city_id));
  192. }
  193. public function getTown($county_id)
  194. {
  195. return Dever::db('area/town')->getAll(array('county_id' => $county_id));
  196. }
  197. public function getVillage($town_id)
  198. {
  199. return Dever::db('area/village')->getAll(array('town_id' => $town_id));
  200. }
  201. # 获取城市并根据首字母排序的
  202. public function getCityToFirst()
  203. {
  204. $result = array();
  205. $data = Dever::db('area/city')->getAll();
  206. if (Dever::import('pinyin')) {
  207. $result = Dever::sortPinyinFirst($data, 'pinyin_first');
  208. }
  209. return $result;
  210. }
  211. # 获取价值
  212. public function getPrice($type, $area)
  213. {
  214. $price = 0;
  215. $area = explode(',', $area);
  216. $city = $area[1];
  217. $city = Dever::db('area/city')->find($city);
  218. if ($city && $city['level_id'] > 0) {
  219. $level = Dever::db('area/level')->find($city['level_id']);
  220. if (!$level) {
  221. return $price;
  222. }
  223. $num = 10000;
  224. if ($type == 1) {
  225. # 城市
  226. $price = $level['city_price'] * $num;
  227. } elseif ($type == 2) {
  228. # 区县
  229. $county = $area[2];
  230. $county = Dever::db('area/county')->find($county);
  231. if ($county && $county['level'] == 2) {
  232. $up_level = Dever::db('area/level')->find(array('level' => $level['level'] - 1));
  233. if ($up_level) {
  234. $level['county_price'] = $up_level['county_price'];
  235. }
  236. }
  237. if ($county && $county['level'] == 3) {
  238. $up_level = Dever::db('area/level')->find(array('level' => $level['level'] - 2));
  239. if ($up_level) {
  240. $level['county_price'] = $up_level['county_price'];
  241. }
  242. }
  243. $price = $level['county_price'] * $num;
  244. } elseif ($type == 3) {
  245. # 街道
  246. $town = $area[3];
  247. $town = Dever::db('area/town')->find($town);
  248. if ($town && $town['type'] > 1) {
  249. $level['town_price'] = $level['county_price'];
  250. }
  251. $price = $level['town_price'] * $num;
  252. }
  253. }
  254. return $price;
  255. }
  256. /**
  257. * 获取三级地区数据:json格式,生成js文件
  258. *
  259. * @return mixed
  260. */
  261. public function createJson()
  262. {
  263. $path = Dever::data() . 'upload/';
  264. $create = Dever::input('create', 1);
  265. $type = Dever::input('type', 'js');
  266. if ($type == 'klist') {
  267. $file = $path . 'city.' . $type . '.js';
  268. } else {
  269. $file = $path . 'city.' . $type;
  270. }
  271. if (!is_file($file)) {
  272. $create = 2;
  273. }
  274. if ($create == 2) {
  275. $array = array
  276. (
  277. array
  278. (
  279. 'value' => "-1",
  280. 'name' => '请选择',
  281. ),
  282. );
  283. $klist = Dever::db('area/province')->getAll();
  284. if ($type == 'klist') {
  285. $province = $klist;
  286. } else {
  287. $province = array_merge($array, $klist);
  288. }
  289. $province_data = array();
  290. $city_data = array();
  291. $county_data = array();
  292. foreach ($province as $k => $v) {
  293. $province_data[$k]['name'] = $v['name'];
  294. $province_data[$k]['id'] = $v['value'];
  295. if ($v['value'] <= 0) {
  296. continue;
  297. }
  298. $klist[$k]['text'] = $v['name'];
  299. $klist[$k]['value'] = $v['value'];
  300. $klist[$k]['children'] = Dever::db('area/city')->getAll(array('province_id' => $v['value']));
  301. if ($type == 'klist') {
  302. $city = $klist[$k]['children'];
  303. } else {
  304. $city = array_merge($array, $klist[$k]['children']);
  305. }
  306. foreach ($city as $k1 => $v1) {
  307. $city_data[$v['value']][$k1]['province'] = $v['name'];
  308. $city_data[$v['value']][$k1]['name'] = $v1['name'];
  309. $city_data[$v['value']][$k1]['id'] = $v1['value'];
  310. if ($v1['value'] <= 0) {
  311. continue;
  312. }
  313. $klist[$k]['children'][$k1]['text'] = $v1['name'];
  314. $klist[$k]['children'][$k1]['value'] = $v1['value'];
  315. $klist[$k]['children'][$k1]['children'] = Dever::db('area/county')->getAll(array('city_id' => $v1['value']));
  316. if ($type == 'klist') {
  317. $county = $klist[$k]['children'][$k1]['children'];
  318. } else {
  319. $county = array_merge($array, $klist[$k]['children'][$k1]['children']);
  320. }
  321. foreach ($county as $k2 => $v2) {
  322. $klist[$k]['children'][$k1]['children'][$k2]['text'] = $v2['name'];
  323. $klist[$k]['children'][$k1]['children'][$k2]['value'] = $v2['value'];
  324. $county_data[$v1['value']][$k2]['city'] = $v1['name'];
  325. $county_data[$v1['value']][$k2]['name'] = $v2['name'];
  326. $county_data[$v1['value']][$k2]['id'] = $v2['value'];
  327. }
  328. }
  329. }
  330. if ($type == 'klist') {
  331. $content = 'var cities = ' . Dever::json_encode($klist) . ';';
  332. } elseif ($type == 'js') {
  333. $content = 'var provinces = ' . Dever::json_encode($province_data) . ';';
  334. $content .= 'var citys = ' . Dever::json_encode($city_data) . ';';
  335. $content .= 'var areas = ' . Dever::json_encode($county_data) . ';';
  336. } elseif ($type == 'plist') {
  337. $content = '<?xml version="1.0" encoding="UTF-8"?>
  338. <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  339. <plist version="1.0">
  340. <array>' . "\r\n";
  341. foreach ($province_data as $k => $v) {
  342. $content .= ' <dict>
  343. <key>province</key>
  344. <string>'.$v['name'].'</string>
  345. <key>citys</key>
  346. <array>';
  347. if (isset($city_data[$v['id']])) {
  348. foreach ($city_data[$v['id']] as $k1 => $v1) {
  349. $content .= "\r\n" . ' <dict>
  350. <key>city</key>
  351. <string>'.$v1['name'].'</string>
  352. <key>districts</key>
  353. <array>';
  354. if (isset($county_data[$v1['id']])) {
  355. foreach ($county_data[$v1['id']] as $k2 => $v2) {
  356. $content .= "\r\n" . ' <string>'.$v2['name'].'</string>';
  357. }
  358. $content .= "\r\n ";
  359. }
  360. $content .= '</array>' . "\r\n" . ' </dict>';
  361. }
  362. $content .= "\r\n ";
  363. }
  364. $content .= '</array>' . "\r\n" . ' </dict>' . "\r\n";
  365. }
  366. $content .= '</array>' . "\r\n" . '</plist>';
  367. }
  368. file_put_contents($file, $content);
  369. }
  370. $assets = Dever::config('host')->uploadRes;
  371. return str_replace($path, $assets, $file);
  372. }
  373. }