uniapp 在小程序獲取當前經緯度,微信賬號信息


index.vue

<template>
    <view>
        <view class="">
            當前經度:{{local.long}}
            當前緯度:{{local.lat}}
        </view>
        <view v-if="!hasLogin">
            游客你好{{hasLogin}}
        </view>
        <view v-if="hasLogin">
            你好:{{userinfo.nickName}}
        </view>
    </view>
</template>

<script>
    import { mapState,mapMutations } from 'vuex'
    export default {
        computed:{
            ...mapState(['hasLogin','userinfo','local'])
        },
        data() {
            return {
                jwd:{
                    long:"",//當前位置的經度
                    lat:"",//當前位置的緯度
                }
            }
        },
        onLoad() {
            this.getjwd()
            this.islogin()
        },
        methods: {
            ...mapMutations(['login','savejwd']),
            getjwd(){
                var _this=this
                uni.getLocation({
                    type: 'wgs84',                
                    success(res) {                        
                        let jwd={
                            long:res.longitude,//當前位置的經度
                            lat:res.latitude,//當前位置的緯度
                        }
                        _this.savejwd(jwd)
                        console.log('當前位置的經度:' + res.longitude);
                        console.log('當前位置的緯度:' + res.latitude);                        
                    }
                });
            },
            islogin(){
                var _this=this
                uni.getStorage({
                    key: 'userinfo',
                    success(res) {
                        _this.login(res.data)
                        console.log(res.data);
                                                
                    }
                });
            }
        }
    }
</script>

<style>
    
</style>

user.vue

<template>
    <view>
        
        <button class='testbutton' open-type="getUserInfo" @getuserinfo="tologin" withCredentials="true">
        登陸</button>
        <view v-if="!hasLogin">
            請先登陸{{userinfo}}
        </view>
        <view v-if="hasLogin">
            {{userinfo.nickName}}
        </view>
    </view>
</template>

<script>
    import { mapState,mapMutations } from 'vuex'
    export default {
        computed:{
            ...mapState(['hasLogin','userinfo'])
        },
        data() {
            return {
                
            }
        },
        methods: {
            ...mapMutations(['login','aa']),
            tologin(){
                this.aa(2)
                var _this=this
                uni.getUserInfo({
                    provider:"weixin",
                    success(info) {
                        console.log(info)
                        _this.login(info.userInfo)
                        
                    }
            })
            }
        }
    }
</script>

<style>

</style>

store/index.js

import Vue from 'vue'
import Vuex from 'vuex'
 
Vue.use(Vuex)
 
const store = new Vuex.Store({
  state: {
    hasLogin: false,
    userinfo: {},
    local:{},
   
  },
  mutations: {
    // 登錄
    login(state,res) {
      state.hasLogin = true
      state.userinfo = res
      uni.setStorage({
        key: 'userinfo',
        data: res
      })
    },
    
    // 經緯度
    savejwd(state,jwd) {
      state.local = jwd      
    },
    aa(state,n){
        console.log(n)
    }
  },
})
 
export default store

main.js

 

 

 

因為涉及到定位,所以要直接修改微信代碼的 app.json。注意是修改微信代碼,不是uniapp代碼

"permission": {
    "scope.userLocation": {
      "desc": "你的位置信息將用於小程序位置接口的效果展示" 
    }
  },

 

 

 效果圖未登陸前

 

 

 點擊登陸后

 

 將信息經緯度儲存到vuex,方便其他頁面全局調用。如果下次在其他位置進入小程序,經緯度也會變

將用戶信息存儲到localstore 方便下次進入小程序時可以免登陸

 


免責聲明!

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



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