小程序---獲取地理位置及獲取用戶userinfo信息



<template> <view> <!-- 測試 --> <view class="bar"> <button open-type="getUserInfo" lang="zh_CN" @getuserinfo="wxLogin">登錄/注冊</button> <button open-type="openSetting" bindopensetting="callback">打開設置</button> </view> <van-button type="default" @tap="show">默認按鈕</van-button> <van-popup show="{{ show }}" @close="onClose">內容</van-popup> </view> </view> </template> <script> import wepy from 'wepy'; export default class Personal extends wepy.page { config = { navigationBarTitleText: '個人中心', usingComponents: {} }; data = {}; methods = { async wxLogin(e) { //獲取wx的login信息 let resLogin = await wepy.login(); //調用查詢/獲取地理位置方法 this.methods.getUserLocation(); //獲取用戶userinfo this.userinfo = e.detail.userInfo; //存入localstorge wepy.setStorage({ key: 'userinfo', data: this.userinfo }); //從localstorge拿地址信息 let locadate = wepy.getStorageSync('locationdate'); //從localstorge拿userinfo信息 let obj = wepy.getStorageSync('userinfo'); this.userInfo = obj; if (!obj) { //如果userinfo不存在說明該用戶不同意授權 //將虛擬token放入localstorge wepy.setStorage({ key: 'token', data: '123456789' }); } else { //如果userinfo存在說明該用戶同意授權 //將真實token放入localstorge wepy.setStorage({ key: 'token', data: resinfo.data.Token }); this.tokendate = resinfo.data.Token; } }, openSetting() { wx.openSetting(); }, getUserLocation() { wx.getSetting({ success: res => { // res.authSetting['scope.userLocation'] == undefined 表示 初始化進入該頁面 // res.authSetting['scope.userLocation'] == false 表示 非初始化進入該頁面,且未授權 // res.authSetting['scope.userLocation'] == true 表示 地理位置授權 if ( res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true ) { //未授權 wepy.showModal({ title: '請求授權當前位置', content: '需要獲取您的地理位置,請確認授權', success: res => { if (res.cancel) { //取消授權 wepy.showToast({ title: '拒絕授權', icon: 'none', duration: 1000 }); } else if (res.confirm) { //確定授權,通過wx.openSetting發起授權請求 wepy.openSetting({ success: res => { if (res.authSetting['scope.userLocation'] == true) { wepy.showToast({ title: '授權成功', icon: 'success', duration: 1000 }); //再次授權,調用wx.getLocation的API this.geo(); } else { wepy.showToast({ title: '授權失敗', icon: 'none', duration: 1000 }); } } }); } } }); } else if (res.authSetting['scope.userLocation'] == undefined) { //用戶首次進入頁面,調用wx.getLocation的API this.geo(); } else { console.log('授權成功'); //調用wx.getLocation的API this.geo(); } } }); }, async geo() { //地理位置授權獲取 let result = await wepy.getLocation(); //拼接為字符串 this.locationdate = result.latitude + ',' + result.longitude; //存入locastorge wepy.setStorage({ key: 'locationdate', data: this.locationdate }); } }; } </script> <style lang="less"> </style>

 1. 因為微信更新后,userinfo接口只能由用戶自己去觸發,所以只能設置按鈕,但是可以修改按鈕樣式,比如,:登陸/注冊

 2. location獲取地理位置的接口有一點很坑的地方就是.如果第一次用戶點擊了拒絕,那么當你再次調用這個接口獲取信息或者重新授權時,系統默認用戶上次點擊了拒絕,則不會彈框,直接按用戶已拒絕處理,所以解決辦法就是從新寫一個彈框,讓用戶知道自己沒有授權,然后自己主動從opensetting開啟權限,這樣在此調用的時候就會直接獲取用戶的地理位置信息,但是如果用戶再次沒有開權限,就又需要判定一遍,所以,層層邏輯下還是挺麻煩的.

3.因為需求更改,現在又有了promise和es6模式下的代碼.過幾天上傳一下


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM