index.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. import React, {Component} from 'react'
  2. import {withRouter} from 'react-router-dom'
  3. import {NavBar, Icon, List, Picker, ActivityIndicator, InputItem} from 'antd-mobile'
  4. import classNames from 'classnames'
  5. import {Query, Mutation} from "react-apollo"
  6. import gql from "graphql-tag"
  7. import moment from 'moment'
  8. import {user_default_address, create_order, create_order_product} from "../../../utils/gql"
  9. import {idGen} from "../../../utils/func"
  10. import {getCookie} from "../../../utils/cookie"
  11. import './index.css'
  12. const Item = List.Item
  13. const Brief = Item.Brief
  14. const delivery = [
  15. {
  16. label: "快递配送",
  17. value: "快递配送",
  18. },
  19. {
  20. label: "门店自提",
  21. value: "门店自提",
  22. }
  23. ]
  24. class CartOrders extends Component {
  25. constructor(props) {
  26. super(props)
  27. this.state = {
  28. cartList: [],
  29. unfoldList: [],
  30. totalPrice: JSON.parse(sessionStorage.getItem('totalPrice')),
  31. totalCount: JSON.parse(sessionStorage.getItem('totalCount')),
  32. delivery: ["快递配送"],
  33. height: '100%',
  34. unfoldStatus: true,
  35. foldStatus: false,
  36. selectAddress: JSON.parse(sessionStorage.getItem('ordersAddress')),
  37. remark:''
  38. }
  39. }
  40. componentWillMount() {
  41. // console.log('CartOrders componentWillMount',this.props)
  42. let type = this.props.history.location.state.dataType
  43. let cartList = JSON.parse(sessionStorage.getItem(type))
  44. if (cartList.length > 3) {
  45. let cartList1 = cartList.slice(0, 3)
  46. let unfoldList = cartList.slice(3)
  47. this.setState({
  48. cartList: cartList1,
  49. unfoldList
  50. })
  51. } else {
  52. this.setState({
  53. cartList
  54. })
  55. }
  56. }
  57. onChangeDelivery = (val) => {
  58. this.setState({
  59. delivery: val,
  60. })
  61. }
  62. onChangeHeight = (height, unfoldStatus, foldStatus) => {
  63. this.setState({
  64. height,
  65. unfoldStatus,
  66. foldStatus
  67. })
  68. }
  69. onSubmitOrderAndProduct = (user_id,create_order,create_order_product) => {
  70. let {totalCount, totalPrice, remark, delivery} = this.state
  71. let createdAt = moment().format('YYYY-MM-DD HH:mm:ss')
  72. let ordersAddress = JSON.parse(sessionStorage.getItem('ordersAddress'))
  73. let {id:userAddress_id, telephone, username, province, city, area, address} = ordersAddress
  74. let addressData = String(province + city + area + address)
  75. let tag = telephone ? telephone.replace(/[^0-9]/ig, "").slice(-4) : Math.random().toString(10).substr(2,4)
  76. const orderId = createdAt.replace(/[^0-9]/ig, "").substr(2) + tag
  77. let orderLogisticsId = idGen('deliver')
  78. const orderContent = {
  79. remark,
  80. updatedAt: "",
  81. orderLogistics_id: orderLogisticsId,
  82. orderTotalPay: totalPrice,
  83. createdAt,
  84. orderStatus: "0",
  85. userAddress_id,
  86. id:orderId,
  87. count: totalCount,
  88. user_id,
  89. productTotalPay: totalPrice,
  90. orderPay_id: "",
  91. deleteId:[]
  92. }
  93. const orderLogistics = {
  94. updatedAt: "",
  95. deliveryTime: "",
  96. serviceStore: "",
  97. expressName:delivery[0],
  98. logisticsFee: 0.0,
  99. expressId: "",
  100. createdAt,
  101. order_id: orderId,
  102. consigneeTel: telephone,
  103. orderLogisticsId,
  104. consignAddress: addressData,
  105. LogisticsStatus: "0",
  106. user_id,
  107. consigneeName: username
  108. }
  109. let type = this.props.history.location.state.dataType
  110. let shopping = JSON.parse(sessionStorage.getItem(type))
  111. if(type === 'cartSelected') orderContent.deleteId = shopping.map(item => item.id)
  112. // console.log('createOrder orderContent',orderContent)
  113. let createOrder = create_order({variables:{...orderContent, ...orderLogistics}})
  114. let createOrderProduct = shopping.map((item,index) => {
  115. let createdAt = moment().format('YYYY-MM-DD HH:mm:ss')
  116. let orderProductId = createdAt.replace(/[^0-9]/ig, "").substr(2) + tag +index
  117. let {count, product_id:productData, specificationStock_id:specData} = item
  118. let {id:product_id, img, name, price, unit} = productData
  119. let {id:specId, color, size} = specData
  120. // console.log('product',index,item,product_id)
  121. const orderProduct = {
  122. updatedAt: "",
  123. productColor: color,
  124. unit,
  125. product_id,
  126. specificationStock_id:specId,
  127. productSize:size,
  128. orderPay: price,
  129. createdAt,
  130. productImg:img,
  131. productName: name,
  132. order_id: orderId,
  133. productPrice:price,
  134. id:orderProductId,
  135. user_id,
  136. count,
  137. productPay: price,
  138. orderPay_id: "",
  139. }
  140. console.log(`orderProduct${index}`,orderProduct)
  141. return create_order_product({variables:orderProduct}).then((data)=>{
  142. console.log('ok data',index,data)
  143. return data.data
  144. })
  145. })
  146. Promise.all([createOrder, createOrderProduct]).then((data)=> {
  147. console.log('onSubmitOrderAndProduct data',data);
  148. if(type === 'cartSelected'){
  149. let cartCount = JSON.parse(localStorage.getItem("cartCount")) - totalCount
  150. localStorage.setItem("cartCount",JSON.stringify(cartCount))
  151. localStorage.removeItem("cartList")
  152. }
  153. this.props.history.push({
  154. pathname:'/cart/pay',
  155. state:{}
  156. })
  157. }).catch((err)=>{
  158. console.log('submit error',err)
  159. })
  160. }
  161. render() {
  162. let {cartList, unfoldList, height, unfoldStatus, foldStatus, totalPrice, selectAddress} = this.state
  163. let user_id = getCookie('user_id')
  164. return (
  165. <div className='orders-wrap'>
  166. <div className='orders-navbar-wrap navbar'>
  167. <NavBar
  168. className='orders-navbar'
  169. mode="light"
  170. icon={<Icon type="left"/>}
  171. onLeftClick={() => {
  172. // this.props.history.goBack()
  173. this.props.history.push({
  174. pathname:'/cart',
  175. state:{
  176. updateData:true,
  177. tabHidden:false
  178. }
  179. })
  180. }}
  181. >订单确认</NavBar>
  182. </div>
  183. <div className='orders-content-wrap content-wrap'>
  184. <div className='orders-address'>
  185. {
  186. selectAddress ?
  187. <OrdersAddress props={this.props} selectAddress={selectAddress} />:
  188. <Query query={gql(user_default_address)} variables={{user_id, default:1}}>
  189. {
  190. ({loading, error, data}) => {
  191. if (loading) {
  192. return (
  193. <div className="loading-center">
  194. <ActivityIndicator size="large"/>
  195. <span>加载中...</span>
  196. </div>
  197. )
  198. }
  199. if (error) {
  200. return 'error!'
  201. }
  202. let defaultAddress = data.defaultAddress[0]
  203. if(defaultAddress){
  204. return (
  205. <OrdersAddress props={this.props} selectAddress={defaultAddress} />
  206. )
  207. }else {
  208. return (
  209. <div className='orders-address-add'
  210. onClick={() => {
  211. this.props.history.push({
  212. pathname:'/my/tools',
  213. state: {
  214. page: 'address',
  215. prePage: 'orders',
  216. single: true
  217. }})
  218. }}
  219. >+ 添加收货地址</div>
  220. )
  221. }
  222. }
  223. }
  224. </Query>
  225. }
  226. </div>
  227. <div className='orders-detail'>
  228. <div className='cart-content'>
  229. {
  230. cartList.map((item, index) => {
  231. return (
  232. <div key={'cart-orders-'+item.id}>
  233. <div className="cart-list">
  234. <div className="cart-list-image">
  235. <img src={item.product_id.img || "https://gw.alipayobjects.com/zos/rmsportal/nywPmnTAvTmLusPxHPSu.png"} alt=""/>
  236. </div>
  237. <div className="cart-orders-intro">
  238. <div className='hide-extra-text'>{item.product_id.name}</div>
  239. <div>{item.specificationStock_id.color} {item.specificationStock_id.size}</div>
  240. <div>¥ {item.product_id.price}</div>
  241. </div>
  242. <div className="cart-orders-count">
  243. x {item.count}
  244. </div>
  245. </div>
  246. </div>
  247. )
  248. })
  249. }
  250. <div className={classNames({'packup': !unfoldList.length, 'packup-unfold': true})}
  251. style={{height: height}}>
  252. {
  253. unfoldStatus ?
  254. <div onClick={() => {
  255. this.onChangeHeight(96 * unfoldList.length + 42, false, true)
  256. }}>
  257. <div className='packup-title'>展开全部商品</div>
  258. <div>∨</div>
  259. </div>
  260. : ''
  261. }
  262. {
  263. foldStatus ?
  264. <div onClick={() => {
  265. this.onChangeHeight('100%', true, false)
  266. }}>
  267. {
  268. unfoldList.map((item, index) => {
  269. return (
  270. <div key={index}>
  271. <div className="cart-list">
  272. <div className="cart-list-image">
  273. <img src={item.product_id.img} alt=""/>
  274. </div>
  275. <div className="cart-orders-intro">
  276. <div>{item.product_id.name}</div>
  277. <div>{item.specificationStock_id.color} {item.specificationStock_id.size}</div>
  278. <div>¥ {item.product_id.price}</div>
  279. </div>
  280. <div className="cart-orders-count">
  281. x {item.count}
  282. </div>
  283. </div>
  284. </div>
  285. )
  286. })
  287. }
  288. <div className='packup-title'>收起</div>
  289. <div>∧</div>
  290. </div> : ''
  291. }
  292. </div>
  293. </div>
  294. </div>
  295. <div className='orders-delivery'>
  296. <div>
  297. <Picker
  298. data={delivery}
  299. value={this.state.delivery}
  300. cols={1}
  301. onChange={this.onChangeDelivery}
  302. >
  303. <List.Item arrow="horizontal">配送方式</List.Item>
  304. </Picker>
  305. </div>
  306. <div className="orders-message">
  307. <InputItem
  308. labelNumber={4}
  309. placeholder="输入留言内容(50字以内)"
  310. maxLength={50}
  311. onBlur={(val) => {
  312. // console.log('orders-remark val',val)
  313. this.setState({
  314. remark:val
  315. })
  316. }}
  317. >
  318. <div className='orders-message-title'>买家留言</div>
  319. </InputItem>
  320. </div>
  321. </div>
  322. <div className='orders-price'>
  323. <div>商品金额
  324. <span>¥ {totalPrice}</span>
  325. </div>
  326. <div>运费
  327. <span>¥ 0.00</span>
  328. </div>
  329. </div>
  330. </div>
  331. <Mutation mutation={gql(create_order)}
  332. onError={error=>console.log('create_order error',error)}
  333. >
  334. {(create_order,{ loading, error }) => (
  335. <div className="orders-footer">
  336. <div className="jiesuan">
  337. <div className='jiesuan-total'>
  338. <span>合计:</span>
  339. <span className="jiesuan-total_price">¥ {totalPrice}</span>
  340. </div>
  341. <Mutation mutation={gql(create_order_product)}
  342. onError={error=>console.log('create_order_product error',error)}
  343. >
  344. {(create_order_product,{ loading, error }) => (
  345. <button className="jiesuan-button"
  346. onClick={()=>{
  347. this.onSubmitOrderAndProduct(user_id,create_order,create_order_product)
  348. }}>
  349. <span>提交订单</span>
  350. </button>
  351. )}
  352. </Mutation>
  353. </div>
  354. </div>
  355. )}
  356. </Mutation>
  357. </div>
  358. )
  359. }
  360. }
  361. export default withRouter(CartOrders)
  362. const OrdersAddress =({props,selectAddress}) => {
  363. console.log('props',props)
  364. console.log('selectAddress',selectAddress)
  365. let {default:isDefault, username, telephone, province, area, city, address} = selectAddress
  366. sessionStorage.setItem('ordersAddress',JSON.stringify(selectAddress))
  367. return (
  368. <List>
  369. <Item
  370. arrow="horizontal"
  371. multipleLine
  372. onClick={() => {
  373. props.history.push({
  374. pathname:'/my/tools',
  375. state: {
  376. page: 'address',
  377. prePage: 'orders'
  378. }})
  379. }}>
  380. <div>
  381. <span>{username}</span>&nbsp;&nbsp;
  382. <span>{telephone}</span>
  383. </div>
  384. <div>
  385. <div>
  386. {
  387. isDefault ?
  388. <div className="orders-address-label">
  389. <span className='address-label'>默认</span>
  390. </div>:''
  391. }
  392. <Brief style={{fontSize: 13}}>{province}{area}{city}{address}</Brief>
  393. </div>
  394. </div>
  395. </Item>
  396. </List>
  397. )
  398. }