uniapp小程序用戶信息獲取、登錄以及數據請求


uniapp進行微信小程序開發時,用戶信息的獲取以及登錄方面梳理:

<template>
    <view>
        <!-- 如果用手機號登錄,獲取手機號碼相關信息 -->
        <button type="primary" open-type="getPhoneNumber" lang="zh_CN" @getphonenumber="getPhoneNumber">手機號一鍵登錄</button>
        <!-- 如果用微信登錄,獲取微信相關用戶信息 -->
        <button open-type="getUserInfo" lang="zh_CN" @getuserinfo="onGotUserInfo">獲取用戶信息</button>
    </view>
</template>

<script>
    export default {
        data() {
            return {}
        },
        onLoad() {
            // 執行查看授權選項
            this.getSettingMes();
        },
        methods: {
            // 查看已授權選項
            getSettingMes() {
                let _this = this;
                uni.getSetting({
                    success(res) {
                        if (res.authSetting['scope.userInfo']) {
                            // 用戶信息已授權,獲取用戶信息
                            uni.getUserInfo({
                                success(res) {
                                    console.log(res);
                                },
                                fail() {
                                    console.log("獲取用戶信息失敗")
                                }
                            })
                        } else if (!res.authSetting['scope.userInfo']) {
                            console.log("需要點擊按鈕手動授權")
                        }
                    },
                    fail() {
                        console.log("獲取已授權選項失敗")
                    }
                })
            },
            // 手動授權方法
            onGotUserInfo(e) {
                // 獲取用戶信息
                uni.getUserInfo({
                    // 獲取信息成功
                    success(res) {
                        console.log(res);
                        // 成功后進行登錄,獲取code
                        uni.login({
                          success (res) {
                             console.log(res);
                            if (res.code) {
                              //發起網絡請求
                              uni.request({
                                // 請求路徑
                                url: 'https://test.com/onLogin',
                                // 請求參數code
                                data: {
                                  code: res.code
                                },
                                method: 'GET',
                                success(res){
                                    // 請求成功后獲取openid和session_key
                                    console.log(res)
                                }
                              })
                            } else {
                              console.log('登錄失敗!' + res.errMsg)
                            }
                          }
                        })
                    },
                    fail() {
                        console.log("獲取用戶信息失敗");
                    }
                })
            },
            // 手機登錄時獲取手機號碼相關信息的函數
            getPhoneNumber(e) {
                console.log(e);
            }
        }
    }
</script>

附截圖:

 

 

 

 

 


免責聲明!

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



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