数通互联化工云平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
2.8 KiB

<!--
@ 作者: 秦东
@ 时间: 2024-09-12 14:41:53
@ 备注: 获取行政组织数据
-->
<script lang='ts' setup>
import { getgovcont } from '@/api/hr/org/index'
const props = defineProps({
orgid:{
type: String,
default:""
}
})
const orgName = ref("")
/* const orgName = computed(()=>{
return str.value
}) */
//let str = ref("")
//选择行政组织
const pickOrgVal = (val:any) => {
if(val != "" && val != null){
if(hasComma(val)){
let arr = commaStringToNumberArray(val)
let str= ''
arr.forEach((element, index, array) => {
// 判断当前是否是最后一个元素
const isLast = index === array.length - 1;
str = ''
let item = element+""
getgovcont({id:element*1,idstr:item})
.then(({data}) =>{
str+=data.name+","
})
.finally(()=>{
if(isLast){
//alert(1)
str = removeLastChar(str)
orgName.value = str
}
})
});
}else{
getgovcont({id:val*1,idstr:val})
.then(({data}) =>{
// console.log("选择行政组织-3->",data,data.name)
orgName.value = data.name
})
.finally(()=>{})
}
}
}
function removeLastChar(str: string | any[]) {
// 检查输入是否为字符串
if (typeof str !== 'string') {
throw new Error('输入必须是字符串类型');
}
// 处理空字符串情况
if (str.length === 0) {
return str; // 空字符串返回自身
}
// 截取从开始到倒数第二个字符的部分
return str.slice(0, -1);
}
function hasComma(str: any) {
// 检查输入是否为字符串类型
if (typeof str !== 'string') {
return false
}
// 使用indexOf检查是否包含逗号
// 如果存在逗号,返回true;否则返回false
return str.indexOf(',') !== -1;
}
function commaStringToNumberArray(str: string) {
// 检查输入是否为字符串
if (typeof str !== 'string') {
throw new Error('输入必须是字符串');
}
// 处理空字符串的情况
if (str.trim() === '') {
return [];
}
// 按逗号分割字符串,处理可能的空格,并转换为数字
return str.split(',').map(item => {
// 去除前后空格
const trimmed = item.trim();
// 转换为数字
const number = Number(trimmed);
// 检查是否为有效数字
if (isNaN(number)) {
throw new Error(`无法将 "${trimmed}" 转换为有效数字`);
}
return number;
});
}
onBeforeMount(() => {
pickOrgVal(props.orgid)
})
onMounted(() => {
pickOrgVal(props.orgid)
});
watch(() =>props.orgid,(val:string) => {
// console.log("选择行政组织-4->",val)
pickOrgVal(val)
})
</script>
<template>
<div>{{ orgName }}</div>
</template>
<style lang='scss' scoped>
</style>