办学质量监测教学评价系统
shenrongliang
2025-06-13 11d86cc6c26bb4f709e407acadf4805c2024e79f
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
<script setup lang="ts">
import type { LeaveVO } from './api/model';
 
import type { StartWorkFlowReqData } from '#/api/workflow/task/model';
 
import { computed, onMounted, ref, useTemplateRef } from 'vue';
import { useRoute, useRouter } from 'vue-router';
 
import { useVbenModal } from '@vben/common-ui';
 
import { Card } from 'ant-design-vue';
import dayjs from 'dayjs';
import { cloneDeep, omit } from 'lodash-es';
 
import { useVbenForm } from '#/adapter/form';
import { startWorkFlow } from '#/api/workflow/task';
 
import { applyModal } from '../components';
import { leaveAdd, leaveInfo, leaveUpdate } from './api';
import { modalSchema } from './data';
import LeaveDescription from './leave-description.vue';
 
const route = useRoute();
const readonly = route.query?.readonly === 'true';
const id = route.query?.id as string;
 
/**
 * id存在&readonly时候
 */
const showActionBtn = computed(() => {
  return !readonly;
});
 
const [BasicForm, formApi] = useVbenForm({
  commonConfig: {
    // 默认占满两列
    formItemClass: 'col-span-2',
    // 默认label宽度 px
    labelWidth: 100,
    // 通用配置项 会影响到所有表单项
    componentProps: {
      class: 'w-full',
      disabled: readonly,
    },
  },
  schema: modalSchema(!readonly),
  showDefaultActions: false,
  wrapperClass: 'grid-cols-2',
});
 
const leaveDescription = ref<LeaveVO>();
const showDescription = computed(() => {
  return readonly && leaveDescription.value;
});
const cardRef = useTemplateRef('cardRef');
onMounted(async () => {
  // 只读 获取信息赋值
  if (id) {
    const resp = await leaveInfo(id);
    leaveDescription.value = resp;
    await formApi.setValues(resp);
    const dateRange = [dayjs(resp.startDate), dayjs(resp.endDate)];
    await formApi.setFieldValue('dateRange', dateRange);
 
    /**
     * window.parent(最近的上一级父页面)
     * 主要解决内嵌iframe卡顿的问题
     */
    if (readonly) {
      // 渲染完毕才显示表单
      window.parent.postMessage({ type: 'mounted' }, '*');
      // 获取表单高度 内嵌时保持一致
      setTimeout(() => {
        const el = cardRef.value?.$el as HTMLDivElement;
        // 获取高度
        const height = el?.offsetHeight ?? 0;
        if (height) {
          window.parent.postMessage({ type: 'height', height }, '*');
        }
      });
    }
  }
});
 
const router = useRouter();
 
/**
 * 提取通用逻辑
 */
async function handleSaveOrUpdate() {
  const { valid } = await formApi.validate();
  if (!valid) {
    return;
  }
  let data = cloneDeep(await formApi.getValues()) as any;
  data = omit(data, 'flowType');
  // 处理日期
  data.startDate = dayjs(data.dateRange[0]).format('YYYY-MM-DD HH:mm:ss');
  data.endDate = dayjs(data.dateRange[1]).format('YYYY-MM-DD HH:mm:ss');
  if (id) {
    data.id = id;
    return await leaveUpdate(data);
  } else {
    return await leaveAdd(data);
  }
}
 
const [ApplyModal, applyModalApi] = useVbenModal({
  connectedComponent: applyModal,
});
/**
 * 暂存 草稿状态
 */
async function handleTempSave() {
  try {
    await handleSaveOrUpdate();
    router.push('/demo/leave');
  } catch (error) {
    console.error(error);
  }
}
 
/**
 * 保存业务 & 发起流程
 */
async function handleStartWorkFlow() {
  try {
    // 保存业务
    const leaveResp = await handleSaveOrUpdate();
    // 启动流程
    const taskVariables = {
      leaveDays: leaveResp!.leaveDays,
      userList: ['1', '3', '4'],
    };
    const formValues = await formApi.getValues();
    const flowCode = formValues?.flowType ?? 'leave1';
    const startWorkFlowData: StartWorkFlowReqData = {
      businessId: leaveResp!.id,
      flowCode,
      variables: taskVariables,
    };
    const { taskId } = await startWorkFlow(startWorkFlowData);
    // 打开窗口
    applyModalApi.setData({
      taskId,
      taskVariables,
      variables: {},
    });
    applyModalApi.open();
  } catch (error) {
    console.error(error);
  }
}
 
function handleComplete() {
  formApi.resetForm();
  router.push('/demo/leave');
}
 
/**
 * 显示详情时 需要较小的padding
 */
const cardSize = computed(() => {
  return showDescription.value ? 'small' : 'default';
});
</script>
 
<template>
  <Card ref="cardRef" :size="cardSize">
    <div id="leave-form">
      <!-- 使用v-if会影响生命周期 -->
      <BasicForm v-show="!showDescription" />
      <LeaveDescription v-if="showDescription" :data="leaveDescription!" />
      <div v-if="showActionBtn" class="flex justify-end gap-2">
        <a-button @click="handleTempSave">暂存</a-button>
        <a-button type="primary" @click="handleStartWorkFlow">提交</a-button>
      </div>
      <ApplyModal @complete="handleComplete" />
    </div>
  </Card>
</template>
 
<style lang="scss">
html:has(#leave-form) {
  /**
  去除顶部进度条样式
  */
  #nprogress {
    display: none;
  }
}
</style>