刘光辉
11 小时以前 0dfe84494048ce27ba8449831782128412d3eb13
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
<script lang="ts" setup>
import { nextTick, reactive } from 'vue';
 
import { useMessage } from '@jnpf/hooks';
import { BasicPopup, usePopupInner } from '@jnpf/ui/popup';
import { createAsyncComponent } from '@jnpf/utils';
 
import { getConfigData } from '#/api/onlineDev/visualDev';
import { getFlowStartFormId } from '#/api/workFlow/template';
import { router } from '#/router';
import { useBaseStore } from '#/store';
import { resolveOpenFlowListConfig } from '#/utils/jnpf';
 
const DynamicList = createAsyncComponent(() => import('#/views/common/dynamicModel/list/index.vue'));
 
defineOptions({ name: 'GlobalFlowListPopup' });
defineEmits(['register']);
 
interface OpenFlowListConfig {
  flow_id?: number | string;
  flowId?: number | string;
  menuId?: number | string;
  params?: Record<string, any>;
  path?: string;
  query?: Record<string, any>;
  routeQuery?: Record<string, any>;
  title?: string;
}
 
interface FlowListState {
  config: any;
  externalParams: Record<string, any>;
  flowId: string;
  key: number;
  loading: boolean;
  menuId: string;
  modelId: string;
  ready: boolean;
  title: string;
}
 
const baseStore = useBaseStore();
const { createMessage } = useMessage();
const [registerPopup, { closePopup }] = usePopupInner(init);
const state = reactive<FlowListState>({
  config: {},
  externalParams: {},
  flowId: '',
  key: Date.now(),
  loading: false,
  menuId: '',
  modelId: '',
  ready: false,
  title: '流程列表',
});
 
function parseRouteListQuery(value) {
  if (!value || typeof value !== 'string') return {};
  try {
    const data = JSON.parse(value);
    return data && typeof data === 'object' && !Array.isArray(data) ? data : {};
  } catch {
    return {};
  }
}
 
function parsePathQuery(path = '') {
  const query: Record<string, any> = {};
  const queryString = path.includes('?') ? path.split('?')[1] : '';
  if (!queryString) return query;
  const params = new URLSearchParams(queryString);
  params.forEach((value, key) => {
    query[key] = value;
  });
  return query;
}
 
function getExternalParams(config: OpenFlowListConfig, menuQuery: Record<string, any> = {}) {
  const params = config.params || config.query || {};
  const routeQuery = { ...parsePathQuery(config.path), ...config.routeQuery };
  const listQueryParams = parseRouteListQuery(routeQuery.jnpfListQuery);
  delete routeQuery.jnpfListQuery;
  return { ...menuQuery, ...routeQuery, ...listQueryParams, ...(params && typeof params === 'object' && !Array.isArray(params) ? params : {}) };
}
 
function getRouteInfo(config: OpenFlowListConfig) {
  const routes = router.getRoutes();
  let target: any = null;
  const configFlowId = config.flowId ?? config.flow_id;
  if (config.path) {
    const path = config.path.split('?')[0];
    target = routes.find((o) => o.path === path);
  }
  if (!target && config.menuId) target = routes.find((o) => String(o.meta?.modelId || '') === String(config.menuId));
  if (!target && configFlowId) {
    target = routes.find((o) => String(o.meta?.type || '') === '9' && String(o.meta?.relationId || '') === String(configFlowId));
  }
  if (!target) return null;
  return {
    flowId: String(target.meta?.relationId || configFlowId || ''),
    menuId: String(target.meta?.modelId || config.menuId || ''),
    query: target.meta?.query && typeof target.meta.query === 'object' && !Array.isArray(target.meta.query) ? { ...target.meta.query } : {},
    title: target.meta?.title,
    type: target.meta?.type,
  };
}
 
async function getTargetConfig(config: OpenFlowListConfig) {
  const resolvedConfig = await resolveOpenFlowListConfig(config);
  const routeInfo = getRouteInfo(resolvedConfig);
  const flowId = String(resolvedConfig.flowId ?? resolvedConfig.flow_id ?? routeInfo?.flowId ?? '');
  if (!flowId) throw new Error('未找到目标流程菜单');
  if (routeInfo && String(routeInfo.type) !== '9') throw new Error('目标不是流程列表页面');
 
  const formRes = await getFlowStartFormId(flowId);
  const modelId = formRes?.data?.formId;
  if (!modelId) throw new Error('未找到流程发起表单');
 
  const configRes = await getConfigData(modelId, { onlineUtilsOpen: true });
  const listConfig = configRes.data;
  if (!listConfig) throw new Error('未找到流程列表配置');
  if (listConfig.webType == '1' || listConfig.webType == 1) throw new Error('目标不是列表页面');
 
  return {
    config: { ...listConfig, enableFlow: 1, flowId },
    flowId,
    menuId: routeInfo?.menuId || String(resolvedConfig.menuId || flowId),
    modelId: String(modelId),
    query: routeInfo?.query || {},
    title: routeInfo?.title,
  };
}
 
async function init(config: OpenFlowListConfig) {
  state.ready = false;
  state.loading = true;
  state.config = {};
  state.flowId = '';
  state.menuId = '';
  state.modelId = '';
  state.externalParams = {};
  try {
    await baseStore.getDictionaryAll();
    const target = await getTargetConfig(config);
    state.config = target.config;
    state.flowId = target.flowId;
    state.modelId = target.modelId;
    state.menuId = target.menuId;
    state.title = config.title || target.title || target.config.fullName || '流程列表';
    state.externalParams = getExternalParams(config, target.query);
    state.key = Date.now();
    state.ready = true;
    await nextTick();
  } catch (error: any) {
    console.error('[onlineUtils.openFlowList] open flow list modal failed:', error);
    createMessage.warning(error?.message || '打开流程列表失败');
    closePopup();
  } finally {
    state.loading = false;
  }
}
 
function handleClose() {
  state.ready = false;
  state.config = {};
  return Promise.resolve(true);
}
</script>
 
<template>
  <BasicPopup v-bind="$attrs" @register="registerPopup" destroy-on-close :show-ok-btn="false" :close-func="handleClose" class="full-popup global-flow-list-popup">
    <template #title>
      <div class="text-[16px] font-medium">{{ state.title }}</div>
    </template>
    <div class="jnpf-common-form-wrapper" v-loading="state.loading">
      <div class="jnpf-common-form-wrapper__main">
        <DynamicList
          v-if="state.ready"
          :key="state.key"
          :config="state.config"
          :model-id="state.modelId"
          :menu-id="state.menuId"
          is-online-utils-open
          :external-params="state.externalParams" />
      </div>
    </div>
  </BasicPopup>
</template>