<script>
	import {
		onMounted,
		ref
	} from 'vue'
	import * as link from '@/lib/link'
	import {
		computed
	} from "vue";
	import rest from '@/stores/rest'
	import dd from 'gdt-jsapi';
	import {
		useUserStore
	} from '@/lib/store'
	import {
		storeToRefs
	} from 'pinia'

	export default {
		setup() {
			const store = useUserStore();
			const globalParameter = computed(() => store.getGlobalParameter);

			const formData = ref({
				appkey: null,
				appsecret: null,
				authCode: null,
				domainname: null,
			})
			const getBasicInfo = () => {
				console.log("监听咯")
				getAuthCode()
			}

			const getAuthCode = () => {
				console.log("监听咯-getAuthCode")
				dd.ready(() => {
					dd.getAuthCode({}).then((result) => {
						if (result.code) {
							formData.value.authCode = result.code
						} else {
							formData.value.authCode = result.auth_code
						}
						let obj = Object.assign({}, globalParameter.value)
						obj.authCode = formData.value.authCode;
						store.setGlobalParameter(obj)
					}).catch(err => {
						link.goLogin()
					})
				})
			}

			const getUserInfo = async () => {
				try {
					await rest.post('/zyyp/ykz/getUserInfo?authCode=' + formData.value.authCode, '')
				} catch (error) {
					console.error(error);
				}
			};

			const startIntervalTask = () => {
				// 每 60 分钟执行一次任务
				setInterval(() => {
					getBasicInfo();
				}, 60 * 60 * 1000); // 60 分钟
			};

			onMounted(() => {
				console.log('App Created');
				startIntervalTask();
			});

			return {
				formData,
				getBasicInfo,
				getAuthCode,
				getUserInfo,
				startIntervalTask,
			};
		}
	}
</script>

<style lang="scss">
	/*每个页面公共css */
	@import "uni.scss";
	@import "static/style/base.scss";
</style>