<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'

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

			const formData = ref({
				appkey: null,
				appsecret: null,
				authCode: null,
				domainname: null,
			})
			const getBasicInfo = () => {
				getAuthCode()
			}

			const getAuthCode = () => {
				dd.ready(() => {
					dd.getAuthCode({}).then((result) => {
						if (result.code) {
							formData.value.authCode = result.code
						} else {
							formData.value.authCode = result.auth_code
						}
						setTimeout(() => {
							getUserInfo(formData.value.authCode);
						}, 0);
					}).catch(err => {
						link.goLogin()
					})
				})
			}

			const getUserInfo = async (authCode) => {
				try {
					let res = await rest.post('/zyyp/ykz/getUserInfo?authCode=' + authCode, '')
					if (!res) return
					let obj = Object.assign({}, globalParameter.value)
					obj.authCode = formData.value.authCode;
					obj.zjhm = res.openid;
					obj.userName = res.lastName;
					obj.isLeader = res.leader;
					store.setGlobalParameter(obj)
					
				} catch (error) {
					console.error(error);
				}
			};

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

			onMounted(() => {
				startIntervalTask();
			});

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

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