import React, {Component} from 'react' import {withRouter} from 'react-router-dom' import {NavBar, Icon, List, Picker, ActivityIndicator, InputItem} from 'antd-mobile' import classNames from 'classnames' import {Query, Mutation} from "react-apollo" import gql from "graphql-tag" import moment from 'moment' import {user_default_address, create_order, create_order_product} from "../../../utils/gql" import {idGen} from "../../../utils/func" import {getCookie} from "../../../utils/cookie" import './index.css' const Item = List.Item const Brief = Item.Brief const delivery = [ { label: "快递配送", value: "快递配送", }, { label: "门店自提", value: "门店自提", } ] class CartOrders extends Component { constructor(props) { super(props) this.state = { cartList: [], unfoldList: [], totalPrice: JSON.parse(sessionStorage.getItem('totalPrice')), totalCount: JSON.parse(sessionStorage.getItem('totalCount')), delivery: ["快递配送"], height: '100%', unfoldStatus: true, foldStatus: false, selectAddress: JSON.parse(sessionStorage.getItem('ordersAddress')), remark:'' } } componentWillMount() { // console.log('CartOrders componentWillMount',this.props) let type = this.props.history.location.state.dataType let cartList = JSON.parse(sessionStorage.getItem(type)) if (cartList.length > 3) { let cartList1 = cartList.slice(0, 3) let unfoldList = cartList.slice(3) this.setState({ cartList: cartList1, unfoldList }) } else { this.setState({ cartList }) } } onChangeDelivery = (val) => { this.setState({ delivery: val, }) } onChangeHeight = (height, unfoldStatus, foldStatus) => { this.setState({ height, unfoldStatus, foldStatus }) } onSubmitOrderAndProduct = (user_id,create_order,create_order_product) => { let {totalCount, totalPrice, remark, delivery} = this.state let createdAt = moment().format('YYYY-MM-DD HH:mm:ss') let ordersAddress = JSON.parse(sessionStorage.getItem('ordersAddress')) let {id:userAddress_id, telephone, username, province, city, area, address} = ordersAddress let addressData = String(province + city + area + address) let tag = telephone ? telephone.replace(/[^0-9]/ig, "").slice(-4) : Math.random().toString(10).substr(2,4) const orderId = createdAt.replace(/[^0-9]/ig, "").substr(2) + tag let orderLogisticsId = idGen('deliver') const orderContent = { remark, updatedAt: "", orderLogistics_id: orderLogisticsId, orderTotalPay: totalPrice, createdAt, orderStatus: "0", userAddress_id, id:orderId, count: totalCount, user_id, productTotalPay: totalPrice, orderPay_id: "", deleteId:[] } const orderLogistics = { updatedAt: "", deliveryTime: "", serviceStore: "", expressName:delivery[0], logisticsFee: 0.0, expressId: "", createdAt, order_id: orderId, consigneeTel: telephone, orderLogisticsId, consignAddress: addressData, LogisticsStatus: "0", user_id, consigneeName: username } let type = this.props.history.location.state.dataType let shopping = JSON.parse(sessionStorage.getItem(type)) if(type === 'cartSelected') orderContent.deleteId = shopping.map(item => item.id) // console.log('createOrder orderContent',orderContent) let createOrder = create_order({variables:{...orderContent, ...orderLogistics}}) let createOrderProduct = shopping.map((item,index) => { let createdAt = moment().format('YYYY-MM-DD HH:mm:ss') let orderProductId = createdAt.replace(/[^0-9]/ig, "").substr(2) + tag +index let {count, product_id:productData, specificationStock_id:specData} = item let {id:product_id, img, name, price, unit} = productData let {id:specId, color, size} = specData // console.log('product',index,item,product_id) const orderProduct = { updatedAt: "", productColor: color, unit, product_id, specificationStock_id:specId, productSize:size, orderPay: price, createdAt, productImg:img, productName: name, order_id: orderId, productPrice:price, id:orderProductId, user_id, count, productPay: price, orderPay_id: "", } console.log(`orderProduct${index}`,orderProduct) return create_order_product({variables:orderProduct}).then((data)=>{ console.log('ok data',index,data) return data.data }) }) Promise.all([createOrder, createOrderProduct]).then((data)=> { console.log('onSubmitOrderAndProduct data',data); if(type === 'cartSelected'){ let cartCount = JSON.parse(localStorage.getItem("cartCount")) - totalCount localStorage.setItem("cartCount",JSON.stringify(cartCount)) localStorage.removeItem("cartList") } this.props.history.push({ pathname:'/cart/pay', state:{} }) }).catch((err)=>{ console.log('submit error',err) }) } render() { let {cartList, unfoldList, height, unfoldStatus, foldStatus, totalPrice, selectAddress} = this.state let user_id = getCookie('user_id') return (