|
|
@@ -1,31 +1,55 @@
|
|
|
-import { Text, View } from "@tarojs/components";
|
|
|
-import "./index.scss";
|
|
|
-import { Button, Form, Input } from "@nutui/nutui-react-taro";
|
|
|
-import { verifyPhone } from "../../utils";
|
|
|
-import { sendCodeApi, smsLoginApi } from "../../api/auth";
|
|
|
-import { setStorageSync, redirectTo } from "@tarojs/taro";
|
|
|
+import "./index1.scss";
|
|
|
import { useState } from "react";
|
|
|
-
|
|
|
+import {
|
|
|
+ showModal,
|
|
|
+ showToast,
|
|
|
+ navigateTo,
|
|
|
+ setStorageSync,
|
|
|
+ redirectTo,
|
|
|
+} from "@tarojs/taro";
|
|
|
+import { sendCodeApi, smsLoginApi } from "../../api/auth";
|
|
|
const LoginIndex = () => {
|
|
|
- const [form] = Form.useForm();
|
|
|
- const [showCode, setShowCode] = useState(false);
|
|
|
- const mobile = Form.useWatch("mobile", form);
|
|
|
- const code = Form.useWatch("code", form);
|
|
|
- /**
|
|
|
- * 发送验证码
|
|
|
- */
|
|
|
- const sendCode = async () => {
|
|
|
- console.log(mobile);
|
|
|
- setShowCode(true);
|
|
|
- await sendCodeApi({ mobile }).then((res) => {
|
|
|
- console.log(res);
|
|
|
- });
|
|
|
+ const [countdown, setCountdown] = useState(0);
|
|
|
+ const [formData, setFormData] = useState({
|
|
|
+ phone: "",
|
|
|
+ code: "",
|
|
|
+ });
|
|
|
+ const handleInputChange = (e: any) => {
|
|
|
+ setFormData({ ...formData, [e.target.name]: e.target.value });
|
|
|
};
|
|
|
- /**
|
|
|
- * 短信登录
|
|
|
- */
|
|
|
- const smsLogin = async () => {
|
|
|
- await smsLoginApi({ mobile, code }).then((res: any) => {
|
|
|
+ // 处理登录
|
|
|
+ const handleSubmit = async (e: React.FormEvent) => {
|
|
|
+ e.preventDefault();
|
|
|
+
|
|
|
+ // 表单验证
|
|
|
+ if (!formData.phone || !formData.code) {
|
|
|
+ showModal({
|
|
|
+ content: "请填写完整信息",
|
|
|
+ });
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ const phoneRegex = /^1[3-9]\d{9}$/;
|
|
|
+ if (!phoneRegex.test(formData.phone)) {
|
|
|
+ showModal({
|
|
|
+ content: "请输入正确的手机号",
|
|
|
+ });
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (formData.code.length !== 4) {
|
|
|
+ showModal({
|
|
|
+ content: "验证码长度应为4位",
|
|
|
+ });
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ const res: any = await smsLoginApi({
|
|
|
+ mobile: formData.phone,
|
|
|
+ code: formData.code,
|
|
|
+ });
|
|
|
+
|
|
|
const { data } = res;
|
|
|
const {
|
|
|
token,
|
|
|
@@ -41,64 +65,130 @@ const LoginIndex = () => {
|
|
|
setStorageSync("vipInfo", vip_info);
|
|
|
setStorageSync("circleInfo", circle_info);
|
|
|
redirectTo({ url: "/pages/index/index" });
|
|
|
- });
|
|
|
+
|
|
|
+ showToast({
|
|
|
+ title: "登录成功",
|
|
|
+ icon: "none",
|
|
|
+ duration: 2000,
|
|
|
+ });
|
|
|
+
|
|
|
+ navigateTo({ url: "/pages/index/index" });
|
|
|
+ } catch (error) {
|
|
|
+ showToast({
|
|
|
+ title: "登录失败,请重试",
|
|
|
+ icon: "none",
|
|
|
+ duration: 2000,
|
|
|
+ });
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ // 发送验证码
|
|
|
+ const handleSendCode = async () => {
|
|
|
+ try {
|
|
|
+ if (!formData.phone) {
|
|
|
+ showModal({
|
|
|
+ content: "请先输入手机号",
|
|
|
+ showCancel: false,
|
|
|
+ confirmColor: "#4d7ced",
|
|
|
+ });
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ const phoneRegex = /^1[3-9]\d{9}$/;
|
|
|
+ if (!phoneRegex.test(formData.phone)) {
|
|
|
+ showModal({
|
|
|
+ content: "请输入正确的手机号",
|
|
|
+ showCancel: false,
|
|
|
+ confirmColor: "#4d7ced",
|
|
|
+ });
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ await sendCodeApi({ mobile: formData.phone });
|
|
|
+ showToast({
|
|
|
+ title: "验证码已发送",
|
|
|
+ icon: "none",
|
|
|
+ duration: 2000,
|
|
|
+ });
|
|
|
+ setCountdown(60);
|
|
|
+ const timer = setInterval(() => {
|
|
|
+ setCountdown((prev) => {
|
|
|
+ if (prev <= 1) {
|
|
|
+ clearInterval(timer);
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ return prev - 1;
|
|
|
+ });
|
|
|
+ }, 1000);
|
|
|
+ } catch (error: any) {
|
|
|
+ showToast({
|
|
|
+ title: error.msg || "发送失败,请重试",
|
|
|
+ icon: "none",
|
|
|
+ duration: 2000,
|
|
|
+ });
|
|
|
+ }
|
|
|
};
|
|
|
|
|
|
return (
|
|
|
- <View className="login-page">
|
|
|
- <View className="login-contain">
|
|
|
- <View className="text-[16px] font-bold text-center">手机号登录</View>
|
|
|
- <Form form={form}>
|
|
|
- {!showCode && (
|
|
|
- <Form.Item name="mobile">
|
|
|
- <Input placeholder="手机号码" className="input-box" />
|
|
|
- </Form.Item>
|
|
|
- )}
|
|
|
+ <div className="login-page">
|
|
|
+ <div className="login-container">
|
|
|
+ <div className="login-header">
|
|
|
+ <h1>Hello!</h1>
|
|
|
+ <p>很高兴见到您,欢迎来到玩家中心</p>
|
|
|
+ </div>
|
|
|
+
|
|
|
+ <div className="login-card">
|
|
|
+ <h2>短信登录</h2>
|
|
|
+ <form onSubmit={handleSubmit} className="login-form">
|
|
|
+ <div className="form-item">
|
|
|
+ <div className="label">账号</div>
|
|
|
+ <div className="input-container">
|
|
|
+ <i className="phone-icon"></i>
|
|
|
+ <input
|
|
|
+ type="tel"
|
|
|
+ name="phone"
|
|
|
+ className="custom-input"
|
|
|
+ placeholder="请输入手机号"
|
|
|
+ value={formData.phone}
|
|
|
+ onChange={handleInputChange}
|
|
|
+ maxLength={11}
|
|
|
+ />
|
|
|
+ </div>
|
|
|
+ </div>
|
|
|
|
|
|
- {/* <View className="flex flex-wrap justify-between items-center bg-[#ffffff] relative w-full"> */}
|
|
|
- {showCode && (
|
|
|
- <Form.Item name="code">
|
|
|
- <Input placeholder="请输入短信验证码" className="input-box" />
|
|
|
- </Form.Item>
|
|
|
- )}
|
|
|
+ <div className="form-item">
|
|
|
+ <div className="label">验证码</div>
|
|
|
+ <div className="code-container">
|
|
|
+ <div className="input-container">
|
|
|
+ <i className="shield-icon"></i>
|
|
|
+ <input
|
|
|
+ type="text"
|
|
|
+ name="code"
|
|
|
+ className="custom-input"
|
|
|
+ placeholder="输入验证码"
|
|
|
+ value={formData.code}
|
|
|
+ onChange={handleInputChange}
|
|
|
+ maxLength={6}
|
|
|
+ />
|
|
|
+ </div>
|
|
|
+ <button
|
|
|
+ type="button"
|
|
|
+ className="code-btn"
|
|
|
+ disabled={countdown > 0}
|
|
|
+ onClick={handleSendCode}
|
|
|
+ >
|
|
|
+ {countdown > 0 ? `${countdown}s` : "获取验证码"}
|
|
|
+ </button>
|
|
|
+ </div>
|
|
|
+ </div>
|
|
|
|
|
|
- {/* <Button
|
|
|
- type="primary"
|
|
|
- disabled={!verifyPhone(mobile)}
|
|
|
- size="small"
|
|
|
- className="flex-shrink-1 absolute right-0"
|
|
|
- onClick={() => sendCode()}
|
|
|
- >
|
|
|
- <Text className="text-[12px] text-[#ffffff]">获取验证码</Text>
|
|
|
- </Button> */}
|
|
|
- {/* </View> */}
|
|
|
- </Form>
|
|
|
- <View className="mt-[22px]">
|
|
|
- {showCode ? (
|
|
|
- <Button
|
|
|
- size="large"
|
|
|
- block
|
|
|
- type="primary"
|
|
|
- disabled={!verifyPhone(mobile) || !code}
|
|
|
- onClick={() => smsLogin()}
|
|
|
- >
|
|
|
+ <button type="submit" className="submit-btn">
|
|
|
登录
|
|
|
- </Button>
|
|
|
- ) : (
|
|
|
- <Button
|
|
|
- size="large"
|
|
|
- block
|
|
|
- type="primary"
|
|
|
- disabled={!verifyPhone(mobile)}
|
|
|
- className="flex-shrink-1 absolute right-0"
|
|
|
- onClick={() => sendCode()}
|
|
|
- >
|
|
|
- 下一步
|
|
|
- </Button>
|
|
|
- )}
|
|
|
- </View>
|
|
|
- </View>
|
|
|
- </View>
|
|
|
+ </button>
|
|
|
+ </form>
|
|
|
+ </div>
|
|
|
+ </div>
|
|
|
+ </div>
|
|
|
);
|
|
|
};
|
|
|
|