uni-quick-login.vue 6.2 KB
Newer Older
L
linju 已提交
1
<template>
L
213  
linju 已提交
2
	<view class="quick-login-box">
芊里 已提交
3 4 5
		<view class="item" v-for="(item,index) in servicesList" :key="index" @click="item.path?to(item.path):login(item.id,false)">
			<image class="logo" :src="item.logo" mode="widthFix"></image>
			<text class="login-title">{{item.text}}</text>
L
linju 已提交
6 7 8
		</view>
	</view>
</template>
L
123  
linju 已提交
9
<script>
L
23  
linju 已提交
10 11
	import {mapGetters,mapMutations} from 'vuex';
	//前一个窗口的页面地址。控制点击切换快捷登陆方式是创建还是返回
DCloud_JSON's avatar
DCloud_JSON 已提交
12
	import loginSuccess from 'uni_modules/uni-login-page/common/loginSuccess.js';
L
123  
linju 已提交
13
	export default {
L
linju 已提交
14
		data() {
L
213  
linju 已提交
15
			return {
L
linju 已提交
16 17 18
				config: {
					"weixin": {
						"text": "微信登陆",
L
linju 已提交
19
						"logo": "../../static/login/weixin.png",
L
123  
linju 已提交
20
						"isChecked":true
L
linju 已提交
21
					},
L
123  
linju 已提交
22 23
					"apple": {
						"text": "苹果登陆",
L
linju 已提交
24
						"logo": "../../static/login/apple.png",
L
123  
linju 已提交
25 26 27 28
						"isChecked":true
					},
					"univerify": {
						"text": "一键登陆",
L
linju 已提交
29
						"logo": "../../static/login/univerify.png",
L
123  
linju 已提交
30 31
						"isChecked":true
					},
L
linju 已提交
32 33
					"qq": {
						"text": "QQ登陆",
L
linju 已提交
34
						"logo": "../../static/login/qq.png",
L
123  
linju 已提交
35
						"isChecked":false //暂未提供该登陆方式的接口示例
L
linju 已提交
36 37 38
					},
					"xiaomi": {
						"text": "小米登陆",
L
linju 已提交
39
						"logo": "../../static/login/qq.png",
L
123  
linju 已提交
40
						"isChecked":false //暂未提供该登陆方式的接口示例
L
linju 已提交
41
					},
L
23  
linju 已提交
42 43
					"sinaweibo": {
						"text": "微博登录",
L
linju 已提交
44
						"logo": "../../static/login/weibo.png",
L
23  
linju 已提交
45 46
						"isChecked":false //暂未提供该登陆方式的接口示例
					}
L
linju 已提交
47
				},
L
23  
linju 已提交
48 49 50
				servicesList:[
					{
						"text": "账号登陆",
L
linju 已提交
51
						"logo": "../../static/login/db.png",
L
23  
linju 已提交
52 53 54 55
						"path":"/uni_modules/uni-login-page/pages/pwd-login/pwd-login"
					},
					{
						"text": "短信登陆",
L
linju 已提交
56
						"logo": "../../static/login/smsCode.png",
L
23  
linju 已提交
57 58 59 60
						"path":"/uni_modules/uni-login-page/pages/index/index"
					}
				],
				univerifyStyle: { //一键登陆弹出窗的样式配置参数
L
213  
linju 已提交
61
					"fullScreen": true, // 是否全屏显示,true表示全屏模式,false表示非全屏模式,默认值为false。
L
linju 已提交
62 63 64
					"backgroundColor": "#ffffff", // 授权页面背景颜色,默认值:#ffffff  
				}
			}
L
23  
linju 已提交
65 66 67 68 69 70 71 72 73
		},
		created() {
			let servicesList = this.servicesList
			//去掉当前页面对应的登陆选项
			for (var i = 0; i < servicesList.length; i++) {
				if(servicesList[i].path == this.getRoute(1)){
					servicesList.splice(i,1)
				}
			}
L
linju 已提交
74
		},
L
123  
linju 已提交
75 76 77 78 79
		mounted() {
			//获取当前环境能用的快捷登陆方式
			// #ifdef APP-PLUS
				plus.oauth.getServices(oauthServices=>{
					this.oauthServices = oauthServices
L
23  
linju 已提交
80 81 82 83 84
					oauthServices.forEach(({id})=>{
						if(this.config[id].isChecked){
							this.servicesList.push({...this.config[id],id})
						}
					})
L
23  
linju 已提交
85
					// console.log(oauthServices);
L
123  
linju 已提交
86
				},err=>{
L
123  
linju 已提交
87
					uni.hideLoading()
L
123  
linju 已提交
88 89 90 91 92 93 94 95
					uni.showModal({
						title: '获取服务供应商失败:' +JSON.stringify(err),
						showCancel: false,
						confirmText: '知道了'
					});
					console.error('获取服务供应商失败:' + JSON.stringify(err));
				})
			// #endif
L
linju 已提交
96
		},
L
123  
linju 已提交
97 98 99
		methods: {
			...mapMutations({
				setUserInfo: 'user/login'
L
23  
linju 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
			}),
			getRoute(n=0){
				let pages = getCurrentPages();
				console.log('route-pages-length',pages.length);
				if(n>pages.length){ return '' }
				return '/'+pages[pages.length - n].route
			},
			to(path){
				console.log('比较',this.getRoute(2),path)
				if(this.getRoute(2)==path){ // 控制路由是重新打开还是返回,避免重复打开页面
					uni.navigateBack();
				}else{
					uni.navigateTo({url:path})
				}
			},
			login(type,navigateBack=true) {
				console.log(arguments);
				console.log('services',services);
L
123  
linju 已提交
118
				let oauthService = this.oauthServices.find((service) => service.id == type)
L
123  
linju 已提交
119
				console.log(type);
DCloud_JSON's avatar
DCloud_JSON 已提交
120 121
				
				// #ifdef APP-PLUS
L
123  
linju 已提交
122
				//请勿直接使用前端获取的unionid或openid直接用于登陆,前端的数据都是不可靠的
L
123  
linju 已提交
123
				if(type=='weixin'){
DCloud_JSON's avatar
DCloud_JSON 已提交
124
					return	oauthService.authorize(({code})=>{
L
123  
linju 已提交
125 126
						console.log(code);
						this.quickLogin({code},type)
L
21  
linju 已提交
127 128
					},
					err=>{
L
123  
linju 已提交
129
						uni.hideLoading()
L
21  
linju 已提交
130
						console.log(err);
L
123  
linju 已提交
131 132
					})
				}
DCloud_JSON's avatar
DCloud_JSON 已提交
133
				// #endif
L
123  
linju 已提交
134
				
L
linju 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147 148
				uni.login({
					"provider": type,
					"univerifyStyle":this.univerifyStyle,
					success:async e => {
						console.log(e);
						if(type=='apple'){
							let res = await this.getUserInfo({provider:"apple"})
							Object.assign(e.authResult,res.userInfo)
						}
						this.quickLogin(e.authResult,type)
					},
					fail: (err) => {
						uni.hideLoading()
						console.log(err);
L
23  
linju 已提交
149 150 151 152 153 154
						
						if(type=='univerify'){
							if(err.metadata.error_data){
								uni.showToast({
									title: "一键登陆:"+err.metadata.error_data,
									icon: 'none'
L
123  
linju 已提交
155
								});
L
23  
linju 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
							}
							switch (err.errCode){
								case 30002:
									console.log('在一键登陆界面,点击其他登陆方式');
									break;
								case 30003:
									console.log('关闭了登陆');
									if(navigateBack){ uni.navigateBack() }
									break;
								case 30006:
									uni.showModal({
										title: "登陆服务初始化错误",
										content:err.metadata.error_data,
										showCancel: false,
										confirmText: '知道了',
									});
									break;
								default:
									break;
							}
L
linju 已提交
176 177
						}
					}
178
				})
L
linju 已提交
179
			},
L
123  
linju 已提交
180 181 182 183 184 185 186
			quickLogin(params,type){//联网验证登陆
				console.log(params,type);
				this.request('user-center/login_by_'+type,params,(data,result)=>{
					console.log(result);
					if(result.code === 0){
						if(type=='univerify'){
							uni.closeAuthView()
L
linju 已提交
187
						}
L
21  
linju 已提交
188
						uni.hideLoading()
DCloud_JSON's avatar
DCloud_JSON 已提交
189 190 191
						loginSuccess(result)
						delete result.userInfo.token
						this.setUserInfo(result.userInfo)
L
linju 已提交
192
					}
L
linju 已提交
193
				},{showLoading:true})
L
linju 已提交
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
			},
			async getUserInfo(e){
				return new Promise((resolve, reject)=>{
					uni.getUserInfo({
						...e,
						success: (res) => {
							resolve(res);
						},
						fail: (err) => {
							uni.showModal({
								content: JSON.stringify(err),
								showCancel: false
							});
							reject(err);
						}
					})
				})
			}
		}
	}
</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;
L
linju 已提交
231
		height: 60rpx;
L
linju 已提交
232 233 234 235 236 237
	}

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