Data.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 = array
  108. (
  109. array
  110. (
  111. 'value' => "-1",
  112. 'name' => '请选择',
  113. ),
  114. );
  115. $klist = Dever::load('data', 'area')->getProvince();
  116. if ($type == 'klist') {
  117. $province = $klist;
  118. } else {
  119. $province = array_merge($array, $klist);
  120. }
  121. $province_data = array();
  122. $city_data = array();
  123. $county_data = array();
  124. foreach ($province as $k => $v) {
  125. $province_data[$k]['name'] = $v['name'];
  126. $province_data[$k]['id'] = $v['value'];
  127. if ($v['value'] <= 0) {
  128. continue;
  129. }
  130. $klist[$k]['text'] = $v['name'];
  131. $klist[$k]['value'] = $v['value'];
  132. $klist[$k]['children'] = Dever::load('data', 'area')->getCity($v['value']);
  133. if ($type == 'klist') {
  134. $city = $klist[$k]['children'];
  135. } else {
  136. $city = array_merge($array, $klist[$k]['children']);
  137. }
  138. foreach ($city as $k1 => $v1) {
  139. $city_data[$v['value']][$k1]['province'] = $v['name'];
  140. $city_data[$v['value']][$k1]['name'] = $v1['name'];
  141. $city_data[$v['value']][$k1]['id'] = $v1['value'];
  142. if ($v1['value'] <= 0) {
  143. continue;
  144. }
  145. $klist[$k]['children'][$k1]['text'] = $v1['name'];
  146. $klist[$k]['children'][$k1]['value'] = $v1['value'];
  147. $klist[$k]['children'][$k1]['children'] = Dever::load('data', 'area')->getCounty($v1['value']);
  148. if ($type == 'klist') {
  149. $county = $klist[$k]['children'][$k1]['children'];
  150. } else {
  151. $county = array_merge($array, $klist[$k]['children'][$k1]['children']);
  152. }
  153. foreach ($county as $k2 => $v2) {
  154. $klist[$k]['children'][$k1]['children'][$k2]['text'] = $v2['name'];
  155. $klist[$k]['children'][$k1]['children'][$k2]['value'] = $v2['value'];
  156. $county_data[$v1['value']][$k2]['city'] = $v1['name'];
  157. $county_data[$v1['value']][$k2]['name'] = $v2['name'];
  158. $county_data[$v1['value']][$k2]['id'] = $v2['value'];
  159. }
  160. }
  161. }
  162. if ($type == 'klist') {
  163. $content = 'var cities = ' . Dever::json_encode($klist) . ';';
  164. } elseif ($type == 'js') {
  165. $content = 'var provinces = ' . Dever::json_encode($province_data) . ';';
  166. $content .= 'var citys = ' . Dever::json_encode($city_data) . ';';
  167. $content .= 'var areas = ' . Dever::json_encode($county_data) . ';';
  168. } elseif ($type == 'plist') {
  169. $content = '<?xml version="1.0" encoding="UTF-8"?>
  170. <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  171. <plist version="1.0">
  172. <array>' . "\r\n";
  173. foreach ($province_data as $k => $v) {
  174. $content .= ' <dict>
  175. <key>province</key>
  176. <string>'.$v['name'].'</string>
  177. <key>citys</key>
  178. <array>';
  179. if (isset($city_data[$v['id']])) {
  180. foreach ($city_data[$v['id']] as $k1 => $v1) {
  181. $content .= "\r\n" . ' <dict>
  182. <key>city</key>
  183. <string>'.$v1['name'].'</string>
  184. <key>districts</key>
  185. <array>';
  186. if (isset($county_data[$v1['id']])) {
  187. foreach ($county_data[$v1['id']] as $k2 => $v2) {
  188. $content .= "\r\n" . ' <string>'.$v2['name'].'</string>';
  189. }
  190. $content .= "\r\n ";
  191. }
  192. $content .= '</array>' . "\r\n" . ' </dict>';
  193. }
  194. $content .= "\r\n ";
  195. }
  196. $content .= '</array>' . "\r\n" . ' </dict>' . "\r\n";
  197. }
  198. $content .= '</array>' . "\r\n" . '</plist>';
  199. }
  200. file_put_contents($file, $content);
  201. }
  202. $assets = Dever::config('host')->uploadRes;
  203. return str_replace($path, $assets, $file);
  204. }
  205. }