| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688 |
- <template>
- <el-dialog
- :title="dialogTitle"
- :visible.sync="dialogVisible"
- :width="pickerWidth"
- :append-to-body="true"
- :close-on-click-modal="false"
- @closed="onClosed"
- >
- <div class="asset-picker">
- <!-- 上传区:整块可点击 / 可拖拽 -->
- <div class="ap-upload-row">
- <el-upload
- class="ap-upload-drop"
- drag
- action=""
- :http-request="handleUploadRequest"
- :show-file-list="false"
- :accept="uploadAccept"
- :before-upload="beforeUpload"
- :disabled="uploading"
- >
- <div class="ap-drop-inner" v-loading="uploading">
- <i class="el-icon-upload"></i>
- <div class="ap-drop-text">点击或拖拽文件到此处,上传到素材库</div>
- </div>
- </el-upload>
- <div class="ap-upload-side">
- <el-select
- v-model="uploadTags"
- multiple
- collapse-tags
- filterable
- size="small"
- placeholder="上传后自动打标签(留空则按分类自动打)"
- class="ap-upload-tags"
- >
- <el-option v-for="t in tags" :key="t.id" :label="t.name" :value="t.id">
- <span :style="{ color: t.color }">{{ t.name }}</span>
- </el-option>
- </el-select>
- <div class="ap-upload-hint">
- 入库分类:<el-tag size="mini" type="info">{{ uploadCategoryLabel }}</el-tag>
- <el-tag
- v-for="id in uploadTags"
- :key="id"
- size="mini"
- :style="{ color: colorOf(id), borderColor: colorOf(id) }"
- >{{ nameOf(id) }}</el-tag>
- </div>
- </div>
- </div>
- <!-- 筛选区 -->
- <div class="ap-filter-row">
- <el-radio-group v-model="typeFilter" size="small" @change="onTypeChange">
- <el-radio-button label="">全部</el-radio-button>
- <el-radio-button label="image">图片</el-radio-button>
- <el-radio-button label="video" v-if="showVideoFilter">视频</el-radio-button>
- <el-radio-button label="document">文档</el-radio-button>
- </el-radio-group>
- <el-select
- v-model="categoryFilter"
- size="small"
- clearable
- placeholder="全部分类"
- class="ap-category"
- @change="onCategoryChange"
- >
- <el-option label="全部分类" value="" />
- <el-option
- v-for="c in categoryOptions"
- :key="c.value"
- :label="c.label + '(' + c.count + ')'"
- :value="c.value"
- />
- </el-select>
- <el-input
- v-model="keyword"
- size="small"
- placeholder="搜索文件名 / 描述"
- clearable
- class="ap-search"
- @keyup.enter.native="onSearch"
- @clear="onSearch"
- >
- <i slot="prefix" class="el-icon-search"></i>
- </el-input>
- <el-button size="small" icon="el-icon-search" @click="onSearch">查询</el-button>
- <el-button size="small" icon="el-icon-refresh-right" @click="onRefresh">刷新</el-button>
- </div>
- <!-- 标签筛选 -->
- <div v-if="tags.length > 0" class="ap-tag-row">
- <span class="ap-tag-label">标签:</span>
- <el-tag
- v-for="t in tags"
- :key="t.id"
- size="small"
- class="ap-tag-chip"
- :effect="isTagActive(t.id) ? 'dark' : 'plain'"
- :style="tagChipStyle(t)"
- @click="onTagFilterToggle(t.id)"
- >{{ t.name }}</el-tag>
- <el-tag
- v-if="selectedTagIds.length > 0"
- size="small"
- type="info"
- class="ap-tag-clear"
- @click="clearTagFilter"
- >清除标签筛选</el-tag>
- </div>
- <!-- 网格 -->
- <div v-loading="loading" class="ap-grid-wrap">
- <el-row v-if="records.length > 0" :gutter="10">
- <el-col v-for="r in records" :key="r.id" :span="4" class="ap-grid-col">
- <div
- class="ap-item"
- :class="{ 'is-selected': isSelected(r), 'is-disabled': !isSelected(r) && isMaxReached }"
- @click="toggleSelect(r)"
- >
- <div class="ap-thumb">
- <el-image
- v-if="r.fileType === 'image'"
- :src="thumbUrl(r)"
- fit="cover"
- class="ap-thumb-img"
- :preview-src-list="[thumbUrl(r)]"
- />
- <div v-else class="ap-thumb-doc">
- <i :class="docIcon(r)"></i>
- <span class="ap-thumb-type">{{ r.fileType || 'other' }}</span>
- </div>
- <div v-if="isSelected(r)" class="ap-check">
- <i class="el-icon-circle-check"></i>
- </div>
- </div>
- <div class="ap-item-name" :title="r.originalFilename">{{ r.originalFilename }}</div>
- <div class="ap-item-meta">
- <span>{{ formatSize(r.fileSize) }}</span>
- <span v-if="r.category">{{ r.category }}</span>
- </div>
- <div v-if="(r.tags || []).length > 0" class="ap-item-tags">
- <el-tag
- v-for="t in r.tags"
- :key="t.id"
- size="mini"
- :style="{ color: t.color, borderColor: t.color, background: 'rgba(255,255,255,0.85)' }"
- >{{ t.name }}</el-tag>
- </div>
- </div>
- </el-col>
- </el-row>
- <div v-else-if="!loading" class="ap-empty">
- <i class="el-icon-picture-outline"></i>
- <p>没有匹配的素材,可上传新文件</p>
- </div>
- </div>
- <!-- 分页 -->
- <div class="ap-pagination">
- <el-pagination
- background
- layout="total, prev, pager, next"
- :current-page="page"
- :page-size="pageSize"
- :total="total"
- @current-change="onPageChange"
- ></el-pagination>
- </div>
- </div>
- <div slot="footer">
- <span class="ap-footer-info">已选 {{ selectedRecords.length }} 项</span>
- <el-button @click="dialogVisible = false">取消</el-button>
- <el-button type="primary" :disabled="selectedRecords.length === 0" @click="onConfirm">确定</el-button>
- </div>
- </el-dialog>
- </template>
- <script>
- import { listLibrary, uploadToLibrary, getTags, getLibraryStats, resolveAssetUrl } from '@/utils/asset'
- /** 素材分类 → 中文展示名(标签也按这个规则自动生成) */
- const CATEGORY_LABELS = {
- general: '通用素材',
- products: '商品图片',
- 'product-skus': '商品SKU图',
- articles: '文章配图',
- 'articles-audio': '文章音频',
- activities: '活动图片',
- banners: '轮播图',
- 'guide-packages': '攻略包',
- 'kb-docs': '知识库文档',
- 'kb-images': '知识库图片',
- 'health-reports': '健康报告',
- uploads: '其它上传'
- }
- export default {
- name: 'AssetPicker',
- props: {
- /** 弹窗显隐(配合 :visible.sync 使用) */
- visible: { type: Boolean, default: false },
- /** 单选 / 多选 */
- mode: { type: String, default: 'single' },
- /** 素材类型:image / document / all(决定默认筛选与上传类型) */
- assetType: { type: String, default: 'image' },
- /** 上传入库的素材分类 */
- category: { type: String, default: '' },
- /** 默认打上的标签 id(上传与筛选初始值) */
- defaultTagIds: { type: Array, default: function () { return [] } },
- /** 多选模式下的最大可选数量(0 表示不限) */
- max: { type: Number, default: 0 },
- /** 已选记录(外部回显) */
- value: { type: Array, default: function () { return [] } }
- },
- data() {
- return {
- loading: false,
- uploading: false,
- records: [],
- tags: [],
- page: 1,
- pageSize: 24,
- total: 0,
- keyword: '',
- typeFilter: this.initTypeFilter(),
- categoryFilter: '',
- categories: [],
- selectedTagIds: [],
- uploadTags: [],
- selectedRecords: []
- }
- },
- computed: {
- dialogVisible: {
- get() { return this.visible },
- set(v) { this.$emit('update:visible', v) }
- },
- dialogTitle() {
- const tip = this.mode === 'multiple' ? '(多选)' : '(单选)'
- return '从素材库选择' + tip
- },
- pickerWidth() {
- return this.mode === 'multiple' ? '960px' : '860px'
- },
- uploadCategory() {
- return this.category || 'general'
- },
- uploadCategoryLabel() {
- const key = this.uploadCategory
- const hit = this.categories.find(function (c) { return c.value === key })
- return (hit && hit.label) || CATEGORY_LABELS[key] || key
- },
- categoryOptions() {
- return this.categories
- },
- uploadAccept() {
- if (this.assetType === 'image') return 'image/*'
- if (this.assetType === 'video') return 'video/*'
- if (this.assetType === 'document') return '.pdf,.doc,.docx,.xls,.xlsx,.ppt,.pptx,.txt,.md,.csv'
- return 'image/*,video/*,.pdf,.doc,.docx,.xls,.xlsx,.ppt,.pptx,.txt,.md,.csv'
- },
- showVideoFilter() {
- return this.assetType === 'video' || this.assetType === 'all'
- },
- isMaxReached() {
- return this.mode === 'multiple' && this.max > 0 && this.selectedRecords.length >= this.max
- }
- },
- watch: {
- visible: {
- immediate: true,
- handler(val) {
- if (val) this.open()
- }
- }
- },
- methods: {
- initTypeFilter() {
- if (this.assetType === 'image') return 'image'
- if (this.assetType === 'video') return 'video'
- if (this.assetType === 'document') return 'document'
- return ''
- },
- open() {
- this.page = 1
- this.keyword = ''
- this.typeFilter = this.initTypeFilter()
- this.categoryFilter = ''
- this.selectedTagIds = this.defaultTagIds.slice()
- this.uploadTags = this.defaultTagIds.slice()
- this.selectedRecords = (this.value || []).map(function (r) { return Object.assign({}, r) })
- this.loadCategories()
- this.loadTags().then(this.loadData)
- },
- /** 分类筛选项:来自素材库统计,避免把页面 category 当硬过滤条件导致空列表 */
- loadCategories() {
- return getLibraryStats().then(function (res) {
- if (res.code === 200 && res.data && Array.isArray(res.data.byCategory)) {
- this.categories = res.data.byCategory
- .filter(function (c) { return c.category })
- .map(function (c) {
- return { value: c.category, label: CATEGORY_LABELS[c.category] || c.category, count: Number(c.cnt) || 0 }
- })
- .sort(function (a, b) { return b.count - a.count })
- }
- }.bind(this)).catch(function () {})
- },
- loadTags() {
- return getTags().then(function (res) {
- if (res.code === 200 && Array.isArray(res.data)) {
- this.tags = res.data
- }
- }.bind(this)).catch(function () {})
- },
- loadData() {
- this.loading = true
- return listLibrary({
- page: this.page,
- pageSize: this.pageSize,
- fileType: this.typeFilter || '',
- category: this.categoryFilter || '',
- search: this.keyword || '',
- tagIds: this.selectedTagIds.length > 0 ? this.selectedTagIds : []
- }).then(function (res) {
- if (res.code === 200 && res.data) {
- this.records = res.data.records || []
- this.total = res.data.total || 0
- }
- }.bind(this)).catch(function () {}).finally(function () {
- this.loading = false
- }.bind(this))
- },
- onTypeChange() {
- this.page = 1
- this.loadData()
- },
- onCategoryChange() {
- this.page = 1
- this.loadData()
- },
- isTagActive(tagId) {
- return this.selectedTagIds.indexOf(tagId) !== -1
- },
- tagChipStyle(t) {
- const color = (t && t.color) || '#409EFF'
- return this.isTagActive(t.id)
- ? { backgroundColor: color, borderColor: color, color: '#fff', cursor: 'pointer' }
- : { color: color, borderColor: color, cursor: 'pointer' }
- },
- clearTagFilter() {
- this.selectedTagIds = []
- this.page = 1
- this.loadData()
- },
- onSearch() {
- this.page = 1
- this.loadData()
- },
- onRefresh() {
- this.page = 1
- this.loadData()
- },
- onPageChange(page) {
- this.page = page
- this.loadData()
- },
- onTagFilterToggle(tagId) {
- const idx = this.selectedTagIds.indexOf(tagId)
- if (idx === -1) {
- this.selectedTagIds.push(tagId)
- } else {
- this.selectedTagIds.splice(idx, 1)
- }
- this.page = 1
- this.loadData()
- },
- isSelected(r) {
- return this.selectedRecords.some(function (s) { return s.id === r.id })
- },
- toggleSelect(r) {
- if (this.mode === 'single') {
- this.selectedRecords = [Object.assign({}, r)]
- return
- }
- const idx = this.selectedRecords.findIndex(function (s) { return s.id === r.id })
- if (idx === -1) {
- if (this.isMaxReached) {
- this.$message.warning('最多选择 ' + this.max + ' 项')
- return
- }
- this.selectedRecords.push(Object.assign({}, r))
- } else {
- this.selectedRecords.splice(idx, 1)
- }
- },
- beforeUpload(file) {
- return true
- },
- async handleUploadRequest(options) {
- const file = options && options.file
- if (!file) return
- await this.uploadAndSelect(file)
- },
- async uploadAndSelect(file) {
- if (!file) return
- this.uploading = true
- try {
- const res = await uploadToLibrary(file, {
- category: this.uploadCategory,
- tagIds: this.uploadTags
- })
- if (res.code === 200 && res.data) {
- this.$message.success('已上传到素材库')
- const record = res.data
- this.page = 1
- await this.loadData()
- // 入网格后自动选中
- const merged = this.records.filter(function (r) { return r.id === record.id })
- if (merged.length > 0) {
- const target = merged[0]
- if (this.mode === 'single') {
- this.selectedRecords = [Object.assign({}, target)]
- } else {
- if (!this.isSelected(target) && !this.isMaxReached) {
- this.selectedRecords.push(Object.assign({}, target))
- }
- }
- } else if (!this.isSelected(record)) {
- // 兜底:分页未命中时直接用返回值
- if (this.mode === 'single') {
- this.selectedRecords = [Object.assign({}, record)]
- } else if (!this.isMaxReached) {
- this.selectedRecords.push(Object.assign({}, record))
- }
- }
- } else {
- this.$message.error((res && res.message) || '上传失败')
- }
- } catch (e) {
- // request 拦截器已提示
- } finally {
- this.uploading = false
- }
- },
- onConfirm() {
- this.$emit('select', this.selectedRecords.slice())
- this.dialogVisible = false
- },
- onClosed() {
- this.records = []
- this.total = 0
- this.page = 1
- },
- thumbUrl(r) {
- return resolveAssetUrl(r && r.url)
- },
- docIcon(r) {
- const type = (r && r.fileType) || 'other'
- if (type === 'video') return 'el-icon-video-camera'
- if (type === 'audio') return 'el-icon-headphone'
- return 'el-icon-document'
- },
- formatSize(bytes) {
- if (!bytes) return '-'
- if (bytes < 1024) return bytes + ' B'
- if (bytes < 1024 * 1024) return (bytes / 1024).toFixed(1) + ' KB'
- return (bytes / 1024 / 1024).toFixed(2) + ' MB'
- },
- colorOf(id) {
- const t = this.tags.find(function (x) { return x.id === id })
- return t && t.color ? t.color : '#409EFF'
- },
- nameOf(id) {
- const t = this.tags.find(function (x) { return x.id === id })
- return t ? t.name : ''
- }
- }
- }
- </script>
- <style scoped>
- .asset-picker {
- display: flex;
- flex-direction: column;
- min-height: 480px;
- }
- .ap-upload-row {
- display: flex;
- align-items: stretch;
- gap: 12px;
- margin-bottom: 12px;
- }
- .ap-upload-drop {
- flex: 1;
- min-width: 0;
- }
- .ap-upload-drop ::v-deep .el-upload {
- display: block;
- width: 100%;
- }
- .ap-upload-drop ::v-deep .el-upload-dragger {
- width: 100%;
- height: auto;
- padding: 12px;
- box-sizing: border-box;
- }
- .ap-drop-inner {
- display: flex;
- flex-direction: column;
- align-items: center;
- justify-content: center;
- gap: 4px;
- color: #606266;
- }
- .ap-drop-inner i {
- font-size: 26px;
- color: #c0c4cc;
- }
- .ap-drop-text {
- font-size: 13px;
- }
- .ap-upload-side {
- width: 260px;
- display: flex;
- flex-direction: column;
- justify-content: center;
- gap: 8px;
- }
- .ap-upload-tags {
- width: 100%;
- }
- .ap-upload-hint {
- font-size: 12px;
- color: #909399;
- display: flex;
- align-items: center;
- gap: 6px;
- flex-wrap: wrap;
- }
- .ap-category {
- width: 160px;
- }
- .ap-tag-clear {
- cursor: pointer;
- }
- .ap-filter-row {
- display: flex;
- align-items: center;
- gap: 10px;
- margin-bottom: 10px;
- }
- .ap-search {
- width: 240px;
- }
- .ap-tag-row {
- display: flex;
- align-items: center;
- gap: 8px;
- flex-wrap: wrap;
- margin-bottom: 12px;
- }
- .ap-tag-label {
- font-size: 12px;
- color: #909399;
- }
- .ap-tag-chip {
- padding: 4px 10px;
- font-size: 12px;
- }
- .ap-grid-wrap {
- height: 360px;
- overflow-y: auto;
- border: 1px solid #ebeef5;
- border-radius: 4px;
- padding: 10px;
- background: #fafafa;
- }
- .ap-grid-col {
- margin-bottom: 10px;
- }
- .ap-item {
- border: 1px solid #ebeef5;
- border-radius: 6px;
- background: #fff;
- padding: 6px;
- cursor: pointer;
- transition: box-shadow 0.2s, border-color 0.2s;
- text-align: center;
- }
- .ap-item:hover {
- border-color: #c0c4cc;
- box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
- }
- .ap-item.is-selected {
- border-color: #409EFF;
- box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.25);
- }
- .ap-item.is-disabled {
- cursor: not-allowed;
- opacity: 0.7;
- }
- .ap-thumb {
- position: relative;
- height: 96px;
- display: flex;
- align-items: center;
- justify-content: center;
- background: #f5f7fa;
- border-radius: 4px;
- overflow: hidden;
- }
- .ap-thumb-img {
- width: 100%;
- height: 100%;
- }
- .ap-thumb-doc {
- display: flex;
- flex-direction: column;
- align-items: center;
- color: #909399;
- font-size: 12px;
- gap: 4px;
- }
- .ap-thumb-doc i {
- font-size: 30px;
- }
- .ap-check {
- position: absolute;
- top: 4px;
- right: 4px;
- color: #409EFF;
- font-size: 20px;
- text-shadow: 0 0 4px rgba(255, 255, 255, 0.9);
- }
- .ap-item-name {
- margin-top: 6px;
- font-size: 12px;
- color: #303133;
- overflow: hidden;
- text-overflow: ellipsis;
- white-space: nowrap;
- }
- .ap-item-meta {
- margin-top: 2px;
- font-size: 11px;
- color: #909399;
- display: flex;
- justify-content: center;
- gap: 8px;
- }
- .ap-item-tags {
- margin-top: 4px;
- display: flex;
- flex-wrap: wrap;
- justify-content: center;
- gap: 2px;
- }
- .ap-pagination {
- margin-top: 12px;
- display: flex;
- justify-content: flex-end;
- }
- .ap-empty {
- height: 360px;
- display: flex;
- flex-direction: column;
- align-items: center;
- justify-content: center;
- color: #909399;
- }
- .ap-empty i {
- font-size: 42px;
- margin-bottom: 8px;
- }
- .ap-empty p {
- margin: 0;
- font-size: 13px;
- }
- .ap-footer-info {
- margin-right: 16px;
- font-size: 13px;
- color: #909399;
- }
- </style>
|