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

南国老符 南国老符 最后更新于:2023-05-02 浏览:666

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

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)
转载请务必注明: 来源:《建站管家》https://www.df81.com/news/506.html

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

上一篇: uniapp+uviewui上拉加载和下拉刷新最简单示例代码

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

请先登录后,再发表评论 ~ ~
发表评论
评论 (0)

发表帖子
广告位招租( ¥5 / 天 )
点击咨询
最新帖子

南国老符
2025-05-26 16:54
南国老符
2024-09-29 10:29
南国老符
2023-12-15 21:41
广告位招租( ¥5 / 天 )
点击咨询
最近评论

U
user1379
回复:版本号多少?
U
user1379
回复:新版本修复了,下载新版本即可
U
user1379
U
user1304
回复:默认没有这个功能,你可以自行二次开发后期可能会考虑在站群版本上加入此功能及商城功能
Y
yuanis90
回复:后台->语言->全局管理->模型管理->编辑->启用关键词内链即可
Y
yuanis90
回复:后台已经启用了,但是前台产品说明的关分键词还是没有自动链接到,
购买VIP
购买授权
余额充值
发表帖子
客服微信