<template>
	<button open-type="getPhoneNumber" @getphonenumber="onGetPhoneNumber" class="no_style">
		<span class="biggerSize" hover-class="none" hover-stop-propagation="false">
			<slot></slot>
		</span>
	</button>
</template>

<script>
export default {
	data() {
		return {};
	},
	created() {
		this.getCode((res) => {
			uni.setStorageSync("apply-code", res);
		});
	},
	props: {},
	methods: {
		onGetPhoneNumber(e) {
			let that = this;
			// that.$emit("successLogin");
			// return;
			console.log("e", e);
			// 进行临时变量的判断, 如果在注册页面获取过手机号授权, 则不再重新获取手机号, 直接登录
			if (uni.getStorageSync("TEMPORARY_CODE")) {
				let wxRes = uni.getStorageSync("TEMPORARY_CODE");
				that.G.Get(
					that.api.login_wechat,
					{
						openId: wxRes.openId,
						tel: wxRes.phoneNumber,
						unionid: wxRes.unionid,
						createFakerAgency: 1,
					},
					(res) => {
						console.log("res", res);
						that.setToken(res);
					}
				);
				return;
			}
			if (e.detail.code) {
				// 允许授权。拿button返回信息换取token
				uni.checkSession({
					success(e1) {
						console.log("success", e1);
						//session_key 未过期，并且在本生命周期一直有效
						that.loginWX(e);
					},
					fail() {
						// session_key 已经失效，需要重新执行登录流程
						that.getCode((res) => {
							console.log("res", res);

							uni.setStorageSync("apply-code", res);
							setTimeout(() => {
								that.loginWX(e);
							}, 300);
						});
					},
				});
			} else {
				// 拒绝授权。
			}
		},
		loginWX(e) {
			let that = this;
			console.log("loginWX", e);
			that.G.Get(
				that.api.login_getWxInfo,
				{
					code: uni.getStorageSync("apply-code"),
					btnCode: e.detail.code,
					iv: e.detail.iv,
					encryptedData: e.detail.encryptedData,
				},
				(wxRes) => {
					that.G.Get(
						that.api.login_wechat,
						{
							openId: wxRes.openId,
							tel: wxRes.phoneNumber,
							unionid: wxRes.unionid,
							createFakerAgency: 1,
						},
						(res) => {
							that.setToken(res);
						}
					);
				}
			);
		},
		setToken($data) {
			let that = this;

			console.log("获取登录用户信息：", $data);
			uni.setStorageSync("apply-userinfo", $data); // 获取用户信息, 参与实际逻辑
			uni.setStorageSync("apply-userinfo-copy", JSON.stringify($data)); // 仅用于查看数据，不参与实际逻辑
			uni.setStorageSync("apply-avatar", $data.imgSrc); // 单独存储 -- 头像，方便修改
			uni.setStorageSync("apply-username", $data.userName); // 单独存储 -- 用户名，方便修改
			uni.setStorageSync("apply-tel", $data.tel); // 单独存储 -- 手机号，方便修改
			uni.setStorageSync("apply-token", $data.token); // 单独存储 -- token，方便获取
			uni.setStorageSync("apply-uid", $data.user.id); // 单独存储 -- 用户名，方便修改
			uni.setStorageSync("apply-agencyId", $data.user.agencyId); // 单独存储 -- 代理id，方便获取
			uni.setStorageSync("apply-supplierAccount", $data.supplierAccount); // 单独存储 -- 是否是发单号，方便获取 0.不是发单号 1.是发单号
			uni.$emit("isGlogin", {
				uid: $data.user.id,
			});
			uni.removeStorageSync("TEMPORARY_CODE");
			that.$emit("successLogin", $data);
		},
		getCode(callback = () => {}) {
			uni.login({
				success(wxRes) {
					console.log("wxRes", wxRes);
					callback(wxRes.code);
				},
				fail(res) {
					console.log(res);
				},
			});
		},
	},
};
</script>

<style>
.no_style {
	width: 100%;
	height: 100%;
	background-color: transparent;
	border: none;
	padding: 0;
	margin: 0;
	color: inherit;
	font-size: inherit;
	/* line-height: 0; */
}
.biggerSize {
	position: relative;
}
.biggerSize::after {
	content: "";
	/* display: inline-block; */
	width: 150%;
	height: 140%;
	position: absolute;
	left: 50%;
	top: 50%;
	z-index: 99;
	transform: translate(-50%, -50%);
}
</style>
