Data.php 8.8 KB

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