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 () {
// 应用启动,或从后台进入前台显示 // 应用启动,或从后台进入前台显示
......
<!-- ******************* 文件上传 ******************* --> <!-- ******************* 文件上传 ******************* -->
<template> <template>
<view> <view>
<view class="file-box"> <view class="file-box">
<!-- <view class="u-flex"> <!-- <view class="u-flex">
<view class="txt u-flex-1">{{item.fieldsTitle}}</view> <view class="txt u-flex-1">{{item.fieldsTitle}}</view>
<u-icon name="arrow-right" color="#666" size="28"></u-icon> <u-icon name="arrow-right" color="#666" size="28"></u-icon>
</view> --> </view> -->
<view class="pics u-flex u-flex-wrap"> <view class="pics u-flex u-flex-wrap">
<view class="img-list" v-for="(item,index) in imgList" :key="index"> <view class="img-list" v-for="(item,index) in imgList" :key="index">
<u-image class="pic" width="160" height="160" :src="item.path ? item.path : item.src + '?imageView2/1/w/100/h/100/q/75'" :border-radius="10" @click.stop="prviewImage(item,index)"></u-image> <u-image class="pic" width="160" height="160" :src="item.path ? item.path : item.src + '?imageView2/1/w/100/h/100/q/75'" :border-radius="10" @click.stop="prviewImage(item,index)"></u-image>
<view class="icon-del-box" @click.stop="delFile(index)"> <view class="icon-del-box" @click.stop="delFile(index)">
<view class="icon-del"></view> <view class="icon-del"></view>
<view class="icon-del rotate"></view> <view class="icon-del rotate"></view>
</view> </view>
</view> </view>
<view class="img-list"> <view class="img-list">
<u-image class="pic" width="160rpx" height="160rpx" :border-radius="10" @click="selectUpload" :src="uploadImage"></u-image> <u-image class="pic" width="160rpx" height="160rpx" :border-radius="10" @click="selectUpload" :src="uploadImage"></u-image>
</view> </view>
</view> </view>
<u-action-sheet :list="list" v-model="maskShow" :cancel-btn="true" @click="uploadSelect"></u-action-sheet> <u-action-sheet :list="list" v-model="maskShow" :cancel-btn="true" @click="uploadSelect"></u-action-sheet>
</view> </view>
<take-photo v-if="takeStatus" @close="closeTake" :upload="false" :currentItem="currentItemDate"></take-photo> <take-photo v-if="takeStatus" @close="closeTake" :upload="false" :currentItem="currentItemDate"></take-photo>
</view> </view>
</template> </template>
<script> <script>
import arrMixin from './arrMixin' import arrMixin from './arrMixin'
import baseFile from '@/components/upload/index'; import baseFile from '@/components/upload/index';
import takePhoto from '@/components/take/index.vue' import takePhoto from '@/components/take/index.vue'
// import uploader from '@/plugins/uploader/index.js' // import uploader from '@/plugins/uploader/index.js'
export default { export default {
name: 'XhFiles', name: 'XhFiles',
components: { components: {
'take-photo': takePhoto 'take-photo': takePhoto
}, },
mixins: [arrMixin,baseFile], mixins: [arrMixin,baseFile],
props: { props: {
files: { files: {
type: Array, type: Array,
}, },
partnerCompanyId: { partnerCompanyId: {
type: String, type: String,
default: '' default: ''
}, },
categoryId: { categoryId: {
type: String, type: String,
default: '' default: ''
}, },
brandId: { brandId: {
type: String, type: String,
default: '' default: ''
}, },
}, },
filters:{}, filters:{},
data() { data() {
return { return {
maskShow:false, maskShow:false,
list: [ list: [
{ {
text: '相册' text: '相册'
}, },
{ {
text: '拍照' text: '拍照'
} }
], ],
takeStatus: false, takeStatus: false,
currentItemDate:{}, currentItemDate:{},
} }
}, },
computed: { computed: {
settings() { settings() {
return this.item.settings || [] return this.item.settings || []
}, },
uploadImage() { uploadImage() {
return process.uniEnv.qn_base_url + 'upload-file.png' return process.uniEnv.qn_base_url + 'upload-file.png'
}, },
}, },
mounted() { mounted() {
if(this.value){ if(this.value){
this.imgList = this.value.map(img =>{ this.imgList = this.value.map(img =>{
return {src:img} return {src:img}
}) })
this.setValue() this.setValue()
} }
}, },
methods: { methods: {
selectUpload(){ selectUpload(){
if (this.imgList.length === 10) { if (this.imgList.length === 10) {
return this.$refs.uToast.show({ return this.$refs.uToast.show({
title: '图片已超出最大数量', title: '图片已超出最大数量',
type: 'error', type: 'error',
}) })
} }
// 弹窗展示 // 弹窗展示
this.maskShow = true this.maskShow = true
}, },
uploadSelect(index) { uploadSelect(index) {
if (index === 0) { if (index === 0) {
this.uploadFile('photo') this.uploadFile('photo')
} else { } else {
this.uploadFile() this.uploadFile()
} }
}, },
async uploadFile(type){ async uploadFile(type){
const self = this const self = this
if(type&&type==='photo'){ if(type&&type==='photo'){
// 直接打开相册 // 直接打开相册
let options = { let options = {
sourceType:['album'],//['album', 'camera'] sourceType:['album'],//['album', 'camera']
count: 3, count: 3,
sizeType: ['compressed'] sizeType: ['compressed']
} }
const value = await this.chooseImg(options) const value = await this.chooseImg(options)
self.setTmpValue(value) self.setTmpValue(value)
//await this.chooseImage('qiniu',config) //await this.chooseImage('qiniu',config)
//self.setValue() //self.setValue()
return return
} }
// 使用拍照工具拍摄 // 使用拍照工具拍摄
this.photograph() this.photograph()
this.getWatermark() this.getWatermark()
}, },
setTmpValue(values) { setTmpValue(values) {
this.imgList = this.imgList.concat(values) this.imgList = this.imgList.concat(values)
const dataValue = values.map(v => v.key) const dataValue = values.map(v => v.key)
this.valueChange(dataValue) this.valueChange(dataValue)
}, },
setValue() { setValue() {
const dataValue = [] const dataValue = []
this.imgList.map(item=>{ this.imgList.map(item=>{
dataValue.push(item.src) dataValue.push(item.src)
}) })
this.valueChange(dataValue) this.valueChange(dataValue)
}, },
closeTake(val) { closeTake(val) {
if(val && val.length > 0){ if(val && val.length > 0){
// val.forEach(item=>{ // val.forEach(item=>{
// this.imgList.push(item) // this.imgList.push(item)
// }) // })
// const files = val.map(v => { // const files = val.map(v => {
// return { path: v.path } // return { path: v.path }
// }) // })
const files = val.map(v => v.path) const files = val.map(v => v.path)
this.uploadTakeImg(files) this.uploadTakeImg(files)
// this.setValue() // this.setValue()
} }
this.takeStatus = false this.takeStatus = false
}, },
async uploadTakeImg(files) { async uploadTakeImg(files) {
const value = await this.saveToTask(files) const value = await this.saveToTask(files)
this.setTmpValue(value) this.setTmpValue(value)
// const options = { // const options = {
// files:files, // 必填 临时文件路径 格式: [{path: "图片地址"}] // files:files, // 必填 临时文件路径 格式: [{path: "图片地址"}]
// load: false, //(默认 true 说明:本接口是否提示加载动画) // load: false, //(默认 true 说明:本接口是否提示加载动画)
// maxSize: 300000, //(默认 无 说明:上传的文件最大字节数限制,默认不限制) // maxSize: 300000, //(默认 无 说明:上传的文件最大字节数限制,默认不限制)
// onEachUpdate: res => { // onEachUpdate: res => {
// const value = [uploader.qiniuTokenObj.visitPrefix + res.url] // const value = [uploader.qiniuTokenObj.visitPrefix + res.url]
// this.valueChange(value) // this.valueChange(value)
// }, // },
// Update: res => { // Update: res => {
// } // }
// } // }
// uploader.qnFileUpload(options) // uploader.qnFileUpload(options)
}, },
prviewImage(item, index) { prviewImage(item, index) {
uni.previewImage({ uni.previewImage({
urls: [item.path ? item.path : item.src], urls: [item.path ? item.path : item.src],
current: index current: index
}); });
}, },
delFile(index) { delFile(index) {
this.imgList.splice(index, 1) this.imgList.splice(index, 1)
this.setValue() this.setValue()
}, },
photograph(){// 获取拍照规范接口 photograph(){// 获取拍照规范接口
let self = this let self = this
let param = {"partnerCompanyId":self.partnerCompanyId, let param = {"partnerCompanyId":self.partnerCompanyId,
"categoryId":self.categoryId, "categoryId":self.categoryId,
"brandId":self.brandId, "brandId":self.brandId,
"fieldName":self.item.fieldsName} "fieldName":self.item.fieldsName}
this.$u.api.orderStandardItem(param).then((res)=>{ this.$u.api.orderStandardItem(param).then((res)=>{
if (res.code == 200 && res.data.length>0) { if (res.code == 200 && res.data.length>0) {
self.currentItemDate = res.data[0] self.currentItemDate = res.data[0]
}else{ }else{
console.log("获取完工项的拍照标准异常",res.data.message) console.log("获取完工项的拍照标准异常",res.data.message)
} }
self.takeStatus = true self.takeStatus = true
}) })
}, },
getWatermark(){ getWatermark(){
if(getApp().globalData.photo.waterSetting){ if(getApp().globalData.photo.waterSetting){
return return
} }
var data={partnerCompanyId:this.partnerCompanyId} var data={partnerCompanyId:this.partnerCompanyId}
this.$u.api.getWatermark(data).then((res) => { this.$u.api.getWatermark(data).then((res) => {
if (res.code == 200) { if (res.code == 200) {
getApp().globalData.photo.waterSetting = res.data getApp().globalData.photo.waterSetting = res.data
} else { } else {
console.log("获取水印备注异常",res.data.message) console.log("获取水印备注异常",res.data.message)
} }
}); });
} }
} }
} }
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>
.img-list { .img-list {
position: relative; position: relative;
margin: 8rpx; margin: 8rpx;
width: 160rpx; width: 160rpx;
height: 160rpx; height: 160rpx;
.pic{ .pic{
// margin-right: 15rpx; // margin-right: 15rpx;
// margin-bottom: 15rpx; // margin-bottom: 15rpx;
&:nth-child(3n){ &:nth-child(3n){
margin-right: 0; margin-right: 0;
} }
} }
.icon-del-box { .icon-del-box {
/* #ifndef APP-NVUE */ /* #ifndef APP-NVUE */
display: flex; display: flex;
/* #endif */ /* #endif */
align-items: center; align-items: center;
justify-content: center; justify-content: center;
position: absolute; position: absolute;
top: 10rpx; top: 10rpx;
right: 10rpx; right: 10rpx;
height: 40rpx; height: 40rpx;
width: 40rpx; width: 40rpx;
border-radius: 50%; border-radius: 50%;
background-color: rgba(0, 0, 0, 0.5); background-color: rgba(0, 0, 0, 0.5);
z-index: 2; z-index: 2;
transform: rotate(-45deg); transform: rotate(-45deg);
} }
.icon-del { .icon-del {
width: 30rpx; width: 30rpx;
height: 4rpx; height: 4rpx;
background-color: #fff; background-color: #fff;
border-radius: 2px; border-radius: 2px;
} }
.rotate { .rotate {
position: absolute; position: absolute;
transform: rotate(90deg); transform: rotate(90deg);
} }
} }
// .list { // .list {
// display: flex; // display: flex;
// .txt { // .txt {
// color: #666; // color: #666;
// padding: 10rpx; // padding: 10rpx;
// &.active { // &.active {
// background-color: red; // background-color: red;
// color: #fff; // color: #fff;
// } // }
// } // }
// } // }
</style> </style>
...@@ -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