du
8 天以前 cf14ceb730244e3feabb706a6bfcd59113396eb7
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
<template>
  <el-dialog
    v-model="dialogVisible"
    :title="t('courseCenter.intelligentSpeech')"
    width="80%"
    :before-close="handleClose"
  >
    <div class="rewriter-container flex-col">
      <div class="content-wrapper flex-col">
        <div class="main-content flex-row">
          <!-- 左侧功能区域 -->
          <div class="left-area flex-col">
            <!-- 智写方式 -->
            <div class="config-item flex items-center">
              <div class="config-label" style="width: 120px"
                >{{ t('courseCenter.smartWritingMethod') }}:</div
              >
              <el-select
                v-model="writeMode"
                :placeholder="t('common.selectText') + t('courseCenter.smartWritingMethod')"
              >
                <el-option :label="t('courseCenter.imageRewriting')" value="image" />
                <el-option :label="t('courseCenter.textRewriting')" value="text" />
                <el-option :label="t('courseCenter.expandWritten')" value="expand" />
                <el-option :label="t('courseCenter.abbreviation')" value="shrink" />
              </el-select>
            </div>
 
            <!-- 图片预览区,仅图片改写时显示 -->
            <div v-if="writeMode === 'image'" class="config-item">
              <div class="config-label">{{ t('courseCenter.PPTReview') }}:</div>
              <div class="preview-image flex-center">
                <el-image
                  :src="imageUrl"
                  :style="{
                    width: '320px',
                    height: imageUrl ? '320px / (imageWidth / imageHeight)' : '0'
                  }"
                  :preview-src-list="[imageUrl]"
                  :initial-index="0"
                  fit="contain"
                  preview-teleported
                />
              </div>
            </div>
 
            <!-- 写作风格,仅图片改写和文字改写时显示 -->
            <div v-if="['image', 'text'].includes(writeMode)" class="config-item flex items-center">
              <div class="config-label" style="width: 80px">{{ t('courseCenter.style') }}:</div>
              <el-select
                v-model="writeStyle"
                :placeholder="t('common.selectText') + t('courseCenter.writingStyle')"
              >
                <el-option :label="t('courseCenter.professionalFormal')" value="professional" />
                <el-option :label="t('courseCenter.popularDailyLife')" value="casual" />
                <el-option :label="t('courseCenter.confidenceMotivation')" value="inspiring" />
              </el-select>
            </div>
 
            <div v-if="writeMode === 'image'" class="config-item flex items-center">
              <div class="config-label" style="width: 80px">{{ t('courseCenter.theme') }}:</div>
              <el-input v-model="pageTheme" :placeholder="t('courseCenter.placeholderTheme')" />
            </div>
 
            <!-- 个性化要求,仅图片改写和文字改写时显示 -->
            <div v-if="['image', 'text'].includes(writeMode)" class="config-item flex items-center">
              <div class="config-label" style="width: 80px"
                >{{ t('courseCenter.requirement') }}:</div
              >
              <el-input
                v-model="customRequirements"
                type="textarea"
                :rows="2"
                resize="none"
                :placeholder="t('courseCenter.placeholderRequirement')"
              />
            </div>
 
            <!-- 生成讲稿按钮 -->
            <div class="rewrite-actions">
              <el-button type="primary" :loading="isRewriting" @click="handleRewrite">
                {{ t('courseCenter.generateSpeech') }}
              </el-button>
            </div>
          </div>
          <!-- 中间原始讲稿区 -->
          <div class="output-area flex-col justify-between">
            <div class="original-content flex-col">
              <el-input
                v-model="originalContent"
                type="textarea"
                :rows="25"
                resize="none"
                readonly
                :placeholder="t('courseCenter.originalContent')"
              />
            </div>
          </div>
          <!-- 右侧输出区 -->
          <div class="output-area flex-col justify-between">
            <div class="output-content flex-col">
              <el-input
                v-model="displayContent"
                type="textarea"
                :rows="25"
                resize="none"
                readonly
                :placeholder="
                  isRewriting
                    ? t('courseCenter.speechContent') + '...'
                    : t('courseCenter.rewrittenContent')
                "
              />
            </div>
            <!-- <div class="text-wrapper_17 flex-col">
              <span class="text_41">
                请输入对原文稿拓写的要求,如丰富某部分内容、让文稿更口语化等。也可以直接点击右侧按钮进行标准化拓写
              </span>
            </div> -->
          </div>
        </div>
        <!-- <div class="text-wrapper_18 flex-col"><span class="text_42">确定使用文案</span></div> -->
        <!-- 底部确认按钮 -->
        <div class="footer-actions flex-row justify-end">
          <el-button type="primary" @click="handleConfirm" :disabled="!outputContent || isTyping">
            {{t('courseCenter.confirmUseScript')}}
          </el-button>
        </div>
      </div>
    </div>
  </el-dialog>
</template>
 
<script setup lang="ts">
import { ref } from 'vue'
import { ChatApi } from '@/api/digitalcourse/chat'
import { ElMessage } from 'element-plus'
const { t } = useI18n() // 国际化
const props = defineProps({
  imageUrl: {
    type: String,
    required: true
  },
  title: {
    type: String,
    required: true
  },
  content: {
    type: String,
    required: true
  }
})
 
const emit = defineEmits(['update:modelValue', 'confirm'])
 
const dialogVisible = ref(false)
const rewriteRequirement = ref('')
const rewrittenText = ref('')
 
const writeMode = ref('image') // 默认图片改写
// 使用 props.title 作为 pageTheme 的默认值
const pageTheme = ref(props.title)
const writeStyle = ref('professional')
const customRequirements = ref('')
const originalContent = ref(props.content)
console.log('传入的content:', props.content)
 
const outputContent = ref('') // 存储完整的生成内容
const displayContent = ref('') // 用于显示打字机效果的内容
const isRewriting = ref(false)
const isTyping = ref(false) // 新增打字机效果进行中的状态
 
// 业务编码映射
const BUSINESS_CODES = {
  image: 'BUSI_0003', // 图片改写
  text: 'BUSI_0004', // 文字改写
  expand: 'BUSI_0001', // 扩写
  shrink: 'BUSI_0002' // 缩写
}
 
const open = () => {
  dialogVisible.value = true
  // 每次打开时更新内容
  originalContent.value = props.content
  // 重置其他状态
  writeMode.value = 'image'
  writeStyle.value = 'professional'
  pageTheme.value = props.title
  customRequirements.value = ''
  outputContent.value = ''
  displayContent.value = ''
  isTyping.value = false
}
 
const handleClose = () => {
  dialogVisible.value = false
  rewriteRequirement.value = ''
  rewrittenText.value = ''
  writeMode.value = 'image'
  pageTheme.value = props.title // 重置为默认值
  customRequirements.value = ''
}
 
const typewriterEffect = (text: string, speed = 30) => {
  // 200ms 输出一个字符,相当于每秒5个字符
  return new Promise<void>((resolve) => {
    let index = 0
    displayContent.value = ''
    isTyping.value = true // 开始打字效果时设置状态
 
    const timer = setInterval(() => {
      if (index < text.length) {
        displayContent.value += text[index]
        index++
      } else {
        clearInterval(timer)
        isTyping.value = false // 打字效果完成后重置状态
        resolve()
      }
    }, speed)
  })
}
 
const handleRewrite = async () => {
  try {
    isRewriting.value = true
    outputContent.value = ''
    displayContent.value = ''
 
    const params = {
      business_code: BUSINESS_CODES[writeMode.value], // 根据写作模式获取对应的业务编码
      content_param: JSON.stringify({
        ppt_title: pageTheme.value,
        ppt_content: props.content,
        image_url: writeMode.value === 'image' ? props.imageUrl : '',
        custom_requirements: ['image', 'text'].includes(writeMode.value)
          ? customRequirements.value
          : '',
        write_style: ['image', 'text'].includes(writeMode.value) ? writeStyle.value : ''
      })
    }
 
    // 获取完整内容
    const chat = await ChatApi.streamChat(params)
    let fullContent = ''
 
    for await (const chunk of chat.iterateStream()) {
      try {
        if (chunk.choices?.[0]?.delta?.content) {
          fullContent += chunk.choices[0].delta.content
        }
      } catch (e) {
        console.error('处理数据块时出错:', e)
      }
    }
 
    outputContent.value = fullContent // 保存完整内容
 
    // 开始打字机效果
    await typewriterEffect(fullContent)
  } catch (error) {
    console.error('改写失败:', error)
    ElMessage.error('改写失败,请重试')
  } finally {
    isRewriting.value = false
  }
}
 
// 组件卸载时清理可能的定时器
onUnmounted(() => {
  displayContent.value = ''
  outputContent.value = ''
  isTyping.value = false
})
 
const handleConfirm = () => {
  if (!outputContent.value || isTyping.value) {
    ElMessage.warning('请等待讲稿生成完成')
    return
  }
  emit('confirm', outputContent.value)
  handleClose()
}
 
defineExpose({
  open
})
</script>
 
<style scoped lang="scss">
.footer-actions {
  margin-top: 10px;
  border-top: 1px solid #eee;
}
 
// 添加打字机效果的光标样式
:deep(.el-textarea__inner) {
  &::after {
    content: '|';
    animation: cursor-blink 1s infinite;
  }
}
 
.left-area {
  width: 320px; // 修改为固定宽度
  min-width: 320px; // 添加最小宽度确保不会被压缩
  padding: 20px;
  border-right: 1px solid #eee;
  display: flex;
  flex-direction: column;
 
  .title {
    font-size: 16px;
    font-weight: bold;
    margin-bottom: 20px;
    color: #333;
  }
 
  .preview-image {
    flex: 1; // 占据剩余空间
    margin-bottom: 20px;
  }
 
  .rewrite-actions {
    height: 60px; // 给按钮区域固定高度
    margin-top: auto;
  }
 
  .config-item {
    width: '320px';
    margin-bottom: 20px;
 
    .config-label {
      font-size: 14px;
      color: #606266;
      margin-bottom: 0px;
    }
 
    .preview-image {
      margin: 10px 0;
      border: 1px solid #eee;
      border-radius: 4px;
      padding: 10px;
    }
 
    :deep(.el-radio-group) {
      display: flex;
      flex-wrap: wrap;
      gap: 15px;
 
      .el-radio {
        margin-right: 0;
      }
    }
  }
}
 
// 居中布局的通用类
.flex-center {
  display: flex;
  justify-content: center;
  align-items: center;
}
 
.flex-col {
  display: flex;
  flex-direction: column;
}
.flex-row {
  display: flex;
  flex-direction: row;
}
// .rewriter-container {
//   background-color: rgba(0, 0, 0, 0.71);
//   height: 1080px;
//   width: 1920px;
//   position: absolute;
//   left: 0;
//   top: 0;
// }
.content-wrapper {
  background-color: rgba(255, 255, 255, 1);
  border-radius: 16px;
  width: 100%;
  height: 588px;
  margin: 0px 0 0 0px;
}
 
.group_14 {
  background-color: rgba(247, 249, 251, 1);
  width: 100%;
  height: 68px;
}
 
.text_37 {
  width: 32px;
  height: 28px;
  overflow-wrap: break-word;
  color: rgba(51, 51, 51, 1);
  font-size: 16px;
  font-family: MiSans VF-Semibold;
  font-weight: 600;
  text-align: left;
  white-space: nowrap;
  line-height: 28px;
  margin: 20px 0 0 24px;
}
 
.label_14 {
  width: 24px;
  height: 24px;
  margin: 22px 24px 0 0;
}
 
.main-content {
  background-color: rgba(236, 240, 244, 1);
  width: 100%;
  height: 554px;
}
 
.text_38 {
  width: 279px;
  height: 168px;
  overflow-wrap: break-word;
  color: rgba(102, 102, 102, 1);
  font-size: 14px;
  font-family: PingFang SC-Regular;
  font-weight: NaN;
  text-align: left;
  line-height: 150px;
  margin: 20px 0 0 24px;
}
 
.feature-area {
  background-color: rgba(255, 255, 255, 1);
  border-radius: 12px;
  width: 140px;
  height: 444px;
  border: 1px solid rgba(234, 234, 234, 1);
  margin-left: 20px;
  justify-content: flex-center;
}
 
.section_5 {
  background-color: rgba(239, 248, 253, 1);
  border-radius: 12px;
  width: 100px;
  height: 100px;
  border: 1px solid rgba(50, 115, 246, 1);
  justify-content: flex-center;
  margin: 32px 0 0 20px;
}
 
.label_15 {
  width: 32px;
  height: 32px;
  margin: 20px 0 0 34px;
}
 
.text_39 {
  width: 56px;
  height: 20px;
  overflow-wrap: break-word;
  color: rgba(50, 115, 246, 1);
  font-size: 14px;
  font-family: PingFang SC-Semibold;
  font-weight: 600;
  text-align: center;
  white-space: nowrap;
  line-height: 14px;
  margin: 8px 0 20px 22px;
}
 
.feature-item {
  background-color: rgba(249, 249, 249, 1);
  border-radius: 12px;
  width: 100px;
  height: 100px;
  margin: 40px 0 0 20px;
}
 
.align-center {
  align-items: center; // flex 布局下的子元素水平居中
}
 
.feature-icon-wrapper {
  width: 70px;
  height: 60px;
  margin: 20px 0 0 15px;
  .feature-icon {
    width: 32px;
    height: 32px;
    margin: 0 auto; // 图片水平居中
  }
 
  .text-group_11 {
    text-align: center; // 文字水平居中
  }
}
 
.feature-icon {
  width: 32px;
  height: 32px;
  margin-left: 19px;
}
 
.text-group_11 {
  width: 70px;
  height: 20px;
  overflow-wrap: break-word;
  color: rgba(51, 51, 51, 1);
  font-size: 14px;
  font-family: PingFang SC-Regular;
  font-weight: NaN;
  text-align: center;
  white-space: nowrap;
  line-height: 14px;
  margin-top: 8px;
}
 
.section_7 {
  background-color: rgba(249, 249, 249, 1);
  border-radius: 12px;
  width: 100px;
  height: 100px;
  margin: 40px 0 32px 20px;
}
 
.image-text_24 {
  width: 56px;
  height: 60px;
  margin: 20px 0 0 22px;
}
 
.label_17 {
  width: 32px;
  height: 32px;
  margin-left: 12px;
}
 
.text-group_12 {
  width: 56px;
  height: 20px;
  overflow-wrap: break-word;
  color: rgba(51, 51, 51, 1);
  font-size: 14px;
  font-family: PingFang SC-Regular;
  font-weight: NaN;
  text-align: center;
  white-space: nowrap;
  line-height: 14px;
  margin-top: 8px;
}
 
.output-area {
  width: calc(100% - 16px); // 减去左右各8px的边距
  height: calc(100% - 20px); // 减去上下各8px的边距
  margin: 8px; // 四周统一8px边距
}
.output-content {
  background-color: rgba(255, 255, 255, 1);
  border-radius: 8px;
  height: 100%;
  border: 1px solid rgba(234, 234, 234, 1);
  width: 100%;
}
 
.text_40 {
  width: 84px;
  height: 21px;
  overflow-wrap: break-word;
  color: rgba(153, 153, 153, 1);
  font-size: 14px;
  font-family: PingFang SC-Regular;
  font-weight: NaN;
  text-align: left;
  white-space: nowrap;
  line-height: 150px;
  margin: 20px 0 0 20px;
}
 
.text-wrapper_17 {
  background-color: rgba(255, 255, 255, 1);
  border-radius: 12px;
  height: 74px;
  border: 1px solid rgba(234, 234, 234, 1);
  margin-top: 12px;
  width: 531px;
}
 
.text_41 {
  width: 495px;
  height: 42px;
  overflow-wrap: break-word;
  color: rgba(153, 153, 153, 1);
  font-size: 14px;
  font-family: PingFang SC-Regular;
  font-weight: NaN;
  text-align: left;
  line-height: 150px;
  margin: 16px 0 0 16px;
}
 
.text-wrapper_18 {
  background-color: rgba(50, 115, 246, 1);
  border-radius: 8px;
  height: 40px;
  width: 140px;
  margin: 16px 0 20px 870px;
}
 
.text_42 {
  width: 84px;
  height: 20px;
  overflow-wrap: break-word;
  color: rgba(255, 255, 255, 1);
  font-size: 14px;
  font-family: PingFang SC-Semibold;
  font-weight: 600;
  text-align: center;
  white-space: nowrap;
  line-height: 14px;
  margin: 10px 0 0 28px;
}
// 样式保持不变,只是修改了一些类名
.script-rewriter {
  display: flex;
  flex-direction: column;
  height: 80vh;
 
  .preview-image {
    width: 600px;
    margin: 0 auto;
 
    :deep(.el-image) {
      width: 100%;
      display: block;
    }
  }
}
</style>