<template>
	<view class="">
		<view class="backView">
			<view class="noticeView">
				<image class="noticeImage" :src="icon_auth_notice_new" mode=""></image>
			</view>
			<view class="noticeTitleView">
				<text class="noticeTitle">{{ noticeTitle }}</text>
			</view>
			<button class="loginBtn" v-if="needAuth" lang="zh_CN" @tap="getUserProfile">
				<text class="u-font-32 white">立即登录</text>
			</button>
			<button class="loginBtn" v-else open-type="getPhoneNumber" throttle-time="1000"
					@getphonenumber="signupLogin">
				<text class="u-font-32 white">授权登录</text>
			</button>
			<view class="cancelView">
				<text class="cancelText" @click="cancelClick()">取消</text>
			</view>
		</view>
	</view>
</template>

<script>
import util from '@/utils/util.js'
import login from '@/utils/login.js'
const app = getApp()
export default {
	data() {
		return {
			isNewUser: false,
			needAuth: true,
			unionID: '',
			openID: '',
			icon_notice: '',
			TimeID: -1,
			icon_auth_notice_new: '',
			noticeTitle: '申请获得您的公开信息（昵称，头像等）以便于为您提供更好的服务',
		}
	},
	onLoad() {
		// getApp().trackPage('授权登录页')
		// this.icon_notice = getApp().globalData.icon_auth_notice
		// this.icon_auth_notice_new = getApp().globalData.icon_auth_notice_new
	},
	onShow() {
		app.globalData.jumpLoginStatus = true
		// 微信授权过用户信息，则无需重复授权
		if (app.globalData.authUserInfo) {
			this.needAuth = false
		}
	},
	methods: {
		// 授权用户信息
		getUserProfile() {
			var that = this
			uni.getUserProfile({
				desc: '用于授权登录',
				success: function (res) {
					const userInfo = res.userInfo;
					app.globalData.authUserInfo = userInfo
					app.globalData.authUserInfo.nickName = app.globalData.authUserInfo.nickName || ''
					app.globalData.authUserInfo.avatarUrl = app.globalData.authUserInfo.avatarUrl || ''
					uni.showLoading({
						title: '授权中..',
						mask: true
					});
					that.authLogin()
				},
				fail: function (res) {
					uni.showModal({
						title: '授权失败',
						content: '请允许获取用户信息的授权',
						showCancel: false
					});
				}
			})
		},
		authLogin() {
			var that = this
			let params = {
				nickname: app.globalData.authUserInfo.nickName,
				avatar: app.globalData.authUserInfo.avatarUrl,
				wxAppNo: 1,
			}
			login.appLogin().then(res => {
				params.code = res.code
				return login.authorization(params);
			}).then(res => {
				uni.hideLoading();
				if (res && util.getToken()) {
					that.goBackPage()
				} else {
					this.setLoginStatus()
				}
			}).catch(res => {
				uni.hideLoading()
				this.setLoginStatus()
			})
		},

		signupLogin(e) {
			var that = this
			if (!e || !e.detail || e.detail.errMsg.indexOf('user deny') > -1) {
				wx.showModal({
					title: '提示',
					showCancel: false,
					content: '用户未授权不能完成注册'
				});
			} else {
				let params = {
					nickname: app.globalData.authUserInfo.nickName,
					avatar: app.globalData.authUserInfo.avatarUrl,
					encryptedData: e.detail.encryptedData,
					iv: e.detail.iv,
					wxAppNo: 1,
				}
				login.appLogin().then(res => {
					params.code = res.code
					return login.signup(params);
				}).then(res => {
					uni.hideLoading()
					if (res && util.getToken()) {
						that.goBackPage()
					} else {
						uni.showToast({
							title: '授权失败，请重新授权',
							icon: 'none'
						});
						this.setLoginStatus(false)
					}
				}).catch(res => {
					uni.hideLoading()
					uni.showToast({
						title: res.message,
						icon: 'none'
					});
					this.setLoginStatus(false)
				})
			}
		},

		goBackPage() {
			let pages = getCurrentPages();
			if(pages.length) {
				let prevPage = pages[pages.length - 1]; //上一个页面
				if (prevPage && prevPage.$vm) {
					prevPage.$vm.isRefresh = true;
				}
				uni.navigateBack({
					delta: 1
				})
			} else {
				uni.navigateTo({
					url: "pages/mine/index"
				})
			}
			
		},

		setLoginStatus(authed = true) {
			if (authed) {
				this.needAuth = false
				uni.setNavigationBarTitle({
					title: '登录页'
				});
			} else {
				if(!app.globalData.authUserInfo) {
					this.needAuth = true
					uni.setNavigationBarTitle({
						title: '授权页'
					});
				}
			}
		},

		//取消点击
		cancelClick: function () {
			uni.navigateBack({})
		},
	}
}
</script>

<style>
.backView {
	background-color: #FFFFFF;
	position: absolute;
	top: 0;
	left: 0;
	width: 100%;
	height: 100%;
	display: flex;
	flex-direction: column;
}

.noticeView {
	background-color: #FFFFFF;
	width: 100%;
	height: 470rpx;
	display: flex;
	flex-direction: column;
	align-items: center;
}

.noticeImage {
	background-color: #FFFFFF;
	width: 386rpx;
	height: 270rpx;
	margin-top: 180rpx;
}

.noticeTitleView {
	margin-top: 80rpx;
	width: 100%;
	display: flex;
	align-items: center;
	justify-content: center;
}

.noticeTitle {
	width: 60%;
	font-size: 24rpx;
	color: #999999;
	text-align: center;
	line-height: 40rpx;
}

.loginBtn {
	background-color: #2272FF;
	width: 390rpx;
	height: 80rpx;
	border-radius: 40rpx;
	margin-top: 334rpx;
	display: flex;
	align-items: center;
	justify-content: center;
}

.cancelView {
	width: 100%;
	margin-top: 30rpx;
	display: flex;
	align-items: center;
	justify-content: center;
}

.cancelText {
	color: #666666;
	font-size: 32rpx;
}

.white {
	color: #FFFFFF;
}
</style>
