SmsInput.tsx 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 { TextInputMask } from 'react-native-masked-text';
  5. import { useTranslation } from 'react-i18next';
  6. import { useInterval } from 'ahooks';
  7. import { sendSms } from '../utils/SmsUtil';
  8. import { toastInfo } from '../utils/SystemUtils';
  9. export default function SmsInput({ phone, onCodeChange, type }) {
  10. const [code, setCode] = React.useState();
  11. const [interval, setInterval] = React.useState(null);
  12. const [count, setCount] = React.useState(60);
  13. const { t } = useTranslation();
  14. useInterval(
  15. () => {
  16. setCount(count - 1);
  17. if (count === 0) {
  18. setInterval(null);
  19. }
  20. },
  21. interval,
  22. { immediate: true }
  23. );
  24. return (
  25. <Div row alignItems="center" py={10}>
  26. <Text textAlign="right" w={90}>
  27. 验证码
  28. </Text>
  29. <Div
  30. flex={1}
  31. bg="gray100"
  32. rounded="sm"
  33. px={15}
  34. ml={5}
  35. h={30}
  36. alignItems="stretch"
  37. >
  38. <TextInputMask
  39. type={'custom'}
  40. value={code}
  41. keyboardType="numeric"
  42. options={{
  43. mask: '9999',
  44. }}
  45. onChangeText={(text) => {
  46. setCode(text);
  47. onCodeChange(text);
  48. }}
  49. placeholderTextColor="#a6a6a6"
  50. style={{ flex: 1, fontSize: 14 }}
  51. placeholder={t('shu-ru-yan-zheng-ma')}
  52. />
  53. </Div>
  54. <Button
  55. onPress={() => {
  56. sendSms(phone, type || 'register')
  57. .then(() => {
  58. setCount(60);
  59. setInterval(1000);
  60. })
  61. .catch((e) => {
  62. console.log(e);
  63. toastInfo(e.error);
  64. });
  65. }}
  66. bg="transparent"
  67. color={interval ? 'gray500' : 'yellow500'}
  68. disabled={interval || !phone}
  69. >
  70. {interval ? `已发送(${count})S` : '发送验证码'}
  71. </Button>
  72. </Div>
  73. );
  74. }