ny
23 小时以前 282fbc6488f4e8ceb5fda759f963ee88fbf7b999
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
<script lang="ts" setup>
import type { TreeActionType } from '@jnpf/ui';
import type { BasicColumn } from '@jnpf/ui/vxeTable';
import type { FormInstance } from 'ant-design-vue';
 
import { computed, nextTick, reactive, ref, toRefs, unref, watch } from 'vue';
 
import { BasicLeftTree } from '@jnpf/ui';
import { ModalClose } from '@jnpf/ui/modal';
 
import { Modal as AModal, Form } from 'ant-design-vue';
import { cloneDeep, pick } from 'lodash-es';
 
import { getAuthSystemList } from '#/api/system/system';
import { getFlowEngineListByIds, getFlowSelector } from '#/api/workFlow/template';
import { $t } from '#/locales';
import { useBaseStore } from '#/store';
 
interface State {
  list: any[];
  listQuery: any;
  loading: boolean;
  total: number;
  selectedRowKeys: any[];
  selectedRows: any[];
  innerValue: string | string[] | undefined;
  options: any[];
  visible: boolean;
  treeLoading: boolean;
  treeData: any[];
  selectRow: any;
}
 
defineOptions({ inheritAttrs: false });
const props = defineProps({
  value: { default: [] },
  popupTitle: { type: String, default: '选择流程' },
  placeholder: { type: String, default: '请选择' },
  disabled: { type: Boolean, default: false },
  allowClear: { type: Boolean, default: true },
  multiple: { type: Boolean, default: true },
  size: { type: String, default: 'default' },
  entrustType: { type: Number, default: 0 },
  isGlobal: { type: Boolean, default: true },
});
const emit = defineEmits(['update:value', 'change']);
const state = reactive<State>({
  list: [],
  listQuery: {
    keyword: '',
    currentPage: 1,
    pageSize: 20,
  },
  loading: false,
  total: 0,
  selectedRowKeys: [],
  selectedRows: [],
  innerValue: undefined,
  options: [],
  visible: false,
  treeLoading: false,
  treeData: [],
  selectRow: null,
});
const { innerValue, options, visible, treeLoading, treeData, selectRow } = toRefs(state);
const formItemContext = Form.useInjectFormItemContext();
const baseStore = useBaseStore();
const columns: BasicColumn[] = [
  { title: '流程名称', dataIndex: 'fullName' },
  { title: '流程编码', dataIndex: 'enCode' },
];
const searchInfo = reactive({
  category: '',
  systemId: '',
  isDelegate: 1,
  isAuthority: 1,
  delegateUser: '',
});
const leftTreeRef = ref<Nullable<TreeActionType>>(null);
const formElRef = ref<FormInstance>();
const tableElRef = ref<any>(null);
const scrollHeight = window.innerHeight * 0.7 - 52 - 39 - 56;
 
const getSelectBindValue = computed(() => {
  return {
    ...pick(props, ['disabled', 'size', 'allowClear', 'placeholder']),
    fieldNames: { label: 'fullName', value: 'id' },
    open: false,
    mode: props.multiple ? 'multiple' : '',
    showSearch: false,
    showArrow: true,
  };
});
const getRowSelection = computed<any>(() => ({
  type: props.multiple ? 'checkbox' : 'radio',
  selectedRowKeys: state.selectedRowKeys,
  onChange: setSelectedRowKeys,
}));
const getTableBindValues = computed(() => {
  return {
    columns,
    rowSelection: unref(getRowSelection),
    customRow,
    loading: state.loading,
    size: 'small',
    rowKey: 'id',
    scroll: {
      y: scrollHeight,
    },
    pagination: {
      current: state.listQuery.currentPage,
      pageSize: state.listQuery.pageSize,
      size: 'small',
      defaultPageSize: state.listQuery.pageSize,
      showTotal: (total) => $t('component.table.total', { total }),
      showSizeChanger: true,
      pageSizeOptions: ['20', '50', '80', '100'],
      showQuickJumper: true,
      total: state.total,
    },
    class: 'jnpf-basic-table',
  };
});
 
watch(
  () => props.value,
  () => {
    setValue();
  },
  { immediate: true },
);
 
function customRow(record: Recordable) {
  return {
    onClick: (e: Event) => {
      e?.stopPropagation();
      const tr: HTMLElement = (e as MouseEvent).composedPath?.().find((dom: any) => dom.tagName === 'TR') as HTMLElement;
      if (!tr) return;
      const isCheckbox = unref(getTableBindValues).rowSelection.type === 'checkbox';
      const key = record[unref(getTableBindValues).rowKey];
      if (isCheckbox) {
        // 找到Checkbox,检查是否为disabled
        const checkBox = tr.querySelector('input[type=checkbox]');
        if (!checkBox || checkBox.hasAttribute('disabled')) return;
        if (state.selectedRowKeys.includes(key)) {
          const keyIndex = state.selectedRowKeys.indexOf(key);
          state.selectedRowKeys.splice(keyIndex, 1);
          state.selectedRows = state.selectedRows.filter((o) => o[unref(getTableBindValues).rowKey] !== key);
        } else {
          state.selectedRowKeys.push(key);
          state.selectedRows.push(record);
        }
      } else {
        // 找到radio,检查是否为disabled
        const radio = tr.querySelector('input[type=radio]');
        if (!radio || radio.hasAttribute('disabled')) return;
        state.selectedRowKeys = [key];
        state.selectedRows = [record];
      }
    },
  };
}
function setValue() {
  if (!props.value || !props.value.length) {
    state.innerValue = [];
    state.options = [];
    state.selectRow = null;
    state.selectedRowKeys = [];
    state.selectedRows = [];
    return;
  }
  const ids = props.value as any[];
  getFlowEngineListByIds(ids).then((res) => {
    const selectedList: any[] = res.data;
    const innerIds = selectedList.map((o) => o.id);
    state.innerValue = innerIds;
    state.options = cloneDeep(selectedList);
    state.selectRow = cloneDeep(selectedList);
    state.selectedRowKeys = innerIds;
    state.selectedRows = cloneDeep(selectedList);
  });
}
function onChange(val, option) {
  if (!val || !val.length) {
    state.options = [];
    emit('update:value', []);
    emit('change', '', []);
    state.selectedRowKeys = [];
    state.selectedRows = [];
  } else {
    state.options = option;
    emit('update:value', val);
    emit('change', '', state.options);
  }
  formItemContext.onFieldChange();
}
async function openSelectModal() {
  if (props.disabled) return;
  state.visible = true;
  getCategoryData();
}
async function getCategoryData() {
  const firstItem = { id: '', encode: 'all', fullName: '全部流程' };
  if (props.isGlobal) {
    state.treeLoading = true;
    const res = await getAuthSystemList();
    if (!res) return (state.treeLoading = false);
    state.treeData = [firstItem, ...(res.data || [])];
    state.treeLoading = false;
    searchInfo.category = '';
    searchInfo.systemId = state.treeData[0].id;
  } else {
    const res = (await baseStore.getDictionaryData('businessType')) as any[];
    state.treeData = [firstItem, ...res];
    searchInfo.category = state.treeData[0].id;
    searchInfo.systemId = '';
  }
  searchInfo.isAuthority = props.entrustType === 0 ? 1 : 0;
  const leftTree = unref(leftTreeRef);
  leftTree?.setSelectedKeys([searchInfo.category]);
  state.loading = true;
  handleReset();
  const tableEl = tableElRef.value?.$el;
  const bodyEl = tableEl.querySelector('.ant-table-body');
  bodyEl!.style.height = `${scrollHeight}px`;
}
function initData() {
  state.loading = true;
  const query = {
    ...state.listQuery,
    ...unref(searchInfo),
    isLaunch: 1,
  };
  getFlowSelector(query)
    .then((res) => {
      state.list = res.data.list || [];
      state.total = res.data.pagination.total;
      state.loading = false;
    })
    .catch(() => {
      state.loading = false;
    });
}
function handleTreeSelect(id) {
  if (props.isGlobal) {
    if (searchInfo.systemId === id) return;
    searchInfo.systemId = id;
  } else {
    if (searchInfo.category === id) return;
    searchInfo.category = id;
  }
  nextTick(() => initData());
}
function handleCancel() {
  state.visible = false;
}
function getForm() {
  const form = unref(formElRef);
  if (!form) {
    throw new Error('form is null!');
  }
  return form;
}
function handleSearch() {
  state.listQuery.currentPage = 1;
  state.listQuery.pageSize = 20;
  updateSelectRow();
  nextTick(() => initData());
}
function handleReset() {
  getForm().resetFields();
  state.listQuery.keyword = '';
  handleSearch();
}
function setSelectedRowKeys(selectedRowKeys, selectedRows) {
  state.selectedRowKeys = selectedRowKeys;
  state.selectedRows = selectedRows;
}
function handleTableChange(pagination) {
  state.listQuery.currentPage = pagination.current;
  state.listQuery.pageSize = pagination.pageSize;
  updateSelectRow();
  nextTick(() => initData());
}
function updateSelectRow() {
  if (!state.selectRow) state.selectRow = [];
  const newSelectRow = state.selectedRows;
  for (const item of newSelectRow) {
    if (!state.selectRow.some((o) => o.id === item.id)) state.selectRow.push(item);
  }
  state.selectRow = state.selectRow.filter((o) => !(state.list.some((l) => l.id === o.id) && !newSelectRow.some((l) => l.id === o.id)));
}
function handleSubmit() {
  updateSelectRow();
  state.options = state.selectRow;
  state.innerValue = unref(selectRow).map((o) => o.id);
  emit('update:value', unref(innerValue));
  emit('change', unref(innerValue), unref(selectRow));
  formItemContext.onFieldChange();
  handleCancel();
}
</script>
 
<template>
  <div class="common-container">
    <a-select v-model:value="innerValue" v-bind="getSelectBindValue" :options="options" @change="onChange" @click="openSelectModal" />
    <AModal
      v-model:open="visible"
      :title="popupTitle"
      :width="1000"
      class="common-container-modal"
      @ok="handleSubmit"
      @cancel="handleCancel"
      :mask-closable="false">
      <template #closeIcon>
        <ModalClose :can-fullscreen="false" @cancel="handleCancel" />
      </template>
      <div class="jnpf-content-wrapper">
        <div class="jnpf-content-wrapper-left">
          <BasicLeftTree ref="leftTreeRef" :show-search="false" :tree-data="treeData" :loading="treeLoading" @select="handleTreeSelect" />
        </div>
        <div class="jnpf-content-wrapper-center">
          <div class="jnpf-content-wrapper-content">
            <div class="jnpf-common-search-box jnpf-common-search-box-modal">
              <a-form :colon="false" label-align="right" :model="state.listQuery" ref="formElRef">
                <a-row :gutter="10">
                  <a-col :span="8">
                    <a-form-item :label="$t('common.keyword')" name="keyword">
                      <a-input v-model:value="state.listQuery.keyword" :placeholder="$t('common.enterKeyword')" allow-clear @press-enter="handleSearch" />
                    </a-form-item>
                  </a-col>
                  <a-col :span="8">
                    <a-form-item label=" ">
                      <a-button type="primary" class="mr-2" @click="handleSearch">{{ $t('common.queryText') }}</a-button>
                      <a-button @click="handleReset">{{ $t('common.resetText') }}</a-button>
                    </a-form-item>
                  </a-col>
                </a-row>
              </a-form>
            </div>
            <a-table :data-source="state.list" v-bind="getTableBindValues" @change="handleTableChange" ref="tableElRef" />
          </div>
        </div>
      </div>
    </AModal>
  </div>
</template>