123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- <?php
- namespace Area\Api;
- use Dever;
- class Data
- {
- private $default = array
- (
- 'id' => -1,
- 'name' => '请选择',
- );
- private $search_default = array
- (
- 'id' => -1,
- 'name' => '地区选择',
- );
- /**
- * 获取地区数据
- *
- * @return mixed
- */
- public function get()
- {
- # 联动总数,默认到县区
- $total = Dever::input('total');
- if (!$total) {
- $total = 3;
- }
- # 当前联动级别
- $level = Dever::input('level');
- if (!$level) {
- $level = 1;
- }
- # 当前联动的上级id
- $parent = Dever::input('parent');
- if (!$parent) {
- $parent = 0;
- }
- # 是否是搜索列表页
- $search = Dever::input('search');
- if ($search) {
- $default = $this->search_default;
- if ($level == 1) {
- $default['name'] = '省份选择';
- } elseif ($level == 2) {
- $default['name'] = '城市选择';
- } elseif ($level == 3) {
- $default['name'] = '县区选择';
- } elseif ($level == 4) {
- $default['name'] = '街道选择';
- } else {
- $default['name'] = '社区选择';
- }
- } else {
- $default = $this->default;
- }
- if ($parent < 0) {
- Dever::error('error');
- }
- # 四级联动
- if ($level == 1) {
- $data = Dever::load('data', 'area')->getProvince();
- } elseif ($level == 2) {
- $data = Dever::load('data', 'area')->getCity($parent);
- } elseif ($level == 3) {
- $data = Dever::load('data', 'area')->getCounty($parent);
- } elseif ($level == 4) {
- $data = Dever::load('data', 'area')->getTown($parent);
- } else {
- $data = Dever::load('data', 'area')->getVillage($parent);
- }
- if (!$data) {
- Dever::error('error');
- }
- if ($search) {
- array_unshift($data, $default);
- }
- if ($level >= $total) {
- foreach ($data as &$v) {
- $v['leaf'] = true;
- }
- }
- $result['total'] = $total;
- $result['list'] = $data;
- return $result;
- }
- # 获取区域状态
- public function getStatus($area)
- {
- $temp = explode(',', $area);
- $num = count($temp);
- if ($num == 4 && isset($temp[3]) && $temp[3] > 0) {
- # 街道
- $where['id'] = $temp[3];
- $table = 'town';
- } elseif ($num == 3 && isset($temp[2]) && $temp[2] > 0) {
- # 区县
- $where['id'] = $temp[2];
- $table = 'county';
- } elseif ($num == 2 && isset($temp[1]) && $temp[1] > 0) {
- # 城市
- $where['id'] = $temp[1];
- $table = 'city';
- }
- if ($table) {
- $where['clear'] = true;
- $info = Dever::db($table, 'area')->find($where);
- if ($info && $info['status'] == 2) {
- return true;
- }
- }
- return false;
- }
- # 修改区域状态
- public function upStatus($area, $status = 2)
- {
- $table = '';
- $update['status'] = $status;
- $temp = explode(',', $area);
- $num = count($temp);
- if ($num == 4 && isset($temp[3]) && $temp[3] > 0) {
- # 街道
- $update['where_id'] = $temp[3];
- $table = 'town';
- } elseif ($num == 3 && isset($temp[2]) && $temp[2] > 0) {
- # 区县
- $update['where_id'] = $temp[2];
- $table = 'county';
- } elseif ($num == 2 && isset($temp[1]) && $temp[1] > 0) {
- # 城市
- $update['where_id'] = $temp[1];
- $table = 'city';
- }
- $state = false;
- if ($table) {
- $update['clear'] = true;
- $state = Dever::db($table, 'area')->update($update);
- }
- return $state;
- }
- /**
- * 获取三级地区数据:json格式,生成js文件
- *
- * @return mixed
- */
- public function createJson()
- {
- $path = Dever::data() . 'upload/';
- $create = Dever::input('create');
- if (!$create) {
- $create = 1;
- }
- $type = Dever::input('type');
- if (!$type) {
- $type = 'js';
- }
- if ($type == 'klist') {
- $file = $path . 'city.' . $type . '.js';
- } else {
- $file = $path . 'city.' . $type;
- }
-
- if (!is_file($file)) {
- $create = 2;
- }
- if ($create == 2) {
- $array = array
- (
- array
- (
- 'value' => "-1",
- 'name' => '请选择',
- ),
- );
- $klist = Dever::load('data', 'area')->getProvince();
- if ($type == 'klist') {
- $province = $klist;
- } else {
- $province = array_merge($array, $klist);
- }
- $province_data = array();
- $city_data = array();
- $county_data = array();
- foreach ($province as $k => $v) {
- $province_data[$k]['name'] = $v['name'];
- $province_data[$k]['id'] = $v['value'];
- if ($v['value'] <= 0) {
- continue;
- }
- $klist[$k]['text'] = $v['name'];
- $klist[$k]['value'] = $v['value'];
- $klist[$k]['children'] = Dever::load('data', 'area')->getCity($v['value']);
- if ($type == 'klist') {
- $city = $klist[$k]['children'];
- } else {
- $city = array_merge($array, $klist[$k]['children']);
- }
- foreach ($city as $k1 => $v1) {
- $city_data[$v['value']][$k1]['province'] = $v['name'];
- $city_data[$v['value']][$k1]['name'] = $v1['name'];
- $city_data[$v['value']][$k1]['id'] = $v1['value'];
- if ($v1['value'] <= 0) {
- continue;
- }
- $klist[$k]['children'][$k1]['text'] = $v1['name'];
- $klist[$k]['children'][$k1]['value'] = $v1['value'];
- $klist[$k]['children'][$k1]['children'] = Dever::load('data', 'area')->getCounty($v1['value']);
- if ($type == 'klist') {
- $county = $klist[$k]['children'][$k1]['children'];
- } else {
- $county = array_merge($array, $klist[$k]['children'][$k1]['children']);
- }
- foreach ($county as $k2 => $v2) {
- $klist[$k]['children'][$k1]['children'][$k2]['text'] = $v2['name'];
- $klist[$k]['children'][$k1]['children'][$k2]['value'] = $v2['value'];
- $county_data[$v1['value']][$k2]['city'] = $v1['name'];
- $county_data[$v1['value']][$k2]['name'] = $v2['name'];
- $county_data[$v1['value']][$k2]['id'] = $v2['value'];
- }
- }
- }
- if ($type == 'klist') {
- $content = 'var cities = ' . Dever::json_encode($klist) . ';';
- } elseif ($type == 'js') {
- $content = 'var provinces = ' . Dever::json_encode($province_data) . ';';
- $content .= 'var citys = ' . Dever::json_encode($city_data) . ';';
- $content .= 'var areas = ' . Dever::json_encode($county_data) . ';';
- } elseif ($type == 'plist') {
- $content = '<?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
- <plist version="1.0">
- <array>' . "\r\n";
- foreach ($province_data as $k => $v) {
- $content .= ' <dict>
- <key>province</key>
- <string>'.$v['name'].'</string>
- <key>citys</key>
- <array>';
- if (isset($city_data[$v['id']])) {
- foreach ($city_data[$v['id']] as $k1 => $v1) {
- $content .= "\r\n" . ' <dict>
- <key>city</key>
- <string>'.$v1['name'].'</string>
- <key>districts</key>
- <array>';
- if (isset($county_data[$v1['id']])) {
- foreach ($county_data[$v1['id']] as $k2 => $v2) {
- $content .= "\r\n" . ' <string>'.$v2['name'].'</string>';
- }
- $content .= "\r\n ";
- }
- $content .= '</array>' . "\r\n" . ' </dict>';
- }
- $content .= "\r\n ";
- }
-
- $content .= '</array>' . "\r\n" . ' </dict>' . "\r\n";
- }
- $content .= '</array>' . "\r\n" . '</plist>';
- }
- file_put_contents($file, $content);
- }
- $assets = Dever::config('host')->uploadRes;
- return str_replace($path, $assets, $file);
- }
- }
|