index.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. <template>
  2. <div class="h-full flex flex-col gap-y-3">
  3. <n-page-header @back="$router.back">
  4. <template #title> 报告生成 </template>
  5. <template #extra>
  6. <n-space>
  7. <n-popconfirm @positive-click="putTemplateData">
  8. <template #trigger>
  9. <n-button type="primary">生成报告</n-button>
  10. </template>
  11. 确认生成报告?
  12. </n-popconfirm>
  13. <n-button type="primary" @click="showUploadModal"
  14. >上传验证报告</n-button
  15. >
  16. </n-space>
  17. </template>
  18. </n-page-header>
  19. <n-scrollbar :style="{ maxHeight: `${height - 150}px` }" trigger="none">
  20. <FormList :form-list="formValue.formList" />
  21. <!-- <n-button @click="handleClick">button</n-button> -->
  22. </n-scrollbar>
  23. </div>
  24. <n-modal
  25. style="width: 30%"
  26. v-model:show="modal.showModal"
  27. :show-icon="false"
  28. preset="dialog"
  29. :mask-closable="false"
  30. :title="modal.title"
  31. @close="onCloseModal"
  32. >
  33. <n-upload
  34. class="w-full"
  35. list-type="image-card"
  36. :default-upload="false"
  37. :max="1"
  38. @change="handleChange"
  39. v-if="modal.title === '上传验证'"
  40. >
  41. 上传文件
  42. </n-upload>
  43. <template v-else>
  44. <n-card embedded :bordered="false">
  45. <template #header>
  46. <div class="flex justify-center">
  47. <n-progress
  48. :status="
  49. keyInfo.Code === 600
  50. ? 'default'
  51. : keyInfo.Code === 601
  52. ? 'success'
  53. : 'error'
  54. "
  55. type="dashboard"
  56. gap-position="bottom"
  57. :percentage="keyInfo.Schedule"
  58. />
  59. </div>
  60. </template>
  61. <n-scrollbar
  62. style="max-height: 300px"
  63. trigger="none"
  64. ref="scrollbarRef"
  65. >
  66. <div class="flex" v-for="(item, index) of items" :key="index">
  67. <n-badge class="mr-5" type="info" :value="index + 1" />
  68. <span>{{ item }}</span>
  69. </div>
  70. </n-scrollbar>
  71. </n-card>
  72. </template>
  73. <template #action>
  74. <n-space v-if="modal.title === '上传验证'">
  75. <n-button @click="modal.showModal = false">取消</n-button>
  76. <n-button type="primary" @click="editTaskInfo">确认</n-button>
  77. </n-space>
  78. <n-button
  79. v-else
  80. type="primary"
  81. :disabled="keyInfo.Code !== 601"
  82. @click="handleDownload"
  83. >下载文件</n-button
  84. >
  85. </template>
  86. </n-modal>
  87. </template>
  88. <script setup>
  89. import {
  90. putVerifyTemplateMapData,
  91. editTask,
  92. getTaskDataClassList,
  93. getVerifyTemplateMapDataList,
  94. generateReport,
  95. generateKey,
  96. } from '@/api';
  97. import * as qiniu from 'qiniu-js';
  98. import { getToken } from '@/utils/storage/sessionToken';
  99. import { getFileToken } from '@/common';
  100. import { useWindowSize } from '@vueuse/core';
  101. import { NButton } from 'naive-ui';
  102. import { onBeforeUnmount } from 'vue';
  103. import FormList from '@/components/FormList.vue';
  104. const { height } = useWindowSize();
  105. const message = useMessage();
  106. const scrollbarRef = ref(null);
  107. const task = window.sessionStorage.getItem('task')
  108. ? JSON.parse(window.sessionStorage.getItem('task'))
  109. : {};
  110. // 表单对象
  111. const formValue = reactive({
  112. pdf: '',
  113. formList: [],
  114. });
  115. const handleClick = () => {
  116. console.log(formValue.formList);
  117. };
  118. // 查询数据
  119. const queryData = reactive({
  120. T_source: 2,
  121. T_task_id: task.T_task_id,
  122. T_VerifyTemplate_id: task.T_VerifyTemplate_id,
  123. });
  124. //
  125. const classList = ref([]);
  126. // 模态框数据源
  127. const modal = reactive({
  128. showModal: false,
  129. title: '',
  130. });
  131. //
  132. const handleChange = async ({ file }) => {
  133. const token = await getFileToken(file.name.split('.')[1]);
  134. const observable = qiniu.upload(
  135. file.file,
  136. file.name,
  137. token,
  138. {},
  139. {
  140. useCdnDomain: true,
  141. }
  142. );
  143. observable.subscribe({
  144. next: (result) => {
  145. // 主要用来展示进度
  146. console.warn(result);
  147. },
  148. error: () => {
  149. message.error('上传失败');
  150. },
  151. complete: (res) => {
  152. formValue.pdf = res.key;
  153. },
  154. });
  155. };
  156. // 显示上传验证对话框
  157. const showUploadModal = () => {
  158. modal.title = '上传验证';
  159. modal.showModal = true;
  160. };
  161. // 上传验证
  162. const editTaskInfo = async () => {
  163. try {
  164. const { data: res } = await editTask({
  165. T_task_id: queryData.T_task_id,
  166. T_pdf2: formValue.pdf,
  167. });
  168. if (res.Code === 200) {
  169. message.success(res.Msg);
  170. modal.showModal = false;
  171. }
  172. } catch (e) {
  173. console.log(e);
  174. }
  175. };
  176. // 验证报告生成 报告
  177. const generateReportInfo = async (obj) => {
  178. const { data: res } = await generateReport({
  179. T_task_id: task.T_task_id,
  180. T_VerifyTemplate_id: task.T_VerifyTemplate_id,
  181. ...obj,
  182. });
  183. if (res.Code === 200) {
  184. generateKeyInfo(res.Data);
  185. }
  186. };
  187. let timer = 0;
  188. const keyInfo = ref({});
  189. const items = computed(() => {
  190. return keyInfo.value.Item
  191. ? keyInfo.value.Item.split('\n').filter((item) => item)
  192. : [];
  193. });
  194. // 验证报告生成 获取生成结果
  195. const generateKeyInfo = async (key) => {
  196. const { data: res } = await generateKey({
  197. key,
  198. });
  199. if (res.Code === 600) {
  200. timer = setTimeout(() => {
  201. generateKeyInfo(key);
  202. }, 3000);
  203. } else {
  204. clearTimeout(timer);
  205. }
  206. scrollbarRef.value.scrollTo({
  207. top: 9999,
  208. behavior: 'smooth',
  209. });
  210. keyInfo.value = res;
  211. };
  212. // 关闭对话框时触发
  213. const onCloseModal = () => {
  214. keyInfo.value = {};
  215. clearTimeout(timer);
  216. };
  217. // 下载文件
  218. const handleDownload = () => {
  219. window.open(keyInfo.value.Data);
  220. };
  221. // 生成报告
  222. const putTemplateData = async () => {
  223. try {
  224. const arr = toRaw(formValue.formList);
  225. const VerifyTemplateMapData = arr.map((item) => {
  226. if (item.T_label === 3 || item.T_label === 9) {
  227. return {
  228. T_VerifyTemplateMap_id: item.T_VerifyTemplateMap_id,
  229. T_value: item.T_value.join('|'),
  230. T_source: item.T_source,
  231. T_flow_sort: item.T_flow_sort,
  232. T_max_time: item.T_max_time,
  233. T_min_time: item.T_min_time,
  234. };
  235. } else if (item.T_label === 12) {
  236. return {
  237. T_VerifyTemplateMap_id: item.T_VerifyTemplateMap_id,
  238. T_value: item.T_value.join('/'),
  239. T_source: item.T_source,
  240. T_flow_sort: item.T_flow_sort,
  241. T_max_time: item.T_max_time,
  242. T_min_time: item.T_min_time,
  243. };
  244. } else if (item.T_label === 13) {
  245. return {
  246. T_VerifyTemplateMap_id: item.T_VerifyTemplateMap_id,
  247. T_value: `${item.T_value[0].join('|')}/${item.T_value[1]}`,
  248. T_source: item.T_source,
  249. T_flow_sort: item.T_flow_sort,
  250. T_max_time: item.T_max_time,
  251. T_min_time: item.T_min_time,
  252. };
  253. } else {
  254. return {
  255. T_VerifyTemplateMap_id: item.T_VerifyTemplateMap_id,
  256. T_value: item.T_value + '',
  257. T_source: item.T_source,
  258. T_flow_sort: item.T_flow_sort,
  259. T_max_time: item.T_max_time,
  260. T_min_time: item.T_min_time,
  261. };
  262. }
  263. });
  264. const token = getToken();
  265. const { data: res } = await putVerifyTemplateMapData({
  266. User_tokey: token,
  267. T_source: queryData.T_source,
  268. T_task_id: queryData.T_task_id,
  269. T_VerifyTemplate_id: queryData.T_VerifyTemplate_id,
  270. VerifyTemplateMapData,
  271. });
  272. if (res.Code === 200) {
  273. const obj = {};
  274. for (let i of arr) {
  275. if (i.T_label === 3 || i.T_label === 9) {
  276. obj[i.T_name] = i.T_value.join('|');
  277. } else if (i.T_label === 12) {
  278. obj[i.T_name] = i.T_value.join('/');
  279. } else if (i.T_label === 13) {
  280. obj[i.T_name] = `${i.T_value[0].join('|')}/${i.T_value[1]}`;
  281. } else {
  282. obj[i.T_name] = i.T_value;
  283. }
  284. }
  285. modal.title = '生成报告';
  286. modal.showModal = true;
  287. generateReportInfo(obj);
  288. }
  289. } catch (e) {
  290. console.log(e);
  291. }
  292. };
  293. // 获取设备列表
  294. const getClassList = async () => {
  295. try {
  296. const { data: res } = await getTaskDataClassList({
  297. T_task_id: queryData.T_task_id,
  298. });
  299. classList.value = res.Data || [];
  300. } catch (e) {
  301. console.log(e);
  302. }
  303. };
  304. // 模板标签数据(列表)
  305. const getTemplateList = async () => {
  306. try {
  307. const { data: res } = await getVerifyTemplateMapDataList(queryData);
  308. formValue.formList = res.Data;
  309. formValue.formList.forEach((item) => {
  310. if (item.T_label === 3 || item.T_label === 9) {
  311. item.T_value = item.T_value ? item.T_value.split('|') : null;
  312. } else if (item.T_label === 12) {
  313. item.T_value = item.T_value
  314. ? [item.T_value.split('/')[0], item.T_value.split('/')[1]]
  315. : null;
  316. } else if (item.T_label === 13) {
  317. item.T_value = item.T_value
  318. ? [item.T_value.split('/')[0].split('|'), item.T_value.split('/')[1]]
  319. : null;
  320. } else {
  321. item.T_value = item.T_value ? item.T_value : null;
  322. }
  323. });
  324. } catch (e) {
  325. console.log(e);
  326. }
  327. };
  328. onBeforeUnmount(() => {
  329. // 取消订阅
  330. clearTimeout(timer);
  331. });
  332. // getClassList();
  333. getTemplateList();
  334. </script>
  335. <style scoped></style>