<template>
|
<scroll-view class="edit-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">编辑任务</view>
|
</view>
|
|
<view class="form-section" v-if="taskDetail">
|
<!-- 使用车辆选择器组件 -->
|
<VehicleSelector
|
v-model="taskForm.vehicleId"
|
:dept-id="currentUser.deptId"
|
:auto-select-bound="false"
|
@change="onVehicleChange"
|
/>
|
|
<!-- 使用任务类型选择器组件 -->
|
<TaskTypeSelector
|
v-model="taskForm.taskType"
|
@change="onTaskTypeChange"
|
/>
|
|
<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>
|
|
<view class="loading" v-else>
|
<uni-icons type="spinner-cycle" size="40" color="#007AFF"></uni-icons>
|
<text>加载中...</text>
|
</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 { getTask, updateTask } from "@/api/task"
|
import MapSelector from './components/map-selector.vue'
|
import VehicleSelector from './components/VehicleSelector.vue'
|
import TaskTypeSelector from './components/TaskTypeSelector.vue'
|
import distanceCalculator from '@/mixins/distanceCalculator.js'
|
|
export default {
|
components: {
|
uniDatetimePicker,
|
uniPopup,
|
MapSelector,
|
VehicleSelector,
|
TaskTypeSelector
|
},
|
mixins: [distanceCalculator],
|
data() {
|
return {
|
taskId: null,
|
taskDetail: null,
|
mapSelectorType: '',
|
// 扩展 addressCoordinates 支持多种键名
|
addressCoordinates: {
|
start: null,
|
end: null,
|
startLocation: null,
|
endLocation: null
|
},
|
taskForm: {
|
taskDescription: '',
|
taskType: '',
|
vehicleId: null,
|
plannedStartTime: '',
|
plannedEndTime: '',
|
startLocation: '',
|
endLocation: '',
|
distance: '',
|
remark: ''
|
},
|
loading: false
|
}
|
},
|
computed: {
|
...mapState({
|
currentUser: state => ({
|
name: state.user.nickName || '张三',
|
position: '司机',
|
deptId: state.user.deptId || 100
|
})
|
})
|
},
|
onLoad(options) {
|
if (options.id) {
|
this.taskId = options.id
|
this.loadTaskDetail()
|
} else {
|
this.$modal.showToast('任务ID不能为空')
|
setTimeout(() => {
|
uni.navigateBack()
|
}, 1500)
|
}
|
},
|
methods: {
|
// 加载任务详情
|
loadTaskDetail() {
|
if (!this.taskId) {
|
this.$modal.showToast('任务ID不能为空')
|
return
|
}
|
|
getTask(this.taskId).then(response => {
|
this.taskDetail = response.data || response
|
|
// 填充表单数据
|
this.taskForm.taskDescription = this.taskDetail.taskDescription || ''
|
this.taskForm.taskType = this.taskDetail.taskType || ''
|
this.taskForm.plannedStartTime = this.taskDetail.plannedStartTime || ''
|
this.taskForm.plannedEndTime = this.taskDetail.plannedEndTime || ''
|
this.taskForm.startLocation = this.taskDetail.departureAddress || ''
|
this.taskForm.endLocation = this.taskDetail.destinationAddress || ''
|
this.taskForm.distance = this.taskDetail.estimatedDistance || ''
|
this.taskForm.remark = this.taskDetail.remark || ''
|
|
// 设置车辆信息
|
if (this.taskDetail.assignedVehicles && this.taskDetail.assignedVehicles.length > 0) {
|
const firstVehicle = this.taskDetail.assignedVehicles[0]
|
this.taskForm.vehicleId = firstVehicle.vehicleId
|
}
|
|
// 设置地址坐标(使用mixin中的方法)
|
if (this.taskDetail.departureLongitude && this.taskDetail.departureLatitude) {
|
const startLocation = {
|
lng: this.taskDetail.departureLongitude,
|
lat: this.taskDetail.departureLatitude
|
}
|
this.setStartLocation(startLocation)
|
// 同时保存到 startLocation 键
|
this.addressCoordinates.startLocation = startLocation
|
}
|
if (this.taskDetail.destinationLongitude && this.taskDetail.destinationLatitude) {
|
const endLocation = {
|
lng: this.taskDetail.destinationLongitude,
|
lat: this.taskDetail.destinationLatitude
|
}
|
this.setEndLocation(endLocation)
|
// 同时保存到 endLocation 键
|
this.addressCoordinates.endLocation = endLocation
|
}
|
}).catch(error => {
|
console.error('加载任务详情失败:', error)
|
this.$modal.showToast('加载任务详情失败')
|
setTimeout(() => {
|
uni.navigateBack()
|
}, 1500)
|
})
|
},
|
|
// 车辆选择变化
|
onVehicleChange(vehicle) {
|
// 组件已经处理了vehicleId的更新
|
console.log('选中车辆:', vehicle)
|
},
|
|
// 任务类型选择变化
|
onTaskTypeChange(taskType) {
|
// 组件已经处理了taskType的更新
|
console.log('选中任务类型:', taskType)
|
},
|
|
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) {
|
const formattedAddress = address.title + ' - ' + address.address
|
|
if (this.mapSelectorType === 'startLocation') {
|
this.taskForm.startLocation = formattedAddress
|
const location = this.setLocationByAddress('start', address)
|
// 同时保存到 startLocation 键
|
this.addressCoordinates.startLocation = location
|
} else if (this.mapSelectorType === 'endLocation') {
|
this.taskForm.endLocation = formattedAddress
|
const location = this.setLocationByAddress('end', address)
|
// 同时保存到 endLocation 键
|
this.addressCoordinates.endLocation = location
|
}
|
|
// 监听mixin中的距离计算完成事件
|
this.$once('distance-calculated', (distance) => {
|
this.taskForm.distance = this.formatDistance(distance)
|
})
|
|
this.closeMapSelector()
|
},
|
|
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() {
|
// 构建提交数据 - 使用与 TaskCreateVO 一致的结构
|
const submitData = {
|
taskId: this.taskId,
|
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,
|
departureLongitude: this.addressCoordinates.startLocation ? this.addressCoordinates.startLocation.lng : null,
|
departureLatitude: this.addressCoordinates.startLocation ? this.addressCoordinates.startLocation.lat : null,
|
|
// 目标地地址和坐标
|
destinationAddress: this.taskForm.endLocation,
|
destinationLongitude: this.addressCoordinates.endLocation ? this.addressCoordinates.endLocation.lng : null,
|
destinationLatitude: this.addressCoordinates.endLocation ? this.addressCoordinates.endLocation.lat : null,
|
|
// 距离(主任务字段)
|
distance: this.taskForm.distance ? parseFloat(this.taskForm.distance) : null,
|
|
remark: this.taskForm.remark
|
}
|
|
return submitData
|
},
|
|
submitTask() {
|
if (!this.validateForm()) {
|
return
|
}
|
|
this.$modal.confirm('确定要保存修改吗?').then(() => {
|
this.loading = true
|
const submitData = this.buildSubmitData()
|
|
updateTask(submitData).then(response => {
|
this.loading = false
|
this.$modal.showToast('任务更新成功')
|
setTimeout(() => {
|
uni.navigateBack()
|
}, 1500)
|
}).catch(error => {
|
this.loading = false
|
console.error('任务更新失败:', error)
|
this.$modal.showToast('任务更新失败,请重试')
|
})
|
}).catch(() => {})
|
},
|
|
goBack() {
|
uni.navigateBack()
|
}
|
}
|
}
|
</script>
|
|
<style lang="scss" scoped>
|
.edit-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;
|
}
|
}
|
}
|
}
|
|
.loading {
|
display: flex;
|
flex-direction: column;
|
align-items: center;
|
justify-content: center;
|
height: 400rpx;
|
color: #999;
|
|
text {
|
margin-top: 20rpx;
|
font-size: 28rpx;
|
}
|
}
|
|
.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>
|