<template>
		<view class="login-container">
			<!-- 提示登录的图标 -->
			<view class="icon-css">
			<uni-row >
			<uni-icons  type="contact-filled" size="100" color="#AFAFAF"></uni-icons>
			</uni-row>
			</view>
			<!-- 登录按钮 -->
			<uni-row>
			<button type="primary" class="btn-login" @click="getToken" :disabled="flag">一键登录</button>		
			</uni-row>
			<!-- 登录提示 -->
			<uni-row>
			<view class="tips-text">登录后尽享更多权益</view>
			</uni-row>
		</view>
</template>

<script>
	// 按需导入 mapMutations 辅助函数
	import { mapMutations, mapState } from 'vuex'
	import websocketUtil from '@/mixins/websockUtil.js'
	
	export default {
		mixins: [websocketUtil],
		computed: {
			...mapState('m_user', ['recoUID']),
		},
		data() {
			return {
				queryObj: {
					latitude: 0,
					longitude: 0,
				},
				flag: false,
			};
		},
		methods: {
			// 调用 mapMutations 辅助方法，把 m_user 模块中的 updateUserInfo 映射到当前组件中使用
			...mapMutations('m_user', ['updateUserInfo', 'updateToken', 'updateLocation']),
			
			// 引导用户开启位置权限
			openSetting() {
				var that = this
				wx.showModal({
					title: '位置信息',
					content: '开启后将为你推送最近的资源',
					showCancel: true,
					cancelText: '取消',
					cancelColor: '#000000',
					confirmText: '开启',
					confirmColor: '#3CC51F',
					success: res => {
						if (res.confirm) {
							wx.openSetting({
								success(res) {
									if (res.authSetting['scope.userFuzzyLocation']) {
										// 获取经纬度
										wx.getFuzzyLocation({
											type: 'wgs84',
											success (res) {
											   that.queryObj.latitude = res.latitude
											   that.queryObj.longitude = res.longitude
											   that.updateLocation(that.queryObj)
											   that.getLogin()
											}
										})
									}
								},
								fail(res) {
									uni.$showMsg(res)
								},
								complete() {
									that.flag = false
								}
							})
						} else if(res.cancel) {
							that.flag = false
							uni.$showMsg("需要位置权限")
						}
					}
				})
			},
			
			// 调用登录接口，换取永久的 token
			async getToken() {
				var that = this
				this.flag = true
			
				// 获取位置信息
				wx.getSetting({
					success(res) {
						if (!res.authSetting['scope.userFuzzyLocation']) {
							wx.authorize({
								scope: 'scope.userFuzzyLocation',
								success() {
									// 获取经纬度
									wx.getFuzzyLocation({
										type: 'wgs84',
										success (res) {
										   that.queryObj.latitude = res.latitude
										   that.queryObj.longitude = res.longitude
										   that.updateLocation(that.queryObj)
										   that.getLogin()
										}
									})
								},
								fail() {
									// 拒绝后引导用户开启
									if (wx.openSetting) {
										that.openSetting()
									}
									that.flag = false
									return
								}
							})
						} else {
							// 获取经纬度
							wx.getFuzzyLocation({
								type: 'wgs84',
								success (res) {
								   that.queryObj.latitude = res.latitude
								   that.queryObj.longitude = res.longitude
								   that.updateLocation(that.queryObj)
								},
								complete() {
									that.getLogin()
								}
							})
						}
					}
				})
			},
			
			// 真正的登录逻辑
			async getLogin() {
				
				// 调用微信登录接口
				const [err, res] = await uni.login().catch(err => err)
				// 判断是否 wx.login() 调用失败
				if (err || res.errMsg !== 'login:ok') return uni.$showError('登录失败！')
				// 准备参数对象
				const query = {
					code: res.code,
					recoUID: this.recoUID
				}
				
				// 换取 token
				const { data: loginResult } = await uni.$http.get('/ucenter/mini-program-openid-uid/wxlogin', query)
				if (loginResult.code === 20000) {
					uni.$showMsg('登录成功！')
				}
				
				// 更新vuex中的token
				this.updateToken(loginResult.data.token)
				
				// // 将用户的基本信息存储到 vuex 中
				this.userInfo.alias = loginResult.data.alias
				this.userInfo.login = loginResult.data.login
				this.userInfo.city = loginResult.data.city
				this.userInfo.uid = loginResult.data.uid
				this.userInfo.count = loginResult.data.count
				this.userInfo.avatar = loginResult.data.avatar
				this.userInfo.isAdmin = loginResult.data.isAdmin
				this.updateUserInfo(this.userInfo)
				this.createWebSocket(this.userInfo.uid)
								
				uni.switchTab({
					url:'/pages/my/my'
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
.login-container {

	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;

}
	
	.icon-css{
		margin-top: 200rpx;
	}
	// 登录按钮的样式
	.btn-login {
		width: 500rpx;
		margin-top: 60rpx;
		border-radius: 50rpx;
		background-color: #35b882;
		font-size: 35rpx;
		//font-weight: bold;
		padding: 10rpx;
	}
	
	// 按钮下方提示消息的样式
	.tips-text {
		margin-top: 10rpx;
		font-size: 12px;
		color: gray;
	}

</style>