9/30
1. 设备管理界面完成 2. 用户管理界面完成 3. 会话列表完成
This commit is contained in:
346
src/components/Editor/cskb.vue
Normal file
346
src/components/Editor/cskb.vue
Normal file
@@ -0,0 +1,346 @@
|
||||
<template>
|
||||
<div class="editor-shell">
|
||||
<EditorToolbar
|
||||
:default-config="toolbarConfig"
|
||||
:editor="editorRef"
|
||||
class="editor-toolbar"
|
||||
mode="default"
|
||||
/>
|
||||
<div :style="styles" class="editor-body">
|
||||
<WangEditor
|
||||
v-model="content"
|
||||
:default-config="editorConfig"
|
||||
class="editor-content"
|
||||
mode="default"
|
||||
@onChange="handleChange"
|
||||
@onCreated="handleCreated"
|
||||
/>
|
||||
</div>
|
||||
<!-- 底部提示、示例、字数统计 -->
|
||||
<div v-if="simpleToolbar" class="editor-tip">
|
||||
<p>请输入标准回复话术,可使用「有序步骤」插入分步指引,例如:</p>
|
||||
<div class="demo-box">
|
||||
<ol>
|
||||
<li>确认设备已通电并处于配网模式(指示灯橙色快闪)</li>
|
||||
<li>打开APP「我的设备」→ 添加设备,选择对应型号</li>
|
||||
<li>输入2.4G Wi-Fi密码并等待约30秒完成连接</li>
|
||||
</ol>
|
||||
</div>
|
||||
<p>以上为示例格式</p>
|
||||
<p>支持插入图片与链接;回复中可引用设备型号,帮助用户快速对号入座</p>
|
||||
<div class="word-count">{{ wordCount }}/2000字</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script lang="ts" setup>
|
||||
import '@wangeditor-next/editor/dist/css/style.css';
|
||||
import type {IDomEditor, IEditorConfig, IToolbarConfig} from '@wangeditor-next/editor';
|
||||
import {Editor as WangEditor, Toolbar as EditorToolbar} from '@wangeditor-next/editor-for-vue';
|
||||
import modal from '@/plugins/modal';
|
||||
import {propTypes} from '@/utils/propTypes';
|
||||
import {globalHeaders} from '@/utils/request';
|
||||
import {shallowRef, ref, computed, watch, onBeforeUnmount} from 'vue';
|
||||
|
||||
const props = defineProps({
|
||||
/* 编辑器的内容 */
|
||||
modelValue: propTypes.string,
|
||||
/* 高度 */
|
||||
height: propTypes.number.def(400),
|
||||
/* 最小高度 */
|
||||
minHeight: propTypes.number.def(400),
|
||||
/* 只读 */
|
||||
readOnly: propTypes.bool.def(false),
|
||||
/* 图片上传文件大小限制(MB) */
|
||||
fileSize: propTypes.number.def(5),
|
||||
/* 视频上传文件大小限制(MB) */
|
||||
videoSize: propTypes.number.def(100),
|
||||
/* 类型(base64格式、url格式) */
|
||||
type: propTypes.string.def('url'),
|
||||
/* 精简工具栏(仅加粗/有序步骤/图片/链接/清除格式) */
|
||||
simpleToolbar: propTypes.bool.def(false)
|
||||
});
|
||||
|
||||
const emit = defineEmits(['update:modelValue']);
|
||||
|
||||
const editorRef = shallowRef<IDomEditor>();
|
||||
const content = ref('');
|
||||
const wordCount = ref(0);
|
||||
|
||||
const baseUrl = import.meta.env.VITE_APP_BASE_API;
|
||||
const uploadOssUrl = baseUrl + '/resource/oss/upload';
|
||||
|
||||
const styles = computed(() => {
|
||||
const style: Record<string, string> = {};
|
||||
if (props.minHeight) {
|
||||
style.minHeight = `${props.minHeight}px`;
|
||||
}
|
||||
if (props.height) {
|
||||
style.height = `${props.height}px`;
|
||||
}
|
||||
return style;
|
||||
});
|
||||
|
||||
const toolbarConfig = computed<Partial<IToolbarConfig>>(() => {
|
||||
if (props.simpleToolbar) {
|
||||
return {
|
||||
toolbarKeys: [
|
||||
'bold',
|
||||
'numberedList',
|
||||
'uploadImage',
|
||||
'insertLink',
|
||||
'clearStyle',
|
||||
],
|
||||
excludeKeys: []
|
||||
};
|
||||
} else {
|
||||
const excludeKeys: string[] = ['fullScreen'];
|
||||
if (!props.type) {
|
||||
excludeKeys.push('uploadImage');
|
||||
excludeKeys.push('uploadVideo');
|
||||
}
|
||||
return {excludeKeys};
|
||||
}
|
||||
});
|
||||
|
||||
/* ==================== OSS 上传 ==================== */
|
||||
|
||||
const uploadToOss = async (file: File): Promise<{ url: string; fileName: string }> => {
|
||||
const formData = new FormData();
|
||||
formData.append('file', file);
|
||||
|
||||
const res = await fetch(uploadOssUrl, {
|
||||
method: 'POST',
|
||||
headers: globalHeaders(),
|
||||
body: formData
|
||||
});
|
||||
const data = await res.json();
|
||||
|
||||
if (data.code === 200) {
|
||||
return data.data;
|
||||
}
|
||||
throw new Error(data.msg || '上传失败');
|
||||
};
|
||||
|
||||
/* ==================== 文件校验 ==================== */
|
||||
|
||||
const validateImageFile = (file: File) => {
|
||||
const allowedTypes = ['image/jpeg', 'image/jpg', 'image/png', 'image/svg', 'image/svg+xml'];
|
||||
if (!allowedTypes.includes(file.type)) {
|
||||
modal.msgError('图片格式错误!');
|
||||
return false;
|
||||
}
|
||||
|
||||
if (props.fileSize) {
|
||||
const isLt = file.size / 1024 / 1024 < props.fileSize;
|
||||
if (!isLt) {
|
||||
modal.msgError(`上传图片大小不能超过 ${props.fileSize} MB!`);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
const validateVideoFile = (file: File) => {
|
||||
const allowedTypes = ['video/mp4', 'video/webm', 'video/ogg'];
|
||||
if (!allowedTypes.includes(file.type)) {
|
||||
modal.msgError('视频格式错误, 请上传 mp4/webm/ogg 格式!');
|
||||
return false;
|
||||
}
|
||||
|
||||
if (props.videoSize) {
|
||||
const isLt = file.size / 1024 / 1024 < props.videoSize;
|
||||
if (!isLt) {
|
||||
modal.msgError(`上传视频大小不能超过 ${props.videoSize} MB!`);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
/* ==================== wangeditor 菜单配置 ==================== */
|
||||
|
||||
const getUploadImageMenuConfig = () => {
|
||||
if (props.type === 'base64') {
|
||||
return {
|
||||
allowedFileTypes: ['image/jpeg', 'image/jpg', 'image/png', 'image/svg+xml'],
|
||||
customUpload(file: File, insertFn: (url: string, alt?: string, href?: string) => void) {
|
||||
if (!validateImageFile(file)) return;
|
||||
|
||||
modal.loading('正在上传图片,请稍候...');
|
||||
const reader = new FileReader();
|
||||
|
||||
reader.onload = () => {
|
||||
insertFn(reader.result as string, file.name);
|
||||
modal.closeLoading();
|
||||
};
|
||||
|
||||
reader.onerror = () => {
|
||||
modal.msgError('图片插入失败');
|
||||
modal.closeLoading();
|
||||
};
|
||||
|
||||
reader.readAsDataURL(file);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
allowedFileTypes: ['image/jpeg', 'image/jpg', 'image/png', 'image/svg+xml'],
|
||||
async customUpload(file: File, insertFn: (url: string, alt?: string, href?: string) => void) {
|
||||
if (!validateImageFile(file)) return;
|
||||
|
||||
modal.loading('正在上传图片,请稍候...');
|
||||
try {
|
||||
const result = await uploadToOss(file);
|
||||
insertFn(result.url, file.name, result.url);
|
||||
} catch {
|
||||
modal.msgError('图片上传失败');
|
||||
} finally {
|
||||
modal.closeLoading();
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
const getUploadVideoMenuConfig = () => ({
|
||||
allowedFileTypes: ['video/mp4', 'video/webm', 'video/ogg'],
|
||||
async customUpload(file: File, insertFn: (url: string, poster?: string) => void) {
|
||||
if (!validateVideoFile(file)) return;
|
||||
|
||||
modal.loading('正在上传视频,请稍候...');
|
||||
try {
|
||||
const result = await uploadToOss(file);
|
||||
insertFn(result.url);
|
||||
} catch {
|
||||
modal.msgError('视频上传失败');
|
||||
} finally {
|
||||
modal.closeLoading();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
const editorConfig = computed<Partial<IEditorConfig>>(() => ({
|
||||
placeholder: props.simpleToolbar ? '请输入标准回复话术...' : '请输入内容',
|
||||
autoFocus: false,
|
||||
maxLength: 2000,
|
||||
MENU_CONF: {
|
||||
uploadImage: getUploadImageMenuConfig() as any,
|
||||
uploadVideo: getUploadVideoMenuConfig() as any
|
||||
},
|
||||
customMenu: [
|
||||
{
|
||||
key: 'numberedList',
|
||||
title: '有序步骤'
|
||||
}
|
||||
]
|
||||
}));
|
||||
|
||||
/* ==================== 事件处理 ==================== */
|
||||
|
||||
const handleChange = () => {
|
||||
if (!editorRef.value) return;
|
||||
wordCount.value = editorRef.value.getText().length;
|
||||
};
|
||||
|
||||
const syncReadOnly = () => {
|
||||
const editor = editorRef.value;
|
||||
if (!editor) return;
|
||||
|
||||
if (props.readOnly) {
|
||||
editor.disable();
|
||||
} else {
|
||||
editor.enable();
|
||||
}
|
||||
};
|
||||
|
||||
const handleCreated = (editor: IDomEditor) => {
|
||||
editorRef.value = editor;
|
||||
syncReadOnly();
|
||||
wordCount.value = editor.getText().length;
|
||||
};
|
||||
|
||||
/* ==================== 双向数据绑定(直接透传HTML) ==================== */
|
||||
|
||||
watch(
|
||||
() => props.modelValue,
|
||||
value => {
|
||||
if (value !== content.value) {
|
||||
content.value = value || '';
|
||||
}
|
||||
},
|
||||
{immediate: true}
|
||||
);
|
||||
|
||||
watch(content, value => {
|
||||
emit('update:modelValue', value);
|
||||
});
|
||||
|
||||
watch(
|
||||
() => props.readOnly,
|
||||
() => {
|
||||
syncReadOnly();
|
||||
}
|
||||
);
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
editorRef.value?.destroy();
|
||||
});
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.editor-shell {
|
||||
border: 1px solid var(--el-border-color);
|
||||
border-radius: var(--el-border-radius-base);
|
||||
overflow: hidden;
|
||||
background: var(--el-bg-color);
|
||||
}
|
||||
|
||||
.editor-toolbar {
|
||||
border-bottom: 1px solid var(--el-border-color);
|
||||
}
|
||||
|
||||
.editor-body {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.editor-content {
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
.editor-shell :deep(.w-e-toolbar) {
|
||||
border: 0 !important;
|
||||
background-color: transparent;
|
||||
}
|
||||
|
||||
.editor-shell :deep(.w-e-text-container) {
|
||||
border: 0 !important;
|
||||
background-color: transparent;
|
||||
}
|
||||
|
||||
.editor-shell :deep(.w-e-text-container [data-slate-editor]) {
|
||||
min-height: inherit;
|
||||
}
|
||||
|
||||
.editor-tip {
|
||||
padding: 12px 16px;
|
||||
font-size: 13px;
|
||||
color: #666;
|
||||
border-top: 1px solid var(--el-border-color);
|
||||
}
|
||||
|
||||
.demo-box {
|
||||
background: #f5f7fa;
|
||||
padding: 8px 12px;
|
||||
margin: 6px 0;
|
||||
border-radius: 4px;
|
||||
}
|
||||
|
||||
.word-count {
|
||||
text-align: right;
|
||||
margin-top: 4px;
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user