<template>
	<view>
		<scroll-view scroll-y class="page">
			<cu-custom bgColor="bg-cyan"><block slot="content">登录检测</block></cu-custom>
			<view class="photoView">
				<view class="photoImage">
					<open-data type="userAvatarUrl"></open-data>
				</view>
			</view>
			<view style="padding-top:20%;">
				<view class='text-center margin-bottom text-lg  text-grey' style="padding-top:20px;">正在检测登录用户人员身份，请稍等...</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				hospital: '',
				sortHospital: '',
				department: '',
				name: '',
				openid: '',
				session_key: '',
				code: ''
			}
		},
		onShow(){
			// 获取用户登录信息
			let that = this;
			uni.login({
				success(res) {
					if(res.code){
						let new_that = that;
						uni.request({
							url: 'https://lekapi.opmonitor.com/?c=app_api&a=getToolOpenid',
							data: {code: res.code},
							header: {
								'Content-type': 'application/json'
							},
							success: function(res) {
								new_that.openid = res.data.data.openid
								new_that.session_key = res.data.data.session_key
								// 判断是否可以登录
								let new_new_that = new_that;
								uni.request({
									url: 'https://lekapi.opmonitor.com/?c=app_api&a=getIsHaveToolOpenid',
									data: {openid: new_new_that.openid},
									header: {
										'Content-type': 'application/json'
									},
									success: function(res) {
										if(res.data.data === 'false'){
											uni.navigateTo({
												url: '/pages/plugin/register'
											})
										}else{
											new_new_that.getIsHaveHandOpenid();
										}
									},
									error: () => {
										console.info('小程序域名不正确，请检查域名正确性')
									}
								})
							},
							fail: () => {
								console.info('小程序域名不正确，请检查域名正确性')
							}
						});
					}
				},
				fail(msg) {
					console.info('小程序登录失败！！！')
				}
			});
		},
		methods: {
			// 判断系统是否存在该用户的openid
			getIsHaveHandOpenid(){
				let that = this;
				// 获取登录人员的基本信息
				uni.request({
					url: 'https://lekapi.opmonitor.com/?c=app_api&a=getToolUserBasisInfo',
					data: {openid: that.openid},
					header: {
						'Content-type': 'application/json'
					},
					success: function(res) {
						that.hospital = res.data.data.hospital
						that.sortHospital = res.data.data.sortHospital
						that.department = res.data.data.department
						that.name = res.data.data.name
						that.phone = res.data.data.phone
						// 将这些变量放置globalData中
						getApp().globalData.hospital = res.data.data.hospital
						getApp().globalData.sortHospital = res.data.data.sortHospital
						getApp().globalData.department = res.data.data.department
						getApp().globalData.name = res.data.data.name
						getApp().globalData.openid = that.openid
						getApp().globalData.phone = res.data.data.phone
						uni.navigateTo({
							url: '/pages/index/index'
						})
					},
					fail: () => {
						console.info('小程序域名不正确，请检查域名正确性')
					}
				});
			}
		}
	}
</script>

<style>
	.page {
		width: 100vw;
		height: 100Vh;
	}
	.photoView {
		padding-top: 150rpx;
		position: relative;
		color: #fff;
		display: flex;
		flex-direction: column;
		align-items: center;
	}
	.photoImage {
		overflow:hidden;
		display: block;
		width: 200rpx;
		height: 200rpx;
		margin: 20rpx;
		border-radius: 50%;
		border: 2px solid #fff;
		box-shadow: 3px 3px 10px rgba(0, 0, 0, 0.2);
	}
</style>
