小程序授权微信地址信息逻辑

第一步:查阅微信小程序文档

第二步:评估实现方法

wx.chooseAddress

app.js 中 requiredPrivateInfos

小程序管理后台 -「开发」-「开发管理」-「接口设置」申请获取地址权限

第三步:新增获取微信按钮

第四步:如何将获取的地址

直接把数据,传到后台处理。通过数据库查找定位城市编号。
考虑地址重复的问题。

微信添加按钮,事件为addressWxHandle

复制addAddress函数,并命名为addWxAddress

并编辑请求接口地址为,wxminiapp

第五步:新增后端代码逻辑

# address
    Route::get('users/address/{address_id}',  'AddressController@detail');
    Route::post('users/address',              'AddressController@add');
    Route::put('users/address/{address_id}',  'AddressController@edit');
    Route::delete('users/address/{address_id}',  'AddressController@delete');
    Route::post('users/address/wxminiapp',              'AddressController@wxminiapp');
public function wxminiapp() {
        $AddressDB = new Address();
        $Region = new Region();
        $data = file_get_contents("php://input");
        $data = json_decode($data,true);
        
        
        $data['member_id'] = $this->memberInfo['member_id'];

        $province_name = str_replace("省" , "", $data['province']);
        $city_name = str_replace("市" , "", $data['city']);
        $district_name = str_replace("" , "", $data['district']);
        
        $province = $Region->where([['region_name','like','%'.$province_name],'level' => 1])->first();
        $city = $Region->where([['region_name','like','%'.$city_name],'level' => 2,'parent_id' => $province->region_id])->first();
        $district = $Region->where([['region_name','like','%'.$district_name],'level' => 3,'parent_id' => $city->region_id])->first();
        
        
        $data['province_id'] = $province['region_id'];
        $data['city_id'] = $city['region_id'];
        $data['district_id'] = $district['region_id'];

        $data['province'] = $province['region_name'];
        $data['city'] = $city['region_name'];
        $data['district'] = $district['region_name'];

        $data['full_address'] = $province['region_name'].$city['region_name'].$district['region_name'].$data['address'];

        if(DB::table('address')->where($data)->count())
        {
            $ret['result'] = 'fail';  
            $ret['error_info'] = '该地址已存在';
            echo json_encode($ret);
            exit;
        }

        $data['created_time'] = time();
        $data['last_updated_time'] = time();
        $address_id = $AddressDB->insertGetId($data);

        $ret['result'] = 'ok';

        $ret['address_id'] = $address_id;
        echo json_encode($ret);
        exit;
    }

ThinkPHP
4 声望3 粉丝