<template>
	<view class="phone-login-page">
		<view class="login-wrap-box">
			<view class="bjcx-head-box">
				<icon class="bjcx-logo-box"></icon>
				<view class="bjcx-logo-title">家政学</view>
			</view>
			<view v-if="fromAppShare" class="wechat-auth-box">
				<view class="auth-tips">欢迎使用小程序</view>
				<button class="wechat-auth-btn" open-type="getPhoneNumber" @getphonenumber="onWechatAuth">
					微信一键登录
				</button>
			</view>
		<!-- 	<view @click="ceshi" class="auth-tips">欢迎使用小程序12312312312312</view> -->
		</view>
	</view>
</template>

<script setup>
	import cacheManager from '@/utils/cacheManager.js'
	import * as httpApi from "@/api/login.js"
	import {
		ref,
		onMounted
	} from "vue"
	import {
		toast
	} from "@/utils/common";
	import {
		useIsCanBack
	} from "@/store/isCanBack.js"
	import {
		onLoad
	} from "@dcloudio/uni-app";

	// 原有变量
	const userName = ref('')
	const password = ref('')
	const showPassword = ref(true)
	const clearTelIcon = ref(false)
	const clearPwIcon = ref(false)
	const isAgreed = ref(false)
	const id = ref('')
	const code = ref('')

	const fromPage = ref('');
	const openId = ref('');

	// 新增变量
	const fromAppShare = ref(false) // 是否来自APP分享

	const store = useIsCanBack();

	onLoad((options) => {
		if (options.from === 'appcx') {
			id.value = options.id
			fromPage.value = options.from;
		}
		uni.login().then(res1 => {
			console.log('res1', res1);
			code.value = res1.code
			httpApi.loginApplet({
				code: res1.code,
			}).then(result => {
				console.log('result', result);
				openId.value = result.data.openId
				if (result.data.bind) {
					// 保存用户信息
					if (result.data.loginVo.type == 4) {
						cacheManager.set('auth', result.data.loginVo)
						store.setIsCanBack(false)
						gotoPage();
					} else if (result.data.loginVo.type == 6) {
						cacheManager.set('auth', result.data.loginVo)
						store.setIsCanBack(false)
						gotoPage2();
					} else {
						toast('登录失败，您的身份有误，请联系管理员。')
						return
					}
				} else {
					fromAppShare.value = true
				}
			})
		})
		getAllImg();
	})

	// function ceshi() {
	// 	httpApi.loginApplet({
	// 		code: '123',
	// 	}).then(result => {
	// 		console.log('result', result);
	// 		openId.value = result.data.openId
	// 		cacheManager.set('auth', result.data.loginVo)
	// 		store.setIsCanBack(false)
	// 		gotoPage();
	// 	})
	// }
	// 微信授权登录
	function onWechatAuth(e) {
		if (e.detail.errMsg.includes('fail')) {
			toast('授权失败')
			return
		}
		uni.showLoading({
			title: '登录中'
		})
		console.log('e123123123', e);
		try {
			httpApi.bindApplet({
				code: e.detail.code,
				openId: openId.value,
			}).then(result => {
				if (result.data.type == 4) {
					cacheManager.set('auth', result.data)
					store.setIsCanBack(false)
					gotoPage();
				} else if (result.data.type == 6) {
					cacheManager.set('auth', result.data)
					store.setIsCanBack(false)
					gotoPage2();
				} else {
					toast('登录失败，您的身份有误，请联系管理员。')
				}
			})

		} catch (error) {
			toast('bindApplet失败')
		} finally {
			uni.hideLoading()
		}
	}

	function gotoPage() {
		console.log('ididid', id.value);
		if (id.value) {
			uni.navigateTo({
				url: `/pages/client/hetong/hetongInfo?id=` + id.value + `&from=${fromPage.value}`
			})
		} else {
			uni.navigateTo({
				url: `/pages/client/ShouYe/shouye`
			})
		}
	}

	function gotoPage2() {
		console.log('ididid222222', id.value);
		if (id.value) {
			uni.navigateTo({
				url: `/pages/kehu/hetong/hetongInfo?id=` + id.value + `&from=${fromPage.value}`
			})
		} else {
			uni.navigateTo({
				url: `/pages/kehu/shouYe/shouye`
			})
		}
	}

	function getAllImg() {
		httpApi.getAllImgList({}).then(res => {
			cacheManager.set('projectImg', res.data)
		});
	}
</script>

<style scoped>
	/* 新增样式 */
	.wechat-auth-box {
		text-align: center;
		padding: 40rpx 0;
	}

	.auth-tips {
		font-size: 32rpx;
		color: #333;
		margin-bottom: 60rpx;
	}

	.wechat-auth-btn {
		background-color: #07C160;
		color: white;
		border-radius: 50rpx;
		margin-bottom: 30rpx;
	}

	.switch-login {
		color: #007AFF;
		font-size: 28rpx;
	}

	/* 你原有的所有样式都保持不变 */
</style>