index.tsx 2.39 KB
Newer Older
姜雷 committed
1 2
import { ComponentClass } from 'react';
import Taro, { Component, Config } from '@tarojs/taro';
3
import { View } from '@tarojs/components';
姜雷 committed
4 5

import './index.scss';
姜雷 committed
6
import { LoginReponse } from '../../api/wx';
7
import { connect } from '@tarojs/redux';
姜雷 committed
8

9
import { updateUserInfo, UserState } from '../../store/rootReducers/userinfo';
姜雷 committed
10
import { appLogin } from '../../api/customer';
姜雷 committed
11

12
type PageDispatchProps = {
姜雷 committed
13
  updateUserInfo: (e: UserState | { token: string }) => void;
14
};
姜雷 committed
15 16 17

type PageOwnProps = {};

姜雷 committed
18 19 20
type PageState = {
  errorText: string;
};
姜雷 committed
21

22
type IProps = PageDispatchProps & PageOwnProps;
姜雷 committed
23 24 25

interface Index {
  props: IProps;
姜雷 committed
26
  state: PageState;
姜雷 committed
27 28
}

29 30 31 32 33 34 35 36
@connect(
  () => ({}),
  dispatch => ({
    updateUserInfo(entity: UserState) {
      dispatch(updateUserInfo(entity));
    },
  }),
)
姜雷 committed
37
class Index extends Component {
姜雷 committed
38 39
  config: Config = {};

40
  constructor(props: IProps) {
姜雷 committed
41
    super(props);
姜雷 committed
42 43 44
    this.state = {
      errorText: '',
    };
姜雷 committed
45
  }
姜雷 committed
46

姜雷 committed
47
  componentWillMount() {
48
    Taro.showLoading();
姜雷 committed
49
    this.loginHandle();
50 51 52
  }

  checkUser() {
姜雷 committed
53 54 55 56
    Taro.getStorage({
      key: 'token',
    })
      .then(res => {
57 58
        const data = res.data;
        console.log('token ' + data);
姜雷 committed
59 60 61 62 63 64 65
      })
      .catch(() => {
        this.loginHandle();
      });
  }

  loginHandle() {
66 67 68 69
    const { updateUserInfo } = this.props;
    Taro.login()
      .then(res => {
        console.log(res);
姜雷 committed
70
        appLogin({
71
          code: res.code,
姜雷 committed
72
        })
73
          .then((res: LoginReponse) => {
姜雷 committed
74 75 76 77 78 79 80 81
            const { register, wxToken, ...userInfo } = res.data;
            console.log(wxToken);

            if (register) {
              updateUserInfo({
                ...userInfo,
                token: wxToken,
              });
82 83 84 85 86
              Taro.redirectTo({
                url: '/pages/Home/Home',
              });
            } else {
              updateUserInfo({
姜雷 committed
87
                token: wxToken,
88 89 90 91 92 93 94 95 96
              });
              Taro.hideLoading();
              Taro.redirectTo({
                url: '/pages/Login/Login',
              });
            }
          })
          .catch(err => {
            console.log(err);
姜雷 committed
97 98 99
            this.setState({
              errorText: err.msg,
            });
姜雷 committed
100
          });
101 102 103 104
      })
      .catch(err => {
        console.log(err);
      });
姜雷 committed
105 106 107
  }

  render() {
姜雷 committed
108 109
    const { errorText } = this.state;
    return <View className='Index'>{errorText}</View>;
姜雷 committed
110 111 112
  }
}

113
export default Index as ComponentClass<PageOwnProps, PageState>;