<template>
</template>

<script setup lang="ts">
	import { ref } from "vue";
	import * as link from '@/lib/link'
	import dd from 'gdt-jsapi';
	import rest from '@/stores/rest'
	import { aesEncrypt, aesDecrypt } from "@/lib/encryption"
	import { onLoad } from "@dcloudio/uni-app";
	
	import { useGlobalStore } from '@/lib/store';
	 
	const globalStore = useGlobalStore();
	
	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
				 }
				globalStore.setAuthCode(formData.value.authcode);
				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,'')
		link.goBJList(aesEncrypt(res.openid),aesEncrypt(res.lastName))
	  } catch (error) {
	    console.error(error);
	  }
	};


	onLoad((res) => {
		if (res.auth_code && res.appkey && res.appSecret && res.domain){
			const auth_code = res.auth_code
			const appkey  = res.appkey
			const appsecret = res.appsecret
			const domainname  = res.domain
			const data = {
				authcode:auth_code,
				appkey:appkey,
				appsecret:appsecret,
				domainname:domainname,
			}
		}else{
			login()
		}
		// 示例：设置 authCode 
		globalStore.setAuthCode('your_auth_code'); // 示例：获取 authCode
		console.log(globalStore.authCode);
	})
</script>

<style>
</style>