index.js 1.97 KB
Newer Older
zhanghaozhe committed
1 2 3 4
import React, { Component } from "react"
import "./index.scss"
import { withRouter } from "react-router-dom"
import { browser, getParam } from "src/utils"
xuzhenghua committed
5

wangshuo committed
6
class HeaderBar extends Component {
zhanghaozhe committed
7 8 9 10
  goBack = () => {
    const { state, hash } = this.props.location
    if (hash.includes("goback")) {
      return window.history.go(-1)
wangshuo committed
11
    }
zhanghaozhe committed
12 13
    if (browser.isWeixin && getParam("code") && getParam("state")) {
      window.history.go(-2)
FE committed
14
    }
zhanghaozhe committed
15 16 17
    if (state.records && state.records.length > 1) {
      window.history.go(-1)
    } else if (state.from && state.from.pathname) {
zhanghaozhe committed
18
      window.location.replace(`${state.from.pathname}${state.from.search}`)
zhanghaozhe committed
19 20
    } else {
      window.location.href = window.location.origin
wangshuo committed
21
    }
zhanghaozhe committed
22
  }
xuzhenghua committed
23

zhanghaozhe committed
24 25 26
  toLink = () => {
    const { toHref } = this.props
    // console.log(toHref);
zhanghaozhe committed
27
    window.location.replace(toHref)
zhanghaozhe committed
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
  }

  goShop = () => {
    this.props.history.push("/shopcart")
  }

  render() {
    const { toHref, home } = this.props
    return (
      <div className="detail-header" style={{ ...this.props.style }}>
        {!toHref && this.props.arrow && (
          <i
            className="iconfont iconiconfront-68"
            onClick={this.props.goBack || this.goBack}
          ></i>
        )}
        {toHref && typeof toHref === "function" && (
          <i className="iconfont iconiconfront-68" onClick={toHref}></i>
        )}
        {toHref && typeof toHref === "string" && (
          <i className="iconfont iconiconfront-68" onClick={this.toLink}></i>
        )}
        <span className="herder">{this.props.title}</span>
        {this.props.cart && (
          <i
            className="iconfont icongouwuche-xianxing"
            onClick={this.goShop}
          ></i>
        )}
        {this.props.delete && (
          <i
            className="iconfont iconiconfront-56"
            onClick={this.props.toDelete}
          ></i>
        )}
        {home && <i className="iconfont iconshouye-xianxing"></i>}
      </div>
    )
  }
}
xuzhenghua committed
68

zhanghaozhe committed
69
export default withRouter(HeaderBar)