import React, {Component} from 'react' import {withRouter} from 'react-router-dom' import {Query, Mutation} from "react-apollo" import gql from "graphql-tag" import {message} from 'antd' import {NavBar, Icon, ActivityIndicator, Badge, Modal} from 'antd-mobile' import classNames from 'classnames' import moment from 'moment' import {productAndSpec_by_id, create_userCart} from "../../../utils/gql" import {idGen} from '../../../utils/func' import './index.css' class Detail extends Component { constructor(props) { super(props) this.state = { id: '' } } componentWillMount() { let {location} = this.props if(location && location.state) { this.setState({ id: location.state.id }) } } render() { let {id} = this.state let contentHeight = window.innerHeight return (
} onLeftClick={() => {this.props.history.go(-1)}} >商品详情
{ ({loading, error, data}) => { if (loading) { return (
) } if (error) { return 'error!' } // console.log('productAndSpec_by_id data',data) return ( ) } }
) } } export default withRouter(Detail) class DetailRender extends Component { constructor(props) { super(props) this.state = { cartCount: localStorage.getItem('cartCount'), openSelect: false, buttonType: 'add' } } showModal = (e,key) => { e.preventDefault(); // 修复 Android 上点击穿透 this.setState({ [key]: true, }) } changeModalState = (state,val) => { this.setState({ [state]:val }) } changeBottomButtonType = (e,val) => { this.setState({ buttonType:val }) this.showModal(e,'openSelect') } render() { let {data} = this.props let {name, img, price, stock} = data.productbyid let {cartCount, openSelect, buttonType} = this.state // console.log('DetailRender openSelect',openSelect) return (
{name}
¥{price}   ¥{price} 库存 {stock}
商品信息
通过商品详情图片展示
{this.props.history.push({pathname: '/home'})}}>
{this.props.history.push({pathname: '/cart'})}}>
{this.changeBottomButtonType(e,'add')}}>加入购物袋 {this.changeBottomButtonType(e,'buy')}}>立即购买
) } } class SelectModal extends Component { constructor(props) { super(props) this.state = { count: 1, selectColor: '', selectSpec:{}, specList: [], colorList: [] } } componentWillMount() { let {productData} = this.props this.handleData(productData.spec) } // 规格表处理 handleData = (specs) => { let flag = true, selectFlag = true let specObject = {}, i = 0, specList = [] let colorObject = {}, j = 0, colorList = [], selectColor = '' specs.forEach((item) => { let {id,color,size,stock,status} = item if(flag && status > 0) { selectColor = color flag = false } specObject[color] ? specList[specObject[color] - 1].spec.push({id, size, stock, status}) : specObject[color] = ++i && specList.push({ color, spec: [{id, size, stock, status}], }) if(!colorObject[color]) { colorObject[color] = ++j colorList.push({ id, color }) } }) specList.forEach((items)=>{ let {spec} = items spec.forEach((item)=>{ item.select = false if(selectFlag && item.status > 0) { item.select = true selectFlag = false } }) selectFlag = true }) this.setState({ selectColor, specList, colorList }) // console.log('specList',specList) // console.log('colorList',colorList) } changeState = (state,val) => { this.setState({ [state]:val }) } //获取输入框的值 getInputValue=(e)=>{ this.setState({ count:e.target.value }) } // 增加 augment=()=>{ this.setState({ count:this.state.count*1+1 }) } // 减少 reduce=()=> { this.setState({ count:this.state.count*1-1 }) } // 添加至购物袋 onCreateUserCart = (create_userCart) => { console.log('add cart') let id = idGen('cart') let user_id = "obR_j5GbxDfGlOolvSeTdZUwfpKA" let {productData} = this.props let product_id = productData.productbyid.id let {count, selectColor, specList} = this.state let specFilter = specList.filter(item=>item.color === selectColor)[0].spec.filter(item=> item.select && item.status > 0)[0] let specificationStock_id = specFilter.id let createdAt = moment().format('YYYY-MM-DD HH:mm:ss') const cartContent = { id, user_id, product_id, specificationStock_id, count, createdAt, updatedAt: "" } console.log('cartContent',cartContent) this.props.changeModalState('openSelect') create_userCart({variables:cartContent}).then((data)=>{ console.log('create_userCart data',data) let cartCount = localStorage.getItem("cartCount")*1 + count console.log('cartCount',cartCount) localStorage.setItem("cartCount",cartCount) }) } // 立即购买 buyNow = () => { console.log('buyNow') let {count, selectColor, specList} = this.state let createdAt = moment().format('YYYY-MM-DD HH:mm:ss') let id = idGen('cart') let {productData} = this.props let {id:product_id, img, intro, name, price, status, stock, unit} = productData.productbyid let specFilter = specList.filter(item=>item.color === selectColor)[0].spec.filter(item=> item.select && item.status > 0)[0] let {id:specificationStock_id, size, stock:specStock, status:specStatus} = specFilter let buyNowContent = [{ count, createdAt, id, product_id:{ id:product_id, img, intro, name, price, status, stock, unit }, specificationStock_id:{ id:specificationStock_id, color:selectColor, size, stock:specStock, status:specStatus } }] console.log('buyNowContent',buyNowContent) sessionStorage.setItem("buyNowContent",JSON.stringify(buyNowContent)) this.props.changeModalState('openSelect') this.props.history.push({ pathname: '/cart/orders', state:{ dataType: 'buyNowContent' } }) } render() { let {price, img, buttonType} = this.props let {count, selectColor, specList, colorList} = this.state let specFilter = specList.filter(item=>item.color === selectColor)[0].spec.filter(item=> item.select && item.status > 0)[0] let specStock = specFilter.stock || 0 let selectSize = specFilter.size return( this.props.changeModalState('openSelect',false)} animationType="slide-up" afterClose={() => { console.log('close model')}} >
this.props.changeModalState('openSelect',false)}> ×
商品图片
¥ {price}
已选择: {selectColor} / {selectSize}
库存 {specStock}
  • 颜色
    { colorList.map((spec)=>(
    { this.changeState('selectColor',spec.color) }} > {spec.color}
    )) }
  • item.color === selectColor)[0]} changeState={this.changeState}/>
购买数量
{this.getInputValue(e)}}/>
console.log('error',error)} > {(create_userCart,{ loading, error }) => (
)}
) } } class Specification extends Component { constructor(props) { super(props) this.state = { spec:this.props.specList.spec } } componentWillReceiveProps(nextProps, nextContext) { this.setState({ spec:nextProps.specList.spec, }) } // 改变选择 changeSelectedStatus=(i)=>{ this.setState((prevState, props) => ({ spec: prevState.spec.map((item,index)=>{ if(index===i){ item.select=true console.log('select item',item) this.props.changeState('selectSpec',item) }else { item.select=false } return item }) })) } render() { let {spec} = this.state // console.log('spec',spec) return (
  • 规格
    { spec.map((item,index)=>(
    0 && item.select })} key={'spec'+item.id} onClick={()=>{ if(item.status > 0) this.changeSelectedStatus(index) }} > {item.size}
    )) }
  • ) } }