index.tsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { Text, View } from "@tarojs/components";
  2. import "./index.scss";
  3. import { Button, Form, Input } from "@nutui/nutui-react-taro";
  4. import { verifyPhone } from "../../utils";
  5. import { sendCodeApi, smsLoginApi } from "../../api/auth";
  6. import { setStorageSync, redirectTo } from "@tarojs/taro";
  7. import { useState } from "react";
  8. const LoginIndex = () => {
  9. const [form] = Form.useForm();
  10. const [showCode, setShowCode] = useState(false);
  11. const mobile = Form.useWatch("mobile", form);
  12. const code = Form.useWatch("code", form);
  13. /**
  14. * 发送验证码
  15. */
  16. const sendCode = async () => {
  17. console.log(mobile);
  18. setShowCode(true);
  19. await sendCodeApi({ mobile }).then((res) => {
  20. console.log(res);
  21. });
  22. };
  23. /**
  24. * 短信登录
  25. */
  26. const smsLogin = async () => {
  27. await smsLoginApi({ mobile, code }).then((res: any) => {
  28. const { data } = res;
  29. const {
  30. token,
  31. username,
  32. mobile,
  33. is_vip,
  34. vip_code,
  35. vip_info,
  36. circle_info,
  37. } = data;
  38. setStorageSync("Authorization", token);
  39. setStorageSync("User", { username, mobile, is_vip, vip_code });
  40. setStorageSync("vipInfo", vip_info);
  41. setStorageSync("circleInfo", circle_info);
  42. redirectTo({ url: "/pages/index/index" });
  43. });
  44. };
  45. return (
  46. <View className="login-page">
  47. <View className="login-contain">
  48. <View className="text-[16px] font-bold text-center">手机号登录</View>
  49. <Form form={form}>
  50. {!showCode && (
  51. <Form.Item name="mobile">
  52. <Input placeholder="手机号码" className="input-box" />
  53. </Form.Item>
  54. )}
  55. {/* <View className="flex flex-wrap justify-between items-center bg-[#ffffff] relative w-full"> */}
  56. {showCode && (
  57. <Form.Item name="code">
  58. <Input placeholder="请输入短信验证码" className="input-box" />
  59. </Form.Item>
  60. )}
  61. {/* <Button
  62. type="primary"
  63. disabled={!verifyPhone(mobile)}
  64. size="small"
  65. className="flex-shrink-1 absolute right-0"
  66. onClick={() => sendCode()}
  67. >
  68. <Text className="text-[12px] text-[#ffffff]">获取验证码</Text>
  69. </Button> */}
  70. {/* </View> */}
  71. </Form>
  72. <View className="mt-[22px]">
  73. {showCode ? (
  74. <Button
  75. size="large"
  76. block
  77. type="primary"
  78. disabled={!verifyPhone(mobile) || !code}
  79. onClick={() => smsLogin()}
  80. >
  81. 登录
  82. </Button>
  83. ) : (
  84. <Button
  85. size="large"
  86. block
  87. type="primary"
  88. disabled={!verifyPhone(mobile)}
  89. className="flex-shrink-1 absolute right-0"
  90. onClick={() => sendCode()}
  91. >
  92. 下一步
  93. </Button>
  94. )}
  95. </View>
  96. </View>
  97. </View>
  98. );
  99. };
  100. export default LoginIndex;