uni-app之APP和小程序微信授權(quán)方法
uni-app 介紹
uni-app 是一個使用 Vue.js 開發(fā)跨平臺應(yīng)用的前端框架。
適用平臺:Android、iOS、微信小程序。實現(xiàn)了一套代碼,同時發(fā)布到Android、iOS、微信小程序。
參考官方:https://uniapp.dcloud.io/
APP微信授權(quán)
檢測服務(wù)商
檢測手機上是否安裝微信、QQ、新浪微博等。
uni.getProvider({ service: 'oauth', success: function (res) { console.log(res.provider); } });
授權(quán)登錄
獲取openid,(unionid)等 uni.login({ provider: 'weixin', success: function (loginRes) { console.log(JSON.stringify(loginRes)); } });
獲取用戶信息
uni.getUserInfo({ provider: 'weixin', success: function(infoRes) { console.log('-------獲取微信用戶所有-----') console.log(JSON.stringify(infoRes.userInfo)); } });
示例代碼
<!-- #ifdef APP-PLUS --> <button class="" @click="appLogin">APP微信授權(quán)登錄</button> <!-- #endif --> appLogin: function() { uni.getProvider({ service: 'oauth', success: function(res) { console.log(res.provider); //支持微信、qq和微博等 if (~res.provider.indexOf('weixin')) { uni.login({ provider: 'weixin', success: function(loginRes) { console.log('-------獲取openid(unionid)-----'); console.log(JSON.stringify(loginRes)); // 獲取用戶信息 uni.getUserInfo({ provider: 'weixin', success: function(infoRes) { console.log('-------獲取微信用戶所有-----'); console.log(JSON.stringify(infoRes.userInfo)); } }); } }); } } }); },
小程序微信授權(quán)
獲取用戶基本信息
為優(yōu)化用戶體驗,使用 wx.getUserInfo 接口直接彈出授權(quán)框的開發(fā)方式將逐步不再支持。從2018年4月30日開始,小程序與小游戲的體驗版、開發(fā)版調(diào)用 wx.getUserInfo 接口,將無法彈出授權(quán)詢問框,默認(rèn)調(diào)用失敗。正式版暫不受影響。開發(fā)者可使用以下方式獲取或展示用戶信息。
小程序使用 button 組件,并將 open-type 指定為 getUserInfo 類型,獲取用戶基本信息。
參考官方:https://developers.weixin.qq.com/miniprogram/dev/component/button.html
示例代碼:
<!-- #ifdef MP-WEIXIN --> <button class="" open-type="getUserInfo" @getuserinfo="wxGetUserInfo" withCredentials="true">微信授權(quán)獲取用戶信息</button> <!-- #endif --> wxGetUserInfo:function(res){ if (!res.detail.iv) { uni.showToast({ title: "您取消了授權(quán),登錄失敗", icon: "none" }); return false; } console.log('-------用戶授權(quán),并獲取用戶基本信息和加密數(shù)據(jù)------') console.log(res.detail); },
微信登錄
參考官方:https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/login.html
(1)頁面uni.login獲取code
(2)后端通過code獲取sessionKey、openid(unionid)等,后端調(diào)用接口如下:
https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code
(3)示例代碼
<!-- #ifdef MP-WEIXIN --> <button class="" @click="wxLogin">微信登錄</button> <!-- #endif --> wxLogin: function() { uni.login({ provider: 'weixin', success: function(loginRes) { console.log('-------獲取code-------') console.log(loginRes.code); wx.request({ url: 'https://xxxxx'+loginRes.code, success: function(info) { console.log('-------獲取sessionKey、openid(unionid)-------') console.log(info); }, fail: function(e) { console.log(e) } }) } }); }
微信登錄狀態(tài)監(jiān)測
<!-- #ifdef MP-WEIXIN --> <button class="" @click="checkLogin">微信登錄檢測</button> <!-- #endif --> checkLogin: function() { wx.checkSession({ success() { console.log('ok'); // session_key 未過期,并且在本生命周期一直有效 }, fail() { // session_key 已經(jīng)失效,需要重新執(zhí)行登錄流程 //wx.login() // 重新登錄 console.log('expire'); } }) },
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
js冒泡法和數(shù)組轉(zhuǎn)換成字符串示例代碼
將數(shù)組轉(zhuǎn)換成字符串的方法有很多,想必大家也不會陌生,下面為大家講解下js冒泡法的使用,感興趣的朋友可以參考下2013-08-08javascript完整操作Table的增加行,刪除行的列子大全
非常漂亮的js操作table行代碼函數(shù)。比較方便2008-10-10ES6 迭代器(Iterator)和 for.of循環(huán)使用方法學(xué)習(xí)(總結(jié))
這篇文章主要介紹了ES6 迭代器(Iterator)和 for.of循環(huán)使用方法學(xué)習(xí)總結(jié),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02微信小程序?qū)崿F(xiàn)多選框全選與反全選及購物車中刪除選中的商品功能
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)多選框全選與反全選及購物車中刪除選中的商品功能,本文通過截圖實例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2019-12-12file模式訪問網(wǎng)頁時iframe高度自適應(yīng)解決方案
最近做到iframe的高度自適應(yīng)這個問題;發(fā)現(xiàn)自己做的網(wǎng)頁是通過file方式訪問的,將網(wǎng)頁代碼放到apache下通過http協(xié)議訪問,在iframe加載的時候調(diào)用如下js方法:果然網(wǎng)頁高度能夠自適應(yīng)(對于其他方案應(yīng)該也有效果,我沒有注意去嘗試)感興趣的朋友可以了解下2013-01-01