Vcode.tsx 4.57 KB
Newer Older
kuangshu committed
1 2
import { ComponentClass } from 'react';
import Taro, { Component } from '@tarojs/taro';
姜雷 committed
3 4 5
import { View, Input, Canvas } from '@tarojs/components';
import { getVcode, getImageVcode } from '../../api/wx';
import Modal from '../Modal/Modal';
kuangshu committed
6
import './Vcode.scss';
姜雷 committed
7
import { ResponseDataEntity } from 'src/api';
kuangshu committed
8 9

type PageOwnProps = {
姜雷 committed
10
  text?: string;
11
  cellphone: string;
kuangshu committed
12 13 14 15 16 17
};
type PageState = {
  defaultText: string;
  counting: boolean;
  count: number;
  timer: number | null;
姜雷 committed
18 19 20
  showImgBox: boolean;
  vcode: number | null;
  inputCode: string;
kuangshu committed
21 22 23 24 25 26 27 28 29 30 31 32
};

interface Vcode {
  props: PageOwnProps;
  state: PageState;
}

class Vcode extends Component {
  static externalClasses = ['vcode-classname'];

  constructor(props: PageOwnProps) {
    super(props);
33

kuangshu committed
34 35 36 37 38
    this.state = {
      defaultText: props.text ? props.text : '获取验证码',
      counting: false,
      count: 10,
      timer: null,
姜雷 committed
39 40 41
      showImgBox: false,
      vcode: null,
      inputCode: '',
kuangshu committed
42 43 44 45 46 47 48 49 50 51
    };
  }

  componentWillUnmount() {
    const { timer } = this.state;
    if (timer) {
      clearTimeout(timer);
    }
  }

姜雷 committed
52
  showImgCode() {
53 54
    const { cellphone } = this.props;
    if (cellphone && cellphone.length == 11) {
姜雷 committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
      getImageVcode({
        loginName: cellphone,
      })
        .then((res: ResponseDataEntity<number>) => {
          const { data } = res;
          this.setState({
            vcode: data,
          });
          this.renderImg(data);
        })
        .catch(console.error);
      this.setState({
        showImgBox: true,
      });
      this.renderImg(1234);
    } else {
      Taro.showToast({
        title: '输入正确的手机号',
        icon: 'none',
        mask: true,
      });
    }
  }
  closeImageBox() {
    const ctx = Taro.createCanvasContext('Vcode', this);
    ctx.setFillStyle('#6180f4');
    ctx.fillRect(0, 0, 73, 38);
    ctx.draw();
    this.setState({
      showImgBox: false,
    });
  }

  renderImg(vcode: number) {
    const ctx = Taro.createCanvasContext('Vcode', this);
    ctx.setFillStyle('#6180f4');
    ctx.fillRect(0, 0, 73, 38);
    ctx.font = '26px arial';
    // ctx.font('normal', '500', 26, 'arial');
    ctx.setFillStyle('#fff');
    ctx.fillText(vcode.toString(), 10, 30, 146); //画布上添加验证码
    ctx.draw();
  }

  clickHandle() {
    const { vcode, inputCode } = this.state;
    if (vcode && vcode.toString() == inputCode) {
      const { cellphone } = this.props;

104 105 106 107 108 109 110 111 112
      getVcode({
        tel: cellphone,
      })
        .then(res => {
          this.countStart();
          Taro.showToast({
            title: res.msg,
            mask: true,
          });
姜雷 committed
113 114 115 116 117
          this.setState({
            vcode: null,
            inputCode: '',
            showImgBox: false,
          });
118 119 120 121
        })
        .catch(console.error);
    } else {
      Taro.showToast({
姜雷 committed
122
        title: '请输入正确的验证码',
123 124 125 126 127
        icon: 'none',
      });
    }
  }

kuangshu committed
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
  countStart() {
    this.setState({
      counting: true,
    });
    this.countDown();
  }

  countDown() {
    const { count } = this.state;
    if (count == 1) {
      this.setState({
        counting: false,
      });
    } else {
      const timer = setTimeout(() => {
        this.countDown();
      }, 1000);
      this.setState({
        count: count - 1,
        timer: timer,
      });
    }
  }

  render() {
姜雷 committed
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
    const { defaultText, counting, count, showImgBox, inputCode } = this.state;
    return (
      <View>
        {counting ? (
          <View className='vcode-classname Vcode Counting'>{`${count}S后重发`}</View>
        ) : (
          <View className='vcode-classname Vcode' onClick={this.showImgCode}>
            {defaultText}
          </View>
        )}
        <Modal
          visiabled={showImgBox}
          title='请输入图形验证码'
          onCancel={this.closeImageBox}
          onSubmit={this.clickHandle}
          body-class='Vcode-Content'>
          <View className='Vcode-Content-box'>
            <Input
              className='Vcode-input'
              type='number'
              maxLength={4}
              value={inputCode}
              onInput={({ detail: { value } }) => {
                this.setState({
                  inputCode: value,
                });
                return value;
              }}
            />
            <Canvas className='Vcode-canvas' canvasId='Vcode' />
          </View>
        </Modal>
kuangshu committed
185 186 187 188 189 190
      </View>
    );
  }
}

export default Vcode as ComponentClass<PageOwnProps, PageState>;