|
|
@@ -26,7 +26,7 @@
|
|
|
import { ref } from 'vue'
|
|
|
import SpreadDesignerGeneric from '@/components/SpreadDesigner/spreadDesignerGeneric.vue'
|
|
|
import { onLoad } from '@dcloudio/uni-app'
|
|
|
-import { buildFileUrl } from '@/utils/index'
|
|
|
+import { getEnvBaseUrl } from '@/utils/index'
|
|
|
import { useConfigStore } from '@/store/config'
|
|
|
import {
|
|
|
SecurityCheckFuncName,
|
|
|
@@ -53,6 +53,7 @@ const businessConfig = ref({
|
|
|
})
|
|
|
const templateBlob = ref<string>('')
|
|
|
const templateData = ref<any>({})
|
|
|
+const skipEntryKeys = ref<string[]>([])
|
|
|
|
|
|
let templateId: string = ''
|
|
|
let orderId: string = ''
|
|
|
@@ -70,6 +71,7 @@ onLoad((options: any) => {
|
|
|
const instId = ref<string>('')
|
|
|
const refId = ref<string>('')
|
|
|
const init = async () => {
|
|
|
+ skipEntryKeys.value = []
|
|
|
if (!templateId) {
|
|
|
return
|
|
|
}
|
|
|
@@ -101,7 +103,12 @@ const init = async () => {
|
|
|
instId.value = dynamicTb?.dynamicTbInsRespVO?.id || ''
|
|
|
for (let i = 0; i < dynamicTb.dynamicTbValRespVOList.length; i++) {
|
|
|
const item = dynamicTb.dynamicTbValRespVOList[i]
|
|
|
- dataMap[item.colCode] = item.valValue
|
|
|
+ const val = item.valValue
|
|
|
+ if (typeof val === 'string' && /\.(jpg|png)$/i.test(val)) {
|
|
|
+ skipEntryKeys.value.push(item.colCode)
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ dataMap[item.colCode] = val
|
|
|
}
|
|
|
|
|
|
templateData.value = {
|
|
|
@@ -116,40 +123,61 @@ const init = async () => {
|
|
|
templateUrl: resData.fileUrl,
|
|
|
}
|
|
|
|
|
|
- const fileUri = resData.fileUrl
|
|
|
- const fileUrl = buildFileUrl(fileUri)
|
|
|
- const fileBase64 = await downloadFileAsBase64(fileUrl)
|
|
|
+ const excelBlob = await fetchExcel(refId.value, templateId)
|
|
|
+ const fileBase64 = await blobToBase64(excelBlob)
|
|
|
templateBlob.value = fileBase64
|
|
|
}
|
|
|
|
|
|
-const downloadFileAsBase64 = (fileUrl: string): Promise<string> => {
|
|
|
+const fetchExcel = async (refId: string | number, templateId: string | number): Promise<Blob> => {
|
|
|
+ const apiPath = '/pressure2/excel/excel'
|
|
|
+ let requestUrl = apiPath
|
|
|
+ if (JSON.parse(import.meta.env.VITE_APP_PROXY) && import.meta.env.MODE === 'development') {
|
|
|
+ requestUrl = import.meta.env.VITE_APP_PROXY_PREFIX + apiPath
|
|
|
+ } else {
|
|
|
+ requestUrl = getEnvBaseUrl() + apiPath
|
|
|
+ }
|
|
|
+
|
|
|
+ const token = uni.getStorageSync('APP_ACCESS_TOKEN')
|
|
|
+ const headers: Record<string, string> = { 'Content-Type': 'application/json' }
|
|
|
+ if (token) {
|
|
|
+ headers.Authorization = `Bearer ${token}`
|
|
|
+ }
|
|
|
+
|
|
|
+ const response = await fetch(requestUrl, {
|
|
|
+ method: 'POST',
|
|
|
+ headers,
|
|
|
+ body: JSON.stringify({ refId, templateId }),
|
|
|
+ })
|
|
|
+
|
|
|
+ if (!response.ok) {
|
|
|
+ throw new Error(`获取Excel失败, status ${response.status}`)
|
|
|
+ }
|
|
|
+
|
|
|
+ return await response.blob()
|
|
|
+}
|
|
|
+
|
|
|
+const blobToBase64 = (blob: Blob): Promise<string> => {
|
|
|
return new Promise((resolve, reject) => {
|
|
|
- uni.request({
|
|
|
- url: fileUrl,
|
|
|
- method: 'GET',
|
|
|
- responseType: 'arraybuffer',
|
|
|
- success: (res) => {
|
|
|
- if (res.statusCode === 200) {
|
|
|
- const arrayBuffer = res.data as ArrayBuffer
|
|
|
- const uint8Array = new Uint8Array(arrayBuffer)
|
|
|
- const binaryString = uint8Array.reduce((data, byte) => {
|
|
|
- return data + String.fromCharCode(byte)
|
|
|
- }, '')
|
|
|
- const base64 = btoa(binaryString)
|
|
|
- resolve(base64) // 成功时 resolve Base64 字符串
|
|
|
- } else {
|
|
|
- reject(new Error(`Request failed with status ${res.statusCode}`))
|
|
|
- }
|
|
|
- },
|
|
|
- fail: (err) => {
|
|
|
- reject(err) // 失败时 reject 错误
|
|
|
- },
|
|
|
- })
|
|
|
+ const reader = new FileReader()
|
|
|
+ reader.onload = () => {
|
|
|
+ const base64 = (reader.result as string).split(',')[1]
|
|
|
+ resolve(base64)
|
|
|
+ }
|
|
|
+ reader.onerror = reject
|
|
|
+ reader.readAsDataURL(blob)
|
|
|
})
|
|
|
}
|
|
|
|
|
|
const handleSave = async (data: any) => {
|
|
|
- const result = await saveDynamicTbVal({ params: data.dataJSON, instId: instId.value })
|
|
|
+ const reqData = {}
|
|
|
+ for (const key in data.dataJSON) {
|
|
|
+ if (skipEntryKeys.value.includes(key)) {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ const element = data.dataJSON[key];
|
|
|
+ reqData[key] = element
|
|
|
+ }
|
|
|
+ const result = await saveDynamicTbVal({ params: reqData, instId: instId.value })
|
|
|
if (result?.code === 0 && result?.data) {
|
|
|
uni.showToast({ title: '保存成功', icon: 'success' })
|
|
|
} else {
|
|
|
@@ -161,7 +189,7 @@ const handleSave = async (data: any) => {
|
|
|
equipType,
|
|
|
{
|
|
|
id: refId.value,
|
|
|
- jsonData: JSON.stringify(data.dataJSON),
|
|
|
+ jsonData: JSON.stringify(reqData),
|
|
|
},
|
|
|
)
|
|
|
}
|