wlzboy
2025-11-01 82d6b98f5b6b88e223259547208ab59829ad723e
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
<template>
  <scroll-view class="create-normal-task-container" scroll-y="true">
    <view class="form-header">
      <view class="back-btn" @click="goBack">
        <uni-icons type="arrowleft" size="20"></uni-icons>
      </view>
      <view class="title">创建{{ categoryName }}任务</view>
    </view>
    
    <view class="form-section">
      <view class="form-item">
        <view class="form-label">任务车辆</view>
        <picker mode="selector" :range="vehicles" @change="onVehicleChange">
          <view class="form-input picker-input">
            {{ selectedVehicle || '请选择任务车辆' }}
            <uni-icons type="arrowright" size="16" color="#999"></uni-icons>
          </view>
        </picker>
      </view>
      
      <view class="form-item">
        <view class="form-label">任务类型</view>
        <picker mode="selector" :range="taskTypeLabels" :value="getTaskTypeIndex()" @change="onTaskTypeChange">
          <view class="form-input picker-input">
            {{ selectedTaskTypeLabel || '请选择任务类型' }}
            <uni-icons type="arrowright" size="16" color="#999"></uni-icons>
          </view>
        </picker>
      </view>
      
      <view class="form-item">
        <view class="form-label">任务描述</view>
        <textarea 
          class="form-textarea" 
          placeholder="请输入任务描述" 
          v-model="taskForm.taskDescription"
        />
      </view>
      
      <view class="form-item">
        <view class="form-label">任务出发地</view>
        <view class="form-input picker-input" @click="selectStartLocation">
          {{ taskForm.startLocation || '请选择任务出发地' }}
          <uni-icons type="arrowright" size="16" color="#999"></uni-icons>
        </view>
      </view>
      
      <view class="form-item">
        <view class="form-label">任务目的地</view>
        <view class="form-input picker-input" @click="selectEndLocation">
          {{ taskForm.endLocation || '请选择任务目的地' }}
          <uni-icons type="arrowright" size="16" color="#999"></uni-icons>
        </view>
      </view>
      
      <view class="form-item">
        <view class="form-label">行驶公里数</view>
        <input 
          class="form-input" 
          type="digit" 
          placeholder="请输入行驶公里数" 
          v-model="taskForm.distance"
        />
      </view>
      
      <view class="form-item">
        <view class="form-label">计划开始时间</view>
        <uni-datetime-picker 
          v-model="taskForm.plannedStartTime" 
          type="datetime" 
          :placeholder="'请选择计划开始时间'"
          class="form-input"
        />
      </view>
      
      <view class="form-item">
        <view class="form-label">计划结束时间</view>
        <uni-datetime-picker 
          v-model="taskForm.plannedEndTime" 
          type="datetime" 
          :placeholder="'请选择计划结束时间'"
          class="form-input"
        />
      </view>
      
      <view class="form-item">
        <view class="form-label">执行人</view>
        <input 
          class="form-input" 
          :value="currentUser.name" 
          disabled
        />
      </view>
      
      <view class="form-item">
        <view class="form-label">备注</view>
        <textarea 
          class="form-textarea" 
          placeholder="请输入备注信息(最多200字)" 
          v-model="taskForm.remark"
          maxlength="200"
        />
      </view>
      
      <view class="form-actions">
        <button class="submit-btn" @click="submitTask" :disabled="loading">
          {{ loading ? '保存中...' : '保存' }}
        </button>
      </view>
    </view>
    
    <!-- 地图选择器弹窗 -->
    <uni-popup ref="mapPopup" type="bottom" :mask-click="false">
      <view class="map-popup-container">
        <view class="popup-header">
          <view class="popup-title">选择地址</view>
          <view class="close-btn" @click="closeMapSelector">
            <uni-icons type="closeempty" size="20" color="#999"></uni-icons>
          </view>
        </view>
        <map-selector 
          :initial-address="getInitialAddress()" 
          @addressSelected="onAddressSelected"
        ></map-selector>
      </view>
    </uni-popup>
  </scroll-view>
</template>
 
<script>
import { mapState } from 'vuex'
import uniDatetimePicker from '@/uni_modules/uni-datetime-picker/components/uni-datetime-picker/uni-datetime-picker.vue'
import uniPopup from '@/uni_modules/uni-popup/components/uni-popup/uni-popup.vue'
import { getUserProfile } from "@/api/system/user"
import { addTask } from "@/api/task"
import { listAvailableVehicles } from "@/api/vehicle"
import { calculateDistance } from "@/api/map"
import { getDicts } from "@/api/dict"
import MapSelector from '@/components/map-selector.vue'
 
export default {
  components: {
    uniDatetimePicker,
    uniPopup,
    MapSelector
  },
  data() {
    return {
      categoryName: '',
      categoryType: '',
      defaultTaskType: '',
      selectedVehicle: '',
      selectedVehicleId: null,
      selectedTaskType: '',
      selectedTaskTypeLabel: '',
      boundVehicle: '',
      boundVehicleId: null,
      taskTypeOptions: [],
      taskTypeLabels: [],
      mapSelectorType: '',
      taskForm: {
        taskDescription: '',
        taskType: '',
        vehicleId: null,
        plannedStartTime: '',
        plannedEndTime: '',
        startLocation: '',
        endLocation: '',
        distance: '',
        remark: ''
      },
      vehicles: [],
      vehicleOptions: [],
      loading: false,
      addressCoordinates: {
        startLocation: null,
        endLocation: null
      }
    }
  },
  computed: {
    ...mapState({
      currentUser: state => ({
        name: state.user.nickName || '张三',
        position: '司机',
        deptId: state.user.deptId || 100
      })
    })
  },
  onLoad(options) {
    // 获取任务类型信息
    if (options.categoryName) {
      this.categoryName = options.categoryName
    }
    if (options.categoryType) {
      this.categoryType = options.categoryType
    }
    if (options.taskType) {
      this.defaultTaskType = options.taskType
      this.selectedTaskType = options.taskType
    }
    
    // 加载数据
    this.loadTaskTypes().then(() => {
      this.getAvailableVehicles().then(() => {
        this.getUserBoundVehicle()
      })
    })
  },
  methods: {
    // 加载任务类型字典
    loadTaskTypes() {
      return getDicts('sys_task_type').then(response => {
        const dictData = response.data || []
        this.taskTypeOptions = dictData.map(item => ({
          label: item.dictLabel,
          value: item.dictValue
        }))
        this.taskTypeLabels = this.taskTypeOptions.map(item => item.label)
        
        if (this.selectedTaskType) {
          const taskTypeOption = this.taskTypeOptions.find(item => item.value === this.selectedTaskType)
          if (taskTypeOption) {
            this.selectedTaskTypeLabel = taskTypeOption.label
            this.taskForm.taskType = taskTypeOption.value
          }
        }
      }).catch(error => {
        console.error('加载任务类型字典失败:', error)
        this.taskTypeOptions = [
          { label: '维修保养', value: 'MAINTENANCE' },
          { label: '加油任务', value: 'FUEL' },
          { label: '其他', value: 'OTHER' }
        ]
        this.taskTypeLabels = this.taskTypeOptions.map(item => item.label)
      })
    },
    
    // 获取任务类型在列表中的索引
    getTaskTypeIndex() {
      if (!this.selectedTaskType) return -1
      return this.taskTypeOptions.findIndex(item => item.value === this.selectedTaskType)
    },
    
    // 任务类型选择变化
    onTaskTypeChange(e) {
      const index = e.detail.value
      const selectedOption = this.taskTypeOptions[index]
      if (selectedOption) {
        this.selectedTaskType = selectedOption.value
        this.selectedTaskTypeLabel = selectedOption.label
        this.taskForm.taskType = selectedOption.value
      }
    },
    
    // 获取用户绑定的车辆信息
    getUserBoundVehicle() {
      getUserProfile().then(response => {
        const userInfo = response.data || response
        if (userInfo.boundVehicle) {
          const boundVehicleNo = userInfo.boundVehicle.vehicleNumber
          const boundVehicleId = userInfo.boundVehicle.vehicleId
          
          const vehicleIndex = this.vehicleOptions.findIndex(v => v.id === boundVehicleId || v.name === boundVehicleNo)
          
          if (vehicleIndex !== -1) {
            this.boundVehicle = this.vehicleOptions[vehicleIndex].name
            this.boundVehicleId = this.vehicleOptions[vehicleIndex].id
            this.selectedVehicle = this.boundVehicle
            this.selectedVehicleId = this.boundVehicleId
            this.taskForm.vehicleId = this.boundVehicleId
          }
        }
      }).catch(error => {
        console.error('获取用户绑定车辆信息失败:', error)
      })
    },
    
    // 获取可用车辆列表
    getAvailableVehicles() {
      const deptId = this.currentUser.deptId
      return listAvailableVehicles(deptId, 'GENERAL').then(response => {
        const vehicleList = response.data || response.rows || []
        this.vehicleOptions = vehicleList.map(vehicle => ({
          id: vehicle.vehicleId,
          name: vehicle.vehicleNo,
          type: vehicle.vehicleType,
          status: vehicle.status
        }))
        this.vehicles = this.vehicleOptions.map(v => v.name)
      }).catch(() => {
        this.vehicles = []
      })
    },
    
    onVehicleChange(e) {
      const index = e.detail.value
      this.selectedVehicle = this.vehicles[index]
      this.selectedVehicleId = this.vehicleOptions[index]?.id
      this.taskForm.vehicleId = this.selectedVehicleId
    },
    
    selectStartLocation() {
      this.mapSelectorType = 'startLocation'
      this.$refs.mapPopup.open()
    },
    
    selectEndLocation() {
      this.mapSelectorType = 'endLocation'
      this.$refs.mapPopup.open()
    },
    
    getInitialAddress() {
      return this.mapSelectorType === 'startLocation' ? this.taskForm.startLocation : this.taskForm.endLocation
    },
    
    onAddressSelected(address) {
      if (this.mapSelectorType === 'startLocation') {
        this.taskForm.startLocation = address.title + ' - ' + address.address
        this.addressCoordinates.startLocation = {
          lat: address.lat,
          lng: address.lng
        }
      } else if (this.mapSelectorType === 'endLocation') {
        this.taskForm.endLocation = address.title + ' - ' + address.address
        this.addressCoordinates.endLocation = {
          lat: address.lat,
          lng: address.lng
        }
      }
      
      this.calculateDistance()
      this.closeMapSelector()
    },
    
    calculateDistance() {
      if (this.addressCoordinates.startLocation && this.addressCoordinates.endLocation) {
        this.getDistanceBetweenPoints(
          this.addressCoordinates.startLocation.lat,
          this.addressCoordinates.startLocation.lng,
          this.addressCoordinates.endLocation.lat,
          this.addressCoordinates.endLocation.lng
        ).then(distance => {
          this.taskForm.distance = distance.toFixed(2)
        }).catch(error => {
          console.error('距离计算失败:', error)
        })
      }
    },
    
    getDistanceBetweenPoints(lat1, lng1, lat2, lng2) {
      return new Promise((resolve, reject) => {
        calculateDistance(lat1, lng1, lat2, lng2).then(response => {
          if (response.code === 200) {
            const responseData = typeof response.data === 'string' ? JSON.parse(response.data) : response.data
            if (responseData && responseData.status === 0 && responseData.result && responseData.result.elements && responseData.result.elements.length > 0) {
              const distanceInKm = responseData.result.elements[0].distance / 1000
              resolve(distanceInKm)
            } else {
              reject(new Error('距离计算接口返回数据格式不正确'))
            }
          } else {
            reject(new Error('距离计算接口调用失败'))
          }
        }).catch(error => {
          reject(error)
        })
      })
    },
    
    closeMapSelector() {
      this.$refs.mapPopup.close()
      this.mapSelectorType = ''
    },
    
    validateForm() {
      if (!this.taskForm.vehicleId) {
        this.$modal.showToast('请选择任务车辆')
        return false
      }
      
      if (!this.taskForm.taskType) {
        this.$modal.showToast('请选择任务类型')
        return false
      }
      
      if (!this.taskForm.taskDescription) {
        this.$modal.showToast('请输入任务描述')
        return false
      }
      
      if (!this.taskForm.startLocation) {
        this.$modal.showToast('请选择任务出发地')
        return false
      }
      
      if (!this.taskForm.endLocation) {
        this.$modal.showToast('请选择任务目的地')
        return false
      }
      
      if (!this.taskForm.plannedStartTime) {
        this.$modal.showToast('请选择开始时间')
        return false
      }
      
      if (!this.taskForm.plannedEndTime) {
        this.$modal.showToast('请选择结束时间')
        return false
      }
      
      return true
    },
    
    buildSubmitData() {
      const submitData = {
        taskDescription: this.taskForm.taskDescription,
        taskType: this.taskForm.taskType,
        vehicleIds: this.taskForm.vehicleId ? [this.taskForm.vehicleId] : [],
        plannedStartTime: this.taskForm.plannedStartTime,
        plannedEndTime: this.taskForm.plannedEndTime,
        departureAddress: this.taskForm.startLocation,
        destinationAddress: this.taskForm.endLocation,
        estimatedDistance: this.taskForm.distance ? parseFloat(this.taskForm.distance) : null,
        remark: this.taskForm.remark
      }
      
      if (this.addressCoordinates.startLocation) {
        submitData.departureLongitude = this.addressCoordinates.startLocation.lng
        submitData.departureLatitude = this.addressCoordinates.startLocation.lat
      }
      
      if (this.addressCoordinates.endLocation) {
        submitData.destinationLongitude = this.addressCoordinates.endLocation.lng
        submitData.destinationLatitude = this.addressCoordinates.endLocation.lat
      }
      
      return submitData
    },
    
    submitTask() {
      if (!this.validateForm()) {
        return
      }
      
      this.$modal.confirm('确定要保存任务吗?').then(() => {
        this.loading = true
        const submitData = this.buildSubmitData()
        
        addTask(submitData).then(response => {
          this.loading = false
          this.$modal.showToast('任务创建成功')
          setTimeout(() => {
            this.$tab.navigateTo('/pages/task/index')
          }, 1500)
        }).catch(error => {
          this.loading = false
          console.error('任务创建失败:', error)
          this.$modal.showToast('任务创建失败,请重试')
        })
      }).catch(() => {})
    },
    
    goBack() {
      uni.navigateBack()
    }
  }
}
</script>
 
<style lang="scss" scoped>
.create-normal-task-container {
  padding: 20rpx;
  background-color: #f5f5f5;
  min-height: 100vh;
  
  .form-header {
    display: flex;
    align-items: center;
    padding: 20rpx 0;
    margin-bottom: 30rpx;
    
    .back-btn {
      width: 60rpx;
      height: 60rpx;
      border-radius: 50%;
      background-color: #f0f0f0;
      display: flex;
      align-items: center;
      justify-content: center;
      margin-right: 20rpx;
    }
    
    .title {
      font-size: 36rpx;
      font-weight: bold;
      color: #333;
    }
  }
  
  .form-section {
    background-color: white;
    border-radius: 15rpx;
    padding: 30rpx;
    box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
    
    .form-item {
      margin-bottom: 40rpx;
      
      .form-label {
        font-size: 28rpx;
        margin-bottom: 15rpx;
        color: #333;
      }
      
      .form-input {
        height: 70rpx;
        padding: 0 20rpx;
        border: 1rpx solid #eee;
        border-radius: 10rpx;
        font-size: 28rpx;
        
        &.picker-input {
          display: flex;
          align-items: center;
          justify-content: space-between;
        }
        
        &[disabled] {
          background-color: #f5f5f5;
          color: #999;
        }
      }
      
      .form-textarea {
        width: 100%;
        min-height: 150rpx;
        padding: 20rpx;
        border: 1rpx solid #eee;
        border-radius: 10rpx;
        font-size: 28rpx;
      }
    }
    
    .form-actions {
      margin-top: 50rpx;
      text-align: center;
      
      .submit-btn {
        width: 80%;
        height: 80rpx;
        background-color: #007AFF;
        color: white;
        border-radius: 10rpx;
        font-size: 32rpx;
        
        &[disabled] {
          background-color: #ccc;
          color: #999;
        }
      }
    }
  }
  
  .map-popup-container {
    height: 80vh;
    background-color: white;
    border-top-left-radius: 20rpx;
    border-top-right-radius: 20rpx;
    overflow: hidden;
    
    .popup-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 20rpx 30rpx;
      border-bottom: 1rpx solid #f0f0f0;
      
      .popup-title {
        font-size: 32rpx;
        font-weight: bold;
        color: #333;
      }
      
      .close-btn {
        width: 50rpx;
        height: 50rpx;
        display: flex;
        align-items: center;
        justify-content: center;
      }
    }
  }
}
</style>