import Taro, { Component } from '@tarojs/taro'
import { connect } from '@tarojs/redux'
import * as actions from '@actions/user'
import './index.scss'
import getUserPhone from '@utils/getUserPhone'
import { queryUserInfo, doUserSignin } from '@services/user'

@connect(state => state.user, { ...actions })
export default class Index extends Component {
  config = {
    navigationBarTitleText: '我的'
  }
  state = {
    user:{}
  }
  componentWillMount() {
    this.loadUserInfo()
  }

  getPhoneNumber(e) {
    getUserPhone(e, (phoneNumber) => {
      if (!phoneNumber) {
        Taro.showToast({
          title: '获取手机号失败',
          icon: 'none'
        })
        return
      }else{
        this.props.dispatchUpdateUserInfoNew(this.state.user.id)
        Taro.reLaunch({
            url: '/pages/person/index'
         })
      }
    })
  }

  loadUserInfo() {
    queryUserInfo().then(user => {
      this.setState({
        user
      })
    })
  }

  handleLogin() {
    Taro.navigateTo({
      url: '/pages/person/login/index'
    })
  }

  render() {
    const {user}=this.state
    return (
      <View className='auth-page'>
        <Image className="avatar_img" src={user.avatar}/>
        <View className="user_name">{user.nickname}</View>
        <Button  className='auth-btn' open-type="getPhoneNumber" onGetphonenumber={this.getPhoneNumber}>微信授权一键登录</Button>
        <View className='adver-btn' onClick={this.handleLogin}><Text>我是置业顾问,立即登录~</Text></View>
      </View>
    )
  }
}
