import { ref, computed } from 'vue' import { isZhWord, isUppercaseWord } from '@/utils/utils' export const useParkingKeyboard = () => { const NOT_ENERGY_PLATE = 7 //非能源车牌长度 const ENERGY_PLATE = 8 //能源车牌长度 const SPACE_MAX_LENGTH = 3 //车位最大允许长度 const LICENSE = ['川', 'A'] //默认车牌前缀 const plate = ref(LICENSE.slice()) const plateToString = computed(() => plate.value.join('')) const searchMethod = ref<'车牌' | '车位'>('车牌') const inputLength = computed(() => (searchMethod.value === '车牌' ? ENERGY_PLATE : SPACE_MAX_LENGTH)) const isEnergy = ref(false) function handleEnergy(energy: boolean) { isEnergy.value = energy if (!energy && plate.value.length === ENERGY_PLATE) { plate.value.pop() } } function handleKeyboard(value: string) { if (searchMethod.value === '车牌') { if (plate.value.length === 1 && isZhWord(value)) { plate.value[0] = value return } if ( (!isEnergy.value && plate.value.length >= NOT_ENERGY_PLATE) || (isEnergy.value && plate.value.length >= ENERGY_PLATE) || (plate.value.length === 0 && !isZhWord(value)) || (plate.value.length === 1 && !isUppercaseWord(value)) || (plate.value.length >= 2 && plate.value.length < 6 && isZhWord(value)) ) { return } } else if (plate.value.length >= SPACE_MAX_LENGTH) { return } plate.value.push(value) } function del() { plate.value.pop() } function changeSearchMethod(method: '车牌' | '车位') { searchMethod.value = method } return { del, handleKeyboard, handleEnergy, inputLength, plate, plateToString, LICENSE, searchMethod, changeSearchMethod } }