<!-- eslint-disable no-template-curly-in-string -->
|
<script lang="ts" setup>
|
import { computed, onMounted, ref, watch } from 'vue';
|
|
import { useGlobSetting } from '@jnpf/hooks';
|
import { encryptByBase64, isUrl } from '@jnpf/utils';
|
|
import { useAccessStore } from '@vben/stores';
|
|
import { APP_PREFIX } from '#/utils/constants';
|
import { getRealJnpfAppEnCode } from '#/utils/jnpf';
|
|
const props = defineProps({
|
linkType: { type: String, default: '' },
|
urlAddress: { type: String, default: '' },
|
linkTarget: { type: String, default: 'self' },
|
type: { type: [Number, String], default: 0 },
|
propertyJson: { type: String },
|
});
|
const to = ref('');
|
const globSetting = useGlobSetting();
|
const accessStore = useAccessStore();
|
const appEnCode = getRealJnpfAppEnCode();
|
|
const getIsUrl = computed(() => {
|
return isUrl(to.value);
|
});
|
const routerType = computed(() => {
|
if (!props.linkType || !props.urlAddress) return 'div';
|
if (getIsUrl.value) return 'a';
|
return 'router-link';
|
});
|
|
watch(
|
() => props.urlAddress,
|
() => {
|
if (props.linkType) init();
|
},
|
{
|
deep: true,
|
},
|
);
|
|
function init() {
|
let path = `/${props.urlAddress}`;
|
if (props.type == 6) {
|
let moduleId = '';
|
const propertyJson = props.propertyJson ? JSON.parse(props.propertyJson) : null;
|
if (propertyJson) moduleId = propertyJson.moduleId || '';
|
path = `${globSetting.dataVUrl || ''}${appEnCode ? `${APP_PREFIX}${appEnCode}/` : ''}view/${moduleId}?token=${accessStore.accessToken}`;
|
}
|
if (props.type == 7 || props.linkType == '2') {
|
path =
|
props.linkTarget === '_self'
|
? `/externalLink?href=${encodeURIComponent(encryptByBase64(props.urlAddress))}`
|
: props.urlAddress
|
.replaceAll('${dataV}', (globSetting.dataVUrl || '') + (appEnCode ? `${APP_PREFIX}${appEnCode}/` : ''))
|
.replaceAll('${jnpfToken}', accessStore.accessToken || '')
|
.replaceAll('${jnpfAppCode}', appEnCode || '');
|
}
|
to.value = path;
|
}
|
function linkProps(to) {
|
if (getIsUrl.value) return { href: to, target: '_blank', rel: 'noopener' };
|
return { to };
|
}
|
|
onMounted(() => {
|
if (props.linkType) init();
|
});
|
</script>
|
|
<template>
|
<component :is="routerType" v-bind="linkProps(to)">
|
<slot></slot>
|
</component>
|
</template>
|