<template>
	<view class="container main">
		<view class="one">
			<view class="one_cont">
				<view class="one_1">用户信息</view>
				<view class="one_2">
					<view class="one_label">
						<view class="left">账号</view>
						<view class="right textOne">
							<text>{{form.account||'暂无账号'}}</text>
						</view>
					</view>
					<view class="one_label">
						<view class="left">昵称</view>
						<view class="right textOne">
							<text>{{form.nick_name||'暂无昵称'}}</text>
						</view>
					</view>
					<view class="one_label">
						<view class="left">性别</view>
						<view class="right textOne">
							<text>{{gender_name||'暂无性别'}}</text>
						</view>
					</view>
					<view class="one_label">
						<view class="left">所属产业</view>
						<view class="right textOne">
							<text>{{form.industry||'暂无所属产业'}}</text>
						</view>
					</view>
					<view class="one_label">
						<view class="left">手机号</view>
						<view class="right textOne">
							<text>{{form.phone||'暂无手机号'}}</text>
						</view>
					</view>
					<view class="one_label">
						<view class="left">电子邮箱</view>
						<view class="right textOne">
							<text>{{form.email||'暂无电子邮箱'}}</text>
						</view>
					</view>
				</view>
			</view>
			<view class="button">
				<button class="warning" size="mini" type="warn" @tap.stop="toBind()">账号绑定</button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		components: {},
		data() {
			return {
				openid: '',
				user: {},
				form: {},
				gender_name: '',
				// 字典表
				genderList: [],
			}
		},
		onLoad: async function(e) {
			const that = this;
			await that.searchOpenids();
			await that.searchToken();
			await that.searchOther();
			await that.search();
		},
		methods: {
			async searchOpenids() {
				const that = this;
				uni.getStorage({
					key: 'openid',
					success: function(res) {
						that.$set(that, `openid`, res.data);
					},
					fail: function(err) {
						uni.login({
							success: async function(res) {
								if (res.code) {
									const aee = await that.$app('/wechat/api/login/app',
										'GET', {
											js_code: res.code,
											config: that.$config.wx_projectkey
										})
									if (aee.errcode == '0') {
										uni.setStorage({
											key: "openid",
											data: aee.data.openid
										})
										that.$set(that, `openid`, aee.data.openid);
									} else {
										uni.showToast({
											title: aee.errmsg,
											icon: 'none'
										})
									}
								} else {
									uni.showToast({
										title: res.errMsg,
										icon: 'none'
									})
								}
							}
						});
					}
				})
			},
			// 用户信息
			searchToken() {
				const that = this;
				try {
					const res = uni.getStorageSync('token');
					if (res) {
						const user = that.$jwt(res);
						that.$set(that, `user`, user);
					}
				} catch (e) {}
			},
			async searchOther() {
				const that = this;
				let res;
				// 查询性别
				res = await that.$api(`/dictData`, 'GET', {
					code: 'gender',
					is_use: '0',
				})
				if (res.errcode == '0') that.$set(that, `genderList`, res.data);
			},
			// 查询
			async search() {
				const that = this;
				if (that.user && that.user.id) {
					let res;
					res = await that.$api(`/user/${that.user.id}`, 'GET', {})
					if (res.errcode == '0') {
						that.$set(that, `form`, res.data)
						if (res.data.gender) {
							let data = that.genderList.find(i => i.value == res.data.gender);
							if (data) that.$set(that, `gender_name`, data.label)
						}
					} else {
						uni.showToast({
							title: res.errmsg,
						});
					}
				}
			},
			// 账号绑定
			toBind() {
				console.log('账号绑定');
			},
		}
	}
</script>

<style lang="scss" scoped>
	.main {
		.one {
			.one_cont {
				padding: 2vw 0 0 0;

				.one_1 {
					text-indent: 10px;
					border-left: 3px solid var(--f3CColor);
					font-weight: bold;
					font-size: var(--font16Size);
				}

				.one_2 {
					padding: 2vw;

					.one_label {
						display: flex;
						justify-content: space-between;
						padding: 4vw;
						border-bottom: 1px solid var(--f9Color);
						font-size: var(--font14Size);
						color: var(--f69Color);

						.right {
							text-align: right;
							width: 60vw;
						}
					}
				}
			}

			.button {
				margin: 2vw 0 0 0;
				text-align: center;

				.warning {
					background: var(--f3CColor);
				}

				.danger {
					background: var(--fF0Color);
				}

				button {
					margin: 0 1vw 0 0;
				}
			}
		}
	}
</style>