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