这段代码当前用不到,以后可能用到,仅我自己看得懂,网友无需理会,我做个记号
1、api/controller/v1/area.php
// 列表 [仅APP小程序用到] public function index(){ try { /** * 百度地图web服务API,普通IP定位,获取城市 * 参考文档:https://lbsyun.baidu.com/index.php?title=webapi/ip-api */ $cityid = input('cityid'); // 如果切换了城市 if(!empty($cityid)){ $areaRs = Db::name('area')->where('id',$cityid)->field('id,parentid,title')->find(); }else{ // $ak = config('other.baidu_ak');/cn/base.php也用到 $ip = request()->ip(); $ak = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'; // $ip = '121.35.2.57'; // 根据ip定位城市 $httpUrl='http://api.map.baidu.com/location/ip?coor=bd09ll&ak='.$ak.'&ip='.$ip; $comClass = new \common\Dofile(); $httpResult = $comClass->get_http_code($httpUrl); if($httpResult[0] == 200){ $city = json_decode($httpResult[1])->content->address_detail->city; $areaWhere[] = ['title','like',"%$city%"]; }else{ $areaWhere[] = ['signid','=',1]; $areaWhere[] = ['parentid','=',0]; $areaWhere[] = ['lang','=',$this->lang]; } $areaRs = Db::name('area')->where($areaWhere)->field('id,parentid,title')->order('sequence desc')->find(); $cityid = $areaRs['id']; } // 省份,父级城市 if(empty($areaRs['parentid'])){ $parentRs = array('id'=>$cityid,'parentid'=>0,'title'=>$areaRs['title']); $parentCurrent = 1; }else{ $parentRs = Db::name('area')->where('id',$areaRs['parentid'])->field('id,parentid,title')->find(); $parentCurrent = 0; } // 城市列表 $num = is_numeric(input('num')) ? input('num') : 50; $parentid = $areaRs['parentid'] == 0 ? $cityid : $areaRs['parentid']; $data = areaModel::list($num, $parentid, 'id,parentid,title', 'sequence desc', $this->lang)->toArray(); $list1[0] = $parentRs + array('current'=>$parentCurrent); foreach ($data as $key=>$rs){ $key ++; if($rs['id'] == $cityid){ $list1[$key] = $rs + array('current'=>1); }else{ $list1[$key] = $rs + array('current'=>0); } } $list = array('current'=>$areaRs,'list'=>$list1); return json($this->msg($list,lang('c_success'),200)); } catch (Exception $e) { return json($this->msg($e->getMessage(),'服务端错误',500)); } }
2、api/model/area.php
/** * 地区 * num 显示数量 * parentid 父级id * field 检索字段 * order 排序 * lang 语言 */ static function list($num = 4, $parentid = 0, $field = 'id,title', $order = 'sequence desc', $lang = 'cn'){ $where[] = ['lang','=',$lang]; $where[] = ['parentid','=',$parentid]; $where[] = ['signid','=',1]; $rs = Db::name('area')->where($where)->field($field)->limit($num)->order($order)->select(); return $rs; }
上一篇: 《建站管家》二次开发文档