周宾
3 天以前 2f93b5a6157c3752230485e5aa56f79fdabd5fb4
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
<template>
    <PageHeader :title="pageTitle" @back="goBack" />
    <view class="account-detail">
        <view class="box">
            <view v-if="type == 'add'">
                <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">
                <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" />
                            <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-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.baseUrl
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,
    taxRate: 0,
    unit: "",
    url: "",
}
 
const goBack = () => {
    uni.navigateBack()
}
const submitForm = async() => {
    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
            }
            uni.showToast({
                title: "上传失败",
                icon: 'none',
                duration: 1500
            })
        }
    })
}
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;
    border-radius: 20rpx;
}
 
.add-img {
    .img {
        background-color: #efefef;
        border-radius: 10rpx;
        width: 200rpx;
        height: 200rpx;
    }
}
</style>