HomeLease/api/upload.js
2025-08-21 13:37:29 +08:00

44 lines
952 B
JavaScript

import request from '@/utils/request.js'
/**
* 获取七牛云上传token
* @returns {Promise}
*/
export function getQiniuUploadToken() {
return request({
url: '/common/qiniuToken',
method: 'GET'
})
}
/**
* 上传图片到七牛云
* @param {string} filePath - 文件路径
* @param {string} token - 七牛云上传token
* @param {string} key - 文件key
* @returns {Promise}
*/
export function uploadToQiniu(filePath, token, key) {
return new Promise((resolve, reject) => {
uni.uploadFile({
url: 'https://up-z2.qiniup.com',
filePath: filePath,
name: 'file',
formData: {
token: token,
key: key,
},
success: (res) => {
try {
const data = JSON.parse(res.data)
resolve(data)
} catch (error) {
reject(new Error('响应数据解析失败'))
}
},
fail: (error) => {
reject(error)
},
})
})
}