城市列表php代码,做个记号,以后可能用得到

知识技术 2023-05-02 20:50 397
转载请务必注明: 来源:《建站管家》https://www.df81.com/news/506.html

这段代码当前用不到,以后可能用到,仅我自己看得懂,网友无需理会,我做个记号

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;
    }



点赞 (0)

收藏 (0)

本站部分信息来源网络,如有侵权,请联系QQ:1062129401删除。

上一篇: 《建站管家》二次开发文档

下一篇: uniapp中switchTab跳转回tabbar页面并传参刷新的方法

请先登录后,再发表评论 ~ ~
发表评论
发表评论
发表帖子
广告位招租( ¥5 / 天 )
点击咨询
相关帖子

如果要判断文件是否存在,用函数 is_file(),如果要判断目录是否存在,用函数 is_dir()
用宝塔安装TP程序后,网站前台可以访问,后台无法访问,不是程序问题,而是web服务器环境配置问题(PHP配置),出现这种问题,一般是windows主机安装宝塔造成的,linux安装宝塔不会出现此错误
广告位招租( ¥3 / 天 )
点击咨询
广告位招租( ¥2 / 天 )
点击咨询
广告位招租( ¥1 / 天 )
点击咨询
最新帖子

小朋友,你爸爸是谁啊?这很单纯的,就像我们碰到别人会问:你吃过饭了吗?类似;许多人初次相识,经常会问:你是做什么的啊?你做什么,好像决定你收入的高低,你,资产的范围
1、升级框架到ThinkPHP8.0.3【要求PHP8.1+环境,推荐PHP8.1】 2、启用/禁用调试模式等小细节改进
后台点击生成站点地图 一直转圈圈没有反应,
在线客服