/*
 * @Description: 这是输入mobile和code页面，可用于登录、绑定
 * @Author: zhoupeng
 * @Date: 2020-04-22 15:29:53
 * @LastEditTime: 2020-07-02 16:38:36
 */

import React, { Component } from 'react';
import { connect } from 'react-redux';

import router from 'umi/router';
import styles from './index.less';

import { makeScanning, wechatAuthorization } from '../common/common';

//APPid
const AppId = process.env.AppId;

@connect(() => ({}))
class Index extends Component {
  constructor(props) {
    super(props);
    this.state = {
      canTouch: true,
      canSubmit: false,

      loading: false,
    };
  }

  componentWillMount = () => {
    this.initUrl();

    //如果是扫描进入，设置有重定向的页面 （与goIndex()配合使用）
    window.sessionStorage.setItem('fromPage', 'scanning');
  };
  componentDidMount = () => {
    this.Authorization();
  };

  //重定向微信，改变url
  initUrl = () => {
    const localHref = window.location.href;
    const newHref = `${window.location.href}&redirec=have`;
    // const newHref = `http://marketing.test.chongaibao.com/#/./ckuh5/scanning?page=transfer&birthCerRegCode=200422226804&identificationFlag=1231&redirec=have`;
    if (localHref && localHref.indexOf('redirec=have') === -1) {
      const url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${AppId}&redirect_uri=${encodeURIComponent(
        newHref,
      )}&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirec`;
      window.location.href = url;
    }
  };

  //重定向后，进行授权
  Authorization = () => {
    const localHref = window.location.href;
    if (localHref && localHref.indexOf('redirec=have') > -1) {
      console.log('开始请求=====================');

      //进行了一次授权处理，不进行第二次
      window.sessionStorage.setItem('haveAuthorization', '1');

      wechatAuthorization();
      makeScanning();
    }
  };

  click = () => {};

  render() {
    console.log(this.props);
    return (
      <div className={styles.scanning}>
        <div>加载中···</div>
      </div>
    );
  }
}

export default Index;
