uni-quick-login.vue 9.9 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
		computed: {
			loginConfig() {
				return getApp().globalData.config.router.login
21 22 23
			},
			agreements() {
				return getApp().globalData.config.about.agreements || []
DCloud_JSON's avatar
DCloud_JSON 已提交
24 25
			}
		},
26 27 28
		data() {
			return {
				servicesList: [{
DCloud_JSON's avatar
DCloud_JSON 已提交
29
						"id": "username",
DCloud_JSON's avatar
DCloud_JSON 已提交
30
						"text": "账号登录",
31 32 33 34
						"logo": "/static/uni-quick-login/user.png",
						"path": "/pages/ucenter/login-page/pwd-login/pwd-login"
					},
					{
DCloud_JSON's avatar
DCloud_JSON 已提交
35
						"id": "smsCode",
36 37 38 39
						"text": "短信验证码",
						"logo": "/static/uni-quick-login/sms.png",
						"path": "/pages/ucenter/login-page/index/index"
					}
DCloud_JSON's avatar
DCloud_JSON 已提交
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
				oauthServices: [],
				config: {
					"weixin": {
						"text": "微信登录",
						"logo": "/static/uni-quick-login/wechat.png",
					},
					"apple": {
						"text": "苹果登录",
						"logo": "/static/uni-quick-login/apple.png",
					},
					"univerify": {
						"text": "一键登录",
						"logo": "/static/uni-quick-login/univerify.png",
					},
					"qq": {
						"text": "QQ登录",//暂未提供该登录方式的接口示例
						"logo": "/static/uni-quick-login/univerify.png",
					},
					"xiaomi": {
						"text": "小米登录",//暂未提供该登录方式的接口示例
						"logo": "/static/uni-quick-login/univerify.png",
					},
					"sinaweibo": {
						"text": "微博登录",//暂未提供该登录方式的接口示例
						"logo": "/static/uni-quick-login/univerify.png",
					}
				},
				univerifyStyle:{ //一键登录弹出窗的样式配置参数
					"fullScreen": true, // 是否全屏显示,true表示全屏模式,false表示非全屏模式,默认值为false。
					"backgroundColor": "#ffffff", // 授权页面背景颜色,默认值:#ffffff
					"buttons": { // 自定义登陆按钮
						"iconWidth": "45px", // 图标宽度(高度等比例缩放) 默认值:45px
						"list": []
					},
					"privacyTerms": {
						"defaultCheckBoxState": false, // 条款勾选框初始状态 默认值: true   
						"textColor": "#BBBBBB", // 文字颜色 默认值:#BBBBBB  
						"termsColor": "#5496E3", //  协议文字颜色 默认值: #5496E3  
						"prefix": "我已阅读并同意", // 条款前的文案 默认值:“我已阅读并同意”  
						"suffix": "并使用本机号码登录", // 条款后的文案 默认值:“并使用本机号码登录”  
						"privacyItems": []
					}
				}
84
			}
85 86 87 88 89 90
		},
		watch: {
			agree(agree) {
				this.univerifyStyle.privacyTerms.defaultCheckBoxState = agree
			}
		},
91
		props: {
92 93 94
			agree: {
				type: Boolean,
				default () {
95 96
					return false
				}
97 98
			}
		},
99 100 101 102 103 104 105 106 107 108 109 110 111
		async created() {
			this.univerifyStyle.privacyTerms.privacyItems = this.agreements
			
			let servicesList = this.servicesList
			//获取当前环境能用的快捷登录方式
			// #ifdef MP-WEIXIN
			let id = 'weixin'
			if (this.loginConfig.includes(id)) {
				servicesList.push({
					...this.config[id],
					id
				})
			}
DCloud_JSON's avatar
DCloud_JSON 已提交
112
			// #endif
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
			// #ifdef APP-PLUS
			this.oauthServices = await new Promise((callBack)=>{
				plus.oauth.getServices(oauthServices => {
					callBack(oauthServices.filter(({nativeClient,id})=>nativeClient&&this.loginConfig.includes(id))) 
					//只返回1.应用支持 && 2.手机已安装对应客户端 && 3.uni-starter.config.js配置项中存在的快捷登录方式
				}, err => {
					callBack([])
					uni.hideLoading()
					uni.showModal({
						title: '获取服务供应商失败:' + JSON.stringify(err),
						showCancel: false,
						confirmText: '知道了'
					});
					console.error('获取服务供应商失败:' + JSON.stringify(err));
				})
			})
			// #endif
			//添加已配置且可用的第三方快捷登陆项
			servicesList = servicesList.concat(this.oauthServices.map( ({id})=>{
				return {...this.config[id],id}
			}))
			//设置一键登录功能底下的快捷登陆按钮
			servicesList.forEach(({id,logo})=>{
				if(id != 'univerify'){
					this.univerifyStyle.buttons.list.push({"iconPath":logo,"provider":id})
				}
			})
			//去掉当前页面对应的登录选项
			this.servicesList = servicesList.filter(item=>{
				return item.path != this.getRoute(1)
			})
			console.log('servicesList',servicesList,this.servicesList);
145 146 147 148 149 150 151
		},
		methods: {
			...mapMutations({
				setUserInfo: 'user/login'
			}),
			getRoute(n = 0) {
				let pages = getCurrentPages();
DCloud_JSON's avatar
DCloud_JSON 已提交
152
				// console.log('route-pages-length', pages.length);
153 154 155 156 157 158
				if (n > pages.length) {
					return ''
				}
				return '/' + pages[pages.length - n].route
			},
			to(path) {
159
				// console.log('比较', this.getRoute(2), path)
160 161 162 163 164 165 166 167
				if (this.getRoute(2) == path) { // 控制路由是重新打开还是返回,避免重复打开页面
					uni.navigateBack();
				} else {
					uni.navigateTo({
						url: path
					})
				}
			},
DCloud_JSON's avatar
DCloud_JSON 已提交
168
			login_before(type, navigateBack = true) {
169
				if (!this.agree&&type!='univerify') {
170 171 172 173 174
					return uni.showToast({
						title: '你未同意隐私政策协议',
						icon: 'none'
					});
				}
175 176 177
				uni.showLoading({
					mask: true
				})
178
				// console.log(arguments);
179
				let oauthService = this.oauthServices.find((service) => service.id == type)
180
				// console.log(type);
181 182

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

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

						if (type == 'univerify') {
233
							if (err.metadata && err.metadata.error_data) {
234
								uni.showToast({
DCloud_JSON's avatar
DCloud_JSON 已提交
235
									title: "一键登录:" + err.metadata.error_data,
236 237 238
									icon: 'none'
								});
							}
239 240 241 242 243 244
							if (err.errMsg) {
								uni.showToast({
									title: "一键登录:" + err.errMsg,
									icon: 'none'
								});
							}
245 246
							switch (err.errCode) {
								case 30002:
DCloud_JSON's avatar
DCloud_JSON 已提交
247
									console.log('在一键登录界面,点击其他登录方式');
248 249
									break;
								case 30003:
DCloud_JSON's avatar
DCloud_JSON 已提交
250
									console.log('关闭了登录');
251 252 253 254
									if (navigateBack) {
										uni.navigateBack()
									}
									break;
255 256 257 258 259 260 261
								case 30006:
									uni.showModal({
										title: "登录服务初始化错误",
										content: err.metadata.error_data,
										showCancel: false,
										confirmText: '知道了',
									});
262
									break;
263 264 265 266 267
								case "30008":
									uni.showToast({
										title: '点击了第三方登陆',
										icon: 'none'
									});
268 269 270 271 272 273 274
									console.log('点击了第三方登陆,provider:',err.provider);
									let {path} = this.servicesList.find(item=>item.id==err.provider)||{}
									console.log('path',path);
									if(path&&path!=this.getRoute(1)){ //存在路径,且并不是当前已经打开的路径
										this.to(path)
									}else{
										this.login_before(err.provider)
275 276 277 278
									}
									break;
								default:
									console.log(9527, err);
279 280 281 282 283 284
									break;
							}
						}
					}
				})
			},
DCloud_JSON's avatar
DCloud_JSON 已提交
285
			login(params, type) { //联网验证登录
286
				console.log('type--', type);
DCloud_JSON's avatar
DCloud_JSON 已提交
287 288 289
				console.log({
					params,
					type
290 291
				});
				let action = 'loginBy' + type.trim().toLowerCase().replace(type[0], type[0].toUpperCase())
292 293 294
				uniCloud.callFunction({
					name: 'uni-id-cf',
					data: {
295 296
						action,
						params
297 298 299 300 301 302 303 304 305 306 307 308 309
					},
					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)
310 311 312 313 314
						} else {
							uni.showModal({
								content: result.msg,
								showCancel: false
							});
315 316 317 318 319
						}
					},
					complete: () => {
						uni.hideLoading()
					}
320
				})
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
			},
			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 已提交
347 348 349
		justify-content: space-around;
		position: fixed;
		bottom: 10rpx;
DCloud_JSON's avatar
DCloud_JSON 已提交
350
		left: 0;
351 352 353 354 355 356 357 358 359 360 361 362 363 364
	}

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

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

DCloud_JSON's avatar
DCloud_JSON 已提交
365
	.login-title {
366 367 368
		margin-top: 4px;
		font-size: 26rpx;
	}
369
</style>