麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

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

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武林網。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 国产成人精品免费视频大全办公室 | 91看片在线播放 | 国产免费久久久久 | 欧美视屏一区二区 | 国产精品自拍av | 久久在线 | 一级免费看片 | 加勒比综合| 嗯哈~不行好大h双性 | 久久综合九色 | 国产一级毛片高清视频完整版 | 成人福利在线 | 99这里精品| 日韩精品久久久久久久九岛 | 香蕉秀| 一本色道久久久888 国产一国产精品一级毛片 国产精品高潮视频 | 狠狠操精品视频 | 色视频欧美| 美女亚洲综合 | 成人免费观看在线视频 | 日韩欧美色综合 | 中午字幕无线码一区2020 | 一级毛片在线免费观看 | 国产精品久久久久久久久久大牛 | 久久久日韩精品一区二区三区 | 黄色av片三级三级三级免费看 | 中国久久久 | 一区二区免费网站 | 女人一级一级毛片 | 日韩做爰视频免费 | 91精品国产乱码久久久久久久久 | 91久久精品一 | 成人福利软件 | 成人福利视频在 | 91精品国产综合久久婷婷香蕉 | 日韩精品网站在线观看 | 免费看一级视频 | 欧美精品激情视频 | h视频免费看| 国产1区2区3区中文字幕 | 久久免费视频3 |