AssetPicker.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. <template>
  2. <el-dialog
  3. :title="dialogTitle"
  4. :visible.sync="dialogVisible"
  5. :width="pickerWidth"
  6. :append-to-body="true"
  7. :close-on-click-modal="false"
  8. @closed="onClosed"
  9. >
  10. <div class="asset-picker">
  11. <!-- 上传区:整块可点击 / 可拖拽 -->
  12. <div class="ap-upload-row">
  13. <el-upload
  14. class="ap-upload-drop"
  15. drag
  16. action=""
  17. :http-request="handleUploadRequest"
  18. :show-file-list="false"
  19. :accept="uploadAccept"
  20. :before-upload="beforeUpload"
  21. :disabled="uploading"
  22. >
  23. <div class="ap-drop-inner" v-loading="uploading">
  24. <i class="el-icon-upload"></i>
  25. <div class="ap-drop-text">点击或拖拽文件到此处,上传到素材库</div>
  26. </div>
  27. </el-upload>
  28. <div class="ap-upload-side">
  29. <el-select
  30. v-model="uploadTags"
  31. multiple
  32. collapse-tags
  33. filterable
  34. size="small"
  35. placeholder="上传后自动打标签(留空则按分类自动打)"
  36. class="ap-upload-tags"
  37. >
  38. <el-option v-for="t in tags" :key="t.id" :label="t.name" :value="t.id">
  39. <span :style="{ color: t.color }">{{ t.name }}</span>
  40. </el-option>
  41. </el-select>
  42. <div class="ap-upload-hint">
  43. 入库分类:<el-tag size="mini" type="info">{{ uploadCategoryLabel }}</el-tag>
  44. <el-tag
  45. v-for="id in uploadTags"
  46. :key="id"
  47. size="mini"
  48. :style="{ color: colorOf(id), borderColor: colorOf(id) }"
  49. >{{ nameOf(id) }}</el-tag>
  50. </div>
  51. </div>
  52. </div>
  53. <!-- 筛选区 -->
  54. <div class="ap-filter-row">
  55. <el-radio-group v-model="typeFilter" size="small" @change="onTypeChange">
  56. <el-radio-button label="">全部</el-radio-button>
  57. <el-radio-button label="image">图片</el-radio-button>
  58. <el-radio-button label="video" v-if="showVideoFilter">视频</el-radio-button>
  59. <el-radio-button label="document">文档</el-radio-button>
  60. </el-radio-group>
  61. <el-select
  62. v-model="categoryFilter"
  63. size="small"
  64. clearable
  65. placeholder="全部分类"
  66. class="ap-category"
  67. @change="onCategoryChange"
  68. >
  69. <el-option label="全部分类" value="" />
  70. <el-option
  71. v-for="c in categoryOptions"
  72. :key="c.value"
  73. :label="c.label + '(' + c.count + ')'"
  74. :value="c.value"
  75. />
  76. </el-select>
  77. <el-input
  78. v-model="keyword"
  79. size="small"
  80. placeholder="搜索文件名 / 描述"
  81. clearable
  82. class="ap-search"
  83. @keyup.enter.native="onSearch"
  84. @clear="onSearch"
  85. >
  86. <i slot="prefix" class="el-icon-search"></i>
  87. </el-input>
  88. <el-button size="small" icon="el-icon-search" @click="onSearch">查询</el-button>
  89. <el-button size="small" icon="el-icon-refresh-right" @click="onRefresh">刷新</el-button>
  90. </div>
  91. <!-- 标签筛选 -->
  92. <div v-if="tags.length > 0" class="ap-tag-row">
  93. <span class="ap-tag-label">标签:</span>
  94. <el-tag
  95. v-for="t in tags"
  96. :key="t.id"
  97. size="small"
  98. class="ap-tag-chip"
  99. :effect="isTagActive(t.id) ? 'dark' : 'plain'"
  100. :style="tagChipStyle(t)"
  101. @click="onTagFilterToggle(t.id)"
  102. >{{ t.name }}</el-tag>
  103. <el-tag
  104. v-if="selectedTagIds.length > 0"
  105. size="small"
  106. type="info"
  107. class="ap-tag-clear"
  108. @click="clearTagFilter"
  109. >清除标签筛选</el-tag>
  110. </div>
  111. <!-- 网格 -->
  112. <div v-loading="loading" class="ap-grid-wrap">
  113. <el-row v-if="records.length > 0" :gutter="10">
  114. <el-col v-for="r in records" :key="r.id" :span="4" class="ap-grid-col">
  115. <div
  116. class="ap-item"
  117. :class="{ 'is-selected': isSelected(r), 'is-disabled': !isSelected(r) && isMaxReached }"
  118. @click="toggleSelect(r)"
  119. >
  120. <div class="ap-thumb">
  121. <el-image
  122. v-if="r.fileType === 'image'"
  123. :src="thumbUrl(r)"
  124. fit="cover"
  125. class="ap-thumb-img"
  126. :preview-src-list="[thumbUrl(r)]"
  127. />
  128. <div v-else class="ap-thumb-doc">
  129. <i :class="docIcon(r)"></i>
  130. <span class="ap-thumb-type">{{ r.fileType || 'other' }}</span>
  131. </div>
  132. <div v-if="isSelected(r)" class="ap-check">
  133. <i class="el-icon-circle-check"></i>
  134. </div>
  135. </div>
  136. <div class="ap-item-name" :title="r.originalFilename">{{ r.originalFilename }}</div>
  137. <div class="ap-item-meta">
  138. <span>{{ formatSize(r.fileSize) }}</span>
  139. <span v-if="r.category">{{ r.category }}</span>
  140. </div>
  141. <div v-if="(r.tags || []).length > 0" class="ap-item-tags">
  142. <el-tag
  143. v-for="t in r.tags"
  144. :key="t.id"
  145. size="mini"
  146. :style="{ color: t.color, borderColor: t.color, background: 'rgba(255,255,255,0.85)' }"
  147. >{{ t.name }}</el-tag>
  148. </div>
  149. </div>
  150. </el-col>
  151. </el-row>
  152. <div v-else-if="!loading" class="ap-empty">
  153. <i class="el-icon-picture-outline"></i>
  154. <p>没有匹配的素材,可上传新文件</p>
  155. </div>
  156. </div>
  157. <!-- 分页 -->
  158. <div class="ap-pagination">
  159. <el-pagination
  160. background
  161. layout="total, prev, pager, next"
  162. :current-page="page"
  163. :page-size="pageSize"
  164. :total="total"
  165. @current-change="onPageChange"
  166. ></el-pagination>
  167. </div>
  168. </div>
  169. <div slot="footer">
  170. <span class="ap-footer-info">已选 {{ selectedRecords.length }} 项</span>
  171. <el-button @click="dialogVisible = false">取消</el-button>
  172. <el-button type="primary" :disabled="selectedRecords.length === 0" @click="onConfirm">确定</el-button>
  173. </div>
  174. </el-dialog>
  175. </template>
  176. <script>
  177. import { listLibrary, uploadToLibrary, getTags, getLibraryStats, resolveAssetUrl } from '@/utils/asset'
  178. /** 素材分类 → 中文展示名(标签也按这个规则自动生成) */
  179. const CATEGORY_LABELS = {
  180. general: '通用素材',
  181. products: '商品图片',
  182. 'product-skus': '商品SKU图',
  183. articles: '文章配图',
  184. 'articles-audio': '文章音频',
  185. activities: '活动图片',
  186. banners: '轮播图',
  187. 'guide-packages': '攻略包',
  188. 'kb-docs': '知识库文档',
  189. 'kb-images': '知识库图片',
  190. 'health-reports': '健康报告',
  191. uploads: '其它上传'
  192. }
  193. export default {
  194. name: 'AssetPicker',
  195. props: {
  196. /** 弹窗显隐(配合 :visible.sync 使用) */
  197. visible: { type: Boolean, default: false },
  198. /** 单选 / 多选 */
  199. mode: { type: String, default: 'single' },
  200. /** 素材类型:image / document / all(决定默认筛选与上传类型) */
  201. assetType: { type: String, default: 'image' },
  202. /** 上传入库的素材分类 */
  203. category: { type: String, default: '' },
  204. /** 默认打上的标签 id(上传与筛选初始值) */
  205. defaultTagIds: { type: Array, default: function () { return [] } },
  206. /** 多选模式下的最大可选数量(0 表示不限) */
  207. max: { type: Number, default: 0 },
  208. /** 已选记录(外部回显) */
  209. value: { type: Array, default: function () { return [] } }
  210. },
  211. data() {
  212. return {
  213. loading: false,
  214. uploading: false,
  215. records: [],
  216. tags: [],
  217. page: 1,
  218. pageSize: 24,
  219. total: 0,
  220. keyword: '',
  221. typeFilter: this.initTypeFilter(),
  222. categoryFilter: '',
  223. categories: [],
  224. selectedTagIds: [],
  225. uploadTags: [],
  226. selectedRecords: []
  227. }
  228. },
  229. computed: {
  230. dialogVisible: {
  231. get() { return this.visible },
  232. set(v) { this.$emit('update:visible', v) }
  233. },
  234. dialogTitle() {
  235. const tip = this.mode === 'multiple' ? '(多选)' : '(单选)'
  236. return '从素材库选择' + tip
  237. },
  238. pickerWidth() {
  239. return this.mode === 'multiple' ? '960px' : '860px'
  240. },
  241. uploadCategory() {
  242. return this.category || 'general'
  243. },
  244. uploadCategoryLabel() {
  245. const key = this.uploadCategory
  246. const hit = this.categories.find(function (c) { return c.value === key })
  247. return (hit && hit.label) || CATEGORY_LABELS[key] || key
  248. },
  249. categoryOptions() {
  250. return this.categories
  251. },
  252. uploadAccept() {
  253. if (this.assetType === 'image') return 'image/*'
  254. if (this.assetType === 'video') return 'video/*'
  255. if (this.assetType === 'document') return '.pdf,.doc,.docx,.xls,.xlsx,.ppt,.pptx,.txt,.md,.csv'
  256. return 'image/*,video/*,.pdf,.doc,.docx,.xls,.xlsx,.ppt,.pptx,.txt,.md,.csv'
  257. },
  258. showVideoFilter() {
  259. return this.assetType === 'video' || this.assetType === 'all'
  260. },
  261. isMaxReached() {
  262. return this.mode === 'multiple' && this.max > 0 && this.selectedRecords.length >= this.max
  263. }
  264. },
  265. watch: {
  266. visible: {
  267. immediate: true,
  268. handler(val) {
  269. if (val) this.open()
  270. }
  271. }
  272. },
  273. methods: {
  274. initTypeFilter() {
  275. if (this.assetType === 'image') return 'image'
  276. if (this.assetType === 'video') return 'video'
  277. if (this.assetType === 'document') return 'document'
  278. return ''
  279. },
  280. open() {
  281. this.page = 1
  282. this.keyword = ''
  283. this.typeFilter = this.initTypeFilter()
  284. this.categoryFilter = ''
  285. this.selectedTagIds = this.defaultTagIds.slice()
  286. this.uploadTags = this.defaultTagIds.slice()
  287. this.selectedRecords = (this.value || []).map(function (r) { return Object.assign({}, r) })
  288. this.loadCategories()
  289. this.loadTags().then(this.loadData)
  290. },
  291. /** 分类筛选项:来自素材库统计,避免把页面 category 当硬过滤条件导致空列表 */
  292. loadCategories() {
  293. return getLibraryStats().then(function (res) {
  294. if (res.code === 200 && res.data && Array.isArray(res.data.byCategory)) {
  295. this.categories = res.data.byCategory
  296. .filter(function (c) { return c.category })
  297. .map(function (c) {
  298. return { value: c.category, label: CATEGORY_LABELS[c.category] || c.category, count: Number(c.cnt) || 0 }
  299. })
  300. .sort(function (a, b) { return b.count - a.count })
  301. }
  302. }.bind(this)).catch(function () {})
  303. },
  304. loadTags() {
  305. return getTags().then(function (res) {
  306. if (res.code === 200 && Array.isArray(res.data)) {
  307. this.tags = res.data
  308. }
  309. }.bind(this)).catch(function () {})
  310. },
  311. loadData() {
  312. this.loading = true
  313. return listLibrary({
  314. page: this.page,
  315. pageSize: this.pageSize,
  316. fileType: this.typeFilter || '',
  317. category: this.categoryFilter || '',
  318. search: this.keyword || '',
  319. tagIds: this.selectedTagIds.length > 0 ? this.selectedTagIds : []
  320. }).then(function (res) {
  321. if (res.code === 200 && res.data) {
  322. this.records = res.data.records || []
  323. this.total = res.data.total || 0
  324. }
  325. }.bind(this)).catch(function () {}).finally(function () {
  326. this.loading = false
  327. }.bind(this))
  328. },
  329. onTypeChange() {
  330. this.page = 1
  331. this.loadData()
  332. },
  333. onCategoryChange() {
  334. this.page = 1
  335. this.loadData()
  336. },
  337. isTagActive(tagId) {
  338. return this.selectedTagIds.indexOf(tagId) !== -1
  339. },
  340. tagChipStyle(t) {
  341. const color = (t && t.color) || '#409EFF'
  342. return this.isTagActive(t.id)
  343. ? { backgroundColor: color, borderColor: color, color: '#fff', cursor: 'pointer' }
  344. : { color: color, borderColor: color, cursor: 'pointer' }
  345. },
  346. clearTagFilter() {
  347. this.selectedTagIds = []
  348. this.page = 1
  349. this.loadData()
  350. },
  351. onSearch() {
  352. this.page = 1
  353. this.loadData()
  354. },
  355. onRefresh() {
  356. this.page = 1
  357. this.loadData()
  358. },
  359. onPageChange(page) {
  360. this.page = page
  361. this.loadData()
  362. },
  363. onTagFilterToggle(tagId) {
  364. const idx = this.selectedTagIds.indexOf(tagId)
  365. if (idx === -1) {
  366. this.selectedTagIds.push(tagId)
  367. } else {
  368. this.selectedTagIds.splice(idx, 1)
  369. }
  370. this.page = 1
  371. this.loadData()
  372. },
  373. isSelected(r) {
  374. return this.selectedRecords.some(function (s) { return s.id === r.id })
  375. },
  376. toggleSelect(r) {
  377. if (this.mode === 'single') {
  378. this.selectedRecords = [Object.assign({}, r)]
  379. return
  380. }
  381. const idx = this.selectedRecords.findIndex(function (s) { return s.id === r.id })
  382. if (idx === -1) {
  383. if (this.isMaxReached) {
  384. this.$message.warning('最多选择 ' + this.max + ' 项')
  385. return
  386. }
  387. this.selectedRecords.push(Object.assign({}, r))
  388. } else {
  389. this.selectedRecords.splice(idx, 1)
  390. }
  391. },
  392. beforeUpload(file) {
  393. return true
  394. },
  395. async handleUploadRequest(options) {
  396. const file = options && options.file
  397. if (!file) return
  398. await this.uploadAndSelect(file)
  399. },
  400. async uploadAndSelect(file) {
  401. if (!file) return
  402. this.uploading = true
  403. try {
  404. const res = await uploadToLibrary(file, {
  405. category: this.uploadCategory,
  406. tagIds: this.uploadTags
  407. })
  408. if (res.code === 200 && res.data) {
  409. this.$message.success('已上传到素材库')
  410. const record = res.data
  411. this.page = 1
  412. await this.loadData()
  413. // 入网格后自动选中
  414. const merged = this.records.filter(function (r) { return r.id === record.id })
  415. if (merged.length > 0) {
  416. const target = merged[0]
  417. if (this.mode === 'single') {
  418. this.selectedRecords = [Object.assign({}, target)]
  419. } else {
  420. if (!this.isSelected(target) && !this.isMaxReached) {
  421. this.selectedRecords.push(Object.assign({}, target))
  422. }
  423. }
  424. } else if (!this.isSelected(record)) {
  425. // 兜底:分页未命中时直接用返回值
  426. if (this.mode === 'single') {
  427. this.selectedRecords = [Object.assign({}, record)]
  428. } else if (!this.isMaxReached) {
  429. this.selectedRecords.push(Object.assign({}, record))
  430. }
  431. }
  432. } else {
  433. this.$message.error((res && res.message) || '上传失败')
  434. }
  435. } catch (e) {
  436. // request 拦截器已提示
  437. } finally {
  438. this.uploading = false
  439. }
  440. },
  441. onConfirm() {
  442. this.$emit('select', this.selectedRecords.slice())
  443. this.dialogVisible = false
  444. },
  445. onClosed() {
  446. this.records = []
  447. this.total = 0
  448. this.page = 1
  449. },
  450. thumbUrl(r) {
  451. return resolveAssetUrl(r && r.url)
  452. },
  453. docIcon(r) {
  454. const type = (r && r.fileType) || 'other'
  455. if (type === 'video') return 'el-icon-video-camera'
  456. if (type === 'audio') return 'el-icon-headphone'
  457. return 'el-icon-document'
  458. },
  459. formatSize(bytes) {
  460. if (!bytes) return '-'
  461. if (bytes < 1024) return bytes + ' B'
  462. if (bytes < 1024 * 1024) return (bytes / 1024).toFixed(1) + ' KB'
  463. return (bytes / 1024 / 1024).toFixed(2) + ' MB'
  464. },
  465. colorOf(id) {
  466. const t = this.tags.find(function (x) { return x.id === id })
  467. return t && t.color ? t.color : '#409EFF'
  468. },
  469. nameOf(id) {
  470. const t = this.tags.find(function (x) { return x.id === id })
  471. return t ? t.name : ''
  472. }
  473. }
  474. }
  475. </script>
  476. <style scoped>
  477. .asset-picker {
  478. display: flex;
  479. flex-direction: column;
  480. min-height: 480px;
  481. }
  482. .ap-upload-row {
  483. display: flex;
  484. align-items: stretch;
  485. gap: 12px;
  486. margin-bottom: 12px;
  487. }
  488. .ap-upload-drop {
  489. flex: 1;
  490. min-width: 0;
  491. }
  492. .ap-upload-drop ::v-deep .el-upload {
  493. display: block;
  494. width: 100%;
  495. }
  496. .ap-upload-drop ::v-deep .el-upload-dragger {
  497. width: 100%;
  498. height: auto;
  499. padding: 12px;
  500. box-sizing: border-box;
  501. }
  502. .ap-drop-inner {
  503. display: flex;
  504. flex-direction: column;
  505. align-items: center;
  506. justify-content: center;
  507. gap: 4px;
  508. color: #606266;
  509. }
  510. .ap-drop-inner i {
  511. font-size: 26px;
  512. color: #c0c4cc;
  513. }
  514. .ap-drop-text {
  515. font-size: 13px;
  516. }
  517. .ap-upload-side {
  518. width: 260px;
  519. display: flex;
  520. flex-direction: column;
  521. justify-content: center;
  522. gap: 8px;
  523. }
  524. .ap-upload-tags {
  525. width: 100%;
  526. }
  527. .ap-upload-hint {
  528. font-size: 12px;
  529. color: #909399;
  530. display: flex;
  531. align-items: center;
  532. gap: 6px;
  533. flex-wrap: wrap;
  534. }
  535. .ap-category {
  536. width: 160px;
  537. }
  538. .ap-tag-clear {
  539. cursor: pointer;
  540. }
  541. .ap-filter-row {
  542. display: flex;
  543. align-items: center;
  544. gap: 10px;
  545. margin-bottom: 10px;
  546. }
  547. .ap-search {
  548. width: 240px;
  549. }
  550. .ap-tag-row {
  551. display: flex;
  552. align-items: center;
  553. gap: 8px;
  554. flex-wrap: wrap;
  555. margin-bottom: 12px;
  556. }
  557. .ap-tag-label {
  558. font-size: 12px;
  559. color: #909399;
  560. }
  561. .ap-tag-chip {
  562. padding: 4px 10px;
  563. font-size: 12px;
  564. }
  565. .ap-grid-wrap {
  566. height: 360px;
  567. overflow-y: auto;
  568. border: 1px solid #ebeef5;
  569. border-radius: 4px;
  570. padding: 10px;
  571. background: #fafafa;
  572. }
  573. .ap-grid-col {
  574. margin-bottom: 10px;
  575. }
  576. .ap-item {
  577. border: 1px solid #ebeef5;
  578. border-radius: 6px;
  579. background: #fff;
  580. padding: 6px;
  581. cursor: pointer;
  582. transition: box-shadow 0.2s, border-color 0.2s;
  583. text-align: center;
  584. }
  585. .ap-item:hover {
  586. border-color: #c0c4cc;
  587. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  588. }
  589. .ap-item.is-selected {
  590. border-color: #409EFF;
  591. box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.25);
  592. }
  593. .ap-item.is-disabled {
  594. cursor: not-allowed;
  595. opacity: 0.7;
  596. }
  597. .ap-thumb {
  598. position: relative;
  599. height: 96px;
  600. display: flex;
  601. align-items: center;
  602. justify-content: center;
  603. background: #f5f7fa;
  604. border-radius: 4px;
  605. overflow: hidden;
  606. }
  607. .ap-thumb-img {
  608. width: 100%;
  609. height: 100%;
  610. }
  611. .ap-thumb-doc {
  612. display: flex;
  613. flex-direction: column;
  614. align-items: center;
  615. color: #909399;
  616. font-size: 12px;
  617. gap: 4px;
  618. }
  619. .ap-thumb-doc i {
  620. font-size: 30px;
  621. }
  622. .ap-check {
  623. position: absolute;
  624. top: 4px;
  625. right: 4px;
  626. color: #409EFF;
  627. font-size: 20px;
  628. text-shadow: 0 0 4px rgba(255, 255, 255, 0.9);
  629. }
  630. .ap-item-name {
  631. margin-top: 6px;
  632. font-size: 12px;
  633. color: #303133;
  634. overflow: hidden;
  635. text-overflow: ellipsis;
  636. white-space: nowrap;
  637. }
  638. .ap-item-meta {
  639. margin-top: 2px;
  640. font-size: 11px;
  641. color: #909399;
  642. display: flex;
  643. justify-content: center;
  644. gap: 8px;
  645. }
  646. .ap-item-tags {
  647. margin-top: 4px;
  648. display: flex;
  649. flex-wrap: wrap;
  650. justify-content: center;
  651. gap: 2px;
  652. }
  653. .ap-pagination {
  654. margin-top: 12px;
  655. display: flex;
  656. justify-content: flex-end;
  657. }
  658. .ap-empty {
  659. height: 360px;
  660. display: flex;
  661. flex-direction: column;
  662. align-items: center;
  663. justify-content: center;
  664. color: #909399;
  665. }
  666. .ap-empty i {
  667. font-size: 42px;
  668. margin-bottom: 8px;
  669. }
  670. .ap-empty p {
  671. margin: 0;
  672. font-size: 13px;
  673. }
  674. .ap-footer-info {
  675. margin-right: 16px;
  676. font-size: 13px;
  677. color: #909399;
  678. }
  679. </style>