cuiqian2004
2025-09-19 d8743368ffda9bc0fb2c6818f695a9a6b1079e57
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
<template>
    <view class="pages-scene">
        <view class="map-content" v-if="opSceneType =='' ">
            <image class="img" src="/images/image 25.png" alt=" 图片" mode="aspectFit" />
            <view class="space">没有找到符合条件的地图</view>
 
            <view class="text-button-group">
                <a-button type="primary" class="button" @click="clickStartConstructScene">
                    开始构建
                </a-button>
                <a-button type="primary" class="button" :disabled="loading && localSceneList.length == 0"
                    @click="clickDownloadScene" disabled>
                    下载场景
                </a-button>
            </view>
 
        </view>
        <view class="content" v-show="opSceneType =='scan'">
            <view class="fabric" :message="ctxDataStr" :change:message="ctx.receiveMsg" id="canvasMap"></view>
            <view class="position" @click="clickVehiclePosition">
                <text class="ico my-location-rounded"></text>
            </view>
        </view>
        <view class="bottom">
            <view class="bottom-content" v-if="opSceneType =='add_name' ">
                <view class="tip">请输入场景名称</view>
                <view class="name-input">
                    <input ref="refInputName" :focus="true" placeholder="请输入场景名称" :value="sceneName"
                        @input="onInputName"></input>
                    <uni-icons class="clear" color="#ccc" type="clear" size="20" v-if="showClearName"
                        @click="clickClearName"></uni-icons>
                </view>
                <view class="text-button-group">
                    <a-button type="primary" class="button" :disabled="loading || sceneName.trim() == ''"
                        @click="clickNameOK">确认</a-button>
                    <a-button type="ghost" class="button" :disabled="loading" @click="clickNameCancel">取消</a-button>
 
                </view>
            </view>
            <view class="bottom-content" v-else-if="opSceneType =='scan'">
                <view class="tip">场景构建中</view>
                <view>
                    请操作搬运车扫描地图覆盖的区域
                </view>
 
                <view class="text-button-group">
                    <a-button type="primary" class="button" :disabled="loading" @click="clickScanFinish">扫描完成</a-button>
                </view>
            </view>
            <view class="bottom-content" v-else-if="opSceneType =='finish'">
                <view class="tip">场景构建完成</view>
                <view>
                    已成功构建“{{sceneName}}”
                </view>
 
                <view class="text-button-group">
                    <a-button type="primary" class="button" :disabled="loading" @click="clickFinish">构建完成</a-button>
                </view>
            </view>
        </view>
    </view>
</template>
<script src="./js/ctx.js" module="ctx" lang="renderjs"></script>
 
<script>
    import {
        showToast,
        showModal,
        session,
        showError,
        showInfo
    } from "@/comm/utils.js"
    import {
        Button
    } from 'antd-mobile-vue-next'
 
    import {
        createScene,
        addMap,
        stopMap,
        //getAgvState,
        getMapLaserData,
        saveDBData,
 
    } from "@/api/vehicle.js"
    export default {
        name: "PagesScene",
        components: {
            'a-button': Button
        },
        data() {
            return {
                opType: "",
                loading: false,
                ip: "",
                opSceneType: "",
                windowWidth: 375,
                sceneName: "",
                showClearName: false,
                mapId: "",
                ctxDataStr: "[]",
                localSceneList: [],
                positioningAgv: true,
                robotPos: {}
 
            }
        },
        computed: {
 
        },
        watch: {
 
        },
        onLoad(option) {
            const _this = this
            this.ip = option.ip || ""
            uni.getSystemInfo({
                success(e) {
                    _this.windowWidth = e.windowWidth
                },
 
            })
            this.sceneName = option.sceneId || ""
            this.opType = option.opType || ""
            this.opSceneType = ""
            this.loadData()
 
        },
        onUnload() {
 
        },
        onBackPress() {
 
            if (this.opSceneType == "scan") {
 
                showModal("已构建场景将会被删除", "是否中断场景构建?").then((res) => {
                    if (res) {
                        this.opSceneType = ""
                        if (this.opType != "") {
                            uni.navigateBack({
                                delta: 1, //返回层数,2则上上页
                            })
                        }
 
                    }
                })
                return true
 
            } else if (this.opSceneType == "finish") {
                const eventChannel = this.getOpenerEventChannel();
                eventChannel.emit('create_finish', this.sceneName);
                return false
            } else {
                return false
            }
 
        },
        methods: {
            setData(obj) {
                let that = this;
                let keys = [];
                let val, data;
 
                Object.keys(obj).forEach(function(key) {
                    keys = key.split(".");
                    val = obj[key];
                    data = that.$data;
                    keys.forEach(function(key2, index) {
                        if (index + 1 == keys.length) {
                            that.$set(data, key2, val);
                        } else {
                            if (!data[key2]) {
                                that.$set(data, key2, {});
                            }
                        }
                        data = data[key2];
                    });
                });
            },
            async loadData() {
                try {
 
                    if (this.opType == "") {
                        this.localSceneList = await this.loadLocalScene()
                    } else {
                        if (this.opType == "create") {
                            this.opSceneType = "add_name"
                        }
                        if (this.opType == "extend") {
                            await addMap(this.ip, this.sceneName, "extend")
                            this.opSceneType = 'scan'
                        }
                    }
 
                    this.refreshMapLaserData()
 
 
                } catch (ex) {
 
                    showError(ex)
                }
            },
            receiveRenderData(param) {
                console.log('接收到视图层的数据:', param);
                if (param.method == "set_backgroud_progress") {
                    if (param.type == "start") {
                        this.setData({
                            bgProgressPercent: 50,
                            bgLoading: true
                        })
                    } else if (param.type == "progress") {
                        this.setData({
                            bgProgressPercent: 50 + (param.percent || 0) / 2
                        })
                    } else if (param.type == "end") {
                        this.setData({
                            bgProgressPercent: 100,
                            bgLoading: true
                        })
                        setTimeout(() => {
                            this.setData({
                                bgLoading: false
                            })
                        }, 500)
 
                    } else if (param.type == "error") {
                        this.setData({
                            bgProgressPercent: 0,
                            bgLoading: false
                        })
                    }
 
                } else if (param.method == "cancel_positioning_agv") {
                    this.positioningAgv = false
                }
            },
 
            async loadLocalScene() {
                try {
                    const list = session.getValue("scene_db") || []
                    return list
                } catch (ex) {
                    showError(ex)
                    return []
                }
            },
            clickStartConstructScene() {
                this.opSceneType = "add_name"
            },
            async clickDownloadScene() {
                try {
                    this.loading = true
                    uni.showLoading({
                        title: "下载场景中"
                    })
                    const data = this.localSceneList[0].data
                    await saveDBData(this.ip, data)
                    showToast("下载场景成功")
                    this.opSceneType = ""
                    const eventChannel = this.getOpenerEventChannel();
                    eventChannel.emit('create_finish', this.sceneName);
                    uni.navigateBack({
                        delta: 1, //返回层数,2则上上页
                    })
                } catch (ex) {
                    showError(ex)
                } finally {
                    this.loading = false
                    uni.hideLoading()
                }
 
 
            },
 
            clickNameCancel() {
                uni.navigateBack({
                    delta: 1, //返回层数,2则上上页
                })
            },
            async clickNameOK() {
                try {
                    this.loading = true
                    uni.showLoading({
                        title: "开始构建场景"
                    })
                    const name = this.sceneName.trim()
                    if (!name) {
                        showToast("站点名称还未输入")
                        return
                    }
 
                    this.sceneName = name
                    await createScene(this.ip, name, 1)
                    await addMap(this.ip, name)
                    uni.showLoading({
                        title: "开始扫描地图"
                    })
                    this.opSceneType = 'scan'
 
 
                } catch (ex) {
                    showError(ex)
                    this.opSceneType = 'add_name'
                } finally {
                    this.loading = false
                    uni.hideLoading()
                }
 
            },
            clickClearName() {
                this.sceneName = ""
                this.showClearName = false
            },
            onInputName(event) {
                this.sceneName = event.detail.value;
                if (this.sceneName)
                    this.showClearName = true
                else
                    this.showClearName = false
            },
            async clickScanFinish() {
                try {
                    this.loading = true
                    uni.showLoading({
                        title: "地图扫描结束"
                    })
                    this.opSceneType = "finish"
                    uni.setNavigationBarTitle({
                        title: this.sceneName
                    })
                    await stopMap(this.ip, this.sceneName)
                    if (this.opType == "extend") {
                        this.opSceneType = ""
                        const eventChannel = this.getOpenerEventChannel();
                        eventChannel.emit('create_finish', this.sceneName);
                        uni.navigateBack({
                            delta: 1, //返回层数,2则上上页
                        })
                    }
 
                } catch (ex) {
 
                    showModal("请检查车辆连接,并重新开始构建场景", "场景构建失败", false, "确定").then((res) => {
                        this.opSceneType = ''
                        if (this.opType != "") {
                            uni.navigateBack({
                                delta: 1, //返回层数,2则上上页
                            })
                        }
                    })
                } finally {
                    this.loading = false
                    uni.hideLoading()
                }
 
            },
            async clickFinish() {
                try {
 
                    this.loading = true
                    uni.showLoading({
                        title: "结束场景构建"
                    })
                    this.opSceneType = ""
                    const eventChannel = this.getOpenerEventChannel();
                    eventChannel.emit('create_finish', this.sceneName);
                    uni.navigateBack({
                        delta: 1, //返回层数,2则上上页
                    })
                } catch (ex) {
                    showModal("请检查车辆连接,并重新开始构建场景", "场景构建失败", false, "确定").then((res) => {
                        this.opSceneType = ''
                        if (this.opType != "") {
                            uni.navigateBack({
                                delta: 1, //返回层数,2则上上页
                            })
                        }
                    })
                } finally {
                    this.loading = false
                    uni.hideLoading()
                }
 
 
            },
            async loadMapLaserData() {
                try {
                    const info = await getMapLaserData(this.ip, this.mapId)
                    return info
                } catch (ex) {
                    showError(ex)
                    return {}
                }
            },
            async refreshMapLaserData() {
                try {
                    if (this.opSceneType === "scan") {
 
                        const data = await this.loadMapLaserData()
 
                        const listCtrData = []
                        if (data.base_map?.image_base64) {
                            this.robotPos = {}
                            this.mapId = data.base_map.map_id
                            listCtrData.push({
                                method: "background",
                                param: {
                                    proportion: 1,
                                    img_proportion: 1,
                                    max_x: data.base_map.width,
                                    max_y: data.base_map.height,
                                    min_x: 0,
                                    min_y: 0,
                                    img_x: data.base_map.width,
                                    img_y: data.base_map.height,
                                    filedata: data.base_map.image_base64
                                }
                            })
 
 
                        }
                        listCtrData.push({
                            method: "agv_laser",
                            param: data.robot_pose
                        })
                        listCtrData.push({
                            method: "point_cloud",
                            param: data.point_cloud
                        })
                        if (this.positioningAgv) {
                            listCtrData.push({
                                method: "move_pt_visible",
                                param: {
                                    x: data.robot_pose.x,
                                    y: data.robot_pose.y,
                                    width:50,
                                    height:50,
                                }
                            })
                        }
 
                        this.ctxDataStr = JSON.stringify(listCtrData)
                        this.robotPos = {
                            x: data.robot_pose.x,
                            y: data.robot_pose.y
                        }
                    }
                    setTimeout(this.refreshMapLaserData, 1000);
                } catch (ex) {
                    showError(ex).then((res) => {
                        setTimeout(this.refreshMapLaserData, 1000);
                    })
                }
            },
            async clickVehiclePosition() {
                this.positioningAgv = true
 
                if (this.robotPos.x) {
                    this.ctxDataStr = JSON.stringify([{
                        method: "move_pt_center",
                        param: {
                            x: this.robotPos.x,
                            y: this.robotPos.y
                        }
                    }])
                }
 
 
            },
 
 
        }
    }
</script>
 
<style lang="scss">
    .pages-scene {
        display: flex;
        width: 750rpx;
        height: 100vh;
        position: relative;
 
        .content {
            overflow: auto;
            position: absolute;
            left: 0;
            right: 0;
            bottom: 0;
            top: 0;
 
            .fabric {
                width: 100%;
                height: 100%;
                background-color: #fff;
            }
 
            .position {
                position: absolute;
                right: 50rpx;
                bottom: 500rpx;
 
                .ico {
                    font-size: 65rpx;
                    color: #1890FF;
                }
            }
 
        }
 
        .map-content {
            width: 100%;
            display: flex;
            flex: 1;
            flex-direction: column;
            margin: auto;
            align-items: center;
            justify-content: center;
 
 
            .text-button-group {
                display: flex;
                width: 100%;
                justify-content: center;
                align-items: center;
                flex-direction: column;
                font-size: 30rpx !important;
 
                .button {
                    margin-top: 20rpx;
                    width: 375rpx;
                }
            }
 
        }
 
 
        .bottom {
            position: fixed;
            left: 50rpx;
            right: 50rpx;
            bottom: 20rpx;
            display: flex;
 
            .bottom-content {
                display: flex;
                flex-direction: column;
                width: 100%;
                // justify-content: center;
                // align-items: center;
                padding: 0 10rpx;
                padding-bottom: 10rpx;
                background-color: #fff;
                border-radius: 10rpx;
                //background-color: #eee;
 
                .title {
                    font-size: 40rpx;
                    margin-bottom: 10rpx;
                }
 
                .tip {
                    color: #888;
                    margin: 10rpx;
                    text-align: left;
                }
 
 
                .disabled {
                    color: #ccc !important;
                }
 
                .name-input {
                    width: calc(100% - 20rpx);
                    margin-bottom: 10rpx;
                    background-color: #fff;
                    padding: 10rpx;
                    border-radius: 8rpx;
                    display: flex;
                    flex-direction: row;
                    color: #1890FF;
 
                    input {
                        flex: 1;
                    }
                }
            }
 
 
            .text-button-group {
                display: flex;
                width: 100%;
                justify-content: center;
                align-items: center;
                flex-direction: column;
                font-size: 30rpx !important;
 
                .button {
                    margin: auto;
                    margin-top: 20rpx;
                    width: calc(100% - 10rpx);
                }
 
                .am-button {
                    border-radius: 30px;
                }
 
                .am-button-ghost {
                    border: 1px solid #1677ff !important;
 
                }
            }
 
 
        }
 
 
    }
</style>