记录师兄写的一个从数据库获取省份的代码

api接口里面:
 public function getCity() {
    if(request()->isGet()) {
        $data = input('get.');
        $res = model('Area')->getCityInfo($data);
        $d = [
            'data' => $res,
            'code' => 1,
            'msg' => "success"
        ];
        $res = json_encode($d);
        return json($d);
    } else {
        $this->result('',0,'请求出错');
    }
}

Model里面:
public function getCityInfo($data) {
    $validate = validate('Area')->check($data);
    if($validate) {
        $type = $data['type'];
        switch ($type){
            case "1":
                $this->field('id1 id,title')->where('id2',0)->where('id3',0);
                break;
            case "2":
                $this->field('id2 id,title')
                    ->where('id2','>','0')
                    ->where('id3',0)
                    ->where('id1',$data['id1']);
                break;
            case "3":
                $this->field('id3 id,title')
                    ->where('id3','>','0')
                    ->where('id2',$data['id2'])
                    ->where('id1',$data['id1']);
                break;
        }
        $order = [
            'px' => 'desc',
            'id' => 'asc'
        ];
        $this->where('flag',1)->order($order);
        return $this->select()->toArray();
    }
}

猜你喜欢

转载自blog.csdn.net/abc455050954/article/details/79306529