securityCheckEditor.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <template>
  2. <div>
  3. <SpreadDesignerGeneric
  4. :businessConfig="businessConfig"
  5. :templateData="templateData"
  6. :templateBlob="templateBlob"
  7. />
  8. </div>
  9. </template>
  10. <script setup lang="ts">
  11. import { ref } from 'vue'
  12. import SpreadDesignerGeneric from '@/components/SpreadDesigner/spreadDesignerGeneric.vue'
  13. import { onLoad } from '@dcloudio/uni-app'
  14. import { getStandardTemplate } from '@/api/index'
  15. import { buildFileUrl } from '@/utils/index'
  16. const businessConfig = ref({
  17. businessType: 'AQJC',
  18. title: '安全检查记录编辑',
  19. disableNavigate: true, // 是否禁用跳转,默认不禁用
  20. ui: {
  21. title: '安全检查记录',
  22. saveButtonText: '保存',
  23. cancelButtonText: '取消',
  24. showAdditionalToolbar: true,
  25. customButtons: [],
  26. },
  27. })
  28. const templateBlob = ref<string>('')
  29. const templateData = ref<any>({})
  30. let templateId: string = ''
  31. onLoad((options: any) => {
  32. templateId = options.templateId
  33. init()
  34. })
  35. const init = async () => {
  36. if (!templateId) {
  37. return
  38. }
  39. const res = await getStandardTemplate({ id: templateId })
  40. const resData = (res as any).data
  41. const dataMap = {}
  42. templateData.value = {
  43. schema: resData.bindingPathSchema ? JSON.parse(resData.bindingPathSchema) : {},
  44. data: {
  45. ...dataMap,
  46. templateId,
  47. templateUrl: resData.fileUrl,
  48. },
  49. pathNameMapping: JSON.parse(resData.bindingPathNameJson),
  50. template: templateId,
  51. templateUrl: resData.fileUrl,
  52. }
  53. const fileUri = resData.fileUrl
  54. const fileUrl = buildFileUrl(fileUri)
  55. const fileBase64 = await downloadFileAsBase64(fileUrl)
  56. templateBlob.value = fileBase64
  57. }
  58. const downloadFileAsBase64 = (fileUrl: string): Promise<string> => {
  59. return new Promise((resolve, reject) => {
  60. uni.request({
  61. url: fileUrl,
  62. method: 'GET',
  63. responseType: 'arraybuffer',
  64. success: (res) => {
  65. if (res.statusCode === 200) {
  66. const arrayBuffer = res.data as ArrayBuffer
  67. const uint8Array = new Uint8Array(arrayBuffer)
  68. const binaryString = uint8Array.reduce((data, byte) => {
  69. return data + String.fromCharCode(byte)
  70. }, '')
  71. const base64 = btoa(binaryString)
  72. resolve(base64) // 成功时 resolve Base64 字符串
  73. } else {
  74. reject(new Error(`Request failed with status ${res.statusCode}`))
  75. }
  76. },
  77. fail: (err) => {
  78. reject(err) // 失败时 reject 错误
  79. },
  80. })
  81. })
  82. }
  83. </script>