Files
frontend/apps/web-antd/src/views/mp/components/wx-reply/wx-reply.vue
2025-11-20 10:34:21 +08:00

275 lines
6.6 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!--
- Copyright (C) 2018-2019
- All rights reserved, Designed By www.joolun.com
芋道源码
移除多余的 rep 为前缀的变量 message 消息更简单
代码优化补充注释提升阅读性
优化消息的临时缓存策略发送消息时只清理被发送消息的 tab不会强制切回到 text 输入
支持发送视频消息时支持新建视频
-->
<script lang="ts" setup>
import type { Reply } from './types';
import { computed, ref, unref, watch } from 'vue';
import { NewsType, ReplyType } from '@vben/constants';
import { IconifyIcon } from '@vben/icons';
import { Row, Tabs } from 'ant-design-vue';
import TabImage from './tab-image.vue';
import TabMusic from './tab-music.vue';
import TabNews from './tab-news.vue';
import TabText from './tab-text.vue';
import TabVideo from './tab-video.vue';
import TabVoice from './tab-voice.vue';
import { createEmptyReply } from './types';
defineOptions({ name: 'WxReplySelect' });
const props = withDefaults(defineProps<Props>(), {
newsType: undefined,
});
const emit = defineEmits<{
(e: 'update:modelValue', v: Reply): void;
}>();
interface Props {
modelValue: Reply | undefined;
newsType?: NewsType;
}
// 提供一个默认的 Reply 对象,避免 undefined 导致的错误
const defaultReply: Reply = {
accountId: -1,
type: ReplyType.Text,
};
const reply = computed<Reply>({
get: () => props.modelValue || defaultReply,
set: (val) => emit('update:modelValue', val),
});
const tabCache = new Map<ReplyType, Reply>(); // 作为多个标签保存各自 Reply 的缓存
const currentTab = ref<ReplyType>(props.modelValue?.type || ReplyType.Text); // 采用独立的 ref 来保存当前 tab避免在 watch 标签变化,对 reply 进行赋值会产生了循环调用
// 监听 modelValue 变化,同步更新 currentTab 和缓存
watch(
() => props.modelValue,
(newValue) => {
if (newValue?.type) {
// 如果类型变化,更新 currentTab
if (newValue.type !== currentTab.value) {
currentTab.value = newValue.type;
}
// 如果 modelValue 有数据,更新对应 tab 的缓存
if (newValue.type) {
tabCache.set(newValue.type, { ...newValue });
}
}
},
{ immediate: true, deep: true },
);
watch(
currentTab,
(newTab, oldTab) => {
// 第一次进入oldTab 为 undefined
// 判断 newTab 是因为 Reply 为 Partial
if (oldTab === undefined || newTab === undefined) {
return;
}
// 保存旧tab的数据到缓存
const oldReply = unref(reply);
// 只有当旧tab的reply有实际数据时才缓存避免缓存空数据
if (oldReply && oldTab === oldReply.type) {
tabCache.set(oldTab, oldReply);
}
// 从缓存里面取出新tab内容有则覆盖Reply没有则创建空Reply
const temp = tabCache.get(newTab);
if (temp) {
reply.value = temp;
} else {
// 如果当前reply的类型就是新tab的类型说明这是从外部传入的数据应该保留
const currentReply = unref(reply);
if (currentReply && currentReply.type === newTab) {
// 这是从外部传入的数据,直接缓存并使用
tabCache.set(newTab, currentReply);
// 不需要修改reply.value因为它已经是正确的了
} else {
// 创建新的空reply
const newData = createEmptyReply(reply);
newData.type = newTab;
reply.value = newData;
}
}
},
{
immediate: true,
},
);
/** 清除除了`type`, `accountId`的字段 */
function clear() {
reply.value = createEmptyReply(reply);
}
defineExpose({
clear,
});
</script>
<template>
<Tabs v-model:active-key="currentTab" type="card">
<!-- 类型 1文本 -->
<Tabs.TabPane :key="ReplyType.Text">
<template #tab>
<Row align="middle">
<IconifyIcon icon="lucide:file-text" class="mr-1" />
文本
</Row>
</template>
<TabText v-model="reply.content" />
</Tabs.TabPane>
<!-- 类型 2图片 -->
<Tabs.TabPane :key="ReplyType.Image">
<template #tab>
<Row align="middle">
<IconifyIcon icon="lucide:image" class="mr-1" />
图片
</Row>
</template>
<TabImage v-model="reply" />
</Tabs.TabPane>
<!-- 类型 3语音 -->
<Tabs.TabPane :key="ReplyType.Voice">
<template #tab>
<Row align="middle">
<IconifyIcon icon="lucide:mic" class="mr-1" />
语音
</Row>
</template>
<TabVoice v-model="reply" />
</Tabs.TabPane>
<!-- 类型 4视频 -->
<Tabs.TabPane :key="ReplyType.Video">
<template #tab>
<Row align="middle">
<IconifyIcon icon="lucide:video" class="mr-1" />
视频
</Row>
</template>
<TabVideo v-model="reply" />
</Tabs.TabPane>
<!-- 类型 5图文 -->
<Tabs.TabPane :key="ReplyType.News">
<template #tab>
<Row align="middle">
<IconifyIcon icon="lucide:newspaper" class="mr-1" />
图文
</Row>
</template>
<TabNews v-model="reply" :news-type="newsType" />
</Tabs.TabPane>
<!-- 类型 6音乐 -->
<Tabs.TabPane :key="ReplyType.Music">
<template #tab>
<Row align="middle">
<IconifyIcon icon="lucide:music" class="mr-1" />
音乐
</Row>
</template>
<TabMusic v-model="reply" />
</Tabs.TabPane>
</Tabs>
</template>
<style lang="scss" scoped>
/** TODO @dylan看看有没适合 tindwind 的哈。 */
.select-item {
width: 280px;
padding: 10px;
margin: 0 auto 10px;
border: 1px solid #eaeaea;
}
.select-item2 {
padding: 10px;
margin: 0 auto 10px;
border: 1px solid #eaeaea;
}
.ope-row {
padding-top: 10px;
text-align: center;
}
.input-margin-bottom {
margin-bottom: 2%;
}
.item-name {
overflow: hidden;
text-overflow: ellipsis;
font-size: 12px;
text-align: center;
white-space: nowrap;
}
.el-form-item__content {
line-height: unset !important;
}
.col-select {
width: 49.5%;
height: 160px;
padding: 50px 0;
border: 1px solid rgb(234 234 234);
}
.col-select2 {
height: 160px;
padding: 50px 0;
border: 1px solid rgb(234 234 234);
}
.col-add {
float: right;
width: 49.5%;
height: 160px;
padding: 50px 0;
border: 1px solid rgb(234 234 234);
}
.avatar-uploader-icon {
width: 100px !important;
height: 100px !important;
font-size: 28px;
line-height: 100px !important;
color: #8c939d;
text-align: center;
border: 1px solid #d9d9d9;
}
.material-img {
width: 100%;
}
.thumb-div {
display: inline-block;
text-align: center;
}
.item-infos {
width: 30%;
margin: auto;
}
</style>