1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import uploader from '@/plugins/uploader/index.js'
const globalUrl = process.uniEnv || {}
const randomChar = function(l, url = "") {
const x = "0123456789qwertyuioplkjhgfdsazxcvbnm";
let tmp = "";
let time = new Date();
for (let i = 0; i < l; i++) {
tmp += x.charAt(Math.ceil(Math.random() * 100000000) % x.length);
}
return (
"file/" +
url +
time.getTime() +
tmp
);
}
export default {
name: 'upload',
components: {},
data() {
return {
imgList:[],
// 上传文件种类:0--头像、1--证件、2--身份证
categories: ['avatar', 'cert', 'id'],
};
},
created() {
// this.initQiniu()
},
methods: {
async uploadPath(type){
// 可以直接上传path
const self = this
let imgList = []
let options = {
files:[] // 文件路径
}
if(type==='qiniu'){
let res = await uploader.qnFileUpload(options)
return
}
let res = await uploader.urlFileUpload(options)
},
/**
* 存储图片到任务队列
* @param {Array} files 图片路径
*/
async saveToTask(files) {
// 生成key返回,然后把key放到异步上传的任务队列
let lists = files.map(v => {
const key = randomChar(10)
return {
key: key,
path: v,
src: key
}
})
// for (let i = 0; i < files.length; i++) {
// const path = files[i]
// const row = await this.saveFiles(path)
// lists.push(row)
// }
const data = this.uploadTask
const saveLists = data.concat(lists)
this.$u.vuex('uploadTask', saveLists)
return lists
},
/**
* 选择图片
* @param {Object} data option
*/
async chooseImg(data){
const self = this
return new Promise((resolve, reject) => {
uni.chooseImage({
count: data.count || 9, //默认9
sizeType: data.sizeType || ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
sourceType: data.sourceType || ['album', 'camera'], //从相册选择
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
const key = randomChar(10)
const img = {
key: key,
path: savedFilePath,
src: key
}
resolve(img)
},
fail: reject
})
})
},
/**
* 上传图片公用组件
* @param {Object} type 文件上传方式
* @param {Object} attr 文件属性:图片种类[category](身份证、证件、头像);
* 图片上传其他配置[options】(如,选择图片或拍摄options.consourceType,['album', 'camera'];)其他
*/
async chooseImage(type, attr={}){
const self = this
let imgList = []
if(type==='qiniu'){
// 图片种类
let category = attr && attr.category || self.categories[0]
let config = attr && attr.options || {} // 图片上传参数配置
// 七牛上传
let options = {
onProgressUpdate(res){
let item = {
path:res.path, // 图片临时缓存路径
key:'', // 图片上传成功后相对路径
src:'', // 访问图片的绝对路径
progress:res.progress // 图片上传进度
}
if(res.fileIndex<imgList.length){
imgList[res.fileIndex].progress = res.progress
}else{
imgList.push(item)
}
imgList[res.fileIndex]
self.imgList = imgList
// 多文件上传每个文件进度条
},
onEachUpdate(res){
// 多文件上传每个文件上传成功触发
if(imgList.length>res.fileIndex){
imgList[res.fileIndex].key = res.url
imgList[res.fileIndex].src = uploader.qiniuTokenObj.visitPrefix + res.url
}
self.imgList = imgList
},
cardType: category,
...config
}
// 不需要进度条等可直接获取
let res = await uploader.qnImgUpload(options)
// let imgList = res.map(item=>{
// return {
// key:item,
// src:uploader.qiniuTokenObj.visitPrefix + item
// }
// })
// this.imgList = imgList
return
}
// 本地服务上传
let res = await uploader.urlUpload()
this.imgList = res
},
perviewImg(index) {
let urls = []
this.imgList.map(item => {
urls.push(item.src)
})
uni.previewImage({
current: index,
urls: urls
});
},
removeImg(index) {
this.imgList.splice(index, 1)
// this.$emit('image-change', this.imgList) //将图片回传父页面
},
}
}