Material 素材选择器
企业微信素材选择器组件库,包含文本、图片、视频、链接、小程序、文件等多种素材类型的选择器组件。
组件路径:@/components/Common/Material/
组件导出
import {
MaterialBtn, // 素材按钮选择器
MaterialImage, // 图片选择器
MaterialVideo, // 视频选择器
MaterialText, // 文本选择器
MaterialLink, // 链接选择器
MaterialMini, // 小程序选择器
MaterialFile, // 文件选择器
MaterialBtnPreview, // 素材按钮预览
MaterialReplyModal, // 素材回复弹窗
} from '@/components/Common/Material';组件列表
| 组件名 | 说明 |
|---|---|
MaterialBtn | 素材类型按钮选择器 |
MaterialText | 文本素材选择器 |
MaterialImage | 图片素材选择器 |
MaterialVideo | 视频素材选择器 |
MaterialLink | 链接素材选择器 |
MaterialMini | 小程序素材选择器 |
MaterialFile | 文件素材选择器 |
MaterialBtnPreview | 素材选择预览组件 |
MaterialReplyModal | 素材回复弹窗 |
MaterialBtn 素材按钮
素材类型按钮,点击后触发选择对应类型的素材。
Props 属性
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| hideTypeList | string[] | [] | 隐藏的素材类型列表 |
Emits 事件
| 事件名 | 参数 | 说明 |
|---|---|---|
| handleMaterial | item: { name, value, replyType } | 选择素材类型时触发 |
可选类型
| value | name | replyType |
|---|---|---|
text | 文字 | 2 |
image | 图片 | 3 |
video | 视频 | 6 |
link | 链接 | 8 |
mini | 小程序 | 9 |
file | 文件 | 10 |
示例
<template>
<MaterialBtn
:hideTypeList="['file', 'mini']"
@handleMaterial="handleMaterial"
/>
</template>
<script setup lang="ts">
import { MaterialBtn } from '@/components/Common/Material';
function handleMaterial(item) {
console.log('选中的素材类型:', item);
// { name: '图片', value: 'image', replyType: '3' }
}
</script>MaterialText 文本选择器
文本素材选择弹窗。
Props 属性
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| title | string | '文字选择器' | 弹窗标题 |
| width | number | 1280 | 弹窗宽度 |
| multiple | boolean | false | 是否支持多选 |
| value | string | - | 已选中的值(JSON 字符串) |
Emits 事件
| 事件名 | 参数 | 说明 |
|---|---|---|
| update:value | value: string | 选中值变化时触发 |
| handleOk | data: array | 确认选择时触发,返回选中项数组 |
Exposed Methods
| 方法名 | 说明 |
|---|---|
| show() | 打开弹窗 |
| handleCancel() | 关闭弹窗 |
示例
<template>
<a-button @click="openTextModal">选择文本</a-button>
<MaterialText ref="textRef" v-model:value="textValue" @handle-ok="handleTextOk" />
</template>
<script setup lang="ts">
import { ref } from 'vue';
import { MaterialText } from '@/components/Common/Material';
const textRef = ref<InstanceType<typeof MaterialText>>();
const textValue = ref('');
function openTextModal() {
textRef.value?.show();
}
function handleTextOk(data) {
console.log('选中的文本素材:', data);
}
</script>MaterialImage 图片选择器
图片素材选择弹窗。
Props 属性
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| value | string | - | 已选中的值(JSON 字符串) |
| title | string | '图片选择器' | 弹窗标题 |
| width | number | 1280 | 弹窗宽度 |
| multiple | boolean | false | 是否支持多选 |
| keyName | string|number | 'id' | 作为返回 ID 的字段名 |
| isMax | boolean | false | 是否限制最大选择数量 |
| numberMax | number | 0 | 最大选择数量 |
Emits 事件
| 事件名 | 参数 | 说明 |
|---|---|---|
| update:value | value: string | 选中值变化时触发(JSON 字符串) |
| handleOk | data: materialItemType[] | 确认选择时触发 |
| update:ids | ids: string | 选中项的 ID(逗号分隔) |
示例
<template>
<a-button @click="openImageModal">选择图片</a-button>
<MaterialImage
ref="imageRef"
v-model:value="imageValue"
:multiple="true"
:isMax="true"
:numberMax="9"
@handle-ok="handleImageOk"
/>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import { MaterialImage } from '@/components/Common/Material';
const imageRef = ref<InstanceType<typeof MaterialImage>>();
const imageValue = ref('');
function openImageModal() {
imageRef.value?.show();
}
function handleImageOk(data) {
console.log('选中的图片素材:', data);
}
</script>MaterialVideo 视频选择器
视频素材选择弹窗。
Props 属性
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| title | string | '视频选择器' | 弹窗标题 |
| width | number | 1280 | 弹窗宽度 |
| multiple | boolean | false | 是否支持多选 |
| keyName | string|number | 'id' | 作为返回 ID 的字段名 |
| isMax | boolean | false | 是否限制最大选择数量 |
| numberMax | number | 0 | 最大选择数量 |
Emits 事件
| 事件名 | 参数 | 说明 |
|---|---|---|
| update:value | value: string | 选中值变化时触发 |
| handleOk | data: array | 确认选择时触发 |
| update:ids | ids: string | 选中项的 ID(逗号分隔) |
MaterialLink 链接选择器
链接素材选择弹窗。
Props 属性
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| title | string | '链接选择器' | 弹窗标题 |
| width | number | 1280 | 弹窗宽度 |
| multiple | boolean | false | 是否支持多选 |
| value | string | - | 已选中的值(JSON 字符串) |
Emits 事件
| 事件名 | 参数 | 说明 |
|---|---|---|
| update:value | value: string | 选中值变化时触发 |
| handleOk | data: array | 确认选择时触发 |
MaterialMini 小程序选择器
小程序素材选择弹窗。
Props 属性
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| title | string | '小程序选择器' | 弹窗标题 |
| width | number | 1280 | 弹窗宽度 |
| multiple | boolean | false | 是否支持多选 |
| keyName | string|number | 'id' | 作为返回 ID 的字段名 |
Emits 事件
| 事件名 | 参数 | 说明 |
|---|---|---|
| update:value | value: string | 选中值变化时触发 |
| handleOk | data: array | 确认选择时触发 |
| update:ids | ids: string | 选中项的 ID(逗号分隔) |
MaterialFile 文件选择器
文件素材选择弹窗。
Props 属性
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| title | string | '文件选择器' | 弹窗标题 |
| width | number | 1280 | 弹窗宽度 |
| multiple | boolean | false | 是否支持多选 |
| keyName | string|number | 'id' | 作为返回 ID 的字段名 |
Emits 事件
| 事件名 | 参数 | 说明 |
|---|---|---|
| update:value | value: string | 选中值变化时触发 |
| handleOk | data: array | 确认选择时触发 |
| update:ids | ids: string | 选中项的 ID(逗号分隔) |
MaterialBtnPreview 素材按钮预览
带素材列表预览的按钮组件,支持排序和删除。
Props 属性
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| value | string | - | 素材值(JSON 字符串) |
| formData | object | - | 表单数据对象 |
| number | object | - | 数量限制配置 |
| keyName | string | 'materialInfo' | formData 中的字段名 |
| single | boolean | false | 是否单选模式 |
Emits 事件
| 事件名 | 参数 | 说明 |
|---|---|---|
| handleOk | data: array | 选择素材确认时触发 |
| update:value | value: string | 值变化时触发 |
| update:list | list: array | 素材列表变化时触发 |
示例
<template>
<MaterialBtnPreview
v-model:value="materialValue"
v-model:list="materialList"
:formData="formData"
:number="{ max: 9 }"
@handle-ok="handleMaterialOk"
/>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import { MaterialBtnPreview } from '@/components/Common/Material';
const materialValue = ref('');
const materialList = ref([]);
const formData = ref({ materialInfo: '' });
function handleMaterialOk(data) {
console.log('选中的素材:', data);
}
</script>MaterialReplyModal 素材回复弹窗
素材回复选择弹窗,用于选择公众号素材。
Props 属性
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| value | string | '' | 已选中的素材 ID(逗号分隔) |
| multiple | boolean | false | 是否支持多选 |
| query | object | {} | 查询参数 |
Emits 事件
| 事件名 | 参数 | 说明 |
|---|---|---|
| register | - | 注册弹窗 |
| success | ids: string | 选择成功时触发,返回素材 ID |
| update:value | ids: string | 值变化时触发 |
| handleOk | data: array | 确认选择时触发,返回素材数据数组 |
Exposed Methods
| 方法名 | 说明 |
|---|---|
| add() | 打开弹窗 |
| disableSubmit | 布尔值,隐藏确定按钮 |
示例
<template>
<a-button @click="openReplyModal">选择回复素材</a-button>
<MaterialReplyModal
ref="replyRef"
:multiple="true"
@success="handleSuccess"
@handle-ok="handleOk"
/>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import { MaterialReplyModal } from '@/components/Common/Material';
const replyRef = ref<InstanceType<typeof MaterialReplyModal>>();
function openReplyModal() {
replyRef.value?.add();
}
function handleSuccess(ids) {
console.log('素材 ID:', ids);
}
function handleOk(data) {
console.log('素材数据:', data);
}
</script>素材数据类型
materialItemType
interface materialItemType {
id: string;
createBy: string;
createTime: string;
updateBy: any;
updateTime: any;
sysOrgCode: string;
name: string; // 素材名称
url: string; // 素材 URL
groupId: string; // 分组 ID
remark: string; // 备注
thumbUrl: string; // 缩略图 URL
platInfo: string; // 平台信息
size: string; // 文件大小
fileSize: number; // 文件大小(数字)
isDelete: any; // 是否删除
groupId_dictText: string;
type: string; // 素材类型
}完整示例:多素材类型选择
<template>
<div class="material-selector-demo">
<div class="mb-4">
<MaterialBtn @handle-material="handleTypeSelect" />
</div>
<MaterialText ref="textRef" @handle-ok="handleOk" />
<MaterialImage ref="imageRef" @handle-ok="handleOk" />
<MaterialVideo ref="videoRef" @handle-ok="handleOk" />
<MaterialLink ref="linkRef" @handle-ok="handleOk" />
<MaterialMini ref="miniRef" @handle-ok="handleOk" />
<MaterialFile ref="fileRef" @handle-ok="handleOk" />
<div class="mt-4">
<h4>已选素材:</h4>
<pre>{{ selectedMaterial }}</pre>
</div>
</div>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import {
MaterialBtn,
MaterialText,
MaterialImage,
MaterialVideo,
MaterialLink,
MaterialMini,
MaterialFile,
} from '@/components/Common/Material';
const textRef = ref<InstanceType<typeof MaterialText>>();
const imageRef = ref<InstanceType<typeof MaterialImage>>();
const videoRef = ref<InstanceType<typeof MaterialVideo>>();
const linkRef = ref<InstanceType<typeof MaterialLink>>();
const miniRef = ref<InstanceType<typeof MaterialMini>>();
const fileRef = ref<InstanceType<typeof MaterialFile>>();
const selectedMaterial = ref<any>(null);
const componentMap = {
text: textRef,
image: imageRef,
video: videoRef,
link: linkRef,
mini: miniRef,
file: fileRef,
};
function handleTypeSelect(item) {
const ref = componentMap[item.value];
ref.value?.show();
}
function handleOk(data) {
selectedMaterial.value = data;
}
</script>相关文件
- 组件路径:
@/components/Common/Material/ - 类型定义:
@/components/Common/Material/src/props.ts - 素材 API:
@/api/sys/material.ts
相关组件
- [[09. BasicForm 表单]] - 表单容器组件
- [[06. Modal 弹窗]] - 弹窗容器组件
页面信息›
已优化是