2024-11-08 16:18:15 +08:00
|
|
|
<template>
|
|
|
|
<nut-signature @confirm="confirm" @clear="clear"></nut-signature>
|
2024-11-11 11:25:08 +08:00
|
|
|
<image mode="widthFix" v-if="imgData" :src="imgData" style="width: 100%"></image>
|
2024-11-08 16:18:15 +08:00
|
|
|
</template>
|
|
|
|
<script setup lang="ts">
|
|
|
|
import { useDailyStore } from '@/store/daily'
|
|
|
|
const store = useDailyStore()
|
|
|
|
import Taro, { useLoad } from '@tarojs/taro'
|
2024-11-11 11:25:08 +08:00
|
|
|
import { ref, computed } from 'vue'
|
|
|
|
const base64_1 = computed(() => store.get_base64_1)
|
|
|
|
const base64_2 = computed(() => store.get_base64_2)
|
2024-11-08 16:18:15 +08:00
|
|
|
|
|
|
|
const _index = ref(0)
|
|
|
|
useLoad((options) => {
|
2024-11-12 09:47:29 +08:00
|
|
|
// console.log(options)
|
2024-11-08 16:18:15 +08:00
|
|
|
_index.value = Number(options.index)
|
2024-11-11 11:25:08 +08:00
|
|
|
if (_index.value === 1) {
|
|
|
|
imgData.value = base64_1.value
|
|
|
|
} else {
|
|
|
|
imgData.value = base64_2.value
|
|
|
|
}
|
2024-11-08 16:18:15 +08:00
|
|
|
})
|
|
|
|
const imgData = ref('')
|
|
|
|
const convertToBase64 = (imgUrl: string): Promise<string> => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const fs = Taro.getFileSystemManager()
|
|
|
|
fs.readFile({
|
|
|
|
filePath: imgUrl, // 本地路径
|
|
|
|
encoding: 'base64',
|
|
|
|
success(res) {
|
2024-11-11 11:25:08 +08:00
|
|
|
// resolve({})
|
2024-11-08 16:18:15 +08:00
|
|
|
resolve('data:image/png;base64,' + res.data) // 前缀加上 image 类型,适应 img 标签
|
|
|
|
},
|
|
|
|
fail(err) {
|
|
|
|
reject(err)
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
const confirm = async (canvas: any, data: string) => {
|
|
|
|
if (data) {
|
2024-11-12 09:47:29 +08:00
|
|
|
console.log('imgData', canvas)
|
2024-11-08 16:18:15 +08:00
|
|
|
try {
|
|
|
|
const base64Image = await convertToBase64(data)
|
|
|
|
if (_index.value === 1) {
|
2024-11-11 11:25:08 +08:00
|
|
|
// imgData.value = base64Image
|
2024-11-08 16:18:15 +08:00
|
|
|
store.change_base64_1(base64Image)
|
2024-11-11 11:25:08 +08:00
|
|
|
Taro.navigateBack({ delta: 1 })
|
2024-11-08 16:18:15 +08:00
|
|
|
} else {
|
2024-11-11 11:25:08 +08:00
|
|
|
// imgData.value = base64Image
|
2024-11-08 16:18:15 +08:00
|
|
|
store.change_base64_2(base64Image)
|
2024-11-11 11:25:08 +08:00
|
|
|
Taro.navigateBack({ delta: 1 })
|
2024-11-08 16:18:15 +08:00
|
|
|
}
|
|
|
|
// console.log('Base64 Image:', base64Image)
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Failed to convert image to Base64:', error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const clear = () => {
|
|
|
|
imgData.value = ''
|
|
|
|
console.log('clear')
|
|
|
|
}
|
|
|
|
</script>
|