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 (
} onLeftClick={() => { // this.props.history.goBack() this.props.history.push({ pathname:'/cart', state:{ updateData:true, tabHidden:false } }) }} >订单确认
{ selectAddress ? : { ({loading, error, data}) => { if (loading) { return (
加载中...
) } if (error) { return 'error!' } let defaultAddress = data.defaultAddress[0] if(defaultAddress){ return ( ) }else { return (
{ this.props.history.push({ pathname:'/my/tools', state: { page: 'address', prePage: 'orders', single: true }}) }} >+ 添加收货地址
) } } }
}
{ cartList.map((item, index) => { return (
{item.product_id.name}
{item.specificationStock_id.color} {item.specificationStock_id.size}
¥ {item.product_id.price}
x {item.count}
) }) }
{ unfoldStatus ?
{ this.onChangeHeight(96 * unfoldList.length + 42, false, true) }}>
展开全部商品
: '' } { foldStatus ?
{ this.onChangeHeight('100%', true, false) }}> { unfoldList.map((item, index) => { return (
{item.product_id.name}
{item.specificationStock_id.color} {item.specificationStock_id.size}
¥ {item.product_id.price}
x {item.count}
) }) }
收起
: '' }
配送方式
{ // console.log('orders-remark val',val) this.setState({ remark:val }) }} >
买家留言
商品金额 ¥ {totalPrice}
运费 ¥ 0.00
console.log('create_order error',error)} > {(create_order,{ loading, error }) => (
合计: ¥ {totalPrice}
console.log('create_order_product error',error)} > {(create_order_product,{ loading, error }) => ( )}
)}
) } } export default withRouter(CartOrders) const OrdersAddress =({props,selectAddress}) => { console.log('props',props) console.log('selectAddress',selectAddress) let {default:isDefault, username, telephone, province, area, city, address} = selectAddress sessionStorage.setItem('ordersAddress',JSON.stringify(selectAddress)) return ( { props.history.push({ pathname:'/my/tools', state: { page: 'address', prePage: 'orders' }}) }}>
{username}   {telephone}
{ isDefault ?
默认
:'' } {province}{area}{city}{address}
) }