SmsInput.jsx 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import * as WebBrowser from "expo-web-browser";
  2. import * as React from "react";
  3. import { Div, Image, Text, Avatar, Input } from "react-native-magnus";
  4. import { Toast } from "@ant-design/react-native";
  5. import { Button } from "@ui-kitten/components";
  6. import { useInterval } from "ahooks";
  7. import { sendSms } from "../Utils/SmsUtil";
  8. export default function SmsInput({
  9. label,
  10. placeholder,
  11. labelWidth,
  12. phone,
  13. onCodeChange,
  14. type,
  15. }) {
  16. const [code, setCode] = React.useState();
  17. const [interval, setInterval] = React.useState(null);
  18. const [count, setCount] = React.useState(60);
  19. useInterval(
  20. () => {
  21. setCount(count - 1);
  22. if (count === 0) {
  23. setInterval(null);
  24. }
  25. },
  26. interval,
  27. { immediate: true }
  28. );
  29. return (
  30. <Div row alignItems="center" px={4} py={10}>
  31. <Text mr={19} textAlign="right" fontSize="sm" w={labelWidth || 80}>
  32. {label}
  33. </Text>
  34. <Input
  35. placeholder={placeholder}
  36. fontSize="sm"
  37. bg="gray100"
  38. flex={1}
  39. h={32}
  40. mr={10}
  41. value={code}
  42. onChangeText={value => {
  43. setCode(value);
  44. onCodeChange(value);
  45. }}
  46. borderColor="gray200"
  47. />
  48. <Button
  49. onPress={() => {
  50. sendSms(phone, type || "register")
  51. .then(() => {
  52. setCount(60);
  53. setInterval(1000);
  54. })
  55. .catch(e => {
  56. console.log(e);
  57. Toast.info(e.response.data.error);
  58. });
  59. }}
  60. appearance="ghost"
  61. size="tiny"
  62. disabled={!phone || interval}
  63. >
  64. {interval ? `已发送(${count})S` : "发送验证码"}
  65. </Button>
  66. </Div>
  67. );
  68. }