uni-app 框架登錄

闡述
開發(fā)uni-app之前建議大家先看看 manifest.json 配置項各參數(shù),對整個項目開發(fā)是有幫助的
文檔路徑:https://uniapp.dcloud.io/collocation/manifest?id=%E5%AE%8C%E6%95%B4-manifestjson
我先歸納一下登錄邏輯
目前我先歸納小程序的登錄
登陸授權(quán)我分兩大步:1.登錄授權(quán)獲取用戶信息 2.后續(xù)使用檢測登錄態(tài)
登錄授權(quán):
因為授權(quán)的彈框是可以取消的,取消之后第二次打開則是無效,所以解決這個問題,我使用兩個api uni.getSetting uni.openSetting ,一個是獲取用戶當(dāng)前獲取權(quán)限的狀態(tài),一個是打開權(quán)限設(shè)置并授權(quán)。
當(dāng)用戶不授權(quán)的時候,做一個同樣的Buttn按鈕去模擬登錄,提示用戶已經(jīng)取消授權(quán)需要手動開啟授權(quán)權(quán)限,當(dāng)點擊授權(quán)完時,需要取得用戶code,uniapp提供檢測當(dāng)前環(huán)境的api uni.getProvider ,所以先取得環(huán)境在獲取code,當(dāng)拿到code iv encryptedData 時就要可以調(diào)用后臺接口拿到用戶對應(yīng)的信息,并緩存下來,代碼例子:

//login.vue
<template>
    <view class="login">
        <image src="../../static/images/loginLogo.png" class="logo"></image>
        <button class="login-button" type="primary" @click="openSetting" v-if="status==0">微信登錄</button>
        <button class="login-button" type="primary" open-type="getUserInfo" @getuserinfo="getuserinfo" v-else>微信登錄</button>
        <view class="phone-login">手機(jī)號登錄/注冊</view>
    </view>
</template>

<script>
    import {getProvider,login,getCode} from '@/api/request/login/login.js'
    import {getSetting} from '@/api/request/login/authorize.js'
    
    export default {
        components: {
            
        },
        data() {
            return {
                code: '',
                status: 0 //2未操作 1已經(jīng)授權(quán)  0拒絕授權(quán)
            }
        },
        onLoad() {
            
        },
        onShow() {
            (async () => {
                //獲取授權(quán)狀態(tài)
                this.status = await getSetting()

                //獲取服務(wù)商信息
                let provider = await getProvider();
                                
                //獲取code
                this.code = await getCode(provider[0])
            })()
        },
        methods: {
            getuserinfo(e) {
                if (!this.code) {
                    uni.showToast({
                        icon: 'none',
                        title: '正在加載中,稍等一下',
                        duration: 2000
                    });
                    return
                }
                if (e.detail && e.detail.errMsg == 'getUserInfo:ok') {
                    e.detail.code = this.code//add code
                    login(e.detail, res => {
                        //授權(quán)成功之后的回調(diào)
                        uni.showToast({
                            title: '獲取用戶信息成功',
                            duration: 2000
                        });

                        setTimeout(() => {
                            uni.redirectTo({
                                url: '/pages/index/index'
                            });
                        }, 2000)
                    })
                } else {
                    //授權(quán)成功之后的回調(diào)
                    uni.showToast({
                        icon: 'none',
                        title: '已拒絕授權(quán)',
                        duration: 2000
                    });
                    //用戶拒絕授權(quán)
                    this.status = 0
                }
            },
            openSetting() {
                uni.showModal({
                    title: '提示',
                    content: '你已經(jīng)取消過授權(quán),需設(shè)置授權(quán)權(quán)限',
                    confirmText: '設(shè)置',
                    success: function(res) {
                        if (res.confirm) {
                            uni.openSetting({
                                success(res) {

                                }
                            })
                        } else if (res.cancel) {
                            console.log('用戶點擊取消');
                        }
                    }
                });
            }
        }
    }
</script>

<style lang="scss" scoped>
    
</style>

//驗證授權(quán)狀態(tài) 2未操作 1已經(jīng)授權(quán)  0拒絕授權(quán)
const getSetting = function() {
    const promise= new Promise((resolve,reject) => {
        uni.getSetting({
            success(res) {
                let authSetting=res.authSetting
                //授權(quán)成功
                if(authSetting['scope.userInfo']){
                    resolve(1)
                    return
                }
                //拒絕授權(quán)
                if(authSetting['scope.userInfo']===false){
                    resolve(0)
                    return
                }
                
                resolve(2)
            },
            fail(res) {
                reject(res)
            }
        })
    }).catch(res=>{
        uni.showToast({
            icon:'none',
          title: res.errMsg || '獲取授權(quán)狀態(tài)失敗',
          duration: 2000
        });
    })
    
    return promise
}
export {
    getSetting,//驗證授權(quán)狀態(tài)
}


//login.js
import {
    authDo
} from '@/api/mpLogin/home.js'

//獲取服務(wù)商信息
const getProvider = () => {
    const promise = new Promise((resolve, reject) => {
        uni.getProvider({
            service: 'oauth', //服務(wù)類型  登錄授權(quán)
            success: function(res) {
                resolve(res.provider)
            },
            fail(res) {
                reject(res)
            }
        });
    }).catch(res => {
        uni.showToast({
            icon: 'none',
            title: res.errMsg || '獲取服務(wù)商信息失敗',
            duration: 2000
        });
    })

    return promise
}


//獲取code
const getCode = provider => {
    if (!provider) {
        uni.showToast({
            icon: 'none',
            title: '獲取服務(wù)商信息失敗',
            duration: 2000
        });
        return
    }

    const promise = new Promise((resolve, reject) => {
        uni.login({
            provider: provider,
            success: function(loginRes) {
                if (loginRes && loginRes.code) {
                    resolve(loginRes.code)
                } else {
                    reject(loginRes)
                }
            }
        });
    }).catch(res => {
        uni.showToast({
            icon: 'none',
            title: res.errMsg || '獲取code失敗',
            duration: 2000
        });
    })

    return promise
}

//登錄授權(quán)
const login = async function(e,func) {
    //獲取服務(wù)商信息
    // let provider = await getProvider();

    //獲取code
    // let code = await getCode(provider[0])

    //小程序授權(quán)微信
    let param = {
        code:e.code,
        encryptedData: e.encryptedData,
        iv: e.iv
    }
    let res = await authDo(param);
    if (res.resultCode != '0000') {
        uni.showToast({
            icon: 'none',
            title: res.resultMessage,
            duration: 2000
        });
        return
    }

    let data = res.result || {}
    //儲存用戶信息到本地
    uni.setStorageSync('user', data);
    func&&func(data)
}

export {
    login,//登錄授權(quán)
    getCode,//獲取code
    getProvider//獲取服務(wù)商信息
}

檢測登錄態(tài)
app.vue onShow 里檢查登錄態(tài),這里分兩步,一步是判斷有用戶緩存 沒有前往登錄 如有緩存則判斷登錄態(tài),需要用uni.checkSession檢查登錄態(tài)是否過期,未過期不需要執(zhí)行其他操作,如過期,需要獲取code以及緩存里存的session 調(diào)用后臺接口刷新登錄態(tài),代碼如下:

//app.vue
<script>
    import {
        checkLoginStatus
    } from '@/api/request/login/checkLoginStatus.js' //檢查登錄狀態(tài)

    export default {
        onLaunch: function() {
            console.log('App Launch')
        },
        onShow: function() {
            console.log('App show')
            // #ifdef MP
            //檢查登錄狀態(tài)
            checkLoginStatus()
            // #endif
        },
        onHide: function() {
            console.log('App Hide')
        }
    }
</script>

<style>
    /*每個頁面公共css */
    @import "css/flex.css";
    @import "css/public.css";
</style>
//checkSession.js
//檢測session是否過期
//0未過期 1已過期

const check=()=>{
    const promise= new Promise((resolve,reject) => {
        uni.checkSession({
            success() {
                console.log('狀態(tài)未過期')
                //未過期
                resolve(0)
            },
            fail() {
                console.log('狀態(tài)已過期')
                //已過期
                resolve(1)
            }
        })
    }).catch(res=>{
        uni.showToast({
            icon:'none',
          title: res.errMsg || '驗證session失效',
          duration: 2000
        });
    })
    
    return promise
}

export default check
//checkLoginStatus.js
import check from "@/api/request/login/checkSession.js" //檢驗sessing是否過期 0未過期 1已過期
import {
    getCode,
    getProvider
} from "@/api/request/login/login.js"
import {
    wxLoginState
} from "@/api/mpLogin/home.js"

//用戶緩存信息
const user = (uni.getStorageSync('user'));

//跳轉(zhuǎn)至小程序登錄頁
const toLogin = () => {
//  uni.showToast({
//      icon: 'none',
//      title: '您的登錄已過期,跳轉(zhuǎn)至授權(quán)頁',
//      mask: true,
//      duration: 2000
//  });
    setTimeout(() => {
        uni.redirectTo({
            url: '/pages/login/index'
        });
    }, 2000)
}

//小程序登錄態(tài)刷新
const loadLoginState = async function() {
    let access3rdToken = user.access3rdToken //平臺sessionId
    let provider = await getProvider() //獲取服務(wù)商信息
    let code = await getCode(provider[0]) //獲取code

    let param = {
        access3rdToken,
        code
    }

    let res = await wxLoginState(param);
    if (res.resultCode != '0000') {
        uni.showToast({
            icon: 'none',
            title: res.resultMessage,
            duration: 2000
        });
        return
    }
    console.log('刷新成功')
}

//判斷登錄狀態(tài)
const checkLoginStatus = async function() {
    if (user) {
        // 檢查 session_key 是否過期
        let status = await check()
        //已過期
        if (status == 1) {
            //小程序登錄態(tài)刷新
            loadLoginState()
        } else {
            //未過期
            console.log('執(zhí)行下一步操作')
        }
    } else {
        // 無skey,作為首次登錄
        toLogin();
    }
}



export {
    checkLoginStatus, //判斷登錄狀態(tài)
    toLogin, //跳轉(zhuǎn)至小程序登錄頁
    loadLoginState //小程序登錄態(tài)刷新
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • error code(錯誤代碼)=2000是無效的像素格式。error code(錯誤代碼)=2001是指定的驅(qū)動...
    Heikki_閱讀 2,194評論 0 4
  • ORA-00001: 違反唯一約束條件 (.) 錯誤說明:當(dāng)在唯一索引所對應(yīng)的列上鍵入重復(fù)值時,會觸發(fā)此異常。 O...
    我想起個好名字閱讀 5,972評論 0 9
  • 2018年4月18日天氣晴 今天下午接孩的時候,孩子特別高興。我問開運動會這么高興呢?她說不是,媽媽告訴你一...
    冬末初春閱讀 259評論 0 0
  • 立個flag,我要畫一百張多肉~第一株熊童子,能認(rèn)出來算我輸。。。
    二月出家233閱讀 569評論 1 1
  • 在《修復(fù)關(guān)系》第一天,我注意到文君在礦泉水瓶蓋上細(xì)心的標(biāo)注了編號。小小的體貼,不僅僅溫暖到我,更讓我確信止定文化中...
    湘笨兒閱讀 292評論 1 1

友情鏈接更多精彩內(nèi)容