wlzboy
2025-10-18 b46065a201c09ce69f111806f2bda4a5f476bc4e
app/pages/bind-vehicle.vue
@@ -4,8 +4,8 @@
      <text class="title">绑定车辆</text>
    </view>
    
    <!-- 扫码绑定 -->
    <view class="scan-section">
    <!-- 扫码绑定 - 已隐藏 -->
    <!-- <view class="scan-section">
      <view class="section-title">扫一扫绑定</view>
      <view class="scan-content">
        <view class="scan-icon" @click="scanQRCode">
@@ -13,7 +13,7 @@
          <text class="scan-text">点击扫码绑定车辆</text>
        </view>
      </view>
    </view>
    </view> -->
    
    <!-- 下拉选择绑定 -->
    <view class="form-section">
@@ -37,15 +37,90 @@
</template>
<script>
  import { mapState } from 'vuex'
  import { listAvailableVehicles, bindVehicleToUser, getUserBoundVehicle } from '@/api/vehicle'
  export default {
    data() {
      return {
        selectedVehiclePlate: '',
        // 模拟车辆列表数据
        vehiclePlates: ['粤A12345', '粤B67890', '粤C11111', '粤D22222', '粤E33333']
        selectedVehicleId: null,
        // 车辆列表数据
        vehiclePlates: [],
        vehicleOptions: [],
        loading: false,
        // 当前绑定的车辆信息
        currentBoundVehicle: null
      }
    },
    computed: {
      ...mapState({
        currentUser: state => state.user
      })
    },
    onLoad() {
      // 加载车辆列表
      this.loadVehicleList()
      // 加载当前用户绑定的车辆
      this.loadCurrentBoundVehicle()
    },
    methods: {
      // 加载当前用户绑定的车辆
      loadCurrentBoundVehicle() {
        const userId = this.currentUser.userId
        getUserBoundVehicle(userId).then(response => {
          if (response.code === 200 && response.data) {
            this.currentBoundVehicle = response.data
            console.log('当前绑定车辆:', this.currentBoundVehicle)
          } else {
            this.currentBoundVehicle = null
          }
        }).catch(error => {
          console.error('获取绑定车辆失败:', error)
          this.currentBoundVehicle = null
        })
      },
      // 加载车辆列表(只加载同一分公司的车辆)
      loadVehicleList() {
        this.loading = true
        // 获取当前用户的部门ID
        const deptId = this.currentUser.deptId
        if (!deptId) {
          this.loading = false
          console.error('无法获取用户部门信息')
          this.$modal.showToast('无法获取用户部门信息')
          return
        }
        // 使用用户所在的部门ID加载车辆列表
        listAvailableVehicles(deptId, 'GENERAL').then(response => {
          this.loading = false
          const vehicleList = response.data || response.rows || []
          if (vehicleList.length === 0) {
            console.log('当前分公司暂无可用车辆')
            this.$modal.showToast('当前分公司暂无可用车辆')
          }
          this.vehicleOptions = vehicleList.map(vehicle => ({
            id: vehicle.vehicleId,
            name: vehicle.vehicleNo,
            type: vehicle.vehicleType,
            brand: vehicle.vehicleBrand,
            model: vehicle.vehicleModel
          }))
          this.vehiclePlates = this.vehicleOptions.map(v => v.name)
          console.log(`加载了 ${vehicleList.length} 辆车辆(部门ID: ${deptId})`)
        }).catch(error => {
          this.loading = false
          console.error('加载车辆列表失败:', error)
          this.$modal.showToast('加载车辆列表失败')
        })
      },
      // 扫码绑定车辆
      scanQRCode() {
        // #ifdef H5
@@ -72,23 +147,69 @@
      
      // 车牌号选择
      onVehiclePlateChange(e) {
        this.selectedVehiclePlate = this.vehiclePlates[e.detail.value]
        const index = e.detail.value
        this.selectedVehiclePlate = this.vehiclePlates[index]
        this.selectedVehicleId = this.vehicleOptions[index]?.id
      },
      
      // 绑定车辆
      bindVehicle() {
        if (!this.selectedVehiclePlate) {
        if (!this.selectedVehiclePlate || !this.selectedVehicleId) {
          this.$modal.showToast('请选择车牌号')
          return
        }
        
        // 这里可以调用API进行车辆绑定
        this.$modal.confirm('确认绑定车辆 ' + this.selectedVehiclePlate + ' 吗?').then(() => {
          this.$modal.showToast('车辆绑定成功')
          // 返回上一页
          this.$tab.navigateBack()
        }).catch(() => {
          // 用户取消操作
        // 检查是否选择的是当前已绑定的车辆
        if (this.currentBoundVehicle && this.currentBoundVehicle.vehicleId === this.selectedVehicleId) {
          this.$modal.showToast('当前已绑定此车辆,无需重复绑定')
          return
        }
        // 如果已经绑定了其他车辆,提示是否强制绑定
        if (this.currentBoundVehicle) {
          const currentVehicleNo = this.currentBoundVehicle.vehicleNumber || '未知车牌'
          const confirmMsg = `您当前已绑定车辆:${currentVehicleNo}\n\n确认要解绑旧车辆并绑定新车辆:${this.selectedVehiclePlate} 吗?`
          this.$modal.confirm(confirmMsg).then(() => {
            this.performBind()
          }).catch(() => {
            // 用户取消强制绑定
            console.log('用户取消强制绑定')
          })
        } else {
          // 没有绑定车辆,直接绑定
          this.$modal.confirm('确认绑定车辆 ' + this.selectedVehiclePlate + ' 吗?').then(() => {
            this.performBind()
          }).catch(() => {
            // 用户取消操作
          })
        }
      },
      // 执行绑定操作
      performBind() {
        this.loading = true
        const userId = this.currentUser.userId
        bindVehicleToUser(userId, this.selectedVehicleId).then(response => {
          this.loading = false
          if (response.code === 200) {
            this.$modal.showToast('车辆绑定成功')
            // 更新Vuex中的用户信息
            this.$store.dispatch('GetInfo')
            // 返回上一页
            setTimeout(() => {
              this.$tab.navigateBack()
            }, 1500)
          } else {
            this.$modal.showToast(response.msg || '绑定失败')
          }
        }).catch(error => {
          this.loading = false
          console.error('绑定车辆失败:', error)
          const errorMsg = error.msg || error.message || '绑定车辆失败,请重试'
          this.$modal.showToast(errorMsg)
        })
      },