<template>
|
<div class="app-container">
|
<div class="search_form" style="margin-bottom: 20px;">
|
<div>
|
<span class="search_title">联系人姓名:</span>
|
<el-input v-model="searchForm.contactPerson"
|
style="width: 240px;margin-right: 10px"
|
placeholder="请输入"
|
@keyup.enter="handleQuery"
|
clearable
|
:prefix-icon="Search" />
|
<span class="search_title">联系电话:</span>
|
<el-input v-model="searchForm.contactPhone"
|
style="width: 240px;margin-right: 10px"
|
placeholder="请输入"
|
@keyup.enter="handleQuery"
|
clearable />
|
<span class="search_title">所属客户:</span>
|
<el-select v-model="searchForm.customerId"
|
placeholder="请选择"
|
style="width: 240px"
|
clearable
|
filterable
|
@change="handleQuery">
|
<el-option v-for="item in customerList"
|
:key="item.id"
|
:label="item.customerName"
|
:value="item.id" />
|
</el-select>
|
<el-button type="primary"
|
@click="handleQuery"
|
style="margin-left: 10px">搜索</el-button>
|
<el-button @click="resetSearch">重置</el-button>
|
</div>
|
<div>
|
<el-button type="primary"
|
@click="openForm('add')">新增联系人</el-button>
|
<el-button type="danger"
|
plain
|
@click="handleDelete">删除</el-button>
|
</div>
|
</div>
|
<div class="table_list">
|
<PIMTable rowKey="id"
|
:column="tableColumn"
|
:tableData="tableData"
|
:page="page"
|
:isSelection="true"
|
@selection-change="handleSelectionChange"
|
:tableLoading="tableLoading"
|
@pagination="pagination">
|
<template #customerName="{ row }">
|
<div class="customer-tags">
|
<el-tag v-for="(name, index) in formatCustomerNames(row.customerNames)"
|
:key="index"
|
size="small"
|
type="info"
|
class="customer-tag">
|
{{ name }}
|
</el-tag>
|
</div>
|
</template>
|
</PIMTable>
|
</div>
|
|
<!-- 联系人表单对话框 -->
|
<FormDialog v-model="dialogFormVisible"
|
:title="dialogTitle"
|
:operation-type="operationType"
|
width="600px"
|
@close="closeDia"
|
@confirm="submitForm"
|
@cancel="closeDia">
|
<el-form :model="form"
|
label-width="100px"
|
:rules="rules"
|
ref="formRef">
|
<el-form-item label="联系人:"
|
prop="contactPerson">
|
<el-input v-model="form.contactPerson"
|
placeholder="请输入联系人姓名"
|
clearable />
|
</el-form-item>
|
<el-form-item label="联系电话:"
|
prop="contactPhone">
|
<el-input v-model="form.contactPhone"
|
placeholder="请输入联系电话"
|
clearable />
|
</el-form-item>
|
<el-form-item label="所属客户:"
|
prop="customerIdList">
|
<el-select v-model="form.customerIdList"
|
placeholder="请选择所属客户(可多选)"
|
style="width: 100%"
|
filterable
|
clearable
|
multiple>
|
<el-option v-for="item in customerList"
|
:key="item.id"
|
:label="item.customerName"
|
:value="item.id" />
|
</el-select>
|
</el-form-item>
|
<el-form-item label="备注:"
|
prop="remark">
|
<el-input v-model="form.remark"
|
type="textarea"
|
:rows="3"
|
placeholder="请输入备注"
|
clearable />
|
</el-form-item>
|
</el-form>
|
</FormDialog>
|
|
<!-- 客户详情对话框 -->
|
<FormDialog v-model="customerDetailVisible"
|
title="绑定客户详情"
|
operation-type="detail"
|
width="700px"
|
@close="closeCustomerDetail"
|
@cancel="closeCustomerDetail">
|
<div class="customer-detail">
|
<el-table :data="currentCustomerList"
|
border
|
style="width: 100%">
|
<el-table-column prop="customerName"
|
label="客户名称"
|
min-width="150" />
|
<el-table-column prop="customerType"
|
label="客户分类"
|
width="120" />
|
<el-table-column prop="companyPhone"
|
label="公司电话"
|
width="150" />
|
<el-table-column prop="companyAddress"
|
label="公司地址"
|
min-width="200"
|
show-overflow-tooltip />
|
</el-table>
|
</div>
|
</FormDialog>
|
</div>
|
</template>
|
|
<script setup>
|
import { onMounted, ref, reactive, getCurrentInstance, computed } from "vue";
|
import { Search } from "@element-plus/icons-vue";
|
import { listContact, getContactById, addContact, updateContact, delContacts } from "@/api/basicData/contact.js";
|
import { listCustomer } from "@/api/basicData/customer.js";
|
import { ElMessageBox } from "element-plus";
|
import FormDialog from "@/components/Dialog/FormDialog.vue";
|
|
const { proxy } = getCurrentInstance();
|
|
// 对话框标题
|
const dialogTitle = computed(() => {
|
return operationType.value === "add" ? "新增联系人" : "编辑联系人";
|
});
|
|
// 表格列定义
|
const tableColumn = ref([
|
{
|
label: "联系人姓名",
|
prop: "contactPerson",
|
},
|
{
|
label: "联系电话",
|
prop: "contactPhone",
|
},
|
{
|
label: "所属客户",
|
prop: "customerNames",
|
dataType: "slot",
|
slot: "customerName",
|
},
|
{
|
label: "备注",
|
prop: "remark",
|
showOverflowTooltip: true,
|
},
|
{
|
dataType: "action",
|
label: "操作",
|
align: "center",
|
fixed: "right",
|
width: 200,
|
operation: [
|
{
|
name: "编辑",
|
type: "text",
|
clickFun: row => {
|
openForm("edit", row);
|
},
|
},
|
{
|
name: "查看客户",
|
type: "text",
|
clickFun: row => {
|
viewCustomerDetail(row);
|
},
|
},
|
{
|
name: "删除",
|
type: "text",
|
style: "color: #f56c6c",
|
clickFun: row => {
|
handleDeleteRow(row);
|
},
|
},
|
],
|
},
|
]);
|
|
const tableData = ref([]);
|
const selectedRows = ref([]);
|
const customerList = ref([]);
|
const customerMap = ref(new Map());
|
const tableLoading = ref(false);
|
const page = reactive({
|
current: 1,
|
size: 10,
|
total: 0,
|
});
|
|
// 搜索表单
|
const searchForm = reactive({
|
contactPerson: "",
|
contactPhone: "",
|
customerId: "",
|
});
|
|
// 对话框相关
|
const operationType = ref("");
|
const dialogFormVisible = ref(false);
|
const formRef = ref();
|
|
// 表单数据
|
const form = reactive({
|
id: null,
|
contactPerson: "",
|
contactPhone: "",
|
customerIdList: [],
|
remark: "",
|
});
|
|
// 表单验证规则
|
const rules = {
|
contactPerson: [{ required: true, message: "请输入联系人姓名", trigger: "blur" }],
|
contactPhone: [{ required: true, message: "请输入联系电话", trigger: "blur" }],
|
customerIdList: [{ required: true, message: "请选择所属客户", trigger: "change", type: "array" }],
|
};
|
|
// 客户详情相关
|
const customerDetailVisible = ref(false);
|
const currentCustomerList = ref([]);
|
|
// 获取客户列表(用于下拉选择)
|
const getCustomerList = () => {
|
listCustomer({ current: -1, size: -1 }).then(res => {
|
if (res.data && res.data.records) {
|
customerList.value = res.data.records;
|
// 构建客户ID到客户信息的映射
|
customerMap.value = new Map();
|
res.data.records.forEach(item => {
|
customerMap.value.set(item.id, item);
|
});
|
}
|
});
|
};
|
|
// 格式化客户名称(处理字符串或数组格式)
|
const formatCustomerNames = (customerNames) => {
|
if (!customerNames) return [];
|
if (Array.isArray(customerNames)) return customerNames;
|
if (typeof customerNames === 'string') {
|
return customerNames.split(',').map(s => s.trim()).filter(s => s);
|
}
|
return [];
|
};
|
|
// 重置搜索表单
|
const resetSearch = () => {
|
searchForm.contactPerson = "";
|
searchForm.contactPhone = "";
|
searchForm.customerId = "";
|
handleQuery();
|
};
|
|
// 查询列表
|
const handleQuery = () => {
|
page.current = 1;
|
getList();
|
};
|
|
const pagination = obj => {
|
page.current = obj.page;
|
page.size = obj.limit;
|
getList();
|
};
|
|
// 获取列表数据
|
const getList = () => {
|
tableLoading.value = true;
|
const params = {
|
...searchForm,
|
current: page.current,
|
size: page.size
|
};
|
listContact(params).then(res => {
|
tableLoading.value = false;
|
if (res.data) {
|
tableData.value = res.data.records || [];
|
page.total = res.data.total || 0;
|
}
|
}).catch(() => {
|
tableLoading.value = false;
|
});
|
};
|
|
// 表格选择数据
|
const handleSelectionChange = selection => {
|
selectedRows.value = selection;
|
};
|
|
// 打开表单对话框
|
const openForm = (type, row) => {
|
operationType.value = type;
|
resetForm();
|
if (type === "edit" && row) {
|
form.id = row.id;
|
form.contactPerson = row.contactPerson;
|
form.contactPhone = row.contactPhone;
|
form.customerIdList = row.customerId ? row.customerId.toString().split(',').map(id => Number(id.trim())) : [];
|
form.remark = row.remark || "";
|
}
|
dialogFormVisible.value = true;
|
};
|
|
// 重置表单
|
const resetForm = () => {
|
form.id = null;
|
form.contactPerson = "";
|
form.contactPhone = "";
|
form.customerIdList = [];
|
form.remark = "";
|
if (formRef.value) {
|
formRef.value.resetFields();
|
}
|
};
|
|
// 关闭对话框
|
const closeDia = () => {
|
dialogFormVisible.value = false;
|
resetForm();
|
};
|
|
// 提交表单
|
const submitForm = () => {
|
formRef.value.validate(valid => {
|
if (valid) {
|
if (operationType.value === "edit") {
|
submitEdit();
|
} else {
|
submitAdd();
|
}
|
}
|
});
|
};
|
|
// 提交新增
|
const submitAdd = () => {
|
const submitData = {
|
contactPerson: form.contactPerson,
|
contactPhone: form.contactPhone,
|
customerIdList: form.customerIdList,
|
remark: form.remark,
|
};
|
|
addContact(submitData).then(res => {
|
if (res.code === 200) {
|
proxy.$modal.msgSuccess("添加成功");
|
closeDia();
|
getList();
|
}
|
});
|
};
|
|
// 提交编辑
|
const submitEdit = () => {
|
const submitData = {
|
id: form.id,
|
contactPerson: form.contactPerson,
|
contactPhone: form.contactPhone,
|
customerIdList: form.customerIdList,
|
remark: form.remark,
|
};
|
|
updateContact(submitData).then(res => {
|
if (res.code === 200) {
|
proxy.$modal.msgSuccess("修改成功");
|
closeDia();
|
getList();
|
}
|
});
|
};
|
|
// 删除单行
|
const handleDeleteRow = (row) => {
|
ElMessageBox.confirm(
|
`确定要删除联系人"${row.contactPerson}"吗?`,
|
"提示",
|
{
|
confirmButtonText: "确定",
|
cancelButtonText: "取消",
|
type: "warning",
|
}
|
).then(() => {
|
delContacts([row.id]).then(res => {
|
if (res.code === 200) {
|
proxy.$modal.msgSuccess("删除成功");
|
getList();
|
}
|
});
|
});
|
};
|
|
// 批量删除
|
const handleDelete = () => {
|
if (selectedRows.value.length === 0) {
|
proxy.$modal.msgWarning("请选择要删除的数据");
|
return;
|
}
|
|
ElMessageBox.confirm(
|
`确定要删除选中的 ${selectedRows.value.length} 个联系人吗?`,
|
"提示",
|
{
|
confirmButtonText: "确定",
|
cancelButtonText: "取消",
|
type: "warning",
|
}
|
).then(() => {
|
const ids = selectedRows.value.map(row => row.id);
|
delContacts(ids).then(res => {
|
if (res.code === 200) {
|
proxy.$modal.msgSuccess("批量删除成功");
|
getList();
|
}
|
});
|
});
|
};
|
|
// 查看客户详情
|
const viewCustomerDetail = (row) => {
|
const customerIdStr = row.customerId;
|
if (!customerIdStr) {
|
proxy.$modal.msgError("该联系人未绑定客户");
|
return;
|
}
|
// 获取所有绑定的客户
|
const customerIds = customerIdStr.toString().split(',').map(id => Number(id.trim()));
|
const customers = customerIds.map(id => customerMap.value.get(id)).filter(item => item);
|
if (customers.length > 0) {
|
currentCustomerList.value = customers;
|
customerDetailVisible.value = true;
|
} else {
|
proxy.$modal.msgError("客户信息不存在");
|
}
|
};
|
|
// 关闭客户详情
|
const closeCustomerDetail = () => {
|
customerDetailVisible.value = false;
|
currentCustomerList.value = [];
|
};
|
|
onMounted(() => {
|
getList();
|
getCustomerList();
|
});
|
</script>
|
|
<style scoped>
|
.search_form {
|
display: flex;
|
justify-content: space-between;
|
align-items: center;
|
}
|
|
.search_title {
|
font-size: 14px;
|
color: #606266;
|
margin-right: 5px;
|
}
|
|
.customer-detail {
|
padding: 10px;
|
}
|
|
:deep(.el-descriptions__label) {
|
width: 120px;
|
font-weight: bold;
|
}
|
|
.customer-tags {
|
display: flex;
|
flex-wrap: wrap;
|
gap: 5px;
|
}
|
|
.customer-tag {
|
margin-right: 0;
|
}
|
</style>
|