uni-quick-login.vue 8.3 KB
Newer Older
1 2 3
<template>
	<view class="quick-login-box">
		<view class="item" v-for="(item,index) in servicesList" :key="index"
4
			@click="item.path?to(item.path):login_before(item.id,false)">
5 6 7 8 9 10 11 12 13 14
			<image class="logo" :src="item.logo" mode="widthFix"></image>
			<text class="login-title">{{item.text}}</text>
		</view>
	</view>
</template>
<script>
	import {
		mapGetters,
		mapMutations
	} from 'vuex';
DCloud_JSON's avatar
DCloud_JSON 已提交
15
	//前一个窗口的页面地址。控制点击切换快捷登录方式是创建还是返回
16 17
	import loginSuccess from '@/pages/ucenter/login-page/common/loginSuccess.js';
	export default {
DCloud_JSON's avatar
DCloud_JSON 已提交
18 19 20 21 22
		computed: {
			loginConfig() {
				return getApp().globalData.config.router.login
			}
		},
23 24 25
		data() {
			return {
				servicesList: [{
DCloud_JSON's avatar
DCloud_JSON 已提交
26
						"id": "username",
DCloud_JSON's avatar
DCloud_JSON 已提交
27
						"text": "账号登录",
28 29 30 31
						"logo": "/static/uni-quick-login/user.png",
						"path": "/pages/ucenter/login-page/pwd-login/pwd-login"
					},
					{
DCloud_JSON's avatar
DCloud_JSON 已提交
32
						"id": "smsCode",
33 34 35 36
						"text": "短信验证码",
						"logo": "/static/uni-quick-login/sms.png",
						"path": "/pages/ucenter/login-page/index/index"
					}
DCloud_JSON's avatar
DCloud_JSON 已提交
37 38
				],
				oauthServices: []
39 40
			}
		},
41 42 43 44 45 46
		props: {
			agree:{
				type:Boolean,
				default(){
					return false
				}
47
			},
48 49 50 51 52
			config: {
				type: Object,
				default () {
					return {
						"weixin": {
DCloud_JSON's avatar
DCloud_JSON 已提交
53
							"text": "微信登录",
54 55 56 57
							"logo": "/static/uni-quick-login/wechat.png",
							"isChecked": true
						},
						"apple": {
DCloud_JSON's avatar
DCloud_JSON 已提交
58
							"text": "苹果登录",
59 60 61 62
							"logo": "/static/uni-quick-login/apple.png",
							"isChecked": true
						},
						"univerify": {
DCloud_JSON's avatar
DCloud_JSON 已提交
63
							"text": "一键登录",
64 65 66 67
							"logo": "/static/uni-quick-login/univerify.png",
							"isChecked": true
						},
						"qq": {
DCloud_JSON's avatar
DCloud_JSON 已提交
68
							"text": "QQ登录",
69
							"logo": "/static/uni-quick-login/univerify.png",
DCloud_JSON's avatar
DCloud_JSON 已提交
70
							"isChecked": false //暂未提供该登录方式的接口示例
71 72
						},
						"xiaomi": {
DCloud_JSON's avatar
DCloud_JSON 已提交
73
							"text": "小米登录",
74
							"logo": "/static/uni-quick-login/univerify.png",
DCloud_JSON's avatar
DCloud_JSON 已提交
75
							"isChecked": false //暂未提供该登录方式的接口示例
76 77 78 79
						},
						"sinaweibo": {
							"text": "微博登录",
							"logo": "/static/uni-quick-login/univerify.png",
DCloud_JSON's avatar
DCloud_JSON 已提交
80
							"isChecked": false //暂未提供该登录方式的接口示例
81 82 83 84 85 86 87
						}
					}
				}
			},
			univerifyStyle: {
				type: Object,
				default () {
DCloud_JSON's avatar
DCloud_JSON 已提交
88
					return { //一键登录弹出窗的样式配置参数
89 90 91 92 93 94 95
						"fullScreen": true, // 是否全屏显示,true表示全屏模式,false表示非全屏模式,默认值为false。
						"backgroundColor": "#ffffff", // 授权页面背景颜色,默认值:#ffffff  
					}
				}
			},
		},
		created() {
96 97
			// console.log('loginConfig', this.loginConfig);
			// console.log('this.getRoute(1)', this.getRoute(1));
98
			let servicesList = this.servicesList
DCloud_JSON's avatar
DCloud_JSON 已提交
99
			//去掉当前页面对应的登录选项
100 101 102 103 104
			for (var i = 0; i < servicesList.length; i++) {
				if (servicesList[i].path == this.getRoute(1)) {
					servicesList.splice(i, 1)
				}
			}
DCloud_JSON's avatar
DCloud_JSON 已提交
105 106 107
			//去掉配置项中不存在的项
			for (var i = 0; i < servicesList.length; i++) {
				if (!this.loginConfig.includes(servicesList[i].id)) {
108
					console.log('去掉配置项中不存在的项',servicesList[i].id);
DCloud_JSON's avatar
DCloud_JSON 已提交
109 110 111
					servicesList.splice(i, 1)
				}
			}
112
			// console.log('servicesList', servicesList);
113 114
		},
		mounted() {
DCloud_JSON's avatar
DCloud_JSON 已提交
115
			//获取当前环境能用的快捷登录方式
116 117 118 119 120 121
			// #ifdef APP-PLUS
			plus.oauth.getServices(oauthServices => {
				this.oauthServices = oauthServices
				oauthServices.forEach(({
					id
				}) => {
122
					// console.log(9527,id);
123
					if (this.config[id].isChecked&&this.loginConfig.includes(id)) {
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
						this.servicesList.push({
							...this.config[id],
							id
						})
					}
				})
				// console.log(oauthServices);
			}, err => {
				uni.hideLoading()
				uni.showModal({
					title: '获取服务供应商失败:' + JSON.stringify(err),
					showCancel: false,
					confirmText: '知道了'
				});
				console.error('获取服务供应商失败:' + JSON.stringify(err));
			})
			// #endif
141
			// #ifdef MP-WEIXIN
DCloud_JSON's avatar
DCloud_JSON 已提交
142 143 144 145 146 147 148 149
			let id = 'weixin'
			if (this.loginConfig.includes(id)) {
				this.servicesList.push({
					...this.config[id],
					id
				})
			}
			// #endif
150 151 152 153 154 155 156
		},
		methods: {
			...mapMutations({
				setUserInfo: 'user/login'
			}),
			getRoute(n = 0) {
				let pages = getCurrentPages();
DCloud_JSON's avatar
DCloud_JSON 已提交
157
				// console.log('route-pages-length', pages.length);
158 159 160 161 162 163
				if (n > pages.length) {
					return ''
				}
				return '/' + pages[pages.length - n].route
			},
			to(path) {
164
				// console.log('比较', this.getRoute(2), path)
165 166 167 168 169 170 171 172
				if (this.getRoute(2) == path) { // 控制路由是重新打开还是返回,避免重复打开页面
					uni.navigateBack();
				} else {
					uni.navigateTo({
						url: path
					})
				}
			},
DCloud_JSON's avatar
DCloud_JSON 已提交
173
			login_before(type, navigateBack = true) {
174 175 176 177 178 179
				if(!this.agree){
					return uni.showToast({
						title: '你未同意隐私政策协议',
						icon: 'none'
					});
				}
180
				uni.showLoading({mask:true})
181
				// console.log(arguments);
182
				let oauthService = this.oauthServices.find((service) => service.id == type)
183
				// console.log(type);
184 185

				// #ifdef APP-PLUS
DCloud_JSON's avatar
DCloud_JSON 已提交
186
				//请勿直接使用前端获取的unionid或openid直接用于登录,前端的数据都是不可靠的
187 188 189 190 191
				if (type == 'weixin') {
					return oauthService.authorize(({
							code
						}) => {
							console.log(code);
192
							this.login({
193 194 195 196 197 198 199 200 201 202 203 204 205
								code
							}, type)
						},
						err => {
							uni.hideLoading()
							console.log(err);
						})
				}
				// #endif

				uni.login({
					"provider": type,
					"univerifyStyle": this.univerifyStyle,
DCloud_JSON's avatar
DCloud_JSON 已提交
206 207 208
					complete: (e) => {
						console.log(9527, e);
					},
209 210 211 212 213
					success: async e => {
						console.log(e);
						if (type == 'apple') {
							let res = await this.getUserInfo({
								provider: "apple"
DCloud_JSON's avatar
DCloud_JSON 已提交
214
							})
215
							uni.hideLoading()
216 217
							Object.assign(e.authResult, res.userInfo)
						}
DCloud_JSON's avatar
DCloud_JSON 已提交
218
						// #ifdef MP-WEIXIN
DCloud_JSON's avatar
DCloud_JSON 已提交
219
						if (type == 'weixin') {
DCloud_JSON's avatar
DCloud_JSON 已提交
220 221 222 223 224
							return this.login({
								code: e.code
							}, type)
						}
						// #endif
225
						this.login(e.authResult, type)
226 227 228 229 230 231 232 233
					},
					fail: (err) => {
						uni.hideLoading()
						console.log(err);

						if (type == 'univerify') {
							if (err.metadata.error_data) {
								uni.showToast({
DCloud_JSON's avatar
DCloud_JSON 已提交
234
									title: "一键登录:" + err.metadata.error_data,
235 236 237 238 239
									icon: 'none'
								});
							}
							switch (err.errCode) {
								case 30002:
DCloud_JSON's avatar
DCloud_JSON 已提交
240
									console.log('在一键登录界面,点击其他登录方式');
241 242
									break;
								case 30003:
DCloud_JSON's avatar
DCloud_JSON 已提交
243
									console.log('关闭了登录');
244 245 246 247 248 249
									if (navigateBack) {
										uni.navigateBack()
									}
									break;
								case 30006:
									uni.showModal({
DCloud_JSON's avatar
DCloud_JSON 已提交
250
										title: "登录服务初始化错误",
251 252 253 254 255 256 257 258 259 260 261 262
										content: err.metadata.error_data,
										showCancel: false,
										confirmText: '知道了',
									});
									break;
								default:
									break;
							}
						}
					}
				})
			},
DCloud_JSON's avatar
DCloud_JSON 已提交
263
			login(params, type) { //联网验证登录
DCloud_JSON's avatar
DCloud_JSON 已提交
264 265 266 267
				console.log({
					params,
					type
				});
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
				uniCloud.callFunction({
					name:'uni-id-cf',
					data:{
						action:'login_by_'+type,
						params
					},
					success: ({result}) => {
						console.log(result);
						if (result.code === 0) {
							if (type == 'univerify') {
								uni.closeAuthView()
							}
							uni.hideLoading()
							loginSuccess(result)
							delete result.userInfo.token
							this.setUserInfo(result.userInfo)
						}
					},
					complete: () => {
						uni.hideLoading()
					}
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
				})
			},
			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;
DCloud_JSON's avatar
DCloud_JSON 已提交
316 317 318
		justify-content: space-around;
		position: fixed;
		bottom: 10rpx;
DCloud_JSON's avatar
DCloud_JSON 已提交
319
		left: 0;
320 321 322 323 324 325 326 327 328 329 330 331 332 333
	}

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

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

DCloud_JSON's avatar
DCloud_JSON 已提交
334
	.login-title {
335 336 337
		margin-top: 4px;
		font-size: 26rpx;
	}
338
</style>