uniapp-實現在App端授權qq,微信,微博登錄

App端登陸相關的SDK需要在manifest中配置:

打開 manifest.json -> App模塊權限配置,勾選 OAuth(登陸鑑權)。
打開 manifest.json -> App SDK配置,查看到登陸鑑權。在說明中有藍色鏈接,其中包括向微信、QQ、微博等平臺申請sdk的鏈接。
向微信、QQ、微博等平臺申請到sdk的信息後,回填到manifest裏。
這些配置需要打包生效,真機運行仍然是HBuilder基座的設置,可使用自定義基座包。離線打包請參考離線打包文檔在原生工程中配置。
配置並打包後,通過uni.getProvider可以得到配置的結果列表,注意這裏返回的是manifest配置的,與手機端是否安裝微信、QQ、微博無關。
如果手機端未安裝QQ、微博,調用時會啓動這些平臺的wap頁面登陸,如果已安裝相應客戶端,會啓動它們的客戶端登陸。

APP微信授權登錄

uni.login({
	provider: 'weixin',
	success: res => {
		uni.getUserInfo({
			provider: 'weixin',
			success: function(infoRes) {
				let formdata = {
					nickName: infoRes.userInfo.nickName,
					gender: infoRes.userInfo.gender,
                    headImgUrl: infoRes.userInfo.avatarUrl,
					openId: infoRes.userInfo.openId,
					unionId: infoRes.userInfo.unionId
				};
				uni.request({
					url: 'http://192.168.43.205:8080/thirdPartLogin/app/login',
					method: 'POST',
					data: formdata,
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					success: res => {
						if (res.data.code != 200) {
							uni.showToast({
								title: res.data.err,
								duration: 3000,
								icon: 'none'
							});
							return false;
						} else {
							//登錄成功處理
							uni.showToast({
								title: res.data.message,
								duration: 3000,
								icon: 'none'
							});
							let ticket = res.data.ticket;
							uni.setStorageSync('ticket', ticket);
							uni.reLaunch({
								url: '../my/my'
							});
							return true;
						}
					}
				});
			}
		});
	},
	fail: err => {
		uni.showToast({
			title: '請求出錯啦!',
			icon: 'none',
			duration: 3000
		});
	}
});

後端直接根據unionId來判斷用戶的唯一性。

APPQQ授權登錄

uni.login({
	provider: 'qq',
	success: resp => {
		var access_token = resp.authResult.access_token;
		uni.getUserInfo({
			provider: 'qq',
			success: function(infoRes) {
				var formdata = {
					nickName: infoRes.userInfo.nickname,
					gender: infoRes.userInfo.gender == '男' ? 1 : 2,
                    headImgUrl: infoRes.userInfo.figureurl_qq_2,
					openId: infoRes.userInfo.openId,
					access_token: access_token
				};
				uni.request({
					url: 'http://192.168.43.205:8080/thirdPartLogin/app/login',
					method: 'POST',
					data: formdata,
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					success: res => {
						if (res.data.code != 200) {
							uni.showToast({
								title: res.data.err,
								duration: 3000,
								icon: 'none'
							});
							return false;
						} else {
							//登錄成功處理
							uni.showToast({
								title: res.data.message,
								duration: 3000,
								icon: 'none'
							});
							let ticket = res.data.ticket;
							uni.setStorageSync('ticket', ticket);
							uni.reLaunch({
								url: '../my/my'
							});
							return true;
						}
					}
				});
			}
		});
	},
	fail: err => {
		uni.showToast({
			title: '請求出錯啦!',
			icon: 'none',
			duration: 3000
		});
	}
});

後端直接根據access_token來判斷用戶的唯一性。

以上展示的都是最重要的代碼,複製就可以使用,自己可以加一些加載顯示等。

完整版uni-app實現在App端授權qq,微信,微博登錄代碼如下所示:

<template>
	<view class="flex align-center px-5 py-3">
		<view class="flex-1 flex align-center justify-center" v-for="(item, index) in providerList" :key="index">
			<view
				:class="item.icon + ' ' + item.bgColor"
				class="iconfont font-lg text-white flex align-center justify-center rounded-circle"
				style="width: 100rpx;height: 100rpx;"
				@click="thirdPartyLogin(index)"
			></view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			providerList: []
		};
	},
	mounted() {
		uni.getProvider({
			service: 'oauth',
			success: result => {
				this.providerList = result.provider.map(value => {
					let providerName = '';
					let icon = '';
					let bgColor = '';
					switch (value) {
						case 'weixin':
							providerName = '微信登錄';
							icon = 'icon-weixin';
							bgColor = 'bg-success';
							break;
						case 'qq':
							providerName = 'QQ登錄';
							icon = 'icon-QQ';
							bgColor = 'bg-primary';
							break;
						case 'sinaweibo':
							providerName = '新浪微博登錄';
							icon = 'icon-xinlangweibo';
							bgColor = 'bg-warning';
							break;
					}
					return {
						name: providerName,
						id: value,
						icon: icon,
						bgColor: bgColor
					};
				});
			},
			fail: error => {
				console.log('獲取登錄通道失敗!', error);
				return false;
			}
		});
	},
	methods: {
		//第三方登錄
		thirdPartyLogin(index) {
			if (index === '' || index === undefined || index === 'undefined') {
				uni.showToast({
					title: '參數錯誤,請聯繫管理員!',
					icon: 'none',
					duration: 3000
				});
				return false;
			} else {
				let loginType = '';
				// 1 判斷用戶的登錄類型
				if (index == 0) {
					loginType = 'weixin';
				} else if (index == 1) {
					loginType = 'qq';
				} else if (index == 2) {
					loginType = 'sinaweibo';
				}
				// 2 授權登錄,彈出授權窗口
				uni.login({
					provider: loginType,
					success: res => {
						var access_token = '';
						access_token = res.authResult.access_token;
						// 3 授權登錄成功以後,獲取用戶的信息
						uni.getUserInfo({
							provider: loginType,
							success: function(infoRes) {
								let formdata = {};
								if (index == 0) {
									formdata = {
										nickName: infoRes.userInfo.nickName,
										gender: infoRes.userInfo.gender,
										headImgUrl: infoRes.userInfo.avatarUrl,
										openId: infoRes.userInfo.openId,
										unionId: infoRes.userInfo.unionId,
										access_token: access_token,
										appLoginType: 'WEIXIN'
									};
								} else if (index == 1) {
									formdata = {
										nickName: infoRes.userInfo.nickname,
										gender: infoRes.userInfo.gender == '男' ? 1 : 2,
										headImgUrl: infoRes.userInfo.figureurl_qq_2,
										openId: infoRes.userInfo.openId,
										unionId: '',
										access_token: access_token,
										appLoginType: 'QQ'
									};
								} else if (index == 2) {
									formdata = {
										nickName: infoRes.userInfo.nickname,
										gender: infoRes.userInfo.gender == 'm' ? 1 : 2,
										headImgUrl: infoRes.userInfo.avatar_large,
										openId: infoRes.userInfo.id,
										unionId: '',
										access_token: access_token,
										appLoginType: 'SINAWEIBO'
									};
								}
								// 4 調用開發者後臺,執行一鍵註冊或登錄
								uni.request({
									url: 'http://192.168.43.205:8080/thirdPartLogin/app/login',
									method: 'POST',
									data: formdata,
									header: {
										'content-type': 'application/x-www-form-urlencoded'
									},
									success: res => {
										if (res.data.code != 200) {
											uni.showToast({
												title: res.data.err,
												duration: 3000,
												icon: 'none'
											});
											return false;
										} else {
											//登錄成功處理
											uni.showToast({
												title: res.data.message,
												duration: 3000,
												icon: 'none'
											});
											let ticket = res.data.ticket;
											// 5 保存用戶信息到全局的緩存中
											uni.setStorageSync('ticket', ticket);
											// 6 切換頁面跳轉,使用tab切換的api
											uni.switchTab({
												url: '../my/my'
											});
											return true;
										}
									}
								});
							}
						});
					},
					fail: err => {
						uni.showToast({
							title: '請求出錯啦!',
							icon: 'none',
							duration: 3000
						});
					}
				});
			}
		}
	}
};
</script>

<style></style>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章