cuiqian2004
8 天以前 30311cd24ee3b1567ffafac002494bb67feda657
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
<template>
    <view class="pages-task-list">
        <view class="list-content" v-if="list.length > 0">
            <view class="list-header" v-if="fixedList.length > 0">固定任务</view>
            <uni-swipe-action class="list">
                <uni-swipe-action-item :class="index== 0 ?'':'list-item'" v-for="(item,index) in fixedList" :key="index"
                    :auto-close="true" :disabled="taskStatus.taskGroupID == item.taskGroupID">
                    <TaskItemView :taskData="item" :taskStatus="taskStatus" @click-item="clickEditTask"
                        @click-play="clickPlayTask" @click-pause="clickPauseTask" @click-stop="clickStopTask"
                        @click-skip="clickSkipTask" @click-repeat="clickRepeatTask">
                    </TaskItemView>
                    <template v-slot:right>
                        <view class="btn-del" @click="clickDelSetTask(item)">删除</view>
                    </template>
                </uni-swipe-action-item>
            </uni-swipe-action>
            <view class="list-header" v-if="tempList.length > 0">临时任务</view>
            <uni-swipe-action class="list">
                <uni-swipe-action-item :class="index== 0 ?'':'list-item'" v-for="(item,index) in tempList" :key="index"
                    :auto-close="true" :disabled="taskStatus.taskGroupID == item.taskGroupID">
                    <TaskItemView :taskData="item" :taskStatus="taskStatus" @click-item="clickEditTask"
                        @click-play="clickPlayTask" @click-pause="clickPauseTask" @click-stop="clickStopTask"
                        @click-skip="clickSkipTask" @click-repeat="clickRepeatTask">
                    </TaskItemView>
                    <template v-slot:right>
                        <view class="btn-del" @click="clickDelSetTask(item)">删除</view>
                    </template>
                </uni-swipe-action-item>
            </uni-swipe-action>
        </view>
        <view class="list-no-content" v-else>
            <uni-icons color="#ccc" type="info" size="128"></uni-icons>
            <view class="space">没有找到符合条件的任务</view>
        </view>
        <view class="position-add" @click="clickAddTask()" @touchstart='btnAddTouchStart' @touchmove='btnAddTouchMove'
            :style="{transform:`translate(${btnAddInfo.x}px,${btnAddInfo.y}px) scale(1)`}">
            <uni-icons class="img" type="plus-filled" size="80" color="#1890FF"></uni-icons>
        </view>
    </view>
</template>
 
<script>
    import {
        showToast,
        showModal,
        session,
    } from "@/comm/utils.js"
    import TaskItemView from "./infos/task-item.vue"
    import {
        tasks,
        taskGroupStatus,
        addTaskGroupCmd,
        cancelTask,
        delTask
 
    } from "@/api/vehicle.js"
    import {
        v4 as uuidv4
    } from 'uuid';
    export default {
        name: "PagesTasklist",
        components: {
            TaskItemView
        },
        data() {
            return {
                ip: "",
                sceneId: "",
                list: [],
                startX: 0, // 触摸起始点
                deleteX: [
                    [],
                    []
                ], // 每个项目的偏移量
                options: [{
                    text: '删除',
                    style: {
                        backgroundColor: '#F56C6C'
                    }
                }],
                taskStatus: {},
                btnAddInfo: {
                    lx: 0,
                    ly: 0,
                    x: 0,
                    y: 0
                },
            }
        },
        computed: {
            fixedList() {
                return this.list.filter((a) => a.tasktype == 1)
            },
            tempList() {
                return this.list.filter((a) => a.tasktype != 1)
            },
        },
        onLoad(option) {
            this.ip = option.ip || ""
            this.sceneId = option.sceneId || ""
            this.loadData()
        },
        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 {
                    let btninfo = session.getValue('task_btn_add')
                    if (btninfo) {
 
                        if (btninfo.x > 10) {
                            btninfo.x = 10
                        }
                        if (btninfo.x > 40) {
                            btninfo.x = 40
                        }
                        this.btnAddInfo = btninfo
                    }
                    const res = await tasks(this.ip)
                    const list = res?.data || []
                    this.list = list
                    if (this.list.length > 0) {
                        this.timerCheckTaskGroupStatus()
                    }
                } catch (ex) {
 
                    this.showError(ex)
                }
            },
            async timerCheckTaskGroupStatus() {
                if (this.timerCheckStatusId) {
                    return
                }
                if (this.list.length == 0) {
                    return
                }
                await this.checkTaskGroupStatus()
                this.timerCheckStatusId = setTimeout(() => {
                    this.timerCheckStatusId = 0
                    this.timerCheckTaskGroupStatus()
                }, 1000)
            },
            async checkTaskGroupStatus() {
                try {
                    const key = `task_cmd_id_${this.ip.replace(".","_")}`
                    const cmdID = session.getValue(key)
                    if (cmdID) {
                        const info = await taskGroupStatus(this.ip) || {}
                        if (info.taskGroupCmdID == cmdID) {
 
                            const list = info.taskStatusList || []
                            for (let i in list) {
                                if (list[i].status != 1 && list[i].status != 0) {
                                    this.taskStatus = {}
                                    return
                                }
                            }
                            this.taskStatus = info
                            return
                        }
 
                    }
                    this.taskStatus = {}
 
                } catch (ex) {
 
                    this.taskStatus = {}
                    this.showError(ex)
                }
            },
            setTaskGroupStatus(cmdID) {
                const key = `task_cmd_id_${this.ip.replace(".","_")}`
                session.setValue(key, cmdID)
 
            },
            clickAddTask() {
                const _this = this
                uni.navigateTo({
                    url: `/pages/task/add?ip=${this.ip}&title=新增任务`,
                    events: {
                        // 为指定事件添加一个监听器,获取被打开页面传送到当前页面的数据
                        add_task: function(data) {
                            _this.loadData()
                        },
                    }
                })
            },
            btnAddTouchStart(e) {
                this.btnAddInfo.lx = e.touches[0].clientX;
                this.btnAddInfo.ly = e.touches[0].clientY;
            },
            btnAddTouchMove(e) {
 
                if (this.btnAddInfo.ly < 40) {
                    this.btnAddInfo.x += (e.touches[0].clientX - this.btnAddInfo.lx)
                    this.btnAddInfo.lx = e.touches[0].clientX;
                    this.btnAddInfo.ly = e.touches[0].clientY;
                    if (this.btnAddInfo.x < 50 - this.windowWidth) {
                        this.btnAddInfo.x = 50 - this.windowWidth
                    }
                    if (this.btnAddInfo.x > 40) {
                        this.btnAddInfo.x = 40
                    }
                    session.setValue('task_btn_add', this.btnAddInfo)
                    return
                }
 
                this.btnAddInfo.x += (e.touches[0].clientX - this.btnAddInfo.lx)
                this.btnAddInfo.y += (e.touches[0].clientY - this.btnAddInfo.ly)
                this.btnAddInfo.lx = e.touches[0].clientX;
                this.btnAddInfo.ly = e.touches[0].clientY;
 
 
                if (this.btnAddInfo.x < 50 - this.windowWidth) {
                    this.btnAddInfo.x = 50 - this.windowWidth
                }
                if (this.btnAddInfo.x > 40) {
                    this.btnAddInfo.x = 40
                }
                if (this.btnAddInfo.y > 80) {
                    this.btnAddInfo.y = 80
                }
 
 
 
                session.setValue('task_btn_add', this.btnAddInfo)
            },
 
 
            clickEditTask(item) {
                const _this = this
                if (this.taskStatus.taskGroupID == item.taskGroupID) {
                    return
                }
                uni.navigateTo({
                    url: `/pages/task/update?ip=${this.ip}&title=更新任务&task=${JSON.stringify(item)}`,
                    events: {
                        // 为指定事件添加一个监听器,获取被打开页面传送到当前页面的数据
                        update_task: function(data) {
                            const curIndex = _this.list.findIndex((a) => a.taskGroupID == data.taskGroupID)
                            if (curIndex > -1) {
                                _this.list[curIndex] = {
                                    taskGroupID: data.taskGroupID,
                                    taskGroupName: data.taskGroupName,
                                    cycleTime: data.cycleTime,
                                    taskButton: data.taskButton,
                                    tasktype: data.tasktype,
                                    taskList: data.taskList
                                }
                            }
                        },
                    }
                })
            },
            async clickPlayTask(item) {
                try {
                    const cmdID = uuidv4()
                    await addTaskGroupCmd(this.ip, item.taskGroupID, cmdID, 1)
                    this.setTaskGroupStatus(cmdID)
 
                } catch (ex) {
                    this.showError(ex)
                }
            },
 
            async clickStopTask(item) {
                try {
                    await addTaskGroupCmd(this.ip, item.taskGroupID, this.taskStatus.taskGroupCmdID, 2)
                } catch (ex) {
                    this.showError(ex)
                }
            },
 
            async clickSkipTask(item) {
                try {
 
 
                    const taskStatusList = this.taskStatus.taskStatusList || []
                    let curIdx = taskStatusList.findIndex((a) => a.status == 1)
                    if (curIdx < 0)
                        curIdx = taskStatusList.findIndex((a) => a.status == 0)
                    if (curIdx > -1) {
                        const taskList = []
                        taskList.push({
                            taskID: taskStatusList[curIdx].taskID,
                            is_cancel: 1
                        })
                        await cancelTask(this.ip,
                            item.taskGroupID,
                             this.taskStatus.taskGroupCmdID,
                            taskList
                        )
 
                        showToast("跳过子任务成功")
 
                    } else {
                        showToast("未找到未开始或正在执行的子任务")
                    }
                } catch (ex) {
                    this.showError(ex)
                }
            },
 
            async clickRepeatTask(item) {
 
 
            },
 
            async clickPauseTask(item) {},
            clickDelSetTask(item) {
                showModal("确认删除任务设置", "警告").then((res) => {
                    if (res) {
                        this.deleteTask(item)
 
                    }
                })
            },
            async deleteTask(item) {
                try {
                    await delTask(this.ip, [{
                        taskGroupID: item.taskGroupID,
                    }])
                    const list = this.list
                    const index = list.findIndex((a) =>
                        a.taskGroupID == item.taskGroupID
                    )
                    console.log(index, item, list)
                    if (index < 0)
                        return
                    list.splice(index, 1)
                    this.list = [...list]
 
                } catch (ex) {
                    this.showError(ex)
                }
 
            },
 
 
            showError(ex) {
                let exStr = JSON.stringify(ex)
                if (exStr == "{}")
                    exStr = ex
                let tip = typeof ex.msg == "string" ? ex.msg : exStr
                showModal(tip, "错误", false)
            },
        }
    }
</script>
 
<style lang="scss">
    .pages-task-list {
        display: flex;
        width: 750rpx;
        height: 100vh;
        flex-direction: column;
        position: relative;
 
        .list-content {
            display: flex;
            width: 100%;
            padding: 0 10px;
            flex: 1;
            flex-direction: column;
            overflow-y: auto;
            overflow-x: hidden;
 
            .list-header {
                margin: 10px;
                // border-bottom: 1px solid #ddd;
            }
 
            .list {
                // border: 1px solid #ccc;
                border-radius: 10px;
 
                background-color: #fff;
 
                .list-item {
 
                    border-top: 1px solid #ddd;
                }
            }
 
            .btn-del {
                margin: 10rpx;
                display: flex;
                padding: 0 10rpx;
                background-color: #FF4D4F;
                font-weight: 700;
                color: #fff;
                justify-content: center;
                align-items: center;
            }
 
        }
 
        .list-no-content {
            margin-top: 50px;
            padding: 20rpx 40rpx;
            align-items: center;
            text-align: center;
            display: flex;
            flex-direction: column;
            font-size: 30rpx;
            font-weight: 400;
 
            .img {
                width: 212rpx;
                height: 212rpx;
                color: #ddd;
            }
 
            .space {
                margin-top: 20rpx;
            }
 
        }
 
        .position-add {
            position: fixed;
            right: 10px;
            bottom: 40px;
        }
    }
</style>