index.vue
5.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<template>
<view class="test-poster-page">
<view class="mb-4">
<text class="text-lg font-bold">海报圆角测试</text>
</view>
<!-- 测试按钮 -->
<view class="flex flex-col gap-4 mb-6">
<nut-button @click="generateNormalPoster" type="primary">
生成普通海报(无圆角)
</nut-button>
<nut-button @click="generateRoundPoster" type="success">
生成圆角海报(统一圆角)
</nut-button>
<nut-button @click="generateCustomRoundPoster" type="warning">
生成自定义圆角海报(四角不同)
</nut-button>
</view>
<!-- 海报生成组件 -->
<PosterBuilder
v-if="showPoster"
:config="posterConfig"
:show-loading="true"
@success="onPosterSuccess"
@fail="onPosterFail"
/>
<!-- 生成的海报预览 -->
<view v-if="posterUrl" class="mt-6">
<text class="text-base font-semibold mb-2">生成的海报:</text>
<image
:src="posterUrl"
mode="widthFix"
class="w-full max-w-xs mx-auto block"
/>
</view>
</view>
</template>
<script>
import { defineComponent, ref } from 'vue'
import Taro from '@tarojs/taro'
import PosterBuilder from '../../components/PosterBuilder/index.vue'
export default defineComponent({
name: 'TestPoster',
components: {
PosterBuilder
},
setup() {
const showPoster = ref(false)
const posterUrl = ref('')
const posterConfig = ref({})
/**
* 生成普通海报(无圆角)
*/
const generateNormalPoster = () => {
posterConfig.value = {
width: 600,
height: 800,
backgroundColor: '#4F46E5',
texts: [
{
text: '普通海报测试',
x: 300,
y: 100,
fontSize: 32,
color: '#FFFFFF',
textAlign: 'center',
fontWeight: 'bold'
},
{
text: '这是一个没有圆角的海报',
x: 300,
y: 200,
fontSize: 24,
color: '#E5E7EB',
textAlign: 'center'
}
],
blocks: [
{
x: 50,
y: 300,
width: 500,
height: 200,
backgroundColor: '#FFFFFF',
borderRadius: 0
}
]
}
showPoster.value = true
}
/**
* 生成圆角海报(统一圆角)
*/
const generateRoundPoster = () => {
posterConfig.value = {
width: 600,
height: 800,
backgroundColor: '#10B981',
borderRadius: 30, // 统一圆角
texts: [
{
text: '圆角海报测试',
x: 300,
y: 100,
fontSize: 32,
color: '#FFFFFF',
textAlign: 'center',
fontWeight: 'bold'
},
{
text: '这是一个有统一圆角的海报',
x: 300,
y: 200,
fontSize: 24,
color: '#E5E7EB',
textAlign: 'center'
}
],
blocks: [
{
x: 50,
y: 300,
width: 500,
height: 200,
backgroundColor: '#FFFFFF',
borderRadius: 20
}
]
}
showPoster.value = true
}
/**
* 生成自定义圆角海报(四角不同)
*/
const generateCustomRoundPoster = () => {
posterConfig.value = {
width: 600,
height: 800,
backgroundColor: '#F59E0B',
borderRadiusGroup: [50, 20, 50, 20], // 左上、右上、右下、左下
texts: [
{
text: '自定义圆角海报',
x: 300,
y: 100,
fontSize: 32,
color: '#FFFFFF',
textAlign: 'center',
fontWeight: 'bold'
},
{
text: '四个角有不同的圆角半径',
x: 300,
y: 200,
fontSize: 24,
color: '#1F2937',
textAlign: 'center'
}
],
blocks: [
{
x: 50,
y: 300,
width: 500,
height: 200,
backgroundColor: '#FFFFFF',
borderRadiusGroup: [30, 10, 30, 10]
}
]
}
showPoster.value = true
}
/**
* 海报生成成功回调
*/
const onPosterSuccess = (result) => {
console.log('海报生成成功:', result)
posterUrl.value = result.tempFilePath
showPoster.value = false
Taro.showToast({
title: '海报生成成功',
icon: 'success'
})
}
/**
* 海报生成失败回调
*/
const onPosterFail = (error) => {
console.error('海报生成失败:', error)
showPoster.value = false
Taro.showToast({
title: '海报生成失败',
icon: 'error'
})
}
return {
showPoster,
posterUrl,
posterConfig,
generateNormalPoster,
generateRoundPoster,
generateCustomRoundPoster,
onPosterSuccess,
onPosterFail
}
}
})
</script>
<style lang="less" scoped>
.test-poster-page {
padding: 32rpx;
min-height: 100vh;
background-color: #f5f5f5;
}
</style>