| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- /**
- * Created by RaoMeng on 2018/11/6
- * Desc: 绑定菜单
- */
- import React, {Component} from 'react'
- import 'css/account-bind.css'
- import {Redirect} from 'react-router-dom'
- import {Avatar, Input, Icon, Button} from 'antd'
- import {Toast} from 'antd-mobile'
- import {fetchGet} from "../../utils/fetchRequest";
- import {API} from "../../configs/api.config";
- import {switchUser} from 'action/userInfo'
- import {getIntValue, getStrValue, isObjEmpty} from "../../utils/common";
- import {clearUserInfo} from "../../redux/actions/userInfo";
- import {connect} from "react-redux";
- class BindMenu extends Component {
- constructor() {
- super()
- this.state = {
- bindStatus: 0,
- errorMsg: ''
- }
- }
- componentWillMount() {
- }
- componentDidMount() {
- document.title = '账号绑定'
- this.paramType = this.props.match.params.type
- this.paramId = this.props.match.params.openid
- if (this.paramType === 'app') {
- if (isObjEmpty(this.paramId)) {
- this.setState({
- errorMsg: '公众号信息获取失败'
- })
- } else {
- switchUser({
- appId: this.paramId,
- })()
- this.setState({
- errorMsg: ''
- })
- window.location.href =
- 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' +
- this.paramId + '&redirect_uri=https%3a%2f%2fschool-api.ydyhz.com%2fapi%2fschool%2fwxSchool%2fuser%2fuserLogin&response_type=code&scope=snsapi_userinfo&state=' +
- this.paramId + '&connect_redirect=1#wechat_redirect'
- }
- } else if (this.paramType === 'open') {
- this.setState({
- errorMsg: '获取绑定信息中...'
- })
- this.openid = this.paramId
- this.wxAvatar = this.props.match.params.wxAvatar
- //清除用户信息
- clearUserInfo()()
- switchUser({
- userAvatar: decodeURIComponent(this.wxAvatar),
- })()
- this.obtainBindStatus()
- }
- }
- render() {
- const {bindStatus} = this.state
- if (bindStatus === 0) {
- return this.getLoadingLayout()
- } else if (bindStatus === 1) {
- return this.getMenuLayout()
- } else if (bindStatus === 2) {
- return this.getRedirectMain()
- }
- }
- obtainBindStatus = () => {
- fetchGet(API.USER_ISBINDING, {
- openid: this.openid,
- schoolId: 1,//学校id暂时写死,后续需要添加选择学校的步骤
- headimgurl: decodeURIComponent(this.wxAvatar),
- }).then(response => {
- if (response.data) {
- switchUser({
- userOpenid: this.openid,
- token: response.data.token ? response.data.token.token : ''
- })()
- if (!isObjEmpty(response.data.teacherDOS)) {
- const teacherDOS = response.data.teacherDOS[0]
- switchUser({
- userRole: 2,
- teacher: {
- teacherId: getIntValue(teacherDOS, 'teacherId'),
- userId: getIntValue(teacherDOS, 'userId'),
- openid: getStrValue(teacherDOS, 'openid'),
- userPhone: getStrValue(teacherDOS, 'userPhone'),
- schoolId: getStrValue(teacherDOS, 'schoolId'),
- schoolName: getStrValue(teacherDOS, 'schoolName'),
- teacherNumber: getStrValue(teacherDOS, 'teacherNumber'),
- teacherName: getStrValue(teacherDOS, 'teacherName'),
- teacherPhoto: getStrValue(teacherDOS, 'teacherPhoto'),
- teacherAddress: getStrValue(teacherDOS, 'teacherAddress'),
- teacherEntry: getStrValue(teacherDOS, 'teacherEntry'),
- teacherBirthday: getStrValue(teacherDOS, 'teacherBirthday'),
- teacherSex: getStrValue(teacherDOS, 'teacherSex'),//1:男,2:女
- }
- })()
- } else {
- switchUser({
- teacher: ''
- })()
- }
- if (!isObjEmpty(response.data.parentsDOS)) {
- const parentsDOS = response.data.parentsDOS[0]
- switchUser({
- userRole: 1,
- parent: {
- parentId: getIntValue(parentsDOS, 'parentId'),
- userId: getIntValue(parentsDOS, 'userId'),
- userPhone: getStrValue(parentsDOS, 'userPhone'),
- openid: getStrValue(parentsDOS, 'openid'),
- schoolId: getIntValue(parentsDOS, 'schoolId'),
- schoolName: getStrValue(parentsDOS, 'schoolName'),
- parentsName: getStrValue(parentsDOS, 'parentsName'),
- parentsBirthday: getStrValue(parentsDOS, 'parentsBirthday'),
- parentsSex: getIntValue(parentsDOS, 'parentsSex'),
- parentsPhoto: getStrValue(parentsDOS, 'parentsPhoto'),
- parentsAddress: getStrValue(parentsDOS, 'parentsAddress'),
- }
- })()
- } else {
- switchUser({
- parent: ''
- })()
- }
- this.setState({
- bindStatus: 2
- })
- this.props.history.push('/homePage')
- } else {
- this.setState({
- bindStatus: 1
- })
- }
- }).catch(error => {
- if (typeof error === 'string') {
- this.setState({
- errorMsg: error
- })
- } else {
- this.setState({
- errorMsg: '数据请求异常'
- })
- }
- })
- }
- getRedirectMain = () => {
- return (
- <div>
- {/*<Redirect to='/homePage'></Redirect>*/}
- </div>
- )
- }
- getLoadingLayout = () => {
- return (
- <div className='bindParent' style={{justifyContent: 'center', backgroundImage: 'none'}}>
- <Icon type="loading" spin style={{fontSize: '50px', color: '#3db1af'}}/>
- <span style={{marginTop: '14px', color: '#666'}}>{this.state.errorMsg}</span>
- </div>
- )
- }
- getMenuLayout = () => {
- return (
- <div className='bindParent'>
- <div className='bindTitleLayout'>
- <Avatar size={50} src={require('imgs/ic_bind_menu_head.png')}/>
- <span className='bindTitleText'>您好<br/>请选择您的身份进行绑定</span>
- </div>
- <Button type="primary" block
- style={{
- marginTop: '20px', letterSpacing: '10px',
- borderRadius: '9px', fontSize: '14px', borderRadius: '30px',
- }}
- onClick={this.parentBind}>我是家长</Button>
- <Button type="primary" block
- style={{
- marginTop: '20px', letterSpacing: '10px',
- background: '#05DC40', borderRadius: '30px',
- fontSize: '14px', border: 'none'
- }}
- onClick={this.teacherBind}>我是老师</Button>
- </div>
- )
- }
- parentBind = () => {
- this.props.history.push('/accountBind/parents')
- }
- teacherBind = () => {
- this.props.history.push('/accountBind/teacher')
- }
- }
- let mapStateToProps = (state) => ({
- userInfo: {...state.redUserInfo}
- })
- let mapDispatchToProps = (dispatch) => ({})
- export default connect(mapStateToProps, mapDispatchToProps)(BindMenu)
|