OfficeSystem/composables/useCustomerForm.js

264 lines
7.0 KiB
JavaScript
Raw Normal View History

2025-11-08 10:39:16 +08:00
import { ref, computed } from 'vue';
import {
getRegionTree,
getCustomerIntentDict,
getCustomerIntentLevelDict,
getCustomerSourceDict,
getCustomerTypeDict,
getCustomerStatusDict,
getWechatList
2025-11-12 15:33:53 +08:00
} from '@/api';
2025-11-08 10:39:16 +08:00
/**
* 客户表单共享逻辑
*/
export function useCustomerForm() {
// 字典数据
const customerTypeOptions = ref([]);
const sourceOptions = ref([]);
const intentOptions = ref([]);
const intentLevelOptions = ref([]);
const customerStatusOptions = ref([]);
const workWechatOptions = ref([]);
// 地区数据
const provinces = ref([]);
const citys = ref([]);
const areas = ref([]);
const regionLoading = ref(true);
// 计算属性:返回地址列表
const addressList = computed(() => {
return [provinces.value, citys.value, areas.value];
});
// 加载字典数据
const loadDictData = async () => {
try {
const [typeRes, sourceRes, intentRes, intentLevelRes, statusRes] = await Promise.all([
getCustomerTypeDict(),
getCustomerSourceDict(),
getCustomerIntentDict(),
getCustomerIntentLevelDict(),
getCustomerStatusDict()
]);
customerTypeOptions.value = (typeRes || []).map(item => ({
label: item.dictLabel,
value: item.dictValue
}));
sourceOptions.value = (sourceRes || []).map(item => ({
label: item.dictLabel,
value: item.dictValue
}));
intentOptions.value = (intentRes || []).map(item => ({
label: item.dictLabel,
value: item.dictValue
}));
intentLevelOptions.value = (intentLevelRes || []).map(item => ({
label: item.dictLabel,
value: item.dictValue
}));
customerStatusOptions.value = (statusRes || []).map(item => ({
label: item.dictLabel,
value: item.dictValue
}));
} catch (err) {
console.error('加载字典数据失败:', err);
uni.showToast({
title: '加载字典数据失败',
icon: 'none'
});
}
};
// 加载微信列表数据
const loadWechatList = async () => {
try {
const res = await getWechatList();
const rows = res?.rows || [];
workWechatOptions.value = rows.map(item => ({
label: item.nickName ? `${item.nickName} (${item.wechatId})` : item.wechatId,
value: String(item.id),
id: item.id,
wechatId: item.wechatId
}));
} catch (err) {
console.error('加载微信列表失败:', err);
uni.showToast({
title: '加载微信列表失败',
icon: 'none'
});
}
};
// 加载地区树数据
const loadRegionTree = async () => {
try {
const res = await getRegionTree();
const regionTree = res;
provinces.value = regionTree.sort((left, right) =>
(Number(left.code || left.id) > Number(right.code || right.id) ? 1 : -1)
);
if (provinces.value.length > 0) {
citys.value = provinces.value[0]?.children || [];
if (citys.value.length > 0) {
areas.value = citys.value[0]?.children || [];
}
}
setTimeout(() => {
regionLoading.value = false;
}, 200);
} catch (err) {
console.error('加载地区树失败:', err);
regionLoading.value = false;
uni.showToast({
title: '加载地区数据失败',
icon: 'none'
});
}
};
// 根据已选择的ID设置默认选中项
const handlePickValueDefault = (formData, pickersRef) => {
if (!formData.regionIds || formData.regionIds.length === 0) {
return;
}
const [provinceId, cityId, districtId] = formData.regionIds;
const provinceIndex = provinces.value.findIndex(item => Number(item.id) === Number(provinceId));
if (provinceIndex >= 0) {
citys.value = provinces.value[provinceIndex]?.children || [];
const cityIndex = citys.value.findIndex(item => Number(item.id) === Number(cityId));
if (cityIndex >= 0) {
areas.value = citys.value[cityIndex]?.children || [];
const districtIndex = areas.value.findIndex(item => Number(item.id) === Number(districtId));
if (districtIndex >= 0 && pickersRef) {
pickersRef.setRegionIndexs([provinceIndex, cityIndex, districtIndex]);
}
}
}
};
// 处理地区选择器change事件
const handleRegionChange = (e, pickersRef) => {
if (regionLoading.value) return;
const { columnIndex, index, indexs } = e;
if (columnIndex === 0) {
citys.value = provinces.value[index]?.children || [];
areas.value = citys.value[0]?.children || [];
if (pickersRef) {
pickersRef.setRegionIndexs([index, 0, 0]);
}
} else if (columnIndex === 1) {
areas.value = citys.value[index]?.children || [];
if (pickersRef) {
pickersRef.setRegionIndexs(indexs);
}
}
};
// 处理地区选择器confirm事件
const handleRegionConfirm = (e, formData) => {
const { value } = e;
if (value && value.length > 0) {
const province = value[0];
const city = value[1];
const district = value[2];
const regionNames = [];
const regionIds = [];
if (province) {
regionNames.push(province.name);
regionIds.push(province.id);
if (city) {
regionNames.push(city.name);
regionIds.push(city.id);
if (district) {
regionNames.push(district.name);
regionIds.push(district.id);
}
}
}
formData.region = regionNames.join('/');
formData.regionIds = regionIds;
} else {
formData.region = '';
formData.regionIds = [];
}
};
// 打开地区选择器
const openRegionPicker = (formData, pickersRef) => {
if (regionLoading.value || provinces.value.length === 0) {
uni.showToast({
title: '地区数据加载中,请稍候',
icon: 'none'
});
return;
}
if (formData.regionIds && formData.regionIds.length > 0) {
handlePickValueDefault(formData, pickersRef);
} else {
if (citys.value.length === 0 && provinces.value.length > 0) {
citys.value = provinces.value[0]?.children || [];
if (citys.value.length > 0) {
areas.value = citys.value[0]?.children || [];
}
}
}
if (pickersRef) {
pickersRef.openRegionPicker();
if (!formData.regionIds || formData.regionIds.length === 0) {
setTimeout(() => {
if (pickersRef) {
pickersRef.setRegionIndexs([0, 0, 0]);
}
}, 100);
}
}
};
return {
// 数据
customerTypeOptions,
sourceOptions,
intentOptions,
intentLevelOptions,
customerStatusOptions,
workWechatOptions,
provinces,
citys,
areas,
regionLoading,
addressList,
// 方法
loadDictData,
loadWechatList,
loadRegionTree,
handlePickValueDefault,
handleRegionChange,
handleRegionConfirm,
openRegionPicker
};
}