周宾
17 小时以前 b54a94b098a5a0a3375bab0d6493bacf45dd0a58
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
<template>
    <PageHeader :title="pageTitle" @back="goBack" />
    <view class="account-detail">
        <view class="box">
            <view v-if="type == 'add'" style="margin-bottom: 20rpx;">
                <button class="mini-btn" type="primary" size="mini" style="margin-left: 0;margin-right: 0;"
                    @click="addItem">新增</button>
            </view>
            <view v-for="(item, index) in goodsList" class="form-box">
                <button v-if="type == 'add'" class="mini-btn del-btn" type="warn" size="mini" style="margin-left: 0;margin-right: 0;"
                    @click="delItem(index)">删除</button>
                <uni-forms :key="index" label-position="top" label-width="400rpx">
                    <uni-forms-item label="产品图片" required>
                        <view class="add-img">
                            <image v-if="item.url" class="img" :src="baseUrl + item.url" mode="aspectFill"
                                @click="addImg(index)" />
                            <uni-icons v-else type="camera-filled" size="56" style="color: #8a8a8a"
                                @click="addImg(index)"></uni-icons>
                        </view>
                    </uni-forms-item>
                    <uni-forms-item label="名称" required>
                        <uni-easyinput v-model="item.productCategory" placeholder="请输入名称" />
                    </uni-forms-item>
                    <uni-forms-item label="高度" required>
                        <uni-easyinput v-model="item.specificationModel" type="number" placeholder="请输入高度" />
                    </uni-forms-item>
                    <uni-forms-item label="高度单位" required>
                        <uni-easyinput v-model="item.unit" placeholder="请输入高度单位" />
                    </uni-forms-item>
                    <uni-forms-item label="纸箱规格" required>
                        <uni-easyinput v-model="item.cartonSpecifications" placeholder="请输入纸箱规格" />
                    </uni-forms-item>
                    <uni-forms-item label="入库数量/件" required>
                        <uni-number-box v-model="item.inboundNum" :min="0"></uni-number-box>
                    </uni-forms-item>
                    <uni-forms-item label="每件数量/支" required>
                        <uni-number-box v-model="item.boxNum" :min="0"></uni-number-box>
                    </uni-forms-item>
                    <uni-forms-item label="单价(元)/件" required>
                        <uni-number-box v-model="item.taxInclusiveUnitPrice" :min="0" :step="0.01"></uni-number-box>
                    </uni-forms-item>
                    <uni-forms-item label="单价(美元)/件" required>
                        <uni-number-box v-model="item.dollarPrice" :min="0" :step="0.01"></uni-number-box>
                    </uni-forms-item>
                    <uni-forms-item label="入库日期" required>
                        <uni-datetime-picker type="date" :clear-icon="false" v-model="item.inboundDate" />
                    </uni-forms-item>
                </uni-forms>
            </view>
            <view style="display: flex;justify-content: flex-end;">
                <button class="mini-btn" type="primary" size="mini"
                    style="margin-left: 0;margin-right: 0;margin-right: 20rpx;" @click="submitForm">确定</button>
                <button class="mini-btn" size="mini" style="margin-left: 0;margin-right: 0;" @click="goBack">取消</button>
            </view>
        </view>
    </view>
</template>
 
<script setup>
import { onMounted, ref } from 'vue'
import { getToken } from "@/utils/auth";
import { addCustom } from '@/api/inventoryManagement/receiptManagement.js'
import config from '@/config'
const baseUrl = config.imgUrl
const pageTitle = ref('新增自定义入库')
const goodsList = ref([])
const type = ref('add')
const props = defineProps(['goods'])
const defaultGoods = {
    boxNum: 0,
    cartonSpecifications: "",
    id: null,
    inboundDate: "",
    inboundNum: 0,
    itemType: "",
    productCategory: "",
    specificationModel: "",
    supplierName: "",
    taxExclusiveTotalPrice: 0,
    taxInclusiveTotalPrice: 0,
    taxInclusiveUnitPrice: 0,
    dollarPrice: 0,
    taxRate: 0,
    unit: "",
    url: "",
}
 
const goBack = () => {
    uni.navigateBack()
}
const showToast = (msg)=>{
    uni.showToast({
        title: msg,
        icon: 'none',
        duration: 1500
    })
}
const submitForm = async() => {
    if (!goodsList.value.length) {
      showToast('请至少添加一条产品数据')
      return
    }
 
    // 验证自定义添加的数据必填字段
    for (let i = 0; i < goodsList.value.length; i++) {
      const product = goodsList.value[i];
      if (!product.productCategory || !product.specificationModel || !product.unit) {
        showToast(`第${i + 1}行产品数据未填写完整(产品、产品高度、高度单位为必填)`)
        return
      }
      if (!product.url) {
        showToast(`第${i + 1}行产品未上传产品图片`)
        return
      }
      if (!product.cartonSpecifications) {
        showToast(`第${i + 1}行产品未填写纸箱规格`)
        return
      }
      // if (!product.itemType) {
      //   proxy.$modal.msgError(`第${i + 1}行请选择物品类型`)
      //   return
      // }
      const stock = Number(product?.inboundNum ?? 0);
      if (!Number.isFinite(stock) || stock <= 0) {
        showToast(`第${i + 1}行本次入库数量需大于0`)
        return
      }
      const boxNum = Number(product?.boxNum ?? 0);
      if (!Number.isFinite(boxNum) || boxNum <= 0) {
        showToast(`第${i + 1}行每件数量/支需大于0`)
        return
      }
      const taxInclusiveUnitPrice = Number(product?.taxInclusiveUnitPrice ?? 0);
      if (!Number.isFinite(taxInclusiveUnitPrice) || taxInclusiveUnitPrice <= 0) {
        showToast(`第${i + 1}行单价(元)需大于0`)
        return
      }
      const dollarPrice = Number(product?.dollarPrice ?? 0);
      if (!Number.isFinite(dollarPrice) || dollarPrice <= 0) {
        showToast(`第${i + 1}行单价(美元)需大于0`)
        return
      }
      if (!product.inboundDate) {
        showToast(`第${i + 1}行请选择入库日期`)
        return
      }
      
    }
    let res = await addCustom(goodsList.value)
    if(res.code !== 200){
        uni.showToast({
            title: res.msg,
            icon: 'none'
        })
        return
    }
    uni.showToast({
        title: '新增成功',
        icon: 'none'
    })
    goBack()
}
 
const addImg = (index) => {
    uni.chooseMedia({
        count: 1,
        mediaType: ['image'],
        sourceType: ['album', 'camera'],
        sizeType: ['original', 'compressed'],
        success: (res) => {
            let imgList = []
            let isHeGe = true
            if (res.tempFiles && res.tempFiles.length) {
                res.tempFiles.forEach(item => {
                    imgList.push(item.tempFilePath)
                    if (item.size > 10000000) {
                        isHeGe = false
                    }
                })
            }
            if (!isHeGe) {
                uni.showToast({
                    title: "图片不能大于10M",
                    icon: "none"
                })
                return
            }
            UploadImage(imgList[0], index)
        },
        fail: (e) => {
            console.log('chooseMedia:fail', e)
            if (e.errMsg == 'chooseMedia:fail cancel') {
                return
            }
            if (e.errMsg == 'user cancel') {
                return
            }
            uni.showToast({
                title: "上传失败",
                icon: 'none',
                duration: 1500
            })
        }
    })
}
const delItem = (index)=>{
    console.log('xxxx')
    goodsList.value.splice(index,1)
}
const UploadImage = (url, index) => {
    uni.uploadFile({
        url: baseUrl + '/file/upload', // 替换为实际的上传接口
        filePath: url,
        name: 'file',
        header: { Authorization: 'Bearer ' + getToken() },
        success: (res) => {
            const data = JSON.parse(res.data)
            if (data.code === 200) {
                goodsList.value[index].url = data.data.tempPath
                uni.showToast({
                    title: '上传成功',
                    icon: 'none',
                    duration: 1500
                })
            } else {
                console.log('上传失败', res)
                uni.showToast({
                    title: '上传失败',
                    icon: 'none'
                })
            }
        },
        fail: (e) => {
            console.log('uploadFile:fail', e)
            uni.showToast({
                title: '上传失败',
                icon: 'none'
            })
        }
    })
}
 
onMounted(() => {
    let _goods = JSON.parse(props.goods)
    if (_goods.id) {
        pageTitle.value = '编辑自定义入库'
        type.value = 'edit'
    }
    goodsList.value.push({...defaultGoods,..._goods})
})
const addItem = () => {
    goodsList.value.push({...defaultGoods})
}
</script>
<style lang="scss" scoped>
.box {
    background-color: #fff;
    box-sizing: border-box;
    padding: 20rpx;
    min-height: 80rpx;
    padding-bottom: calc(30rpx + env(safe-area-inset-bottom));
}
 
.form-box {
    box-sizing: border-box;
    margin-bottom: 20rpx;
    padding: 20rpx;
    // background-color: #f5f5f5;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
    transition: box-shadow 0.3s ease;
    border-radius: 20rpx;
    position: relative;
    .del-btn{
        position: absolute;
        right: 20rpx;
        top: 40rpx;
        z-index: 40;
    }
}
 
.form-box:hover {
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
}
 
.add-img {
    .img {
        background-color: #efefef;
        border-radius: 10rpx;
        width: 200rpx;
        height: 200rpx;
    }
}
</style>