uploadPay.vue
8.39 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
<template>
<view class="page">
<scroll-view class="scroll" scroll-y>
<view class="detail-page">
<!-- 基础信息 -->
<view class="section">
<text class="company">{{ form.code || '-' }}</text>
<view class="row"><text class="label">公司简称</text><text class="value">{{ form.companyShortName || form.companyName || '-' }}</text></view>
<view class="row"><text class="label">供应商名称</text><text class="value">{{ form.supplierName || '-' }}</text></view>
<view class="row"><text class="label">采购处</text><text class="value">{{ form.purchaseDepartmentName || form.purchaseDepartment || '-' }}</text></view>
</view>
<!-- 请款附件 -->
<view class="sub-table">
<view class="sub-header">
<image class="sub-bar" src="/static/images/title.png" />
<text class="sub-title">请款附件</text>
<view class="upload-btn" @click="selectFile">上传文件</view>
</view>
<view class="sub-body">
<view v-if="!fileList.length" class="empty-tip">暂无附件,请上传</view>
<view v-for="(file, idx) in fileList" :key="file.id || idx" class="file-item">
<text class="file-name">{{ file.name || '未命名文件' }}</text>
<text class="file-del" @click="removeFile(idx)">删除</text>
</view>
</view>
</view>
</view>
</scroll-view>
<view class="footer">
<button class="btn submit" type="primary" :loading="loading" @click="onSubmit">提交</button>
</view>
</view>
</template>
<script>
import { procurementPlanDetailApi, procurementPlanUploadRequestAttachmentApi } from '@/api/procure-manage/procurementPlan.js'
import upload from '@/utils/upload'
import { uploadFileApi } from '@/api/base.js'
export default {
name: 'DomesticPlanUploadPay',
data() {
return {
id: '',
loading: false,
form: {},
// 请款附件列表 [{ id, name }]
fileList: []
}
},
onLoad(query) {
this.id = (query && query.id) || ''
this.loadDetail()
},
methods: {
async loadDetail() {
if (!this.id) {
uni.showToast({ title: '缺少单据ID', icon: 'none' })
return
}
uni.showLoading({ title: '加载中...', mask: true })
try {
const res = await procurementPlanDetailApi({ id: this.id })
const m = (res && res.data) || {}
this.form = m
// 详情接口返回的请款附件字段为 requestFileIds/requestFileNames
const ids = String(m.requestFileIds || '').split(',').filter(Boolean)
const names = String(m.requestFileNames || '').split(',').filter(Boolean)
this.fileList = ids.map((id, i) => ({ id, name: names[i] || '' }))
} catch (e) {
uni.showToast({ title: '加载详情失败', icon: 'none' })
} finally {
uni.hideLoading()
}
},
// 选择并上传文件(多文件逐个追加)
selectFile() {
if (typeof uni !== 'undefined' && typeof uni.chooseMessageFile === 'function') {
uni.chooseMessageFile({
count: 9,
type: 'all',
success: async res => {
const files = (res && res.tempFiles) || []
if (!files.length) return
uni.showLoading({ title: '上传中...', mask: true })
try {
for (const f of files) {
const name = f.name || 'file'
const r = await upload({ url: '/sw/filebox/uploadFile', filePath: f.path, name: 'file' })
const d = r && r.data ? r.data : r
const id = (d && (d.id || d.fileId || (d.data && (d.data.id || d.data.fileId))))
? String(d.id || d.fileId || d.data.id || d.data.fileId)
: ''
if (id) this.fileList.push({ id, name })
}
} catch (e) {
uni.showToast({ title: '上传失败', icon: 'none' })
} finally {
uni.hideLoading()
}
},
fail: () => {
uni.showToast({ title: '未选择文件', icon: 'none' })
}
})
return
}
// H5 兜底:动态创建原生 input
try {
const input = document.createElement('input')
input.type = 'file'
input.multiple = true
input.style.position = 'fixed'
input.style.left = '-9999px'
document.body.appendChild(input)
input.addEventListener('change', this.onFileChange)
input.click()
setTimeout(() => { document.body.removeChild(input) }, 1000)
} catch (e) {
uni.showToast({ title: '无法打开文件选择', icon: 'none' })
}
},
async onFileChange(e) {
const files = (e && e.target && e.target.files) || []
if (!files.length) return
uni.showLoading({ title: '上传中...', mask: true })
try {
for (const file of files) {
const name = file.name || ''
const fd = new FormData()
fd.append('file', file, name)
const d = await uploadFileApi(fd)
const data = d && d.data ? d.data : d
const id = (data && (data.id || data.fileId || (data.data && (data.data.id || data.data.fileId))))
? String(data.id || data.fileId || data.data.id || data.data.fileId)
: ''
if (id) this.fileList.push({ id, name })
}
} catch (e2) {
uni.showToast({ title: '上传失败', icon: 'none' })
} finally {
uni.hideLoading()
if (e && e.target) e.target.value = ''
}
},
removeFile(idx) {
this.fileList.splice(idx, 1)
},
async onSubmit() {
const planId = (this.form && this.form.id) || this.id
if (!planId) {
uni.showToast({ title: '缺少单据id', icon: 'none' })
return
}
const ids = this.fileList.map(it => it.id).filter(Boolean)
const names = this.fileList.map(it => it.name).filter(Boolean)
const data = {
id: planId,
fileIds: ids.join(','),
fileNames: names.join(',')
}
const confirmRes = await new Promise(resolve => {
uni.showModal({ title: '提交确认', content: '确定提交请款附件吗?', success: resolve })
})
if (!(confirmRes && confirmRes.confirm)) return
this.loading = true
try {
await procurementPlanUploadRequestAttachmentApi(data)
uni.showToast({ title: '提交成功', icon: 'success' })
try {
uni.setStorageSync('DOMESTIC_PLAN_LIST_NEED_REFRESH', '1')
uni.setStorageSync('DOMESTIC_PLAN_DETAIL_NEED_REFRESH', '1')
} catch (e) { }
setTimeout(() => { uni.navigateBack() }, 400)
} catch (e) {
uni.showToast({ title: (e && (e.msg || e.message)) || '提交失败', icon: 'none' })
} finally {
this.loading = false
}
}
}
}
</script>
<style lang="scss" scoped>
.page {
display: flex;
flex-direction: column;
height: 100%;
}
.scroll {
flex: 1;
padding: 8rpx 0 176rpx;
}
.detail-page {
background: #f3f3f3;
}
.footer {
z-index: 2;
position: fixed;
left: 0;
right: 0;
bottom: 0;
padding: 32rpx;
padding-bottom: calc(32rpx + env(safe-area-inset-bottom));
background: #fff;
box-shadow: 0 -8rpx 24rpx rgba(0, 0, 0, 0.06);
.btn {
height: 80rpx;
line-height: 80rpx;
border-radius: 12rpx;
font-size: 32rpx;
}
.submit {
background: $theme-primary;
color: #fff;
}
}
.section {
padding: 32rpx;
background: #fff;
margin-bottom: 20rpx;
.company {
display: block;
font-size: 36rpx;
font-weight: 600;
color: rgba(0, 0, 0, 0.9);
padding: 8rpx 0 24rpx;
line-height: 50rpx;
}
}
.row {
display: flex;
margin-bottom: 28rpx;
&:last-child {
margin-bottom: 0;
}
.label {
max-width: 420rpx;
margin-right: 20rpx;
line-height: 32rpx;
font-size: 28rpx;
color: rgba(0, 0, 0, 0.6);
}
.value {
flex: 1;
line-height: 32rpx;
font-size: 28rpx;
color: rgba(0, 0, 0, 0.9);
text-align: right;
word-break: break-all;
}
}
.sub-table {
background: #fff;
margin-bottom: 20rpx;
}
.sub-header {
display: flex;
align-items: center;
padding: 16rpx 0 16rpx 32rpx;
.sub-bar {
width: 6rpx;
height: 32rpx;
margin-right: 14rpx;
}
.sub-title {
font-size: 30rpx;
font-weight: 600;
color: rgba(0, 0, 0, 0.9);
flex: 1;
line-height: 42rpx;
}
.upload-btn {
margin-right: 32rpx;
height: 56rpx;
line-height: 56rpx;
padding: 0 24rpx;
font-size: 26rpx;
border-radius: 8rpx;
background: $theme-primary;
color: #fff;
}
}
.sub-body {
padding: 0 32rpx 24rpx;
.empty-tip {
text-align: center;
padding: 60rpx 0;
color: rgba(0, 0, 0, 0.4);
font-size: 28rpx;
}
.file-item {
display: flex;
align-items: center;
justify-content: space-between;
padding: 20rpx 24rpx;
margin-bottom: 16rpx;
background: #fafafa;
border-radius: 8rpx;
&:last-child {
margin-bottom: 0;
}
.file-name {
flex: 1;
font-size: 28rpx;
color: rgba(0, 0, 0, 0.9);
word-break: break-all;
margin-right: 20rpx;
}
.file-del {
flex-shrink: 0;
font-size: 26rpx;
color: #d54941;
}
}
}
</style>