Commit 7c9cc916 authored by Damon's avatar Damon

图片队列使用临时路径

parent cfe977aa
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
} }
if (ENV_VAR) { if (ENV_VAR) {
process.uniEnv = {}; process.uniEnv = {};
process.uniEnv['node_env'] = NODE_ENV; process.uniEnv['node_env'] = NODE_ENV;
for (let key in ENV_VAR) { for (let key in ENV_VAR) {
process.uniEnv[key] = ENV_VAR[key]; process.uniEnv[key] = ENV_VAR[key];
} }
......
...@@ -77,7 +77,7 @@ export default { ...@@ -77,7 +77,7 @@ export default {
// 系统自动登录::每次运行小程序重新获取用户信息 // 系统自动登录::每次运行小程序重新获取用户信息
await this.autoLogin(); await this.autoLogin();
this.deleteTempImg() //this.deleteTempImg()
}, },
onShow: async function () { onShow: async function () {
// 应用启动,或从后台进入前台显示 // 应用启动,或从后台进入前台显示
......
This diff is collapsed.
...@@ -87,7 +87,6 @@ ...@@ -87,7 +87,6 @@
if(photo.tempPath.mixWater){ if(photo.tempPath.mixWater){
// 保存水印处理图片到本地 // 保存水印处理图片到本地
path = photo.tempPath.mixWater path = photo.tempPath.mixWater
console.log("path", path)
let result1 = await this.saveLocal(path) let result1 = await this.saveLocal(path)
if(result1){ if(result1){
uni.showToast({ uni.showToast({
......
import uploader from '@/plugins/uploader/index.js' import uploader from '@/plugins/uploader/index.js'
const globalUrl = process.uniEnv || {} const globalUrl = process.uniEnv || {}
const randomChar = function(l, url = "") { const randomChar = function(l, url = "") {
const x = "0123456789qwertyuioplkjhgfdsazxcvbnm"; const x = "0123456789qwertyuioplkjhgfdsazxcvbnm";
let tmp = ""; let tmp = "";
let time = new Date(); let time = new Date();
for (let i = 0; i < l; i++) { for (let i = 0; i < l; i++) {
tmp += x.charAt(Math.ceil(Math.random() * 100000000) % x.length); tmp += x.charAt(Math.ceil(Math.random() * 100000000) % x.length);
} }
return ( return (
"file/" + "file/" +
url + url +
time.getTime() + time.getTime() +
tmp tmp
); );
} }
export default { export default {
name: 'upload', name: 'upload',
components: {}, components: {},
data() { data() {
return { return {
imgList:[], imgList:[],
// 上传文件种类:0--头像、1--证件、2--身份证 // 上传文件种类:0--头像、1--证件、2--身份证
categories: ['avatar', 'cert', 'id'], categories: ['avatar', 'cert', 'id'],
}; };
}, },
created() { created() {
// this.initQiniu() // this.initQiniu()
}, },
methods: { methods: {
async uploadPath(type){ async uploadPath(type){
// 可以直接上传path // 可以直接上传path
const self = this const self = this
let imgList = [] let imgList = []
let options = { let options = {
files:[] // 文件路径 files:[] // 文件路径
} }
if(type==='qiniu'){ if(type==='qiniu'){
let res = await uploader.qnFileUpload(options) let res = await uploader.qnFileUpload(options)
return return
} }
let res = await uploader.urlFileUpload(options) let res = await uploader.urlFileUpload(options)
}, },
/** /**
* 存储图片到任务队列 * 存储图片到任务队列
* @param {Array} files 图片路径 * @param {Array} files 图片路径
*/ */
async saveToTask(files) { async saveToTask(files) {
// 生成key返回,然后把key放到异步上传的任务队列 // 生成key返回,然后把key放到异步上传的任务队列
let lists = [] let lists = files.map(v => {
for (let i = 0; i < files.length; i++) { return {
const path = files[i] key: randomChar(10),
const row = await this.saveFiles(path) path: v,
lists.push(row) src: ''
} }
})
const data = this.uploadTask // for (let i = 0; i < files.length; i++) {
const saveLists = data.concat(lists) // const path = files[i]
this.$u.vuex('uploadTask', saveLists) // const row = await this.saveFiles(path)
return lists // lists.push(row)
}, // }
/** const data = this.uploadTask
* 选择图片 const saveLists = data.concat(lists)
* @param {Object} data option this.$u.vuex('uploadTask', saveLists)
*/ return lists
async chooseImg(data){ },
const self = this /**
return new Promise((resolve, reject) => { * 选择图片
uni.chooseImage({ * @param {Object} data option
count: data.count || 9, //默认9 */
sizeType: data.sizeType || ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有 async chooseImg(data){
sourceType: data.sourceType || ['album', 'camera'], //从相册选择 const self = this
success: function(res) { return new Promise((resolve, reject) => {
const lists = self.saveToTask(res.tempFilePaths) uni.chooseImage({
resolve(lists) count: data.count || 9, //默认9
}, sizeType: data.sizeType || ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
fail: err => { sourceType: data.sourceType || ['album', 'camera'], //从相册选择
console.log("err", err) success: function(res) {
} const lists = self.saveToTask(res.tempFilePaths)
}) resolve(lists)
}) },
}, fail: err => {
// 保存图片到本地 console.log("err", err)
saveFiles(path) { }
return new Promise((resolve, reject) => { })
uni.saveFile({ })
tempFilePath: path, },
success(e) { // 保存图片到本地
const { savedFilePath } = e saveFiles(path) {
const img = { return new Promise((resolve, reject) => {
key: randomChar(10), uni.saveFile({
path: savedFilePath, tempFilePath: path,
src: '' success(e) {
} const { savedFilePath } = e
resolve(img) const img = {
}, key: randomChar(10),
fail: reject path: savedFilePath,
}) src: ''
}) }
}, resolve(img)
/** },
* 上传图片公用组件 fail: reject
* @param {Object} type 文件上传方式 })
* @param {Object} attr 文件属性:图片种类[category](身份证、证件、头像); })
* 图片上传其他配置[options】(如,选择图片或拍摄options.consourceType,['album', 'camera'];)其他 },
*/ /**
async chooseImage(type, attr={}){ * 上传图片公用组件
const self = this * @param {Object} type 文件上传方式
let imgList = [] * @param {Object} attr 文件属性:图片种类[category](身份证、证件、头像);
if(type==='qiniu'){ * 图片上传其他配置[options】(如,选择图片或拍摄options.consourceType,['album', 'camera'];)其他
// 图片种类 */
let category = attr && attr.category || self.categories[0] async chooseImage(type, attr={}){
let config = attr && attr.options || {} // 图片上传参数配置 const self = this
// 七牛上传 let imgList = []
let options = { if(type==='qiniu'){
onProgressUpdate(res){ // 图片种类
let item = { let category = attr && attr.category || self.categories[0]
path:res.path, // 图片临时缓存路径 let config = attr && attr.options || {} // 图片上传参数配置
key:'', // 图片上传成功后相对路径 // 七牛上传
src:'', // 访问图片的绝对路径 let options = {
progress:res.progress // 图片上传进度 onProgressUpdate(res){
} let item = {
if(res.fileIndex<imgList.length){ path:res.path, // 图片临时缓存路径
imgList[res.fileIndex].progress = res.progress key:'', // 图片上传成功后相对路径
}else{ src:'', // 访问图片的绝对路径
imgList.push(item) progress:res.progress // 图片上传进度
} }
imgList[res.fileIndex] if(res.fileIndex<imgList.length){
self.imgList = imgList imgList[res.fileIndex].progress = res.progress
// 多文件上传每个文件进度条 }else{
}, imgList.push(item)
onEachUpdate(res){ }
// 多文件上传每个文件上传成功触发 imgList[res.fileIndex]
if(imgList.length>res.fileIndex){ self.imgList = imgList
imgList[res.fileIndex].key = res.url // 多文件上传每个文件进度条
imgList[res.fileIndex].src = uploader.qiniuTokenObj.visitPrefix + res.url },
} onEachUpdate(res){
self.imgList = imgList // 多文件上传每个文件上传成功触发
}, if(imgList.length>res.fileIndex){
cardType: category, imgList[res.fileIndex].key = res.url
...config imgList[res.fileIndex].src = uploader.qiniuTokenObj.visitPrefix + res.url
} }
self.imgList = imgList
// 不需要进度条等可直接获取 },
let res = await uploader.qnImgUpload(options) cardType: category,
// let imgList = res.map(item=>{ ...config
// return { }
// key:item,
// src:uploader.qiniuTokenObj.visitPrefix + item // 不需要进度条等可直接获取
// } let res = await uploader.qnImgUpload(options)
// }) // let imgList = res.map(item=>{
// this.imgList = imgList // return {
return // key:item,
} // src:uploader.qiniuTokenObj.visitPrefix + item
// 本地服务上传 // }
let res = await uploader.urlUpload() // })
this.imgList = res // this.imgList = imgList
}, return
perviewImg(index) { }
let urls = [] // 本地服务上传
this.imgList.map(item => { let res = await uploader.urlUpload()
urls.push(item.src) this.imgList = res
}) },
uni.previewImage({ perviewImg(index) {
current: index, let urls = []
urls: urls this.imgList.map(item => {
}); urls.push(item.src)
}, })
removeImg(index) { uni.previewImage({
this.imgList.splice(index, 1) current: index,
// this.$emit('image-change', this.imgList) //将图片回传父页面 urls: urls
}, });
},
} removeImg(index) {
} this.imgList.splice(index, 1)
// this.$emit('image-change', this.imgList) //将图片回传父页面
},
}
}
...@@ -32,7 +32,7 @@ export default { ...@@ -32,7 +32,7 @@ export default {
} }
}) })
uni.setStorageSync('delImg', delImg) // 已上传完的图片,会在启动系统的时候做一次清理 // uni.setStorageSync('delImg', delImg) // 已上传完的图片,会在启动系统的时候做一次清理
that.$u.vuex('uploadTask', newTask) that.$u.vuex('uploadTask', newTask)
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment