App.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. import React, {Component} from 'react'
  2. import {Row, Col, Icon} from 'antd'
  3. import {Switch, Route, NavLink, withRouter} from 'react-router-dom'
  4. import classnames from 'classnames'
  5. import {request} from 'graphql-request'
  6. import moment from 'moment'
  7. import Home from './pages/home'
  8. import Cart from './pages/cart'
  9. import My from './pages/my'
  10. import {graphqlFC} from "./configs/url"
  11. import {getCookie, setCookie} from "./utils/cookie"
  12. import {find_user_by_openid, create_user} from "./utils/gql"
  13. import {idGen} from "./utils/func"
  14. import './app.css'
  15. class App extends Component {
  16. constructor(props) {
  17. super(props)
  18. this.state = {
  19. selectedTab: 'home',
  20. tabHidden: false
  21. }
  22. }
  23. // componentWillMount 适用于刷新的 tabbar 的展示
  24. componentWillMount() {
  25. let {location} = this.props,
  26. pathname = location.pathname
  27. // 根据首次的 pathname 显示 icon 选中
  28. // 如 /cart 刷新
  29. if (location && pathname) {
  30. this.setState({
  31. selectedTab: pathname.split('/')[1] === '' ? 'home' : pathname.split('/')[1]
  32. })
  33. }
  34. // 如果不是首页的初始界面,就隐藏 tabbar
  35. // 如在 /home/detail 或 /home/detail/ 刷新
  36. let pathnameArray = location.pathname.split('/')
  37. let length = pathnameArray.length
  38. if (length > 2 && pathnameArray[length - 1] !== '') {
  39. this.setState({
  40. tabHidden: true
  41. })
  42. }
  43. }
  44. // componentWillReceiveProps 适用于跳转的 tabbar 的展示
  45. componentWillReceiveProps(next) {
  46. let {location} = next,
  47. pathname = location.pathname,
  48. state = location.state
  49. // 有 state 的话,根据tabHidden显示或隐藏 tabbar, (进入子界面)
  50. // 无 state 的话,就显示 tabbar (返回到主界面)
  51. if (location && state) {
  52. let tabHidden = state.tabHidden !== undefined ? state.tabHidden : true
  53. this.setState({
  54. tabHidden
  55. })
  56. } else {
  57. this.setState({
  58. tabHidden: false
  59. })
  60. }
  61. // 根据非首次的 pathname 显示 icon 选中
  62. // 如 /home 跳转到 /home/kind, /cart 跳转到 /home 等
  63. if (location && pathname) {
  64. this.setState({
  65. selectedTab: pathname.split('/')[1] === '' ? 'home' : pathname.split('/')[1]
  66. })
  67. }
  68. }
  69. isActiveFunc = (navKind) => (match, location) => {
  70. if (navKind === 'home' && location.pathname.split('/')[1] === '') {
  71. return true
  72. } else {
  73. return navKind === location.pathname.split('/')[1]
  74. }
  75. }
  76. oauthLogin = () => {
  77. let openid = getCookie("openid")
  78. // setCookie("openid","obR_j5GbxDfGlOolvSeTdZUwfpKA")
  79. let user_id = getCookie("user_id")
  80. console.log('oauthLogin openid',openid)
  81. if (!openid) {
  82. window.location.href = "/subscribe"
  83. }else if(!user_id){
  84. request(graphqlFC, find_user_by_openid ,{openid})
  85. .then(data => {
  86. // console.log('find user data',data)
  87. if(data.userbyprops[0].id){
  88. let id = data.userbyprops[0].id
  89. setCookie('user_id',id)
  90. }else {
  91. let createdAt = moment().format('YYYY-MM-DD HH:mm:ss')
  92. let id = idGen('user')
  93. const userContent = {
  94. email: "",
  95. updatedAt: "",
  96. password: "",
  97. telephone: "",
  98. username: "",
  99. createdAt,
  100. openid,
  101. id,
  102. userData_id: ""
  103. }
  104. request(graphqlFC, create_user ,userContent)
  105. .then(data => {
  106. console.log('create user data',data)
  107. setCookie('user_id',id)
  108. })
  109. .catch(err => {
  110. console.log(err, `graphql-request create user error`)
  111. })
  112. }
  113. })
  114. .catch(err => {
  115. console.log(err, `graphql-request find user error`)
  116. })
  117. }
  118. }
  119. render() {
  120. let {selectedTab, tabHidden} = this.state
  121. return (
  122. <div style={{height: '100%'}}>
  123. <div className={classnames('tabbar', {'tabbar-hidden': tabHidden})}>
  124. <Row>
  125. <Col span={8} className='tabbar-content'>
  126. <NavLink exact isActive={this.isActiveFunc('home')} activeClassName="active" to="/home">
  127. {
  128. selectedTab === 'home' ?
  129. <HomeSelectedIcon/>
  130. :
  131. <HomeUnselectedIcon/>
  132. }
  133. <div className='tabbar-title'>
  134. 主页
  135. </div>
  136. </NavLink>
  137. </Col>
  138. <Col span={8} className='tabbar-content'>
  139. <NavLink isActive={this.isActiveFunc('cart')} activeClassName="active" to="/cart">
  140. {
  141. selectedTab === 'cart' ?
  142. <CartSelectedIcon/>
  143. :
  144. <CartUnselectedIcon/>
  145. }
  146. <div className='tabbar-title'>
  147. 购物袋
  148. </div>
  149. </NavLink>
  150. </Col>
  151. <Col span={8} className='tabbar-content'>
  152. <NavLink isActive={this.isActiveFunc('my')} activeClassName="active" to="/my">
  153. {
  154. selectedTab === 'my' ?
  155. <MySelectedIcon/>
  156. :
  157. <MyUnselectedIcon/>
  158. }
  159. <div className='tabbar-title'>
  160. </div>
  161. </NavLink>
  162. </Col>
  163. </Row>
  164. </div>
  165. <div className='tabbar-route-content'>
  166. <Switch>
  167. <Route exact path="/" render={() => {
  168. this.oauthLogin()
  169. return <Home />
  170. }}/>
  171. <Route path="/home" component={Home}/>
  172. <Route path="/cart" component={Cart}/>
  173. <Route path="/my" component={My}/>
  174. </Switch>
  175. </div>
  176. </div>
  177. )
  178. }
  179. }
  180. export default withRouter(App)
  181. const HomeUnselectedIcon = () => (
  182. <Icon type="home" style={{fontSize: 22}}/>
  183. )
  184. const HomeSelectedIcon = () => (
  185. <Icon type="home" theme="twoTone" style={{fontSize: 22}}/>
  186. )
  187. const CartUnselectedIcon = () => (
  188. <Icon type="shopping" style={{fontSize: 22}}/>
  189. )
  190. const CartSelectedIcon = () => (
  191. <Icon type="shopping" theme="twoTone" style={{fontSize: 22}}/>
  192. )
  193. const MyUnselectedIcon = () => (
  194. <Icon type="setting" style={{fontSize: 22}}/>
  195. )
  196. const MySelectedIcon = () => (
  197. <Icon type="setting" theme="twoTone" style={{fontSize: 22}}/>
  198. )