<template>
	<view class="main">
		<view class="user" @click="go('info')">
			<image :src="user.avatarUrl ? user.avatarUrl : '../../static/favicon.png'" class="head"></image>
			<view class="con" v-if="user.id">
				<view class="nickName">微信用户</view>
				<view class="welcome">欢迎使用承揽时代</view>
			</view>
			<view class="con" v-else>
				<view class="nickName">你还没登录</view>
				<view class="welcome">欢迎使用承揽时代</view>
			</view>
			<view class="icon">&#xe62b;</view>
		</view>
		<view class="cmd">
			<view class="s_item" @click="go('auth')">
				<text class="icon ic">&#xe600;</text>
				<text class="title">我的认证</text>
				<text class="icon arrow">&#xe62b;</text>
			</view>
			<view class="s_item" @click="go('contract')">
				<text class="icon ic">&#xe6ed;</text>
				<text class="title">我的签约</text>
				<text class="icon arrow">&#xe62b;</text>
			</view>
			<view class="s_item" @click="go('company')">
				<text class="icon ic">&#xe623;</text>
				<text class="title">关联企业</text>
				<text class="icon arrow">&#xe62b;</text>
			</view>
			<view class="s_item" @click="go('packages')">
				<text class="icon ic">&#xe604;</text>
				<text class="title">我的接包</text>
				<text class="icon arrow">&#xe62b;</text>
			</view>
		</view>
		<!-- <button class="btn exit" @click="exit()" v-if="user.id">退出登录</button> -->
	</view>
</template>
<script>
export default {
	data() {
		return {
			ip: this.http.ip,
			user: {}
		};
	},
	onShow() {
		 		 /* this.user = {
			token: 'eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6IjllZjljY2QyLWM4ZmYtNGU2NS1hM2EwLWZlMzUzNWUxMzQ5MyJ9.ll0aR92AP8iZgQ4pVpQwfxcyIDKdPwkZA23uU_0ON_tAaxlepQqU04pbNPIx1vMKjADecBsvxB3vyfYDDATl3g'
		};
		uni.setStorageSync('user', this.user); */
		if (this.hasLogin()) {
			this.getUserInfo();
		}
	},
	methods: {
		getUserInfo() {
			this.http.request({
				url: '/app/user/info',
				success: (res) => {
					this.user = res.data.data;
				}
			});
		},
		go(url) {
			if (this.hasLogin()) {
				if (url == 'auth') {
					uni.navigateTo({ url: '/pages/auth/index' });
				}
				if (url == 'company' && this.user.isAuthentication === 1) {
					uni.navigateTo({ url: '/pages/company/index' });
				}
				if (url == 'packages' && this.user.isCompany > 0 && this.user.isContract == 1) {
					uni.switchTab({ url: '/pages/packages/index' });
				}
				if (url == 'contract' && this.user.isContract == 1) {
					this.look();
				}
				if (url == 'info') {
					uni.navigateTo({ url: '/pages/user/info' });
				}
			} else {
				uni.navigateTo({ url: '/pages/user/login' });
			}
		},
		//查看合同
		look() {
			uni.showLoading({ title: '正在打开合同...', mask: true });
			uni.downloadFile({
				url: this.user.isContract == 0 ? this.ip + this.contract.url : this.ip + '/app/contract/look',
				header: { Authorization: this.getUser().token },
				success: (res) => {
					uni.openDocument({
						filePath: res.tempFilePath,
						showMenu: true,
						success: (res) => {
							uni.hideLoading();
						}
					});
				},
				fail: (res) => {
					uni.hideLoading();
				}
			});
		},
		//退出登录
		exit(url) {
			uni.showModal({
				title: '提示',
				content: '确定退出登录?',
				success: (res) => {
					if (res.confirm) {
						this.http.request({
							url: this.http.urls.logout,
							success: (res) => {
								uni.removeStorageSync('user');
								uni.$emit('top');
								uni.switchTab({
									url: '/pages/index/index'
								});
							}
						});
					}
				}
			});
		}
	}
};
</script>
<style lang="scss">
.main {
	padding: 20px;
}
.user {
	overflow: hidden;
	image {
		float: left;
		width: 60px;
		height: 60px;
		border-radius: 50%;
	}
	.con {
		float: left;
		padding-left: 15px;
		.nickName {
			padding-top: 5px;
		}
		.welcome {
			font-size: 13px;
			padding-top: 5px;
			color: #989898;
		}
	}
	.icon {
		float: right;
		margin-top: 14px;
	}
}
.exit {
	margin-top: 20px;
}
</style>
