Data.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. namespace Area\Api;
  3. use Dever;
  4. class Data
  5. {
  6. private $default = array
  7. (
  8. 'id' => -1,
  9. 'name' => '请选择',
  10. );
  11. private $search_default = array
  12. (
  13. 'id' => -1,
  14. 'name' => '地区选择',
  15. );
  16. /**
  17. * 获取地区数据
  18. *
  19. * @return mixed
  20. */
  21. public function get()
  22. {
  23. # 联动总数,默认到县区
  24. $total = Dever::input('total');
  25. if (!$total) {
  26. $total = 3;
  27. }
  28. # 当前联动级别
  29. $level = Dever::input('level');
  30. if (!$level) {
  31. $level = 1;
  32. }
  33. # 当前联动的上级id
  34. $parent = Dever::input('parent');
  35. if (!$parent) {
  36. $parent = 0;
  37. }
  38. # 是否是搜索列表页
  39. $search = Dever::input('search');
  40. if ($search) {
  41. $default = $this->search_default;
  42. if ($level == 1) {
  43. $default['name'] = '省份选择';
  44. } elseif ($level == 2) {
  45. $default['name'] = '城市选择';
  46. } elseif ($level == 3) {
  47. $default['name'] = '县区选择';
  48. } elseif ($level == 4) {
  49. $default['name'] = '街道选择';
  50. } else {
  51. $default['name'] = '社区选择';
  52. }
  53. } else {
  54. $default = $this->default;
  55. }
  56. if ($parent < 0) {
  57. Dever::error('error');
  58. }
  59. # 四级联动
  60. if ($level == 1) {
  61. $data = Dever::load('data', 'area')->getProvince();
  62. } elseif ($level == 2) {
  63. $data = Dever::load('data', 'area')->getCity($parent);
  64. } elseif ($level == 3) {
  65. $data = Dever::load('data', 'area')->getCounty($parent);
  66. } elseif ($level == 4) {
  67. $data = Dever::load('data', 'area')->getTown($parent);
  68. } else {
  69. $data = Dever::load('data', 'area')->getVillage($parent);
  70. }
  71. if (!$data) {
  72. Dever::error('error');
  73. }
  74. if ($search) {
  75. array_unshift($data, $default);
  76. }
  77. if ($level >= $total) {
  78. foreach ($data as &$v) {
  79. $v['leaf'] = true;
  80. }
  81. }
  82. $result['total'] = $total;
  83. $result['list'] = $data;
  84. return $result;
  85. }
  86. # 获取区域状态
  87. public function getStatus($area)
  88. {
  89. $temp = explode(',', $area);
  90. $num = count($temp);
  91. if ($num == 4 && isset($temp[3]) && $temp[3] > 0) {
  92. # 街道
  93. $where['id'] = $temp[3];
  94. $table = 'town';
  95. } elseif ($num == 3 && isset($temp[2]) && $temp[2] > 0) {
  96. # 区县
  97. $where['id'] = $temp[2];
  98. $table = 'county';
  99. } elseif ($num == 2 && isset($temp[1]) && $temp[1] > 0) {
  100. # 城市
  101. $where['id'] = $temp[1];
  102. $table = 'city';
  103. }
  104. if ($table) {
  105. $where['clear'] = true;
  106. $info = Dever::db($table, 'area')->find($where);
  107. if ($info && $info['status'] == 2) {
  108. return true;
  109. }
  110. }
  111. return false;
  112. }
  113. # 修改区域状态
  114. public function upStatus($area, $status = 2)
  115. {
  116. $table = '';
  117. $update['status'] = $status;
  118. $temp = explode(',', $area);
  119. $num = count($temp);
  120. if ($num == 4 && isset($temp[3]) && $temp[3] > 0) {
  121. # 街道
  122. $update['where_id'] = $temp[3];
  123. $table = 'town';
  124. } elseif ($num == 3 && isset($temp[2]) && $temp[2] > 0) {
  125. # 区县
  126. $update['where_id'] = $temp[2];
  127. $table = 'county';
  128. } elseif ($num == 2 && isset($temp[1]) && $temp[1] > 0) {
  129. # 城市
  130. $update['where_id'] = $temp[1];
  131. $table = 'city';
  132. }
  133. $state = false;
  134. if ($table) {
  135. $update['clear'] = true;
  136. $state = Dever::db($table, 'area')->update($update);
  137. }
  138. return $state;
  139. }
  140. /**
  141. * 获取三级地区数据:json格式,生成js文件
  142. *
  143. * @return mixed
  144. */
  145. public function createJson()
  146. {
  147. $path = Dever::data() . 'upload/';
  148. $create = Dever::input('create');
  149. if (!$create) {
  150. $create = 1;
  151. }
  152. $type = Dever::input('type');
  153. if (!$type) {
  154. $type = 'js';
  155. }
  156. if ($type == 'klist') {
  157. $file = $path . 'city.' . $type . '.js';
  158. } else {
  159. $file = $path . 'city.' . $type;
  160. }
  161. if (!is_file($file)) {
  162. $create = 2;
  163. }
  164. if ($create == 2) {
  165. $array = array
  166. (
  167. array
  168. (
  169. 'value' => "-1",
  170. 'name' => '请选择',
  171. ),
  172. );
  173. $klist = Dever::load('data', 'area')->getProvince();
  174. if ($type == 'klist') {
  175. $province = $klist;
  176. } else {
  177. $province = array_merge($array, $klist);
  178. }
  179. $province_data = array();
  180. $city_data = array();
  181. $county_data = array();
  182. foreach ($province as $k => $v) {
  183. $province_data[$k]['name'] = $v['name'];
  184. $province_data[$k]['id'] = $v['value'];
  185. if ($v['value'] <= 0) {
  186. continue;
  187. }
  188. $klist[$k]['text'] = $v['name'];
  189. $klist[$k]['value'] = $v['value'];
  190. $klist[$k]['children'] = Dever::load('data', 'area')->getCity($v['value']);
  191. if ($type == 'klist') {
  192. $city = $klist[$k]['children'];
  193. } else {
  194. $city = array_merge($array, $klist[$k]['children']);
  195. }
  196. foreach ($city as $k1 => $v1) {
  197. $city_data[$v['value']][$k1]['province'] = $v['name'];
  198. $city_data[$v['value']][$k1]['name'] = $v1['name'];
  199. $city_data[$v['value']][$k1]['id'] = $v1['value'];
  200. if ($v1['value'] <= 0) {
  201. continue;
  202. }
  203. $klist[$k]['children'][$k1]['text'] = $v1['name'];
  204. $klist[$k]['children'][$k1]['value'] = $v1['value'];
  205. $klist[$k]['children'][$k1]['children'] = Dever::load('data', 'area')->getCounty($v1['value']);
  206. if ($type == 'klist') {
  207. $county = $klist[$k]['children'][$k1]['children'];
  208. } else {
  209. $county = array_merge($array, $klist[$k]['children'][$k1]['children']);
  210. }
  211. foreach ($county as $k2 => $v2) {
  212. $klist[$k]['children'][$k1]['children'][$k2]['text'] = $v2['name'];
  213. $klist[$k]['children'][$k1]['children'][$k2]['value'] = $v2['value'];
  214. $county_data[$v1['value']][$k2]['city'] = $v1['name'];
  215. $county_data[$v1['value']][$k2]['name'] = $v2['name'];
  216. $county_data[$v1['value']][$k2]['id'] = $v2['value'];
  217. }
  218. }
  219. }
  220. if ($type == 'klist') {
  221. $content = 'var cities = ' . Dever::json_encode($klist) . ';';
  222. } elseif ($type == 'js') {
  223. $content = 'var provinces = ' . Dever::json_encode($province_data) . ';';
  224. $content .= 'var citys = ' . Dever::json_encode($city_data) . ';';
  225. $content .= 'var areas = ' . Dever::json_encode($county_data) . ';';
  226. } elseif ($type == 'plist') {
  227. $content = '<?xml version="1.0" encoding="UTF-8"?>
  228. <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  229. <plist version="1.0">
  230. <array>' . "\r\n";
  231. foreach ($province_data as $k => $v) {
  232. $content .= ' <dict>
  233. <key>province</key>
  234. <string>'.$v['name'].'</string>
  235. <key>citys</key>
  236. <array>';
  237. if (isset($city_data[$v['id']])) {
  238. foreach ($city_data[$v['id']] as $k1 => $v1) {
  239. $content .= "\r\n" . ' <dict>
  240. <key>city</key>
  241. <string>'.$v1['name'].'</string>
  242. <key>districts</key>
  243. <array>';
  244. if (isset($county_data[$v1['id']])) {
  245. foreach ($county_data[$v1['id']] as $k2 => $v2) {
  246. $content .= "\r\n" . ' <string>'.$v2['name'].'</string>';
  247. }
  248. $content .= "\r\n ";
  249. }
  250. $content .= '</array>' . "\r\n" . ' </dict>';
  251. }
  252. $content .= "\r\n ";
  253. }
  254. $content .= '</array>' . "\r\n" . ' </dict>' . "\r\n";
  255. }
  256. $content .= '</array>' . "\r\n" . '</plist>';
  257. }
  258. file_put_contents($file, $content);
  259. }
  260. $assets = Dever::config('host')->uploadRes;
  261. return str_replace($path, $assets, $file);
  262. }
  263. }