upload.vue
5.1 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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<template>
<view style="display: flex; flex-wrap: wrap; justify-content: space-evenly; margin-top: 40rpx;">
<view class="box" v-for="(item,index) in fileList">
<u-upload :fileList="item.url" @afterRead="(event) => afterRead(event,item,index)" @delete="deletePic"
name="1" multiple :maxCount="1"></u-upload>
<!-- <u-upload ref="uUpload" :action="action" :auto-upload="true" ></u-upload> -->
<!-- <u-upload
:action="action" :auto-upload="true" :fileList="item.url"
></u-upload> -->
<p>{{item.title}}</p>
</view>
<view class="" style="width: 90%;margin-top: 50rpx;display: flex;justify-content: center;">
<u-button shape="circle" text="确认发布" style="color: white;"
color="linear-gradient(to right, #46BAEC,#2F67D3)" @click="submit"></u-button>
</view>
</view>
</template>
<script>
import config from '@/common/config'
export default {
data() {
return {
action: config.baseUrl + '/api/common/upload',
fileList1: [],
info: {},
flag: null,
fileList: [
{
id:3,
title: '整机左前45°',
url: [
]
},
{
id:3,
title: '整机右后45°',
url: [
]
},
{
id:3,
title: '发动机左侧',
url: []
},
{
id:3,
title: '发动机右侧',
url: []
},
{
id:3,
title: '变速箱°',
url: []
},
{
id:3,
title: '驾驶室工作台',
url: []
},
{
id:3,
title: '工作装置整体',
url: []
},
{
id:3,
title: '整机铭牌',
url: []
},
{
id:3,
title: '环保信息铭牌',
url: []
}
],
}
},
onLoad(option) {
console.log(option.val);
this.flag = option.val
if(option.info){
this.info = JSON.parse(option.info)
this.fileList = JSON.parse(this.info.images_json)
console.log(this.fileList);
}
uni.$u.http.get('/api/equip/equip_image').then(res => {
console.log(res);
})
// this.fileList = JSON.parse(this.info.images_json)
// console.log(this.fileList);
// let arr
// this.fileList.forEach((item,index) => {
// this.fileList[index] = Object.keys(item).map(val => ({
// title: val,
// url: item[val]
// }))
// })
// this.fileList = this.fileList.flat(Infinity)
// console.log(this.fileList);
},
methods: {
submit() {
let p = this.info
delete p.name
delete p.brand_name
delete p.xh_name
console.log(this.info);
console.log(this.fileList);
// let images_json = this.fileList.map(item => {
// return{
// [item.title]: item.url && item.url[0] && item.url[0].thumb || ''
// }
// });
p.province = p.region.slice(0,3)
p.city = p.region.slice(3,6)
p.address = p.region
console.log('sdadasds', this.flag);
uni.$u.http.post( this.flag === "sale" ?'/api/equip/add_equip_sale' : '/api/equip/add_equip_hire' , {
...p,
images_json: JSON.stringify(this.fileList)
}).then(res => {
uni.showToast({
title:res.msg,
icon:'none'
})
if(res.code == 1){
let pagearr = getCurrentPages(); //获取应用页面栈
let currentPage = pagearr[pagearr.length - 2]; //获取当前页面信息
console.log('option:', currentPage) //获取页面传递的信息
currentPage.$vm.category_id = ''
currentPage.$vm.name = ''
setTimeout(()=> {
uni.navigateBack({
delta:2
})
},1500)
}
}).catch(err => {
uni.showToast({
title:err.msg,
icon:'none'
})
uni.reLaunch({
url:'pages/index/buy'
})
})
},
// 删除图片
deletePic(event) {
this[`fileList${event.name}`].splice(event.index, 1)
},
// 新增图片
async afterRead(event, item, index) {
// 当设置 multiple 为 true 时, file 为数组格式,否则为对象格式
let lists = [].concat(event.file)
let fileListLen = this.fileList[index].url.length
lists.map((item) => {
this.fileList[index].url.push({
...item,
status: 'uploading',
message: '上传中'
})
})
for (let i = 0; i < lists.length; i++) {
const result = await this.uploadFilePromise(lists[i].url)
console.log(result);
let item = this.fileList[index].url[fileListLen]
this.fileList[index].url.splice(fileListLen, 1, Object.assign(item, {
status: 'success',
message: '',
url: result
}))
fileListLen++
}
},
uploadFilePromise(url) {
return new Promise((resolve, reject) => {
let a = uni.uploadFile({
url: this.action,
filePath: url,
name: 'file',
formData: {
user: 'test'
},
header: {
token: uni.getStorageSync('token')
},
success: (res) => {
console.log('是是撒',res);
setTimeout(() => {
resolve(JSON.parse(res.data).data.fullurl)
}, 1000)
}
});
})
},
}
}
</script>
<style>
.box {
display: flex;
flex-direction: column;
align-items: center;
margin-right: 18rpx;
}
</style>