<script setup lang="ts">
import { nextTick, onBeforeUnmount, onMounted } from 'vue';
import { useScriptTag } from '@vueuse/core';

defineOptions({
  name: 'BindWechat'
});

let wwLogin: any = null;
const { load } = useScriptTag('http://wwcdn.weixin.qq.com/node/wework/wwopen/js/wwLogin-1.2.7.js');

// 企业微信登陆
async function handleQyWxLogin() {
  await load(true);
  getEvnConfig({
    configKey: 'wxCpEnvConfig'
  }).then(res => {
    if (res.data.captchaOnOff) {
      getWxCode(res.data);
    } else {
      window.$message?.error('请联系管理员开启企业微信登陆');
    }
  });
}
async function getWxCode({ agentId, corpId }) {
  await load(true);
  nextTick(() => {
    wwLogin = new WwLogin({
      id: 'qyWxLogin',
      appid: corpId,
      agentid: agentId,
      redirect_uri: encodeURIComponent('http://192.168.1.63:9528/qyWxLogin'),
      lang: 'zh'
    });
  });
}

onMounted(() => {
  getWxCode({ agentId: '1000110', corpId: 'wx116302f620fe4aee' });
});

onBeforeUnmount(() => {
  // wwLogin.destroyed(); // 注意wwLogin为实例对象
  wwLogin = null;
});
</script>

<template>
  <div id="qyWxLogin" class="flex-center"></div>
</template>

<style scoped></style>
