workInstructionEditor.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <route lang="json5" type="page">
  2. {
  3. layout: 'default',
  4. style: {
  5. navigationBarTitleText: '',
  6. navigationStyle: 'custom',
  7. disableScroll: true,
  8. },
  9. }
  10. </route>
  11. <!-- 安全检查记录编辑 -->
  12. <template>
  13. <div>
  14. <SpreadDesignerGeneric
  15. :businessConfig="businessConfig"
  16. :templateData="templateData"
  17. :templateBlob="templateBlob"
  18. @save="handleSave"
  19. @cancel="handleCancel"
  20. />
  21. </div>
  22. </template>
  23. <script setup lang="ts">
  24. import { ref } from 'vue'
  25. import SpreadDesignerGeneric from '@/components/SpreadDesigner/spreadDesignerGeneric.vue'
  26. import { onLoad } from '@dcloudio/uni-app'
  27. import { getEnvBaseUrl } from '@/utils/index'
  28. import { getStandardTemplate } from '@/api/index'
  29. import { getDynamicTbVal, saveDynamicTbVal } from '@/api/task'
  30. const businessConfig = ref({
  31. businessType: 'CZZD',
  32. title: '指导书编辑',
  33. disableNavigate: true, // 是否禁用跳转,默认不禁用
  34. ui: {
  35. title: '指导书编辑',
  36. saveButtonText: '保存',
  37. cancelButtonText: '取消',
  38. showAdditionalToolbar: true,
  39. customButtons: [],
  40. },
  41. })
  42. const templateBlob = ref<string>('')
  43. const templateData = ref<any>({})
  44. const skipEntryKeys = ref<string[]>([])
  45. let templateId: string = ''
  46. let orderId: string = ''
  47. let mode: 'edit' | 'create' = 'edit'
  48. let workInstructionId: string = ''
  49. onLoad((options: any) => {
  50. templateId = options.templateId
  51. orderId = options.orderId
  52. mode = options.mode || 'edit'
  53. workInstructionId = options.refId || ''
  54. init()
  55. })
  56. const instId = ref<string>('')
  57. const init = async () => {
  58. skipEntryKeys.value = []
  59. if (!templateId) {
  60. return
  61. }
  62. const res = await getStandardTemplate({ id: templateId })
  63. const resData = (res as any).data
  64. const dataMap: any = {}
  65. const dynamicTbValResp = await getDynamicTbVal({ refId: workInstructionId })
  66. const dynamicTb: any = dynamicTbValResp.data
  67. instId.value = dynamicTb?.dynamicTbInsRespVO?.id || ''
  68. for (let i = 0; i < dynamicTb.dynamicTbValRespVOList.length; i++) {
  69. const item = dynamicTb.dynamicTbValRespVOList[i]
  70. const val = item.valValue
  71. if (typeof val === 'string' && /\.(jpg|png)$/i.test(val)) {
  72. skipEntryKeys.value.push(item.colCode)
  73. continue
  74. }
  75. dataMap[item.colCode] = val
  76. }
  77. templateData.value = {
  78. schema: resData.bindingPathSchema ? JSON.parse(resData.bindingPathSchema) : {},
  79. data: {
  80. ...dataMap,
  81. templateId,
  82. templateUrl: resData.fileUrl,
  83. },
  84. pathNameMapping: JSON.parse(resData.bindingPathNameJson),
  85. template: templateId,
  86. templateUrl: resData.fileUrl,
  87. }
  88. const excelBlob = await fetchExcel(workInstructionId, templateId)
  89. const fileBase64 = await blobToBase64(excelBlob)
  90. templateBlob.value = fileBase64
  91. }
  92. const fetchExcel = async (refId: string | number, templateId: string | number): Promise<Blob> => {
  93. const apiPath = '/pressure2/excel/excel'
  94. let requestUrl = apiPath
  95. if (JSON.parse(import.meta.env.VITE_APP_PROXY) && import.meta.env.MODE === 'development') {
  96. requestUrl = import.meta.env.VITE_APP_PROXY_PREFIX + apiPath
  97. } else {
  98. requestUrl = getEnvBaseUrl() + apiPath
  99. }
  100. const token = uni.getStorageSync('APP_ACCESS_TOKEN')
  101. const headers: Record<string, string> = { 'Content-Type': 'application/json' }
  102. if (token) {
  103. headers.Authorization = `Bearer ${token}`
  104. }
  105. const response = await fetch(requestUrl, {
  106. method: 'POST',
  107. headers,
  108. body: JSON.stringify({ refId, templateId }),
  109. })
  110. if (!response.ok) {
  111. throw new Error(`获取Excel失败, status ${response.status}`)
  112. }
  113. return await response.blob()
  114. }
  115. const blobToBase64 = (blob: Blob): Promise<string> => {
  116. return new Promise((resolve, reject) => {
  117. const reader = new FileReader()
  118. reader.onload = () => {
  119. const base64 = (reader.result as string).split(',')[1]
  120. resolve(base64)
  121. }
  122. reader.onerror = reject
  123. reader.readAsDataURL(blob)
  124. })
  125. }
  126. const handleSave = async (data: any) => {
  127. const reqData = {}
  128. for (const key in data.dataJSON) {
  129. if (skipEntryKeys.value.includes(key)) {
  130. continue
  131. }
  132. const element = data.dataJSON[key];
  133. reqData[key] = element
  134. }
  135. const result = await saveDynamicTbVal({ params: reqData, instId: instId.value })
  136. if (result?.code === 0 && result?.data) {
  137. uni.showToast({ title: '保存成功', icon: 'success' })
  138. } else {
  139. const msg = result?.msg || '保存失败'
  140. uni.showToast({ title: msg, icon: 'error' })
  141. }
  142. }
  143. const handleCancel = () => {
  144. uni.navigateBack()
  145. }
  146. </script>