asset.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /**
  2. * 后台素材库(file_record)统一封装。
  3. *
  4. * - resolveAssetUrl:把素材库返回的 URL 规整为可直接用于 <img>/<video> 的地址
  5. * (绝对地址原样返回,相对地址拼 VUE_APP_BASE_API)
  6. * - 其余为素材库 REST 封装,统一走 @/utils/request(已带 Bearer Token 与统一错误提示)
  7. * 返回值为后端 Result 对象:{ code, message, data }
  8. */
  9. import request from './request'
  10. /**
  11. * 规整素材 URL:
  12. * - http(s):// 或 // 开头 → 原样返回(京东云公开地址已是绝对地址)
  13. * - data: / blob: → 原样返回
  14. * - 相对路径(/uploads/xxx)→ 拼接 VUE_APP_BASE_API
  15. */
  16. export function resolveAssetUrl(url) {
  17. if (!url) return ''
  18. const s = String(url)
  19. if (/^(https?:)?\/\//i.test(s)) return s
  20. if (/^(data:|blob:)/i.test(s)) return s
  21. const baseURL = process.env.VUE_APP_BASE_API || ''
  22. if (s.charAt(0) === '/') return baseURL + s
  23. return baseURL + '/' + s
  24. }
  25. /** 素材库分页查询 */
  26. export function listLibrary(params) {
  27. return request.post('/api/admin/file-record/list', params || {})
  28. }
  29. /**
  30. * 上传到素材库(统一入库 + 打标签)
  31. *
  32. * @param {File} file 文件对象
  33. * @param {Object} options { category, tagIds }
  34. */
  35. export function uploadToLibrary(file, options = {}) {
  36. const category = options.category || 'general'
  37. const tagIds = options.tagIds || []
  38. const formData = new FormData()
  39. formData.append('file', file)
  40. formData.append('category', category)
  41. tagIds.forEach(id => {
  42. if (id !== undefined && id !== null && id !== '') {
  43. formData.append('tagIds', id)
  44. }
  45. })
  46. // 不手动设置 Content-Type,由浏览器自动带 multipart boundary
  47. return request.post('/api/admin/file-record/upload', formData)
  48. }
  49. /** 标签列表 */
  50. export function getTags() {
  51. return request.post('/api/admin/file-record/tags')
  52. }
  53. /** 新建标签 */
  54. export function createTag(data) {
  55. return request.post('/api/admin/file-record/tag/create', data)
  56. }
  57. /** 更新标签 */
  58. export function updateTag(data) {
  59. return request.post('/api/admin/file-record/tag/update', data)
  60. }
  61. /** 删除标签 */
  62. export function deleteTag(data) {
  63. return request.post('/api/admin/file-record/tag/delete', data)
  64. }
  65. /** 删除素材 */
  66. export function deleteFile(data) {
  67. return request.post('/api/admin/file-record/delete', data)
  68. }
  69. /** 更新素材(描述 + 标签) */
  70. export function updateFile(data) {
  71. return request.post('/api/admin/file-record/update', data)
  72. }
  73. /** 素材库统计 */
  74. export function getLibraryStats() {
  75. return request.post('/api/admin/file-record/stats')
  76. }
  77. /**
  78. * 从素材库上传响应中提取 url(兼容 { url } 对象与纯字符串)
  79. */
  80. export function extractAssetUrl(res) {
  81. if (!res) return ''
  82. const data = res.data !== undefined ? res.data : res
  83. if (!data) return ''
  84. if (typeof data === 'string') return data
  85. return data.url || ''
  86. }
  87. export default {
  88. resolveAssetUrl,
  89. listLibrary,
  90. uploadToLibrary,
  91. getTags,
  92. createTag,
  93. updateTag,
  94. deleteTag,
  95. deleteFile,
  96. updateFile,
  97. getLibraryStats,
  98. extractAssetUrl
  99. }