index--.nvue 2.8 KB
Newer Older
L
123  
linju 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
<template>
	<view class="content">
		<view class="quick-login-box">
			<view class="item" v-for="({text,logo,name},index) in providerList" :key="index" @click="login(name)">
				<image class="logo" :src="logo" mode="widthFix"></image>
				<text class="login-title">{{text}}</text>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				config: {
					"weixin": {
						"text": "微信登陆",
						"logo": "../../static/login/img/weixin.png"
					},
					"qq": {
						"text": "QQ登陆",
						"logo": "../../static/login/img/qq.png"
					},
					"apple": {
						"text": "苹果登陆",
						"logo": "../../static/login/img/apple.png"
					},
					"sinaweibo": {
						"text": "微博登录",
						"logo": "../../static/login/img/sinaweibo.png"
					},
					"univerify": {
						"text": "一键登陆",
						"logo": "../../static/login/img/univerify.png"
					}
				},
				providerList: [],
				univerifyStyle: {
					"fullScreen": true, // 是否全屏显示,true表示全屏模式,false表示非全屏模式,默认值为false。
					"backgroundColor": "#ffffff", // 授权页面背景颜色,默认值:#ffffff  
				}
			}
		},
		onLoad() {
			uni.getProvider({
				"service": "oauth",
				success: res => {
					this.providerList = res.provider.map((name) => {
						return {...this.config[name],name}
					})
				},
				fail: (err) => {
					console.error('获取服务供应商失败:' + JSON.stringify(err));
				}
			})
		},
		methods: {
			login(type) {
				uni.login({
					"provider": type,
					"univerifyStyle":this.univerifyStyle,
					success: e => {
						console.log(e);
						this.quickLogin(e.authResult,type)
					},
					fail: (err) => {
						console.log(err);
						if(err.errCode===30002){
							
						}
					}
				});
			},
			quickLogin(authResult,type){
				//请勿直接使用authResult中的unionid或openid直接用于登陆,前端的数据都是不可靠的
				console.log({...authResult,type});
				uniCloud.callFunction({//联网验证登陆
					"name": "user",
					"data": {
						"action": "quickLogin",
						"params": {...authResult,type}
					},
					success: (e) => {
						console.log(e.result);
						uni.showModal({
							content: JSON.stringify(e.result),
							showCancel: false
						});
						if(type=='univerify'){
							uni.closeAuthView()
						}
					},
					fail: (err) => {
						console.log(err);
						if(err.errCode===30002){
							
						}
					}
				})
			}
		}
	}
</script>

<style scoped>
	.quick-login-box {
		flex-direction: row;
		width: 750rpx;
		justify-content: space-around;
	}

	.item {
		flex-direction: column;
		justify-content: center;
		align-items: center;
		height: 200rpx;
	}

	.logo {
		width: 60rpx;
		height: 60rpx;
	}

	.login-title {
		font-size: 26rpx;
	}
</style>