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
| <script type="module"> import { createPinia, defineStore, storeToRefs } from "pinia"; const { createApp, reactive, toRefs, ref, computed, onMounted } = Vue; const { ElNotification } = ElementPlus;
const newStore = defineStore("store", () => { const showName = ref("phone"); const phoneNumber = ref(18839764702); const checkNumber = ref(0); const generateCheck = () => { checkNumber.value = Math.floor(Math.random() * 1e6); }; const changeShow = (target) => { showName.value = target; }; return {showName,changeShow,phoneNumber,generateCheck,checkNumber, }; });
const app = createApp({ setup() { let { showName } = storeToRefs(newStore());
return { showName, }; }, }); app.use(ElementPlus); app.use(createPinia());
app.component("phone", { template: "#phone", setup() { const store = newStore(); const { phoneNumber } = storeToRefs(store); const checkValue = ref(true); const check = () => { let phoneReg = /^18[0-9]{9}$/; let phonePass = phoneReg.test(phoneNumber.value);
if (phonePass && checkValue.value) { store.generateCheck(); let { checkNumber } = storeToRefs(store); console.log(checkNumber.value); ElNotification({title: "发送成功",message: `您的验证码为${checkNumber.value}`,type: "success", }); store.changeShow("check"); } else if (checkValue.value) { ElNotification({title: "发送失败",message: `无效的手机号码`,type: "error", }); } else { ElNotification({title: "发送失败",message: `请先阅读并同意下方协议`,type: "error", }); } };
return { check, phoneNumber, checkValue }; }, }); app.component("check", { template: "#check", setup() { const store = newStore(); let { phoneNumber, checkNumber } = storeToRefs(store); let hidedNumber = computed(() => { return phoneNumber.value .toString() .replace(/^(\d{3})\d{6}(\d{2})$/, "$1******$2"); }); onMounted(() => { let inputs = document.querySelectorAll(".code"); inputs[0].focus(); inputs.forEach((item) => { item.addEventListener("input", (e) => { if (item.value.length && item.nextElementSibling) { item.nextElementSibling.focus(); } else if ( item.value.length === 0 && item.previousElementSibling ) { item.previousElementSibling.focus(); } let inputsArray = Array.from(inputs); if (inputsArray.every((elem) => elem.value.length === 1)) { let ans = inputsArray.map((elem) => elem.value).join(""); ans = parseInt(ans); if (ans === checkNumber.value) { ElNotification({ title: "验证成功", message: "欢迎回来", type: "success", }); store.changeShow("success"); } else { ElNotification({ title: "验证失败", message: "您输入的验证码有误", type: "error", }); inputs.forEach((elem) => (elem.value = "")); inputs[0].focus(); } console.log(ans); } }); }); }); const reSend = () => { store.generateCheck(); ElNotification({ title: "发送成功", message: `您的验证码为${checkNumber.value}`, type: "success", }); }; return { phoneNumber, hidedNumber, reSend }; }, }); app.component("success", { template: "#success", }); app.mount("#app"); </script>
|