index.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. import React, {Component} from 'react'
  2. import {withRouter} from 'react-router-dom'
  3. import {Row, Col} from 'antd'
  4. import {NavBar, Icon, ActivityIndicator, Button} from 'antd-mobile'
  5. import {Query} from "react-apollo"
  6. import gql from "graphql-tag"
  7. import classNames from 'classnames'
  8. import {orderbyprops, orderProduct_by_props} from "../../../../utils/gql"
  9. import {getCookie} from "../../../../utils/cookie"
  10. import './index.css'
  11. class Display extends Component {
  12. constructor(props) {
  13. super(props)
  14. this.state = {
  15. navTitle: '待付款',
  16. kind: 'pay',
  17. orderStatus: '0'
  18. }
  19. }
  20. componentWillMount() {
  21. let {location} = this.props
  22. if (location && location.state) {
  23. let navTitle = '',
  24. orderStatus = '0'
  25. let kind = location.state.kind
  26. switch (kind) {
  27. case 'pay':
  28. navTitle = '待付款'
  29. orderStatus = '0'
  30. break
  31. case 'ship':
  32. navTitle = '待发货'
  33. orderStatus = '1'
  34. break
  35. case 'unbox':
  36. navTitle = '待收货'
  37. orderStatus = '2'
  38. break
  39. case 'judge':
  40. navTitle = '待评价'
  41. orderStatus = '3'
  42. break
  43. default:
  44. navTitle = '无效页面'
  45. break
  46. }
  47. this.setState({
  48. navTitle,
  49. kind,
  50. orderStatus
  51. })
  52. }
  53. }
  54. render() {
  55. let {navTitle, orderStatus} = this.state
  56. let user_id = getCookie('user_id')
  57. return (
  58. <div className='order-wrap'>
  59. <div className='navbar'>
  60. <NavBar
  61. mode="light"
  62. icon={<Icon type="left"/>}
  63. onLeftClick={() => {
  64. this.props.history.go(-2)
  65. }}
  66. >{navTitle}</NavBar>
  67. </div>
  68. <Query query={gql(orderbyprops)} variables={{user_id, orderStatus}}>
  69. {
  70. ({loading, error, data}) => {
  71. if (loading) {
  72. return (
  73. <div className="loading-center">
  74. <ActivityIndicator text="Loading..." size="large"/>
  75. </div>
  76. )
  77. }
  78. if (error) {
  79. return 'error!'
  80. }
  81. return (
  82. <DisplayRender data={data.orderbyprops} orderStatus={orderStatus}
  83. history={this.props.history}/>
  84. )
  85. }
  86. }
  87. </Query>
  88. </div>
  89. )
  90. }
  91. }
  92. class DisplayRender extends Component {
  93. constructor(props) {
  94. super(props)
  95. this.state = {}
  96. }
  97. orderCardContentRender = (data) => {
  98. if (data.length === 1) {
  99. return (
  100. <Row style={{width: '100%'}}>
  101. <Col span={6} style={{height: '100%'}}>
  102. <div className='order-product-img'
  103. style={{backgroundImage: `url('${data[0].product_id.img}')`}}/>
  104. </Col>
  105. <Col span={16} offset={2}>
  106. <div className='order-product-name'>{data[0].product_id.name}</div>
  107. </Col>
  108. </Row>
  109. )
  110. } else {
  111. return (data.map(data => (
  112. <div className='order-product-img' style={{backgroundImage: `url('${data.product_id.img}')`}}
  113. key={data.id}/>
  114. )))
  115. }
  116. }
  117. render() {
  118. let {data, orderStatus, button = true} = this.props
  119. let content = orderStatus === '0' ? '需付款' : '实付款'
  120. return (
  121. <div className={classNames({'content-wrap': button})}>
  122. {
  123. data.length === 0 ?
  124. <div>
  125. 还没有这种订单呢
  126. </div>
  127. :
  128. data.map(order => (
  129. <div key={order.id} className='order-card'>
  130. <div className='order-card-top'>订单号: {order.id}</div>
  131. <Query query={gql(orderProduct_by_props)} variables={{order_id: order.id}}>
  132. {
  133. ({loading, error, data}) => {
  134. if (loading) {
  135. return (
  136. <div className="loading-center">
  137. <ActivityIndicator text="Loading..." size="large"/>
  138. </div>
  139. )
  140. }
  141. if (error) {
  142. return 'error!'
  143. }
  144. data = data.orderProductbyprops
  145. return (
  146. <div>
  147. {
  148. button?
  149. <div className='order-card-content' onClick={() => {
  150. this.props.history.push({
  151. pathname: '/my/order/detail',
  152. state: {
  153. data: order
  154. }
  155. })
  156. }}>
  157. {
  158. this.orderCardContentRender(data)
  159. }
  160. </div>
  161. :
  162. <div className='order-card-content'>
  163. {
  164. this.orderCardContentRender(data)
  165. }
  166. </div>
  167. }
  168. </div>
  169. )
  170. }
  171. }
  172. </Query>
  173. <div className='order-card-bottom'>
  174. <div
  175. className='order-card-count'>共{order.count}件商品&nbsp;&nbsp;{content}:
  176. </div>
  177. <div className='order-card-pay'>¥{Math.round(order.productTotalPay * 100) / 100}</div>
  178. </div>
  179. {
  180. button?
  181. <ButtonGroupRender orderStatus={this.props.orderStatus}/>
  182. :
  183. ''
  184. }
  185. </div>
  186. ))
  187. }
  188. </div>
  189. )
  190. }
  191. }
  192. const ButtonGroupRender = (props) => {
  193. let {orderStatus} = props
  194. switch (orderStatus) {
  195. case '0':
  196. return (
  197. <div className='order-card-button-group'>
  198. <Button size="small" className='pay-button order-button'>去支付</Button>
  199. </div>
  200. )
  201. case '1':
  202. return (
  203. <div className='order-card-button-group'>
  204. <Button size="small" className='ship-button order-button'>催发货</Button>
  205. &nbsp;&nbsp;
  206. <Button size="small" className='cancel-button order-button'>取消订单</Button>
  207. </div>
  208. )
  209. case '2':
  210. return (
  211. <div className='order-card-button-group'>
  212. <Button size="small" className='unbox-button order-button'>查看物流</Button>
  213. &nbsp;&nbsp;
  214. <Button size="small" className='cancel-button order-button'>取消订单</Button>
  215. </div>
  216. )
  217. case '3':
  218. return (
  219. <div className='order-card-button-group'>
  220. <Button size="small" className='judge-button order-button'>去评价</Button>
  221. &nbsp;&nbsp;
  222. <Button size="small" className='more-button order-button'>售后</Button>
  223. </div>
  224. )
  225. default:
  226. return (
  227. <div>
  228. ok
  229. </div>
  230. )
  231. }
  232. }
  233. export default withRouter(Display)
  234. export {
  235. DisplayRender,
  236. ButtonGroupRender
  237. }