BarCode.tsx 2.23 KB
Newer Older
姜雷 committed
1 2 3 4 5 6 7 8 9
import Taro, { Component } from '@tarojs/taro';
import { ComponentClass } from 'react';
import { View, Canvas, Image } from '@tarojs/components';
import BarCodeBoxBg from '../../images/barcode/tiaoxingma-1.png';
import BarCodeBoxBigBg from '../../images/barcode/tiaoxingma-2.png';
import RefreshIcon from '../../images/barcode/icon_shuaxin@2x.png';
import wxbarcode from 'wxbarcode';

import './BarCode.scss';
姜雷 committed
10 11
import { UserState } from '../../store/rootReducers/userinfo';
import { connect } from '@tarojs/redux';
姜雷 committed
12

姜雷 committed
13 14 15
type PageStateProps = {
  userinfo: UserState;
};
姜雷 committed
16 17 18 19 20
type PageOwnProps = {};
type PageState = {
  showBig: boolean;
};

姜雷 committed
21
type IProps = PageStateProps & PageOwnProps;
姜雷 committed
22
interface BarCode {
姜雷 committed
23
  props: IProps;
姜雷 committed
24 25
  state: PageState;
}
姜雷 committed
26 27 28 29

@connect(({ userinfo }) => ({
  userinfo,
}))
姜雷 committed
30 31 32 33 34 35 36 37 38
class BarCode extends Component {
  constructor(props: PageOwnProps) {
    super(props);
    this.state = {
      showBig: false,
    };
  }

  componentWillMount() {
姜雷 committed
39 40
    const { userinfo } = this.props;
    wxbarcode.barcode('BarCode', userinfo.idBar, 646, 188);
姜雷 committed
41 42 43
  }

  drawBarCode(showBig: boolean) {
姜雷 committed
44
    const { userinfo } = this.props;
姜雷 committed
45
    if (showBig) {
姜雷 committed
46
      wxbarcode.barcode('BarCode', userinfo.idBar, 646, 188);
姜雷 committed
47
    } else {
姜雷 committed
48
      wxbarcode.barcode('BarCode', userinfo.idBar, 700, 364);
姜雷 committed
49 50 51 52 53 54 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
    }
  }

  toggleBigBarCode() {
    const { showBig } = this.state;
    this.drawBarCode(showBig);
    this.setState({
      showBig: !showBig,
    });
  }

  render() {
    const { showBig } = this.state;
    return (
      <View className='BarCode'>
        <View className={`BarCodeBox ${showBig ? 'BarCodeBoxBig' : ''}`}>
          {showBig ? (
            <Image className='bg' src={BarCodeBoxBigBg} />
          ) : (
            <Image className='bg' src={BarCodeBoxBg} />
          )}
          {!showBig && (
            <Image className='BarCodeBox-refresh' src={RefreshIcon} />
          )}
          <View className={`BarCodeImg ${showBig ? 'BarCodeImgBig' : ''}`}>
            <Canvas className='BarCodeCav' canvasId='BarCode' />
          </View>
          <View className='toggleBtn' onClick={this.toggleBigBarCode}>
            {showBig ? '缩小' : '放大'}
          </View>
        </View>
      </View>
    );
  }
}

export default BarCode as ComponentClass<PageOwnProps, PageState>;