index.js 11 KB

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