securityCheckEditor.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. <template>
  12. <div>
  13. <SpreadDesignerGeneric
  14. :businessConfig="businessConfig"
  15. :templateData="templateData"
  16. :templateBlob="templateBlob"
  17. @save="handleSave"
  18. @cancel="handleCancel"
  19. />
  20. </div>
  21. </template>
  22. <script setup lang="ts">
  23. import { ref } from 'vue'
  24. import SpreadDesignerGeneric from '@/components/SpreadDesigner/spreadDesignerGeneric.vue'
  25. import { onLoad } from '@dcloudio/uni-app'
  26. import { buildFileUrl } from '@/utils/index'
  27. import { useConfigStore } from '@/store/config'
  28. import {
  29. SecurityCheckFuncName,
  30. requestFunc as securityCheckRequestFunc,
  31. } from '@/api/ApiRouter/taskOrderSecurityCheck'
  32. import { getStandardTemplate } from '@/api/index'
  33. import { getDynamicTbVal, saveDynamicTbVal } from '@/api/task'
  34. const configStore = useConfigStore()
  35. const equipType = configStore.getEquipType()
  36. const businessConfig = ref({
  37. businessType: 'AQJC',
  38. title: '安全检查记录编辑',
  39. disableNavigate: true, // 是否禁用跳转,默认不禁用
  40. ui: {
  41. title: '安全检查记录',
  42. saveButtonText: '保存',
  43. cancelButtonText: '取消',
  44. showAdditionalToolbar: true,
  45. customButtons: [],
  46. },
  47. })
  48. const templateBlob = ref<string>('')
  49. const templateData = ref<any>({})
  50. let templateId: string = ''
  51. let orderId: string = ''
  52. let mode: 'edit' | 'create' = 'edit'
  53. let securityCheckId: string = ''
  54. onLoad((options: any) => {
  55. templateId = options.templateId
  56. orderId = options.orderId
  57. mode = options.mode || 'edit'
  58. securityCheckId = options.securityCheckId || ''
  59. init()
  60. })
  61. const instId = ref<string>('')
  62. const refId = ref<string>('')
  63. const init = async () => {
  64. if (!templateId) {
  65. return
  66. }
  67. refId.value = null
  68. if (mode === 'create') {
  69. const createResp = await securityCheckRequestFunc(
  70. SecurityCheckFuncName.createSecurityCheckItem,
  71. equipType,
  72. {
  73. businessType: 300,
  74. conclusion: '',
  75. date: null,
  76. name: '进入现场(设备)前安全检查记录',
  77. orderId,
  78. templateId,
  79. },
  80. )
  81. refId.value = createResp.data
  82. } else {
  83. refId.value = securityCheckId
  84. }
  85. const res = await getStandardTemplate({ id: templateId })
  86. const resData = (res as any).data
  87. const dataMap: any = {}
  88. const dynamicTbValResp = await getDynamicTbVal({ refId: refId.value })
  89. const dynamicTb: any = dynamicTbValResp.data
  90. instId.value = dynamicTb?.dynamicTbInsRespVO?.id || ''
  91. for (let i = 0; i < dynamicTb.dynamicTbValRespVOList.length; i++) {
  92. const item = dynamicTb.dynamicTbValRespVOList[i]
  93. dataMap[item.colCode] = item.valValue
  94. }
  95. templateData.value = {
  96. schema: resData.bindingPathSchema ? JSON.parse(resData.bindingPathSchema) : {},
  97. data: {
  98. ...dataMap,
  99. templateId,
  100. templateUrl: resData.fileUrl,
  101. },
  102. pathNameMapping: JSON.parse(resData.bindingPathNameJson),
  103. template: templateId,
  104. templateUrl: resData.fileUrl,
  105. }
  106. const fileUri = resData.fileUrl
  107. const fileUrl = buildFileUrl(fileUri)
  108. const fileBase64 = await downloadFileAsBase64(fileUrl)
  109. templateBlob.value = fileBase64
  110. }
  111. const downloadFileAsBase64 = (fileUrl: string): Promise<string> => {
  112. return new Promise((resolve, reject) => {
  113. uni.request({
  114. url: fileUrl,
  115. method: 'GET',
  116. responseType: 'arraybuffer',
  117. success: (res) => {
  118. if (res.statusCode === 200) {
  119. const arrayBuffer = res.data as ArrayBuffer
  120. const uint8Array = new Uint8Array(arrayBuffer)
  121. const binaryString = uint8Array.reduce((data, byte) => {
  122. return data + String.fromCharCode(byte)
  123. }, '')
  124. const base64 = btoa(binaryString)
  125. resolve(base64) // 成功时 resolve Base64 字符串
  126. } else {
  127. reject(new Error(`Request failed with status ${res.statusCode}`))
  128. }
  129. },
  130. fail: (err) => {
  131. reject(err) // 失败时 reject 错误
  132. },
  133. })
  134. })
  135. }
  136. const handleSave = async (data: any) => {
  137. const result = await saveDynamicTbVal({ params: data.dataJSON, instId: instId.value })
  138. if (result?.code === 0 && result?.data) {
  139. uni.showToast({ title: '保存成功', icon: 'success' })
  140. } else {
  141. const msg = result?.msg || '保存失败'
  142. uni.showToast({ title: msg, icon: 'error' })
  143. }
  144. const updateResp = await securityCheckRequestFunc(
  145. SecurityCheckFuncName.updateSecurityCheckItem,
  146. equipType,
  147. {
  148. id: refId.value,
  149. jsonData: JSON.stringify(data.dataJSON),
  150. },
  151. )
  152. }
  153. const handleCancel = () => {
  154. uni.navigateBack()
  155. }
  156. </script>