wzp
2022-03-08 1f478b4b033d518f6de21f931d828d2a175b2a3e
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
<template>
    <view>
        <view class="">
            <cu-custom bgColor="bg-gradual-blue" :isBack="true">
                <block slot="backText">返回</block>
                <block slot="content">资产报废</block>
            </cu-custom>
        </view>
        
        <view class="contentBox">
            <u--form labelPosition="left">
                <u-form-item label="资产名称:" labelWidth="80"  borderBottom>
                    <u--input v-model="model1.devInfo.name" border="none"></u--input>
                </u-form-item>
                
                <u-form-item label="资产编号:" labelWidth="80"  borderBottom >
                    <u--input v-model="model1.devInfo.serialNumber" border="none"></u--input>
                </u-form-item>
                
            </u--form>
            
            <view class="margin-top">
                <view>拍照上传</view>
                <u-upload
                    :fileList="fileList"
                    @afterRead="afterRead"
                    @delete="deletePic"
                    multiple
                    :maxCount="3"
                ></u-upload>
            </view>
            
            <view class="margin-top">
                报废原因(必填)
                <u--textarea v-model="reason" placeholder="请输入内容" ></u--textarea>
            </view>
            
            <view class="flex margin-top btnBox">
                <u-button type="primary" size="small" class="btn" @click="submit" text="报废申请"></u-button>
                
            </view>
        </view>
        <u-toast ref="uToast"></u-toast>
    </view>
</template>
 
<script>
    export default {
        data() {
            return {
                fileList:[],
                listMediaId:[],
                number:"",
                model1: {
                    devInfo: {
                        id:'',
                        name: '',
                        serialNumber:"",
                        brand:'',//品牌
                        type:'',//类型
                        category:'',//类别
                        quantity:0
                    },
                },
                templateId:'',//模板ID
                reason:'',//报修描述
                approvalAssetsId:'',//非必传 我的资产报修、报废、归还时填写  审批资产ID
                productCode: "" //非必传 报修报废时 填写
            }
        },
        onLoad(options) {
            this.number= options.number;
            this.getAssetsData();
            this.initTemplateData();
        },
        methods: {
            submit() {
                this.isDisabled = true;
                if (this.reason.length <= 0) {
                    this.isDisabled = false;
                    this.$refs.uToast.show({
                        type: 'error',
                        message: "请填写报修描述"
                    });
                    return;
                }
                if(this.fileList.length<=0){
                    this.isDisabled = false;
                    this.$refs.uToast.show({
                        type: 'error',
                        message: "请拍照上传图片!"
                    });
                    return;
                }
        
                this.submitApply();
            },
            
            submitApply() {
                let assets = [];
                assets.push({
                    "id": this.model1.devInfo.id,
                    "name": this.model1.devInfo.name,
                    "serialNumber": this.model1.devInfo.serialNumber,
                    "quantity": this.model1.devInfo.quantity
                })
            
                // console.log(assets);
                let data = {
                    "assets": assets,
                    "templateId": this.templateId,
                    "reason": this.reason
                }
            
                this.$http.post('/assets/approval/submit', data)
                    .then(res => {
                        // debugger;
                        if (res.data.code === 0) {
                            console.log("成功了");
                            this.reason="";
                            this.$refs.uToast.show({
                                type: 'success',
                                message: "提交成功"
                            });
                        } else {
                            this.$refs.uToast.show({
                                type: 'error',
                                message: res.data.msg
                            });
                        }
                        this.isDisabled = false;
            
                    }).catch(err => {
                        this.isDisabled = false;
                        console.log(err.data)
                    })
            },
            
            getAssetsData(){
                this.$http.get('/assets/approval/assetDeatail',{params:{serialNumber:this.number}})
                    .then(res => {
                        // debugger;
                        if (res.data.code === 0) {
                            let data = res.data.data;
                            this.model1.devInfo.name=data.name;
                            this.model1.devInfo.serialNumber =data.serialNumber;
                            this.model1.devInfo.brand= data.brand;
                            this.model1.devInfo.type =data.typeName;
                            this.model1.devInfo.category = data.categoryName;
                            this.model1.devInfo.quantity =data.quantity;
                            this.model1.devInfo.id =data.id;
                            
                            if(data["approvalAssets"])
                            {
                                this.approvalAssetsId=data.approvalAssets.id;
                            }
                            if(data["product"])
                            {
                                this.productCode=data.product.productCode;
                            }
                        }
                    }).catch(err => {
                        console.log(err)
                    })
            },
            
            
            // 删除图片
            deletePic(event) {
                this[`fileList`].splice(event.index, 1);
                this.listMediaId.splice(event.index,1);
                console.log(this.listMediaId.length)
            },
            
            // 新增图片
            async afterRead(event) {
                console.log(event)
                // 当设置 mutiple 为 true 时, file 为数组格式,否则为对象格式
                let lists = [].concat(event.file)
                let fileListLen = this[`fileList`].length
                lists.map((item) => {
                    this[`fileList`].push({
                        ...item,
                        status: 'uploading',
                        message: '上传中'
                    })
                })
                for (let i = 0; i < lists.length; i++) {
                    // const result = await this.uploadFilePromise(lists[i].url)
                    const result = await this.uploadFile(lists[i].url)
                    let item = this[`fileList`][fileListLen]
                    this[`fileList`].splice(fileListLen, 1, Object.assign(item, {
                        status: 'success',
                        message: '',
                        url: result
                    }))
                    fileListLen++
                }
            },
            
            //作废咯
            uploadFilePromise(url) {
                return new Promise((resolve, reject) => {
                    let a = uni.uploadFile({
                        url: '', // 仅为示例,非真实的接口地址
                        filePath: url,
                        name: 'file',
                        formData: {
                            user: 'test'
                        },
                        success: (res) => {
                            setTimeout(() => {
                                resolve(res.data.data)
                            }, 1000)
                        }
                    });
                })
            },
            
            //上传文件
            async uploadFile(url){
                this.$http.upload('/assets/approval/upload', {
                    params: {}, /* 会加在url上 */
                    // #ifdef APP-PLUS || H5
                    files: [], // 需要上传的文件列表。使用 files 时,filePath 和 name 不生效。App、H5( 2.6.15+)
                    // #endif
                    // #ifdef MP-ALIPAY
                    fileType: 'image', // 仅支付宝小程序,且必填。
                    // #endif
                    filePath: url, // 要上传文件资源的路径。
                    // 注:如果局部custom与全局custom有同名属性,则后面的属性会覆盖前面的属性,相当于Object.assign(全局,局部)
                    custom: {auth: true}, // 可以加一些自定义参数,在拦截器等地方使用。比如这里我加了一个auth,可在拦截器里拿到,如果true就传token
                    name: 'file', // 文件对应的 key , 开发者在服务器端通过这个 key 可以获取到文件二进制内容
                    // #ifdef H5 || APP-PLUS
                    timeout: 60000, // H5(HBuilderX 2.9.9+)、APP(HBuilderX 2.9.9+)
                    // #endif
                    header: {},  /* 会与全局header合并,如有同名属性,局部覆盖全局 */
                    formData: {}, // HTTP 请求中其他额外的 form data
                    // 返回当前请求的task, options。请勿在此处修改options。非必填
                    getTask: (task, options) => {
                      // task.Update((res) => {
                      //   console.log('上传进度' + res.progress);
                      //   console.log('已经上传的数据长度' + res.totalBytesSent);
                      //   console.log('预期需要上传的数据总长度' + res.totalBytesExpectedToSend);
                      //
                      //   // 测试条件,取消上传任务。
                      //   if (res.progress > 50) {
                      //     uploadTask.abort();
                      //   }
                      // });
                    }
                  }).then(res => {
                      // debugger;
                    // 返回的res.data 已经进行JSON.parse
                    if(res.data.code===0){
                        console.log("mediaid="+res.data.data.media_id);
                        this.listMediaId.push(res.data.data.media_id);
                    }
                  }).catch(err => {
                
                  })
            },
            
            //获取报废的模板
            initTemplateData() {
                this.$http.get('/assets/approval/getTemplate')
                    .then(res => {
                        // debugger;
            
                        if (res.data.code === 0) {
                            let data = res.data.data;
                            //获取领用的模板ID
                            for (let i = 0; i < data.length; i++) {
                                if (data[i].type === 5) {
                                    this.templateId = data[i].templateId;
                                    break;
                                }
                            }
                            console.log('模板ID=' + this.templateId)
                        }
            
            
                    }).catch(err => {
            
                        console.log(err.data)
                    })
            },
        }
    }
</script>
 
<style lang="scss">
.contentBox {
        padding: 15px 15px 40px 15px;
    }
    
    .btnBox{
        padding: 20px 0 0 0;
        .btn{
            width: 25%;
        }
    }
</style>