mirror of
https://github.com/palxiao/poster-design.git
synced 2025-07-15 16:02:19 +08:00
feat: convert upload templat component to composition API
This commit is contained in:
parent
372810e19f
commit
7eb44396b4
@ -11,31 +11,48 @@
|
|||||||
<SaveImage ref="canvasImage" />
|
<SaveImage ref="canvasImage" />
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script lang="ts">
|
<script lang="ts" setup>
|
||||||
import api from '@/api'
|
import api from '@/api'
|
||||||
import { defineComponent, reactive, toRefs, getCurrentInstance, ComponentInternalInstance } from 'vue'
|
import { reactive, ref } from 'vue'
|
||||||
import { mapGetters, useStore } from 'vuex'
|
import { useStore } from 'vuex'
|
||||||
import { useRoute, useRouter } from 'vue-router'
|
import { useRoute, useRouter } from 'vue-router'
|
||||||
import useNotification from '@/common/methods/notification'
|
import useNotification from '@/common/methods/notification'
|
||||||
import SaveImage from '@/components/business/save-download/CreateCover.vue'
|
import SaveImage from '@/components/business/save-download/CreateCover.vue'
|
||||||
import { useFontStore } from '@/common/methods/fonts'
|
import { useFontStore } from '@/common/methods/fonts'
|
||||||
import _config from '@/config'
|
import _config from '@/config'
|
||||||
import github from '@/api/github'
|
import github from '@/api/github'
|
||||||
|
import { useSetupMapGetters } from '@/common/hooks/mapGetters'
|
||||||
|
|
||||||
export default defineComponent({
|
type TProps = {
|
||||||
components: { SaveImage },
|
modelValue?: string
|
||||||
props: ['modelValue', 'isDone'],
|
isDone?: boolean
|
||||||
emits: ['change', 'update:modelValue'],
|
}
|
||||||
setup(props, context) {
|
|
||||||
const { proxy }: any = getCurrentInstance() as ComponentInternalInstance
|
type TEmits = {
|
||||||
const route = useRoute()
|
(event: 'change', data: { downloadPercent: number | null, downloadText: string, downloadMsg?: string, cancelText?: string }): void
|
||||||
const router = useRouter()
|
(event: 'update:modelValue', data: string): void
|
||||||
const store = useStore()
|
}
|
||||||
const state: any = reactive({
|
|
||||||
|
type TState = {
|
||||||
|
stateBollean: false,
|
||||||
|
title: '',
|
||||||
|
loading: false,
|
||||||
|
}
|
||||||
|
|
||||||
|
const { dPage, dWidgets } = useSetupMapGetters(['dPage', 'dWidgets'])
|
||||||
|
|
||||||
|
const props = defineProps<TProps>()
|
||||||
|
const emit = defineEmits<TEmits>()
|
||||||
|
|
||||||
|
const route = useRoute()
|
||||||
|
const router = useRouter()
|
||||||
|
const store = useStore()
|
||||||
|
|
||||||
|
const canvasImage = ref<typeof SaveImage | null>(null)
|
||||||
|
const state = reactive<TState>({
|
||||||
stateBollean: false,
|
stateBollean: false,
|
||||||
title: '',
|
title: '',
|
||||||
loading: false,
|
loading: false,
|
||||||
canvasImage: null,
|
|
||||||
})
|
})
|
||||||
|
|
||||||
useFontStore.init() // 初始化加载字体
|
useFontStore.init() // 初始化加载字体
|
||||||
@ -43,28 +60,32 @@ export default defineComponent({
|
|||||||
// 生成封面
|
// 生成封面
|
||||||
const draw = () => {
|
const draw = () => {
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
state.canvasImage.createCover(({ key }: any) => {
|
if (!canvasImage.value) {
|
||||||
|
resolve('')
|
||||||
|
} else {
|
||||||
|
canvasImage.value.createCover(({ key }: { key: string }) => {
|
||||||
resolve(_config.IMG_URL + key)
|
resolve(_config.IMG_URL + key)
|
||||||
})
|
})
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
let addition = 0 // 累加大小
|
let addition = 0 // 累加大小
|
||||||
let lenCount = 0 // 全部大小
|
let lenCount = 0 // 全部大小
|
||||||
let lens = 0 // 任务数
|
let lens = 0 // 任务数
|
||||||
const queue: any[] = [] // 队列
|
const queue: { type: string, imgUrl: string }[] = [] // 队列
|
||||||
let widgets: any = []
|
let widgets: { type: string, imgUrl: string }[] = []
|
||||||
let page: any = {}
|
let page: Record<string, any> = {}
|
||||||
|
|
||||||
async function prepare() {
|
async function prepare() {
|
||||||
store.commit('setShowMoveable', false) // 清理掉上一次的选择框
|
store.commit('setShowMoveable', false) // 清理掉上一次的选择框
|
||||||
addition = 0
|
addition = 0
|
||||||
lenCount = 0
|
lenCount = 0
|
||||||
widgets = proxy.dWidgets
|
widgets = dWidgets.value
|
||||||
page = proxy.dPage
|
page = dPage.value
|
||||||
|
|
||||||
if (page.backgroundImage) {
|
if (page.backgroundImage) {
|
||||||
context.emit('change', { downloadPercent: 1, downloadText: '正在准备上传', downloadMsg: '请等待..' })
|
emit('change', { downloadPercent: 1, downloadText: '正在准备上传', downloadMsg: '请等待..' })
|
||||||
page.backgroundImage = await github.putPic(page.backgroundImage.split(',')[1])
|
page.backgroundImage = await github.putPic(page.backgroundImage.split(',')[1])
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -81,11 +102,12 @@ export default defineComponent({
|
|||||||
async function uploadImgs() {
|
async function uploadImgs() {
|
||||||
if (queue.length > 0) {
|
if (queue.length > 0) {
|
||||||
const item = queue.pop()
|
const item = queue.pop()
|
||||||
|
if (!item) return
|
||||||
const url = await github.putPic(item.imgUrl.split(',')[1])
|
const url = await github.putPic(item.imgUrl.split(',')[1])
|
||||||
addition += item.imgUrl.length
|
addition += item.imgUrl.length
|
||||||
let downloadPercent: any = (addition / lenCount) * 100
|
let downloadPercent: number | null = (addition / lenCount) * 100
|
||||||
downloadPercent >= 100 && (downloadPercent = null)
|
downloadPercent >= 100 && (downloadPercent = null)
|
||||||
context.emit('change', { downloadPercent, downloadText: '上传资源中', downloadMsg: `已完成:${lens - queue.length} / ${lens}` })
|
emit('change', { downloadPercent, downloadText: '上传资源中', downloadMsg: `已完成:${lens - queue.length} / ${lens}` })
|
||||||
item.imgUrl = url
|
item.imgUrl = url
|
||||||
uploadImgs()
|
uploadImgs()
|
||||||
} else {
|
} else {
|
||||||
@ -94,22 +116,16 @@ export default defineComponent({
|
|||||||
}
|
}
|
||||||
|
|
||||||
const uploadTemplate = async () => {
|
const uploadTemplate = async () => {
|
||||||
context.emit('change', { downloadPercent: 95, downloadText: '正在处理封面', downloadMsg: '即将结束...' })
|
emit('change', { downloadPercent: 95, downloadText: '正在处理封面', downloadMsg: '即将结束...' })
|
||||||
const cover = await draw()
|
const cover = await draw()
|
||||||
const { id, stat, msg } = await api.home.saveWorks({ cover, title: '自设计模板', data: JSON.stringify({ page, widgets }), width: page.width, height: page.height })
|
const { id, stat, msg } = await api.home.saveWorks({ cover, title: '自设计模板', data: JSON.stringify({ page, widgets }), width: page.width, height: page.height })
|
||||||
stat !== 0 ? useNotification('保存成功', '可在"我的模板"中查看') : useNotification('保存失败', msg, { type: 'error' })
|
stat !== 0 ? useNotification('保存成功', '可在"我的模板"中查看') : useNotification('保存失败', msg, { type: 'error' })
|
||||||
router.push({ path: '/psd', query: { id }, replace: true })
|
router.push({ path: '/psd', query: { id }, replace: true })
|
||||||
context.emit('change', { downloadPercent: 99.99, downloadText: '上传完成', cancelText: '查看我的作品' }) // 关闭弹窗
|
emit('change', { downloadPercent: 99.99, downloadText: '上传完成', cancelText: '查看我的作品' }) // 关闭弹窗
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
defineExpose({
|
||||||
...toRefs(state),
|
|
||||||
prepare,
|
prepare,
|
||||||
}
|
|
||||||
},
|
|
||||||
computed: {
|
|
||||||
...mapGetters(['dPage', 'dWidgets']),
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user