index.js 4.8 KB
Newer Older
zhanghaozhe committed
1 2
import React, { PureComponent } from 'react'
import { Flex, WhiteSpace, List } from 'antd-mobile'
xuzhenghua committed
3 4 5
import './index.scss'
import Avatar from './image/avatar.png'
import Vip from './image/vip.png'
zhanghaozhe committed
6 7 8 9
import { WithTab } from '@/HOCs'
import { Link } from "react-router-dom"
import { connect } from "react-redux"
import { HeaderBar } from "@/common"
xuzhenghua committed
10
import { http } from "@/utils"
zhanghaozhe committed
11 12 13
import { getCourses } from './../detail/actions';
import { setCurrentUser, startFetchUser } from "@/store/userAction"

.  
baiguangyao committed
14 15 16

const Item = List.Item;
const Brief = Item.Brief;
baiguangyao committed
17

18
@connect(state => ({
zhanghaozhe committed
19 20
  user: state.user,
}), {startFetchUser, setCurrentUser})
21
class My extends PureComponent {
zhanghaozhe committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
  constructor(props) {
    super(props)
    this.state = {
      data: [],
    }
  }


  componentDidMount() {
    this.getUser()
  }

  getUser = () => {
    this.props.startFetchUser()
    http.get(`${API.home}/m/user_info_sample/1`).then(res => {
      this.props.setCurrentUser(this.transformUser(res))
    })
  }

  transformUser = res => {
    let payload
    if (res.data.code === 200) {
      const {
        msg, data: {
          avatar_file: avatar,
          user_name: username,
          is_vip: isVIP,
          uid,
          code,
        },
      } = res.data

      payload = {
        hasError: false,
        msg,
        data: {
          username,
          isVIP,
          avatar,
          uid,
          code,
        },
      }
    } else {
      payload = {
        hasError: true,
        msg: res.data.msg,
        code: res.data.code,
        data: {},
      }
    }
    return payload
  }

  // 去登录
  toLogin = () => {
    this.props.history.push('/passport')
  }

  toCourseDetail = (id) => {
    const {dispatch, history} = this.props;
    // dispatch(getCourses(id, () => {
    history.push(`/detail?id=${id}`);
    return false;
    // }));
  }

  render() {
    const {user} = this.props
    const uid = user && user.data && user.data.uid
    const username = user && user.data && user.data.username
    const avatar = user && user.data && user.data.avatar
    const isVIP = user && user.data && user.data.isVIP

    let list
    if (!uid) {
      list = <List className="my-list">
        <Item
          className="avatar-wrap"
          arrow="horizontal"
          multipleLine
          thumb={Avatar}
          onClick={this.toLogin}>
          <Brief>点击登录</Brief>
        </Item>
      </List>
    } else {
      list = <List className="my-list">
        <Link to='/myedit'>
          <Item
            className="avatar-wrap"
            arrow="horizontal"
            multipleLine
            thumb={avatar}
          >
            <Brief>
              {username}
              {
                isVIP === 1 &&
                <img src={Vip} className='vip' alt=""/>
              }

            </Brief>
            <Brief style={{fontSize: "12px"}}>学号: {uid}</Brief>
          </Item>
        </Link>
        {
          !isVIP &&
          <a href="javascript:;" className="my-isvip" onClick={() => this.toCourseDetail(139)}></a>
xuzhenghua committed
131
        }
zhanghaozhe committed
132
      </List>
xuzhenghua committed
133
    }
zhanghaozhe committed
134 135 136 137 138
    return (
      <div className="flex-container">
        <Flex>
          <Flex.Item>
            <HeaderBar title='我的' arrow={false} cart={false}></HeaderBar>
xuzhenghua committed
139

zhanghaozhe committed
140
            {list}
xuzhenghua committed
141

zhanghaozhe committed
142 143 144
            {
              !uid &&
              <div className="am-list-header"></div>
xuzhenghua committed
145

zhanghaozhe committed
146
            }
xuzhenghua committed
147

zhanghaozhe committed
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
            <List className="my-list-content">
              <Link to='/purchased'>
                <Item arrow="horizontal">
                  <i className="iconfont iconiconfront-27"></i>
                  已购课程
                </Item>
              </Link>
              <Link to='/shopcart'>
                <Item arrow="horizontal">
                  <i className="iconfont icongouwuche-xianxing"></i>
                  购物车
                </Item>
              </Link>
              <Link to='/myorders'>
                <Item arrow="horizontal">
                  <i className="iconfont iconiconfront-24"></i>
                  课程订单
                </Item>
              </Link>
              <Link to={{
                pathname: '/coupons',
                state: {
                  from: this.props.location.pathname,
                },
              }}>
                <Item arrow="horizontal" className="no-border">
                  <i className="iconfont iconiconfront-52"></i>
                  优惠券
                </Item>
              </Link>
              <div className="am-list-header"></div>
179

zhanghaozhe committed
180 181 182 183
              <Link to='/scholarship'>
                <Item arrow="horizontal" className='btm-scholarship'>
                  <i className="iconfont iconiconfront-51"></i>
                  赚奖学金
xuzhenghua committed
184
                </Item>
zhanghaozhe committed
185
              </Link>
xuzhenghua committed
186
            </List>
zhanghaozhe committed
187 188 189 190 191 192 193

          </Flex.Item>
        </Flex>
        <WhiteSpace size="lg"/>
      </div>
    )
  }
baiguangyao committed
194 195 196

}

197
export default WithTab(My);