123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- 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<unknown>}
- */
- 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<unknown>}
- */
- 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<unknown>}
- */
- 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
- };
|