wlzboy
2025-12-02 d294abb765e4ed349907c92ce313689c6299ba7d
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
<template>
  <view class="bind-vehicle-container">
    <view class="header">
      <text class="title">绑定车辆</text>
    </view>
    
    <!-- 扫码绑定 - 已隐藏 -->
    <!-- <view class="scan-section">
      <view class="section-title">扫一扫绑定</view>
      <view class="scan-content">
        <view class="scan-icon" @click="scanQRCode">
          <uni-icons type="scan" size="40" color="#007AFF"></uni-icons>
          <text class="scan-text">点击扫码绑定车辆</text>
        </view>
      </view>
    </view> -->
    
    <!-- 下拉选择绑定 -->
    <view class="form-section">
      <view class="section-title">选择车牌号绑定</view>
      <view class="form-item">
        <view class="form-label">车牌号</view>
        <picker mode="selector" :range="vehiclePlates" @change="onVehiclePlateChange">
          <view class="form-input picker-input">
            {{ selectedVehiclePlate || '请选择车牌号' }}
            <uni-icons type="arrowright" size="16" color="#999"></uni-icons>
          </view>
        </picker>
      </view>
      
      <view class="form-actions">
        <button class="submit-btn" @click="bindVehicle" :disabled="!selectedVehiclePlate">确认绑定</button>
        <button class="cancel-btn" @click="goBack">取消</button>
      </view>
    </view>
  </view>
</template>
 
<script>
  import { mapState } from 'vuex'
  import { listAvailableVehicles, bindVehicleToUser, getUserBoundVehicle } from '@/api/vehicle'
  
  export default {
    data() {
      return {
        selectedVehiclePlate: '',
        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
        this.$modal.showToast('H5环境暂不支持扫码功能')
        // #endif
        
        // #ifndef H5
        uni.scanCode({
          success: (res) => {
            console.log('扫码结果:', res)
            // 解析扫码结果,这里假设扫码结果是车牌号
            this.selectedVehiclePlate = res.result
            this.$modal.showToast('扫码成功,正在绑定车辆...')
            // 扫码成功后自动绑定
            this.bindVehicle()
          },
          fail: (err) => {
            console.log('扫码失败:', err)
            this.$modal.showToast('扫码失败,请重试')
          }
        })
        // #endif
      },
      
      // 车牌号选择
      onVehiclePlateChange(e) {
        const index = e.detail.value
        this.selectedVehiclePlate = this.vehiclePlates[index]
        this.selectedVehicleId = this.vehicleOptions[index]?.id
      },
      
      // 绑定车辆
      bindVehicle() {
        if (!this.selectedVehiclePlate || !this.selectedVehicleId) {
          this.$modal.showToast('请选择车牌号')
          return
        }
        
        // 检查是否选择的是当前已绑定的车辆
        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)
        })
      },
      
      goBack() {
        this.$tab.navigateBack()
      }
    }
  }
</script>
 
<style lang="scss">
  .bind-vehicle-container {
    padding: 20rpx;
    background-color: #f5f5f5;
    min-height: 100vh;
  }
  
  .header {
    text-align: center;
    padding: 40rpx 0;
    
    .title {
      font-size: 40rpx;
      font-weight: bold;
      color: #333;
    }
  }
  
  .section-title {
    font-size: 32rpx;
    font-weight: bold;
    margin: 30rpx 0 20rpx 0;
    color: #333;
  }
  
  .scan-section {
    background-color: white;
    border-radius: 15rpx;
    padding: 30rpx;
    margin-bottom: 20rpx;
    box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
    
    .scan-content {
      text-align: center;
      padding: 40rpx 0;
      
      .scan-icon {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        
        .scan-text {
          margin-top: 20rpx;
          font-size: 28rpx;
          color: #007AFF;
        }
      }
    }
  }
  
  .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;
      
      &:last-child {
        margin-bottom: 0;
      }
      
      .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;
        }
      }
    }
    
    .form-actions {
      display: flex;
      margin-top: 50rpx;
      
      .submit-btn, .cancel-btn {
        flex: 1;
        height: 80rpx;
        border-radius: 10rpx;
        font-size: 32rpx;
        margin: 0 10rpx;
      }
      
      .submit-btn {
        background-color: #007AFF;
        color: white;
        
        &[disabled] {
          background-color: #cccccc;
        }
      }
      
      .cancel-btn {
        background-color: #f0f0f0;
        color: #333;
      }
    }
  }
</style>