| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406 |
- 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 './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 = (create_order,create_order_product) => {
- let user_id = "obR_j5GbxDfGlOolvSeTdZUwfpKA"
- 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
- return (
- <div className='orders-wrap'>
- <div className='orders-navbar-wrap navbar'>
- <NavBar
- className='orders-navbar'
- mode="light"
- icon={<Icon type="left"/>}
- onLeftClick={() => {
- // this.props.history.goBack()
- this.props.history.push({
- pathname:'/cart',
- state:{
- updateData:true,
- tabHidden:false
- }
- })
- }}
- >订单确认</NavBar>
- </div>
- <div className='orders-content-wrap content-wrap'>
- <div className='orders-address'>
- {
- selectAddress ?
- <OrdersAddress props={this.props} selectAddress={selectAddress} />:
- <Query query={gql(user_default_address)} variables={{user_id: "obR_j5GbxDfGlOolvSeTdZUwfpKA", default:1}}>
- {
- ({loading, error, data}) => {
- if (loading) {
- return (
- <div className="loading-center">
- <ActivityIndicator size="large"/>
- <span>加载中...</span>
- </div>
- )
- }
- if (error) {
- return 'error!'
- }
- let defaultAddress = data.defaultAddress[0]
- return (
- <OrdersAddress props={this.props} selectAddress={defaultAddress} />
- )
- }
- }
- </Query>
- }
- </div>
- <div className='orders-detail'>
- <div className='cart-content'>
- {
- cartList.map((item, index) => {
- return (
- <div key={index}>
- <div className="cart-list">
- <div className="cart-list-image">
- <img
- src={item.product_id.img || "https://gw.alipayobjects.com/zos/rmsportal/nywPmnTAvTmLusPxHPSu.png"}
- alt=""/>
- </div>
- <div className="cart-orders-intro">
- <div className='hide-extra-text'>{item.product_id.name}</div>
- <div>{item.specificationStock_id.color} {item.specificationStock_id.size}</div>
- <div>¥ {item.product_id.price}</div>
- </div>
- <div className="cart-orders-count">
- x {item.count}
- </div>
- </div>
- </div>
- )
- })
- }
- <div className={classNames({'packup': !unfoldList.length, 'packup-unfold': true})}
- style={{height: height}}>
- {
- unfoldStatus ?
- <div onClick={() => {
- this.onChangeHeight(96 * unfoldList.length + 42, false, true)
- }}>
- <div className='packup-title'>展开全部商品</div>
- <div>∨</div>
- </div>
- : ''
- }
- {
- foldStatus ?
- <div onClick={() => {
- this.onChangeHeight('100%', true, false)
- }}>
- {
- unfoldList.map((item, index) => {
- return (
- <div key={index}>
- <div className="cart-list">
- <div className="cart-list-image">
- <img src={item.product_id.img} alt=""/>
- </div>
- <div className="cart-orders-intro">
- <div>{item.product_id.name}</div>
- <div>{item.specificationStock_id.color} {item.specificationStock_id.size}</div>
- <div>¥ {item.product_id.price}</div>
- </div>
- <div className="cart-orders-count">
- x {item.count}
- </div>
- </div>
- </div>
- )
- })
- }
- <div className='packup-title'>收起</div>
- <div>∧</div>
- </div> : ''
- }
- </div>
- </div>
- </div>
- <div className='orders-delivery'>
- <div>
- <Picker
- data={delivery}
- value={this.state.delivery}
- cols={1}
- onChange={this.onChangeDelivery}
- >
- <List.Item arrow="horizontal">配送方式</List.Item>
- </Picker>
- </div>
- <div className="orders-message">
- <InputItem
- labelNumber={4}
- placeholder="输入留言内容(50字以内)"
- maxLength={50}
- onBlur={(val) => {
- // console.log('orders-remark val',val)
- this.setState({
- remark:val
- })
- }}
- >
- <div className='orders-message-title'>买家留言</div>
- </InputItem>
- </div>
- </div>
- <div className='orders-price'>
- <div>商品金额
- <span>¥ {totalPrice}</span>
- </div>
- <div>运费
- <span>¥ 0.00</span>
- </div>
- </div>
- </div>
- <Mutation mutation={gql(create_order)}
- onError={error=>console.log('create_order error',error)}
- >
- {(create_order,{ loading, error }) => (
- <div className="orders-footer">
- <div className="jiesuan">
- <div className='jiesuan-total'>
- <span>合计:</span>
- <span className="jiesuan-total_price">¥ {totalPrice}</span>
- </div>
- <Mutation mutation={gql(create_order_product)}
- onError={error=>console.log('create_order_product error',error)}
- >
- {(create_order_product,{ loading, error }) => (
- <button className="jiesuan-button"
- onClick={()=>{
- this.onSubmitOrderAndProduct(create_order,create_order_product)
- }}>
- <span>提交订单</span>
- </button>
- )}
- </Mutation>
- </div>
- </div>
- )}
- </Mutation>
- </div>
- )
- }
- }
- export default withRouter(CartOrders)
- const OrdersAddress =({props,selectAddress}) => {
- let {default:isDefault, username, telephone, province, area, city, address} = selectAddress
- sessionStorage.setItem('ordersAddress',JSON.stringify(selectAddress))
- return (
- <List>
- <Item
- arrow="horizontal"
- multipleLine
- onClick={() => {
- props.history.push({
- pathname:'/my/tools',
- state: {
- page: 'address'
- }})
- }}>
- <div>
- <span>{username}</span>
- <span>{telephone}</span>
- </div>
- <div>
- <div>
- {
- isDefault ?
- <div className="orders-address-label">
- <span className='address-label'>默认</span>
- </div>:''
- }
- <Brief style={{fontSize: 13}}>{province}{area}{city}{address}</Brief>
- </div>
- </div>
- </Item>
- </List>
- )
- }
|