import React, { useState, useEffect } from "react"; import car from "./car.png"; import "./Car.scss"; import plus from "./plus.svg"; import Keyboard from "../Keyboard/Keyboard"; import platepng from "./plate.png"; import plateActive from "./plate_active.png"; import lot from "./lot.png"; import lotActive from "./lot_active.png"; import { post } from "../../js/helpers/data-helper"; const Car = ({ lots, onLot = () => {}, plate, hasReverse }) => { const [isPlate, setIsPlate] = useState(hasReverse); const [qArr, setQArr] = useState(hasReverse ? ["川", "A"] : []); const [opened, setOpened] = useState(false); const [showKeyBoard, setShowKeyBoard] = useState(true); const [focusedIndex, setFocusedIndex] = useState(hasReverse ? 2 : 0); const [isNum, setIsNum] = useState(true); const q = qArr.join(""); const setQ = (str) => setQArr(str.split("")); useEffect(() => { if (!opened) { setOpened(true); if (plate) { setQ(plate); setFocusedIndex(null); } setShowKeyBoard(!plate); } }, [opened]); const toPlate = () => { if (!hasReverse) return window.weui.toast("敬请期待", { className: "toast", }); setQ("川A"); setIsPlate(true); setFocusedIndex(2); setShowKeyBoard(true); }; const toLot = () => { setQ(""); setFocusedIndex(0); setIsPlate(false); setShowKeyBoard(true); }; const handleSubmit = async () => { if (isPlate) { if (!q) { return window.weui.toast("请输入正确车牌号", { className: "toast", }); } const { data: { data, code, msg }, } = await post("/api/ar/v1/applet/GetCarSpace", { CarNum: q }); if (code !== 200) return window.weui.toast(msg, { className: "toast", }); else { if (!lots.includes(data.spaceNum)) return window.weui.toast(`未找到车位号:${data.spaceNum}`, { className: "toast", }); return onLot(data.spaceNum); } } // if (!q || !lots.includes(floor + q.toUpperCase())) { if (!q || !lots.includes(q.toUpperCase())) { return window.weui.toast("请输入正确车位号", { className: "toast", }); } // onLot(floor + q.toUpperCase()); onLot(q.toUpperCase()); }; return (
车牌查找
车位查找
{isPlate ? (
{new Array(8).fill(0).map((_, i) => (
{ setIsNum(i !== 0); setFocusedIndex(i); setShowKeyBoard(true); }} > {i === 7 && !qArr[i] ? ( <> ) : ( qArr[i] )}
))}
) : (
{new Array(4).fill(0).map((_, i) => (
{ setIsNum(true); setFocusedIndex(i); setShowKeyBoard(true); }} > {qArr[i]}
))}
)} {!isPlate && ( <> {/*
setFloor(floor1)}> {floor1}
{floor}
setFloor(floor3)}> {floor3}
*/} )}
寻车
{showKeyBoard && ( { setFocusedIndex(null); setShowKeyBoard(false); }} onSetIsNum={(val) => setIsNum(val)} onInput={(val) => { qArr[focusedIndex] = val; setQArr([...qArr]); if (isPlate) { switch (focusedIndex) { case 6: case 7: setShowKeyBoard(false); break; default: setIsNum(true); setFocusedIndex(focusedIndex + 1); break; } } else { switch (focusedIndex) { case 3: setShowKeyBoard(false); break; default: setIsNum(true); setFocusedIndex(focusedIndex + 1); break; } } }} onBackspace={() => { qArr[focusedIndex] = ""; setQArr([...qArr]); setFocusedIndex(Math.max(0, focusedIndex - 1)); }} onFinish={handleSubmit} > )}
); }; export default Car;