| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- /**
- * 后台素材库(file_record)统一封装。
- *
- * - resolveAssetUrl:把素材库返回的 URL 规整为可直接用于 <img>/<video> 的地址
- * (绝对地址原样返回,相对地址拼 VUE_APP_BASE_API)
- * - 其余为素材库 REST 封装,统一走 @/utils/request(已带 Bearer Token 与统一错误提示)
- * 返回值为后端 Result 对象:{ code, message, data }
- */
- import request from './request'
- /**
- * 规整素材 URL:
- * - http(s):// 或 // 开头 → 原样返回(京东云公开地址已是绝对地址)
- * - data: / blob: → 原样返回
- * - 相对路径(/uploads/xxx)→ 拼接 VUE_APP_BASE_API
- */
- export function resolveAssetUrl(url) {
- if (!url) return ''
- const s = String(url)
- if (/^(https?:)?\/\//i.test(s)) return s
- if (/^(data:|blob:)/i.test(s)) return s
- const baseURL = process.env.VUE_APP_BASE_API || ''
- if (s.charAt(0) === '/') return baseURL + s
- return baseURL + '/' + s
- }
- /** 素材库分页查询 */
- export function listLibrary(params) {
- return request.post('/api/admin/file-record/list', params || {})
- }
- /**
- * 上传到素材库(统一入库 + 打标签)
- *
- * @param {File} file 文件对象
- * @param {Object} options { category, tagIds }
- */
- export function uploadToLibrary(file, options = {}) {
- const category = options.category || 'general'
- const tagIds = options.tagIds || []
- const formData = new FormData()
- formData.append('file', file)
- formData.append('category', category)
- tagIds.forEach(id => {
- if (id !== undefined && id !== null && id !== '') {
- formData.append('tagIds', id)
- }
- })
- // 不手动设置 Content-Type,由浏览器自动带 multipart boundary
- return request.post('/api/admin/file-record/upload', formData)
- }
- /** 标签列表 */
- export function getTags() {
- return request.post('/api/admin/file-record/tags')
- }
- /** 新建标签 */
- export function createTag(data) {
- return request.post('/api/admin/file-record/tag/create', data)
- }
- /** 更新标签 */
- export function updateTag(data) {
- return request.post('/api/admin/file-record/tag/update', data)
- }
- /** 删除标签 */
- export function deleteTag(data) {
- return request.post('/api/admin/file-record/tag/delete', data)
- }
- /** 删除素材 */
- export function deleteFile(data) {
- return request.post('/api/admin/file-record/delete', data)
- }
- /** 更新素材(描述 + 标签) */
- export function updateFile(data) {
- return request.post('/api/admin/file-record/update', data)
- }
- /** 素材库统计 */
- export function getLibraryStats() {
- return request.post('/api/admin/file-record/stats')
- }
- /**
- * 从素材库上传响应中提取 url(兼容 { url } 对象与纯字符串)
- */
- export function extractAssetUrl(res) {
- if (!res) return ''
- const data = res.data !== undefined ? res.data : res
- if (!data) return ''
- if (typeof data === 'string') return data
- return data.url || ''
- }
- export default {
- resolveAssetUrl,
- listLibrary,
- uploadToLibrary,
- getTags,
- createTag,
- updateTag,
- deleteTag,
- deleteFile,
- updateFile,
- getLibraryStats,
- extractAssetUrl
- }
|