zhangjq
2026-02-06 d1ec0e4d4c0031d2e686b1607d8f679747680189
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
<template>
  <div>
    <!--引用表格-->
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <!--插槽:table标题-->
      <template #tableTitle>
        <!-- <a-button  type="primary" v-auth="'copywriting:copywriting:exportXls'" preIcon="ant-design:export-outlined" @click="onExportXls"> 导出</a-button>
          <j-upload-button type="primary" v-auth="'copywriting:copywriting:importExcel'" preIcon="ant-design:import-outlined" @click="onImportXls">导入</j-upload-button> -->
 
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <template #overlay>
            <a-menu>
              <a-menu-item key="1" @click="batchHandleDelete">
                <Icon icon="ant-design:delete-outlined"></Icon>
                删除
              </a-menu-item>
            </a-menu>
          </template>
          <a-button v-auth="'copywriting:copywriting:deleteBatch'"
            >批量操作
            <Icon icon="mdi:chevron-down"></Icon>
          </a-button>
        </a-dropdown>
        
        <!-- 合同筛选文案按钮 -->
        <a-button type="primary" @click="handleContractFilter" style="margin-left: 8px;">
          合同筛选文案
        </a-button>
        
        <!-- 高级查询 -->
        <super-query :config="superQueryConfig" @search="handleSuperQuery" />
      </template>
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)" />
      </template>
      <!--字段回显插槽-->
      <template v-slot:bodyCell="{ column, record }">
        <template v-if="column.dataIndex === 'semanticWord.word'">
          {{ record.semanticWord?.word || '--' }}
        </template>
        <template v-else-if="column.dataIndex === 'title'">
          {{ record.title || '--' }}
        </template>
        <template v-else-if="column.dataIndex === 'semanticWord.lookTime'">
          {{ record.semanticWord?.lookTime || '--' }}
        </template>
        <template v-else-if="column.dataIndex === 'status_dictText'">
          {{ record.status_dictText || '--' }}
        </template>
        <template v-else-if="column.dataIndex === 'semanticWord.remark'">
          <!-- 修复:使用可选链操作符?.访问record.semanticWord.remark
               原因:当record.semanticWord为null或undefined时,直接访问其属性会导致错误
               修复前:{{ record.semanticWord.remark || '--' }}
               修复后:{{ record.semanticWord?.remark || '--' }}
               效果:即使record.semanticWord为null或undefined,也不会出现错误,而是显示'--' -->
          {{ record.semanticWord?.remark || '--' }}
        </template>
        <template v-else-if="column.dataIndex === 'semanticWord.ranking'">
          {{ record.semanticWord?.ranking || '--' }}
        </template>
        <template v-else-if="column.dataIndex === 'semanticWord.nowNo'">
          {{ record.semanticWord?.nowNo || '--' }}
        </template>
      </template>
    </BasicTable>
    <!-- 表单区域 -->
    <CopywritingModal @register="registerModal" @success="handleSuccess"></CopywritingModal>
    
    <!-- 合同筛选模态框 -->
    <a-modal
      v-model:visible="showContractFilterModal"
      title="选择合同筛选文案"
      width="800px"
      :maskClosable="false"
      @ok="handleConfirmContractFilter"
      @cancel="handleCancelContractFilter"
    >
      <div class="contract-filter-modal">
        <a-form layout="vertical">
          <a-form-item label="选择合同" required>
            <a-select
              v-model:value="selectedContractId"
              placeholder="请选择合同"
              style="width: 100%"
              show-search
              option-filter-prop="label"
              :loading="contractLoading"
            >
              <a-select-option v-for="contract in contractList" :key="contract.value" :value="contract.value">
                {{ contract.label }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-form>
      </div>
    </a-modal>
  </div>
</template>
 
<script lang="ts" name="copywriting-copywriting" setup>
  import { reactive, ref } from 'vue';
  import { BasicTable, TableAction } from '/@/components/Table';
  import { useModal } from '/@/components/Modal';
  import { useListPage } from '/@/hooks/system/useListPage';
  import { useRouter } from 'vue-router';
  import { message } from 'ant-design-vue';
  import CopywritingModal from './components/CopywritingModal.vue';
  import { columns, searchFormSchema, superQuerySchema } from './Copywriting.data';
  import { list, deleteOne, batchDelete, getImportUrl, getExportUrl } from './Copywriting.api';
  import { getDateByPicker } from '/@/utils';
  import { defHttp } from '/@/utils/http/axios';
  //日期个性化选择
  const fieldPickers = reactive({});
  const queryParam = reactive<any>({});
  // 获取路由实例
  const router = useRouter();
  //注册model
  const [registerModal, { openModal }] = useModal();
  
  // 合同筛选相关数据
  const showContractFilterModal = ref(false);
  const selectedContractId = ref('');
  const contractList = ref<any[]>([]);
  const contractLoading = ref(false);
  const contractListCache = ref([]);
  // 获取用户store
  //注册table数据
  const {
    tableContext,
    onExportXls: _onExportXls,
    onImportXls: _onImportXls,
  } = useListPage({
    tableProps: {
      title: '文案',
      api: list,
      columns,
      canResize: true,
      formConfig: {
        //labelWidth: 120,
        schemas: searchFormSchema,
        autoSubmitOnEnter: true,
        showAdvancedButton: true,
        fieldMapToNumber: [],
        fieldMapToTime: [],
        compact: true,
        baseColProps: {
          xs: 24,
          sm: 4,
          md: 4,
          lg: 4,
          xl: 4,
          xxl: 4,
        },
        rowProps: {
          gutter: 8,
        },
        actionColOptions: {
          span: 8,
          style: { textAlign: 'left' },
        },
      },
      actionColumn: {
        width: 120,
        fixed: 'right',
      },
      beforeFetch: (params) => {
        if (params && fieldPickers) {
          for (let key in fieldPickers) {
            if (params[key]) {
              params[key] = getDateByPicker(params[key], fieldPickers[key]);
            }
          }
        }
        const finalParams = Object.assign(params, queryParam);
        return finalParams;
      },
    },
    exportConfig: {
      name: '文案',
      url: getExportUrl,
      params: queryParam,
    },
    importConfig: {
      url: getImportUrl,
      success: handleSuccess,
    },
  });
 
  const [registerTable, { reload }, { rowSelection, selectedRowKeys }] = tableContext;
 
  // 高级查询配置
  const superQueryConfig = reactive(superQuerySchema);
 
  /**
   * 高级查询事件
   */
  function handleSuperQuery(params) {
    Object.keys(params).map((k) => {
      queryParam[k] = params[k];
    });
    reload();
  }
  //  /**
  //   * 新增事件
  //   */
  // function handleAdd() {
  //    openModal(true, {
  //      isUpdate: false,
  //      showFooter: true,
  //    });
  // }
  /**
   * 编辑事件 - 直接跳转到生成页面
   */
  function handleEdit(record: Recordable) {
    try {
      // 直接跳转到生成页面,传递当前页面的参数
      router.push({
        path: '/copywriting/generated/index',
        query: {
          semanticWord: record.semanticWord?.word || '',
          ranking: record.semanticWord?.ranking || '',
          outWord: record.semanticWord?.outWord || '',
          contractId: record.semanticWord?.contractId || '',
          id: record.id || '',
          title: record.title || '',
          text: record.text || '',
          fromEdit: 'true', // 添加标识,表示来自编辑按钮跳转
        },
      });
    } catch (error) {
      console.error('跳转失败:', error);
      message.error('跳转失败');
    }
  }
  /**
   * 详情
   */
  function handleDetail(record: Recordable) {
    openModal(true, {
      record,
      isUpdate: true,
      showFooter: false,
    });
  }
 
  /**
   * 删除事件
   */
  async function handleDelete(record) {
    await deleteOne({ id: record.id }, handleSuccess);
  }
  /**
   * 批量删除事件
   */
  async function batchHandleDelete() {
    await batchDelete({ ids: selectedRowKeys.value }, handleSuccess);
  }
  /**
   * 成功回调
   */
  function handleSuccess() {
    (selectedRowKeys.value = []) && reload();
  }
  /**
   * 操作栏
   */
  function getTableAction(record) {
    return [
      //  {
      //    label: '编辑',
      //    onClick: handleEdit.bind(null, record),
      //    auth: 'copywriting:copywriting:edit'
      //  },
      {
        label: '编辑',
        onClick: handleEdit.bind(null, record),
        auth: 'copywriting:copywriting:edit',
      },
    ];
  }
  /**
   * 下拉操作栏
   */
  function getDropDownAction(record) {
    return [
      {
        label: '详情',
        onClick: handleDetail.bind(null, record),
      },
      {
        label: '删除',
        popConfirm: {
          title: '是否确认删除',
          confirm: handleDelete.bind(null, record),
          placement: 'topLeft',
        },
        auth: 'copywriting:copywriting:delete',
      },
    ];
  }
  
  // 归一化合同记录
  const normalizeContractRecords = (response: any) => {
    if (Array.isArray(response)) {
      return response;
    }
    if (Array.isArray(response?.records)) {
      return response.records;
    }
    if (Array.isArray(response?.result?.records)) {
      return response.result.records;
    }
    if (Array.isArray(response?.data?.records)) {
      return response.data.records;
    }
    return [];
  };
  
  // 解析合同标签
  const resolveContractLabel = (item: any) => {
    if (item.contractName) return item.contractName;
    if (item.contractNo) return `合同编号:${item.contractNo}`;
    return '未命名合同';
  };
  
  // 解析合同值
  const resolveContractValue = (item: any) => {
    return item.id || item.contractId || item.contractName || '';
  };
  
  /**
   * 获取合同列表
   */
  async function getContractList() {
    contractLoading.value = true;
    try {
      const response = await defHttp.get({
        url: '/contract/contract/list',
        params: {
          column: 'createTime',
          order: 'desc',
          pageNo: 1,
          pageSize: 9999,
        },
      });
      
      const records = normalizeContractRecords(response);
      contractListCache.value = records;
      
      const options = records.map((item: any) => ({
        label: resolveContractLabel(item),
        value: resolveContractValue(item),
      }));
      
      contractList.value = options;
    } catch (error) {
      console.error('获取合同列表失败:', error);
      contractList.value = [];
      message.error('获取合同列表失败');
    } finally {
      contractLoading.value = false;
    }
  }
  
  /**
   * 打开合同筛选模态框
   */
  async function handleContractFilter() {
    // 先获取合同列表
    await getContractList();
    // 重置选中状态
    selectedContractId.value = '';
    // 显示模态框
    showContractFilterModal.value = true;
  }
  
  /**
   * 确认合同筛选
   */
  function handleConfirmContractFilter() {
    if (!selectedContractId.value) {
      message.warning('请选择合同');
      return;
    }
    
    console.log('选择的合同ID:', selectedContractId.value);
    // 将合同ID添加到查询参数中
    queryParam['contractId'] = selectedContractId.value;
    console.log('查询参数:', queryParam);
    // 刷新表格数据
    reload();
    // 关闭模态框
    showContractFilterModal.value = false;
  }
  
  /**
   * 取消合同筛选
   */
  function handleCancelContractFilter() {
    // 重置选中状态
    selectedContractId.value = '';
    // 关闭模态框
    showContractFilterModal.value = false;
  }
</script>
 
<style lang="less" scoped>
  :deep(.ant-picker),
  :deep(.ant-input-number) {
    width: 100%;
  }
 
  /* 确保搜索表单完全靠左对齐,消除所有左边空隙 */
  :deep(.basic-table-search) {
    padding-left: 0 !important;
    margin-left: 0 !important;
  }
  
  /* 消除表单容器的左边空隙 */
  :deep(.ant-form) {
    margin-left: 0 !important;
    padding-left: 0 !important;
  }
  
  /* 消除行容器的左边空隙 */
  :deep(.ant-form-item-row) {
    margin-left: 0 !important;
  }
  
  /* 消除表单项的左边空隙 */
  :deep(.ant-form-item) {
    margin-left: 0 !important;
  }
</style>