yyb
2026-05-08 eef2bdb568e03f1cfb1deba99fb36c85f2086e42
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
<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>