upload.js
1.9 KB
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
import { pro_http } from '@/utils/baseUrl.js'
export default {
data() {
return {
mainCoverList: [], // 主图列表
detailList: [] // 详情列表
}
},
onReady() {
//如果需要兼容微信小程序,并且校验规则中含有方法等,只能通过setRules方法设置规则。
this.$refs.uForm.setRules(this.rules)
},
methods: {
// 删除视频或图片
deletePic(event) {
if(event.name === 'mainCover') {
this.mainCoverList.splice(event.index, 1)
} else {
this.detailList.splice(event.index, 1)
}
},
// 新增视频或图片
async afterRead(event) {
// 当设置 multiple 为 true 时, file 为数组格式,否则为对象格式
let lists = [].concat(event.file)
let list = event.name === 'mainCover' ? this.mainCoverList : this.detailList
let fileListLen = list.length
lists.map((item) => {
list.push({
...item,
status: 'uploading',
message: '上传中'
})
})
for (let i = 0; i < lists.length; i++) {
const result = await this.uploadFilePromise(lists[i].url)
let item = list[fileListLen]
list.splice(fileListLen, 1, Object.assign(item, {
status: 'success',
message: '',
url: result.fileName,
fileName: result.fileName,
}))
fileListLen++
}
},
uploadFilePromise(url) {
return new Promise((resolve, reject) => {
let a = uni.uploadFile({
url: pro_http + '/common/upload',
filePath: url,
name: 'file',
formData: {
user: 'test'
},
success: (res) => {
setTimeout(() => {
const result = JSON.parse(res.data)
resolve(result)
}, 1000)
}
});
})
},
// 获取当前位置
getMapLocation(){
let _this = this
uni.chooseLocation({
success:(res)=> {
_this.form.address = res.address
_this.form.latitude = res.latitude
_this.form.longitude = res.longitude
}
})
}
}
}