<template>
</template>

<script setup lang="ts">
	import { ref, computed } from "vue";
	import * as link from '@/lib/link'
	import dd from 'gdt-jsapi';
	import rest from '@/stores/rest'
	import { onLoad } from "@dcloudio/uni-app";

	import { useUserStore } from '@/lib/store';
	const store = useUserStore();
	const globalParameter = computed(() => store.getGlobalParameter);

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

	const login = () => {
		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 => {
				// alert('不是钉钉环境', 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)
			// alert("查看内容（2）：" + JSON.stringify(res))
			link.goBJList(true)
		} catch (error) {
			console.error(error);
		}
	};


	onLoad((res) => {
		login()
	})
</script>

<style>
</style>