import { buildDir } from '@/common/file-utils' import request from '@/common/request' import { fetchToken as ossToken } from '@/api/sys/config/oss' import { fetchToken as cosToken } from '@/api/sys/config/cos' import { fetchDetail as uploadDetail } from '@/api/sys/config/upload' import { fetchToken as qiuiuToken } from '@/api/sys/config/qiniu' /** * 获取网站基本信息 */ function fetchSite() { return new Promise((resolve, reject) => { uploadDetail().then(cfg => { console.log('upload', cfg) resolve(cfg) }).catch(err => { uni.showToast({ title: `获取系统基础设置失败: ${err} `, icon: 'none', duration: 5000 }); reject() }) }) } /** * 文件上传入口 * @param file * @param data * @returns {Promise} */ async function uploadFile(file) { // 获取上传设置 let cfg = await fetchSite() const provider = cfg.provider uni.showLoading({ title: '上传中..' }) return new Promise((resolve, reject) => { // 本地文件上传 if (provider === 'local') { request.upload('/api/common/file/upload', file).then(res => { resolve(res.data.url) uni.hideLoading() }, err => { reject(err) uni.hideLoading() }) } // OSS文件上传 if (provider === 'oss') { ossUpload(file).then(res => { resolve(res) uni.hideLoading() }, err => { reject(err) uni.hideLoading() }) } // 七牛云文件上传 if (provider === 'qiniu') { qiniuUpload(file).then(res => { resolve(res) uni.hideLoading() }, err => { reject(err) uni.hideLoading() }) } // 腾讯云文件上传 if (provider === 'cos') { cosUpload(file).then(res => { resolve(res) uni.hideLoading() }, err => { reject(err) uni.hideLoading() }) } }).catch(err=>{ uni.hideLoading() uni.showToast({ title: `处理上传出现错误: ${err} `, icon: 'none', duration: 5000 }); }) } // OSS上传 function ossUpload(file) { return new Promise((resolve, reject) => { // 获得上传令牌 ossToken().then(res => { const conf = res const dataObj = { 'policy': res.policy, 'OSSAccessKeyId': res.accessId, 'success_action_status': 200, 'signature': res.signature, 'key': buildDir() + getFileExt(file) } console.log('调用upload', dataObj) // 调用 request.upload(conf.host, file, dataObj).then(() => { // 保存截图 const url = conf.url + dataObj.key console.log('上传后的地址', url) resolve(url) }) }).catch(err => { uni.showToast({ title: `获取OSS令牌失败: ${err} `, icon: 'none', duration: 5000 }); reject(err) }) }) } /** * 腾讯云文件上传 * @param file * @returns {Promise} */ function cosUpload(file) { return new Promise((resolve, reject) => { // 获得上传令牌 cosToken().then(res => { const conf = res // 上传后文件目录 const key = buildDir() + getFileExt(file) // #ifdef MP-WEIXIN var COS = require('cos-wx-sdk-v5'); const cos = new COS({ SecretId: res.tmpSecretId, SecretKey: res.tmpSecretKey, XCosSecurityToken: res.sessionToken }) cos.postObject({ Bucket: conf.bucket, Region: conf.region, Key: key, FilePath: file.path }, function(err, data) { console.log(err || data); const url = conf.url + key resolve(url) }); // #endif // #ifndef MP-WEIXIN var COS = require('cos-js-sdk-v5'); const cos = new COS({ SecretId: res.tmpSecretId, SecretKey: res.tmpSecretKey, XCosSecurityToken: res.sessionToken }) cos.putObject({ Bucket: conf.bucket, Region: conf.region, Key: key, Body: file }, function(err, data) { console.log(err || data); const url = conf.url + key resolve(url) }); // #endif }).catch(err => { uni.showToast({ title: `获取COS上传令牌失败: ${err} `, icon: 'none', duration: 5000 }); reject(err) }) }) } /** * 七牛云文件上传 * @param file * @returns {Promise} */ function qiniuUpload(file) { return new Promise((resolve, reject) => { uploadDetail().then(res => { const conf = JSON.parse(res.data) // 获得上传Token qiuiuToken().then(res => { const dataObj = { 'token': res.token, 'key': buildDir() + getFileExt(file) } request.upload(conf.endpoint, file, dataObj).then(() => { // 保存数据 const url = conf.url + dataObj.key resolve(url) }) }).catch(err => { uni.showToast({ title: `获取七牛令牌失败: ${err} `, icon: 'none', duration: 5000 }); reject(err) }) }) }) } /** * 获取文件后缀 * @param file * @returns {string} */ function getFileExt(file) { // 取文件名 let name = file.name || file.path || file.tempFilePath // 获取文件后缀 const ext = name.split('.').pop() if (ext != null && ext !== '') { return '.' + ext.toLowerCase() } return '' } module.exports = { uploadFile: uploadFile };