<template>
	<view class="content">
		<view class="one">
			<view class="list" v-for="(item, index) in list" :key="index" @click="toCommon(item.route)">
				<view class="left">
					<text class="title">{{item.title}}</text>
				</view>
				<view class="right">
					<text class="iconfont icon-dayuhao"></text>
				</view>
			</view>
		</view>
		<view class="two">
			<button class="button" type="primary" @click="toExit">退出登录</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				user: {},
				list: [{
						title: '账号详情',
						route: 'pagesMy/account/info'
					},
					{
						title: '修改密码',
						route: 'pagesMy/account/update'
					}
				]
			}
		},
		async onLoad() {
			const that = this;
			await that.searchToken();
		},
		methods: {
			async searchToken() {
				const that = this;
				uni.getStorage({
					key: 'token',
					success: function(res) {
						that.$set(that, `user`, res.data);
					},
					fail: function(err) {
						uni.showToast({
							title: err.errmsg,
							icon: 'error',
							duration: 2000
						});
					}
				})
			},
			// 公共跳转
			toCommon(e) {
				const that = this;
				if (that.user && that.user._id) {
					uni.navigateTo({
						url: `/${e}`
					})
				} else {
					uni.navigateTo({
						url: `/pages/login/index`
					})
				}
			},
			// 退出登录
			toExit() {
				uni.removeStorage({
					key: 'token',
					success: function(res) {
						let url = `/pages/index/index`;
						uni.reLaunch({
							url
						})
					}
				});
			},
		}
	}
</script>

<style lang="scss">
	.content {
		display: flex;
		flex-direction: column;
		padding: 2vw;
	}

	.one {
		.list {
			margin: 2vw 0 0 0;
			background-color: var(--f1Color);
			display: flex;
			justify-content: space-between;
			padding: 3vw;
			border-bottom: 1px solid var(--f9Color);

			.left {
				display: flex;
				align-items: center;
				font-size: var(--font12Size);

				.title {
					display: inline-block;
					font-size: var(--font12Size);
				}
			}

			.right {
				display: flex;
				align-items: center;
				font-size: var(--font12Size);
				color: var(--f99Color);
			}
		}
	}

	.two {
		margin: 2vw 0 0 0;
		background-color: var(--f1Color);

		.button {
			background-color: var(--f3CColor);
			color: var(--mainColor);
			font-size: var(--font14Size);
		}
	}
</style>
