import React, { Component, Fragment } from 'react';
import { connect } from 'dva';
import router from 'umi/router';
import { failTip, saveSettleData, setLocalStorageTime } from '@/utils/utils';

@connect(({ login, common }) => ({
  login, common,
}))

export default class SellerLink extends Component {
  constructor(props) {
    super(props);
    this.state = {
      query: props.location.query,
    };
  }

  componentDidMount() {
    this.sendOauthRequest();
  }

  sendOauthRequest = () => {
    const { dispatch } = this.props;
    let oauthKey = this.state.query.oauthKey;
    if (oauthKey != '' && oauthKey != null && oauthKey != undefined) {
      dispatch({
        type: 'common/sso_seller',
        payload: { oauthKey: oauthKey },
        callback: (res) => {
          if (res.state == 200 || res.state == 267) {
            localStorage.setItem('sld_token', res.data.access_token);
            localStorage.setItem('sld_refresh_token', res.data.refresh_token);
            {
              setLocalStorageTime();
            }
            localStorage.setItem('user_info', JSON.stringify({ user_name: res.data.vendorName }));
            if (res.state == 267) {
              //登录的时候初始化入驻状态
              if (res.data.applyState == 0) {
                //未入驻
                saveSettleData('state', '');
                saveSettleData('cur_step', 0);
                router.push('/apply/settled_protocol');
              } else {
                saveSettleData('cur_step', 3);
                router.push('/apply/open_up');
              }
            }
          } else {
            failTip(res.msg);
            this.getCaptcha('login_captcha');
          }
        },
      });
    }
  };

  render() {
    return null;
  }
}

