国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 課堂 > 小程序 > 正文

uni-app之APP和小程序微信授權方法

2020-03-21 15:57:06
字體:
來源:轉載
供稿:網友

uni-app 介紹

uni-app 是一個使用 Vue.js 開發跨平臺應用的前端框架。

適用平臺:Android、iOS、微信小程序。實現了一套代碼,同時發布到Android、iOS、微信小程序。

參考官方:https://uniapp.dcloud.io/

APP微信授權

檢測服務商

檢測手機上是否安裝微信、QQ、新浪微博等。

uni.getProvider({  service: 'oauth',  success: function (res) {    console.log(res.provider);  }});

uni-app,APP,小程序,微信授權

授權登錄

獲取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微信授權登錄</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));							}						});					}				});			}		}	});},

小程序微信授權

獲取用戶基本信息

為優化用戶體驗,使用 wx.getUserInfo 接口直接彈出授權框的開發方式將逐步不再支持。從2018年4月30日開始,小程序與小游戲的體驗版、開發版調用 wx.getUserInfo 接口,將無法彈出授權詢問框,默認調用失敗。正式版暫不受影響。開發者可使用以下方式獲取或展示用戶信息。

小程序使用 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">微信授權獲取用戶信息</button><!-- #endif -->wxGetUserInfo:function(res){	if (!res.detail.iv) {		uni.showToast({			title: "您取消了授權,登錄失敗",			icon: "none"		});		return false;	}	console.log('-------用戶授權,并獲取用戶基本信息和加密數據------')	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)等,后端調用接口如下:
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)				}			})		}	});}

微信登錄狀態監測

<!-- #ifdef MP-WEIXIN --><button class="" @click="checkLogin">微信登錄檢測</button><!-- #endif -->checkLogin: function() {	wx.checkSession({		success() {			console.log('ok');			// session_key 未過期,并且在本生命周期一直有效		},		fail() {			// session_key 已經失效,需要重新執行登錄流程			//wx.login() // 重新登錄			console.log('expire');		}	})},

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 葵青区| 闻喜县| 章丘市| 云安县| 错那县| 名山县| 宝鸡市| 康保县| 黄龙县| 洪江市| 旬阳县| 麻栗坡县| 河南省| 黄平县| 镶黄旗| 沅陵县| 台南县| 文化| 泸西县| 城固县| 韩城市| 梁山县| 当涂县| 新竹县| 利辛县| 区。| 清水河县| 高青县| 上栗县| 武宣县| 扶绥县| 沂源县| 海伦市| 平舆县| 田东县| 明溪县| 华池县| 大安市| 建水县| 香港| 思茅市|