Api.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. } else {
  38. $default['name'] = '地区选择';
  39. }
  40. } else {
  41. $default = $this->default;
  42. }
  43. # 三级联动
  44. if ($level_num == 1) {
  45. $data = Dever::db('area/province')->getAll();
  46. } elseif ($level_num == 2) {
  47. $data = Dever::db('area/city')->getAll(array('province_id' => $level_id));
  48. } else {
  49. $data = Dever::db('area/county')->getAll(array('city_id' => $level_id));
  50. }
  51. if (!$data) {
  52. Dever::alert('error');
  53. }
  54. if ($level_search || $level_num > 1) {
  55. array_unshift($data, $default);
  56. }
  57. $result['level_total'] = $level_total;
  58. $result['list'] = $data;
  59. return $result;
  60. }
  61. /**
  62. * 获取所有地区数据:json格式,生成js文件
  63. *
  64. * @return mixed
  65. */
  66. public function createJson()
  67. {
  68. $path = Dever::data() . 'upload/';
  69. $create = Dever::input('create', 1);
  70. $type = Dever::input('type', 'js');
  71. if ($type == 'klist') {
  72. $file = $path . 'city.' . $type . '.js';
  73. } else {
  74. $file = $path . 'city.' . $type;
  75. }
  76. if (!is_file($file)) {
  77. $create = 2;
  78. }
  79. if ($create == 2) {
  80. $array = array
  81. (
  82. array
  83. (
  84. 'value' => "-1",
  85. 'name' => '请选择',
  86. ),
  87. );
  88. $klist = Dever::db('area/province')->getAll();
  89. if ($type == 'klist') {
  90. $province = $klist;
  91. } else {
  92. $province = array_merge($array, $klist);
  93. }
  94. $province_data = array();
  95. $city_data = array();
  96. $county_data = array();
  97. foreach ($province as $k => $v) {
  98. $province_data[$k]['name'] = $v['name'];
  99. $province_data[$k]['id'] = $v['value'];
  100. if ($v['value'] <= 0) {
  101. continue;
  102. }
  103. $klist[$k]['text'] = $v['name'];
  104. $klist[$k]['value'] = $v['value'];
  105. $klist[$k]['children'] = Dever::db('area/city')->getAll(array('province_id' => $v['value']));
  106. if ($type == 'klist') {
  107. $city = $klist[$k]['children'];
  108. } else {
  109. $city = array_merge($array, $klist[$k]['children']);
  110. }
  111. foreach ($city as $k1 => $v1) {
  112. $city_data[$v['value']][$k1]['province'] = $v['name'];
  113. $city_data[$v['value']][$k1]['name'] = $v1['name'];
  114. $city_data[$v['value']][$k1]['id'] = $v1['value'];
  115. if ($v1['value'] <= 0) {
  116. continue;
  117. }
  118. $klist[$k]['children'][$k1]['text'] = $v1['name'];
  119. $klist[$k]['children'][$k1]['value'] = $v1['value'];
  120. $klist[$k]['children'][$k1]['children'] = Dever::db('area/county')->getAll(array('city_id' => $v1['value']));
  121. if ($type == 'klist') {
  122. $county = $klist[$k]['children'][$k1]['children'];
  123. } else {
  124. $county = array_merge($array, $klist[$k]['children'][$k1]['children']);
  125. }
  126. foreach ($county as $k2 => $v2) {
  127. $klist[$k]['children'][$k1]['children'][$k2]['text'] = $v2['name'];
  128. $klist[$k]['children'][$k1]['children'][$k2]['value'] = $v2['value'];
  129. $county_data[$v1['value']][$k2]['city'] = $v1['name'];
  130. $county_data[$v1['value']][$k2]['name'] = $v2['name'];
  131. $county_data[$v1['value']][$k2]['id'] = $v2['value'];
  132. }
  133. }
  134. }
  135. if ($type == 'klist') {
  136. $content = 'var cities = ' . Dever::json_encode($klist) . ';';
  137. } elseif ($type == 'js') {
  138. $content = 'var provinces = ' . Dever::json_encode($province_data) . ';';
  139. $content .= 'var citys = ' . Dever::json_encode($city_data) . ';';
  140. $content .= 'var areas = ' . Dever::json_encode($county_data) . ';';
  141. } elseif ($type == 'plist') {
  142. $content = '<?xml version="1.0" encoding="UTF-8"?>
  143. <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  144. <plist version="1.0">
  145. <array>' . "\r\n";
  146. foreach ($province_data as $k => $v) {
  147. $content .= ' <dict>
  148. <key>province</key>
  149. <string>'.$v['name'].'</string>
  150. <key>citys</key>
  151. <array>';
  152. if (isset($city_data[$v['id']])) {
  153. foreach ($city_data[$v['id']] as $k1 => $v1) {
  154. $content .= "\r\n" . ' <dict>
  155. <key>city</key>
  156. <string>'.$v1['name'].'</string>
  157. <key>districts</key>
  158. <array>';
  159. if (isset($county_data[$v1['id']])) {
  160. foreach ($county_data[$v1['id']] as $k2 => $v2) {
  161. $content .= "\r\n" . ' <string>'.$v2['name'].'</string>';
  162. }
  163. $content .= "\r\n ";
  164. }
  165. $content .= '</array>' . "\r\n" . ' </dict>';
  166. }
  167. $content .= "\r\n ";
  168. }
  169. $content .= '</array>' . "\r\n" . ' </dict>' . "\r\n";
  170. }
  171. $content .= '</array>' . "\r\n" . '</plist>';
  172. }
  173. file_put_contents($file, $content);
  174. }
  175. $assets = Dever::config('host')->uploadRes;
  176. return str_replace($path, $assets, $file);
  177. }
  178. /**
  179. * 获取详细信息
  180. *
  181. * @return mixed
  182. */
  183. public function getInfo($area, $col = 'id')
  184. {
  185. if ($area) {
  186. $area = explode(',', $area);
  187. $result = array();
  188. foreach ($area as $k => $v) {
  189. if ($k == 0) {
  190. $result[$k] = $this->getName('province', $v, true, $col);
  191. } elseif ($k == 1) {
  192. $result[$k] = $this->getName('city', $v, true, $col);
  193. if ($col == 'id' && isset($result[1]['name']) && $result[0]['name'] == $result[1]['name']) {
  194. unset($result[1]);
  195. }
  196. } elseif ($k == 2) {
  197. $result[$k] = $this->getName('county', $v, true, $col);
  198. }
  199. }
  200. return $result;
  201. }
  202. return array();
  203. }
  204. /**
  205. * 根据地区id转成名称
  206. *
  207. * @return mixed
  208. */
  209. public function string($area, $im = ',')
  210. {
  211. if ($area) {
  212. $area = explode(',', $area);
  213. $result = array();
  214. foreach ($area as $k => $v) {
  215. if ($k == 0) {
  216. $result[$k] = $this->getName('province', $v);
  217. } elseif ($k == 1) {
  218. $result[$k] = $this->getName('city', $v);
  219. if ($result[0] == $result[1]) {
  220. unset($result[1]);
  221. }
  222. } elseif ($k == 2) {
  223. $result[$k] = $this->getName('county', $v);
  224. }
  225. }
  226. return implode($im, $result);
  227. }
  228. return '';
  229. }
  230. private function getName($table, $value, $state = false, $col = 'id')
  231. {
  232. $name = '不限';
  233. if (($col == 'id' && $value > 0) || ($col != 'id' && $value)) {
  234. $where[$col] = $value;
  235. $data = Dever::db('area/' . $table)->one($where);
  236. if ($state) {
  237. return $data;
  238. }
  239. if ($data) {
  240. $name = $data['name'];
  241. }
  242. }
  243. return $name;
  244. }
  245. # 获取6位地区编码
  246. public function code($id)
  247. {
  248. $id = substr($id, 0, 6);
  249. $id = str_pad($id, 6, '0', STR_PAD_RIGHT);
  250. return $id;
  251. }
  252. public function getProvince()
  253. {
  254. return Dever::db('area/province')->getAll();
  255. }
  256. public function getCity($province_id)
  257. {
  258. return Dever::db('area/city')->getAll(array('province_id' => $province_id));
  259. }
  260. }