You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

167 lines
4.0 KiB

//#ifdef H5
const BASEURL = ''
//#endif
//#ifndef H5
//#endif
import Vue from 'vue'
const BASEURL = Vue.prototype.baseUrl // 取得全局变量
function uploadImage() {
return new Promise((resolve, reject) => {
// 原本的上传图片
// uni.chooseImage({
// count: 1,
// sizeType: ['original', 'compressed'],
// sourceType: ['album', 'camera'],
// 修改为上传图片或者视频
uni.chooseMedia({
count: 1,// 待处理,多个上传需要封装循环上传
mediaType: ['image','video'],
sourceType: ['album', 'camera'],
maxDuration: 30,
camera: 'back',
success(ress) {
if (ress.tempFiles.length > 0) {
console.log(ress,'看下是啥格式')
// 修改--改为选择图片或者视频的接口
// const tempFilePaths = ress.tempFiles[0].path
const tempFilePaths = ress.tempFiles[0].tempFilePath
const size = ress.tempFiles[0].size
if (size < 8388608) {
uni.showLoading({
title:'上传中'
})
// let imageurl = tempFilePaths
let pdfurl = ""
uni.uploadFile({
url: BASEURL+'/api-guarantee/dg-apply-amount-info/uploadFile',
filePath: tempFilePaths,
header: {
// "mini-session": uni.getStorageSync('session'),
"token": uni.getStorageSync('token'),
"Content-Type": "multipart/form-data;boundary=----WebKitFormBoundaryi8lPVoSysovJLNqi",
"Accept": "application/json"
},
name: 'file',
// formData: {
// 'user': 'test'
// },
success(res) {
if (res.statusCode == 500) {
// 判断图片或者视频未做
uni.showToast({
title: '上传失败',
icon: 'none'
})
reject('失败')
} else {
uni.showToast({
title: '上传成功',
icon: 'success'
})
if (res.data) {
let imageurl = JSON.parse(res.data).data[0]
let index= imageurl.lastIndexOf(".");
let ext = imageurl.substr(index+1);
resolve({
imageurl, // 图片地址
ext: ext
})
}
}
}
})
} else {
uni.showToast({
title: '超出限制大小',
icon: "none"
})
}
} else {
uni.showToast({
title: '文件不存在',
icon: "none"
})
}
}
})
})
}
function uploadPdf() {
return new Promise((resolve, reject) => {
uni.chooseMessageFile({
count: 1,
type: 'file',
success(ress) {
if (ress.tempFiles.length > 0) {
if (ress.tempFiles[0].size < 8388608) {
uni.showLoading({
title:'上传中'
})
// let pdfurl = ress.tempFiles[0].name
uni.uploadFile({
url:BASEURL+'/api-guarantee/dg-apply-amount-info/uploadFile',
filePath: ress.tempFiles[0].path,
name: 'file',
header: {
// "mini-session": uni.getStorageSync('session'),
"token": uni.getStorageSync('token'),
"Content-Type": "multipart/form-data; boundary=----WebKitFormBoundaryi8lPVoSysovJLNqi",
"Accept": "application/json"
},
// formData: {
// 'user': 'test'
// },
success(res) {
if (res.statusCode != 500) {
uni.showToast({
title: '文件上传成功',
icon: 'success'
})
if (res.data) {
let pdfurl = JSON.parse(res.data).data[0]
let index= pdfurl.lastIndexOf(".");
let ext = pdfurl.substr(index+1);
resolve({
pdfurl, // 文件地址
ext: ext
})
} else {
reject('失败')
}
} else {
uni.showToast({
title: '服务器错误',
icon: 'none'
})
}
}
})
} else {
uni.showToast({
title: '超出限制大小',
icon: "none"
})
return
}
} else {
uni.showToast({
title: '文件不存在',
icon: "none"
})
}
}
})
})
}
module.exports = {
uploadImage,
uploadPdf
}