gaoluyang
2026-06-29 27cd042df9aca0383a49f3514bc21958dd890912
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
<!-- 商机列表:用于【客户】【联系人】详情中,展示其关联的商机列表 -->
<script lang="ts" setup>
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { CrmBusinessApi } from '#/api/crm/business';
import type { CrmContactApi } from '#/api/crm/contact';
 
import { ref } from 'vue';
import { useRouter } from 'vue-router';
 
import { confirm, useVbenModal } from '../../../../packages/effects/common-ui/src';
 
import { Button, message } from 'ant-design-vue';
 
import { ACTION_ICON, TableAction, useVbenVxeGrid } from '#/adapter/vxe-table';
import {
  getBusinessPageByContact,
  getBusinessPageByCustomer,
} from '#/api/crm/business';
import {
  createContactBusinessList,
  deleteContactBusinessList,
} from '#/api/crm/contact';
import { BizTypeEnum } from '#/api/crm/permission';
import { $t } from '#/locales';
 
import Form from '../modules/form.vue';
import { useBusinessDetailListColumns } from './data';
import ListModal from './detail-list-modal.vue';
 
const props = defineProps<{
  bizId: number; // 业务编号
  bizType: number; // 业务类型
  contactId?: number; // 特殊:联系人编号;在【联系人】详情中,可以传递联系人编号,默认新建的商机关联到该联系人
  customerId?: number; // 关联联系人与商机时,需要传入 customerId 进行筛选
}>();
 
const { push } = useRouter();
 
const [FormModal, formModalApi] = useVbenModal({
  connectedComponent: Form,
  destroyOnClose: true,
});
 
const [DetailListModal, detailListModalApi] = useVbenModal({
  connectedComponent: ListModal,
  destroyOnClose: true,
});
 
const checkedRows = ref<CrmBusinessApi.Business[]>([]);
function setCheckedRows({ records }: { records: CrmBusinessApi.Business[] }) {
  checkedRows.value = records;
}
 
/** 刷新表格 */
function handleRefresh() {
  gridApi.query();
}
 
/** 创建商机 */
function handleCreate() {
  formModalApi
    .setData({ customerId: props.customerId, contactId: props.contactId })
    .open();
}
 
/** 关联商机 */
function handleCreateBusiness() {
  detailListModalApi.setData({ customerId: props.customerId }).open();
}
 
/** 解除商机关联 */
async function handleDeleteContactBusinessList() {
  if (checkedRows.value.length === 0) {
    message.error('请先选择商机后操作!');
    return;
  }
  try {
    await confirm(
      `确定要将${checkedRows.value.map((item) => item.name).join(',')}解除关联吗?`,
    );
  } catch {
    return false;
  }
  const res = await deleteContactBusinessList({
    contactId: props.bizId,
    businessIds: checkedRows.value.map((item) => item.id),
  });
  if (!res) {
    throw new Error($t('ui.actionMessage.operationFailed'));
  }
  // 提示并返回成功
  message.success($t('ui.actionMessage.operationSuccess'));
  handleRefresh();
  return true;
}
 
/** 查看商机详情 */
function handleDetail(row: CrmBusinessApi.Business) {
  push({ name: 'CrmBusinessDetail', params: { id: row.id } });
}
 
/** 查看客户详情 */
function handleCustomerDetail(row: CrmBusinessApi.Business) {
  push({ name: 'CrmCustomerDetail', params: { id: row.customerId } });
}
 
/** 创建联系人关联的商机 */
async function handleCreateContactBusinessList(businessIds: number[]) {
  const data = {
    contactId: props.bizId,
    businessIds,
  } as CrmContactApi.ContactBusinessReqVO;
  await createContactBusinessList(data);
  handleRefresh();
}
 
/** 商机关联表格 */
const [Grid, gridApi] = useVbenVxeGrid({
  gridOptions: {
    columns: useBusinessDetailListColumns(),
    height: 600,
    keepSource: true,
    proxyConfig: {
      ajax: {
        query: async ({ page }, formValues) => {
          if (props.bizType === BizTypeEnum.CRM_CUSTOMER) {
            return await getBusinessPageByCustomer({
              pageNo: page.currentPage,
              pageSize: page.pageSize,
              customerId: props.customerId,
              ...formValues,
            });
          } else if (props.bizType === BizTypeEnum.CRM_CONTACT) {
            return await getBusinessPageByContact({
              pageNo: page.currentPage,
              pageSize: page.pageSize,
              contactId: props.contactId,
              ...formValues,
            });
          } else {
            return [];
          }
        },
      },
    },
    rowConfig: {
      keyField: 'id',
      isHover: true,
    },
    toolbarConfig: {
      refresh: true,
      search: true,
    },
  } as VxeTableGridOptions<CrmBusinessApi.Business>,
  gridEvents: {
    checkboxAll: setCheckedRows,
    checkboxChange: setCheckedRows,
  },
});
</script>
 
<template>
  <div>
    <FormModal @success="handleRefresh" />
    <DetailListModal
      :customer-id="customerId"
      @success="handleCreateContactBusinessList"
    />
    <Grid>
      <template #toolbar-tools>
        <TableAction
          :actions="[
            {
              label: $t('ui.actionTitle.create', ['商机']),
              type: 'primary',
              icon: ACTION_ICON.ADD,
              auth: ['crm:business:create'],
              onClick: handleCreate,
            },
            {
              label: '关联',
              icon: ACTION_ICON.ADD,
              type: 'default',
              auth: ['crm:contact:create-business'],
              ifShow: () => !!contactId,
              onClick: handleCreateBusiness,
            },
            {
              label: '解除关联',
              icon: ACTION_ICON.ADD,
              type: 'default',
              auth: ['crm:contact:create-business'],
              ifShow: () => !!contactId,
              onClick: handleDeleteContactBusinessList,
            },
          ]"
        />
      </template>
      <template #name="{ row }">
        <Button type="link" @click="handleDetail(row)">
          {{ row.name }}
        </Button>
      </template>
      <template #customerName="{ row }">
        <Button type="link" @click="handleCustomerDetail(row)">
          {{ row.customerName }}
        </Button>
      </template>
    </Grid>
  </div>
</template>