1
heshaofeng
2026-01-07 51197dd4ca2a95dc86f11261d255b6cba8b21263
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
<template>
  <vol-box v-model="show" title="撤销组盘" :width="500" :height="300">
    <template #content>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="托盘或条码:" prop="code">
          <el-input
            v-model="form.code"
            placeholder="请扫描/输入托盘或条码"
            @keydown.enter.prevent="submit"
            clearable
            @paste="handlePaste"
            @input="handleInput"
            ref="boxCodeInput"
            autocomplete="off"
            maxlength="50"
          />
        </el-form-item>
      </el-form>
    </template>
 
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="onCancel">取消</el-button>
        <el-button type="primary" @click="submit">确认撤销</el-button>
      </div>
    </template>
  </vol-box>
</template>
 
<script>
import VolBox from '@/components/basic/VolBox.vue'
 
export default {
  components: { VolBox },
  props: {
    value: { type: Boolean, default: false }
  },
  data() {
    return {
      show: false,
      form: {
        code: ''
      },
      rules: {
        code: [
          { required: true, message: '请输入托盘号', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '托盘号长度不能超过50个字符', trigger: ['blur', 'input'] }
        ]
      }
    }
  },
  methods: {
    open() {
      this.show = true
    },
 
    async submit() {
      try {
        await this.$refs.form.validate()
      } catch (error) {
        this.$message.warning('请输入有效的托盘号')
        this.focusAndSelectInput()
        return
      }
 
      try {
        const response = await this.http.post(
          `/api/InboundOrder/UndoPalletGroup?code=${encodeURIComponent(this.form.code.trim())}`
        )
        const { status, message, data } = response
 
        if (status) {
          this.$message.success(message || '撤销组盘成功')
          this.refresh()
 
          if (message && message.includes('托盘仍有剩余明细')) {
            this.form.code = ''
            this.$refs.form.clearValidate('code')
            this.focusAndSelectInput()
          } else {
            this.show = false
          }
        } else {
          this.$message.error(message || data?.message || '撤销组盘失败')
          this.focusAndSelectInput()
        }
      } catch (error) {
        console.error('撤销组盘请求异常:', error)
        this.$message.error('网络异常或接口错误,请稍后重试')
        this.focusAndSelectInput()
      }
    },
 
    handleInput(value) {
      this.form.code = value.replace(/[^a-zA-Z0-9]/g, '').toUpperCase()
    },
 
    handlePaste(e) {
      const clipboardData = e.clipboardData || window.clipboardData
      const pastedText = clipboardData.getData('text')
      const cleanedText = pastedText.replace(/[^a-zA-Z0-9]/g, '').toUpperCase()
      if (cleanedText) {
        this.form.code = cleanedText
        setTimeout(() => {
          this.submit()
        }, 50)
      }
      e.preventDefault()
    },
 
    focusAndSelectInput() {
      this.$nextTick(() => {
        setTimeout(() => {
          const inputRef = this.$refs.boxCodeInput
          if (inputRef) {
            const inputEl = inputRef.$el ? inputRef.$el.querySelector('input') : inputRef
            if (inputEl) {
              inputEl.focus()
              inputEl.select()
            }
          }
        }, 100)
      })
    },
 
    onCancel() {
      this.$message.info('取消撤销组盘')
      this.show = false
    },
 
    refresh() {
      this.$emit('refresh')
    }
  },
  watch: {
    value(val) {
      this.show = val
    },
    show(val) {
      this.$emit('input', val)
      if (val) {
        // 弹窗打开时延迟聚焦,确保DOM已渲染
        this.$nextTick(() => {
          setTimeout(() => {
            const inputRef = this.$refs.boxCodeInput
            if (inputRef) {
              const inputEl = inputRef.$el ? inputRef.$el.querySelector('input') : inputRef
              if (inputEl) {
                inputEl.focus()
                inputEl.select() // 选中内容,方便直接扫码覆盖
              }
            }
          }, 100)
        })
      } else {
        this.form.code = ''
        if (this.$refs.form) {
          this.$refs.form.clearValidate()
        }
      }
    }
  }
}
</script>
 
<style scoped>
.dialog-footer {
  text-align: right;
}
</style>