|
@@ -10,8 +10,11 @@ import {graphqls} from '../../api/graphql_request'
|
|
|
import {getProductById} from '../../api/graphql/product'
|
|
import {getProductById} from '../../api/graphql/product'
|
|
|
import {getAddressByProps} from '../../api/graphql/address'
|
|
import {getAddressByProps} from '../../api/graphql/address'
|
|
|
import {createOrders,createOrderProducts} from '../../api/graphql/order'
|
|
import {createOrders,createOrderProducts} from '../../api/graphql/order'
|
|
|
|
|
+import {getShopCarByProps,deleteCarByProps} from '../../api/graphql/shopCar'
|
|
|
|
|
|
|
|
|
|
+import {successToast,failToast} from '../Toast/Toast'
|
|
|
import './OrederPage.css'
|
|
import './OrederPage.css'
|
|
|
|
|
+import { concatSeries } from 'async';
|
|
|
|
|
|
|
|
class OrderPage extends React.Component{
|
|
class OrderPage extends React.Component{
|
|
|
constructor(props){
|
|
constructor(props){
|
|
@@ -20,13 +23,16 @@ class OrderPage extends React.Component{
|
|
|
this.state={
|
|
this.state={
|
|
|
num:props.num,
|
|
num:props.num,
|
|
|
address:{},
|
|
address:{},
|
|
|
- product:{}
|
|
|
|
|
|
|
+ products:[],
|
|
|
|
|
+ shopCarList:[],
|
|
|
|
|
+ sumPrice:''
|
|
|
}
|
|
}
|
|
|
//this.toPay=this.toPay.bind(this)
|
|
//this.toPay=this.toPay.bind(this)
|
|
|
this.createOrder=this.createOrder.bind(this)
|
|
this.createOrder=this.createOrder.bind(this)
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
- //获取默认地址
|
|
|
|
|
|
|
+ //获取数据
|
|
|
|
|
+ ///获取默认地址
|
|
|
getAddress(){
|
|
getAddress(){
|
|
|
let user_id=sessionStorage.getItem('openid')
|
|
let user_id=sessionStorage.getItem('openid')
|
|
|
console.log('user_id',user_id)
|
|
console.log('user_id',user_id)
|
|
@@ -39,10 +45,40 @@ class OrderPage extends React.Component{
|
|
|
|
|
|
|
|
componentDidMount(){
|
|
componentDidMount(){
|
|
|
this.getAddress()
|
|
this.getAddress()
|
|
|
- this.props.product.then((e)=>{
|
|
|
|
|
- //console.log(e.productbyid)
|
|
|
|
|
|
|
+ if(this.props.fromShopCar){
|
|
|
|
|
+ this.getCarByProps()
|
|
|
|
|
+ }else{
|
|
|
|
|
+ this.props.getProductByIds.then((e)=>{
|
|
|
|
|
+ console.log('getProductByIds',e.productbyid)
|
|
|
|
|
+ this.setState({
|
|
|
|
|
+ products:[e.productbyid],
|
|
|
|
|
+ sumPrice:this.props.num*e.productbyid.price
|
|
|
|
|
+ })
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ componentWillUnmount(){
|
|
|
|
|
+ this.props.setFromShopCar(false)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ getCarByProps(){
|
|
|
|
|
+ const user_id=sessionStorage.getItem('openid')
|
|
|
|
|
+ graphqls(getShopCarByProps,{user_id}).then((response)=>{
|
|
|
|
|
+ let sumPrice=0,num=0;
|
|
|
|
|
+ //如果請求失敗,默认空数组
|
|
|
|
|
+
|
|
|
|
|
+ (response.userCartbyprops||[]).map((item)=>{
|
|
|
|
|
+ sumPrice+=item.count*item.product_id.price
|
|
|
|
|
+ num+=item.count
|
|
|
|
|
+ return 1
|
|
|
|
|
+ })
|
|
|
|
|
+ // console.log('sumPrice',sumPrice,'shopCarCount',shopCarCount)
|
|
|
|
|
+ // console.log('getShopCarByProps',response.userCartbyprops)
|
|
|
this.setState({
|
|
this.setState({
|
|
|
- product:e.productbyid
|
|
|
|
|
|
|
+ shopCarList:response.userCartbyprops,
|
|
|
|
|
+ sumPrice,
|
|
|
|
|
+ num
|
|
|
})
|
|
})
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|
|
@@ -80,12 +116,13 @@ class OrderPage extends React.Component{
|
|
|
graphqls(createOrders,orderData).then((e) => {
|
|
graphqls(createOrders,orderData).then((e) => {
|
|
|
console.log('order创建订单成功', e)
|
|
console.log('order创建订单成功', e)
|
|
|
that.createProductOrder(id)
|
|
that.createProductOrder(id)
|
|
|
|
|
+
|
|
|
})
|
|
})
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
createProductOrder(order_id){
|
|
createProductOrder(order_id){
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
let that=this,openid=sessionStorage.getItem('openid')
|
|
let that=this,openid=sessionStorage.getItem('openid')
|
|
|
let data={
|
|
let data={
|
|
|
"count": that.state.num,
|
|
"count": that.state.num,
|
|
@@ -102,36 +139,47 @@ class OrderPage extends React.Component{
|
|
|
}
|
|
}
|
|
|
graphqls(createOrderProducts,data).then((e)=>{
|
|
graphqls(createOrderProducts,data).then((e)=>{
|
|
|
console.log('orderProduct创建成功',e)
|
|
console.log('orderProduct创建成功',e)
|
|
|
|
|
+ successToast('创建订单成功',1)
|
|
|
|
|
+ that.props.changePage('orderCenter')
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- toPay(){
|
|
|
|
|
- this.createOrder()
|
|
|
|
|
|
|
+ // toPay(){
|
|
|
|
|
+ // this.createOrder()
|
|
|
|
|
+ // }
|
|
|
|
|
+ renderCardList(){
|
|
|
|
|
+ if(this.props.fromShopCar){
|
|
|
|
|
+ return(
|
|
|
|
|
+ this.state.shopCarList.map((item)=>{
|
|
|
|
|
+ return(<OrderCard product={item} key={item.id}/>)
|
|
|
|
|
+ })
|
|
|
|
|
+ )
|
|
|
|
|
+ }else{
|
|
|
|
|
+ return(
|
|
|
|
|
+ this.state.products.map((item)=>{
|
|
|
|
|
+ return(<OrderCard product={item} key={item.id}/>)
|
|
|
|
|
+ })
|
|
|
|
|
+ )
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
render(){
|
|
render(){
|
|
|
const address=this.state.address
|
|
const address=this.state.address
|
|
|
const product=this.state.product
|
|
const product=this.state.product
|
|
|
const num =this.state.num
|
|
const num =this.state.num
|
|
|
return(
|
|
return(
|
|
|
<div className="orderWrap">
|
|
<div className="orderWrap">
|
|
|
- <PageContext.Consumer>
|
|
|
|
|
- {({changePage,triggerAddress})=>{
|
|
|
|
|
- return(
|
|
|
|
|
- <div className="addressWrap" onClick={()=>{
|
|
|
|
|
- changePage('addressPage')
|
|
|
|
|
- triggerAddress(true)
|
|
|
|
|
- }}>
|
|
|
|
|
- <span></span>
|
|
|
|
|
- <span>{address.username+address.telephone+address.province+address.city+address.area+address.address}</span>
|
|
|
|
|
- <span></span>
|
|
|
|
|
- </div>
|
|
|
|
|
- )
|
|
|
|
|
- }}
|
|
|
|
|
- </PageContext.Consumer>
|
|
|
|
|
-
|
|
|
|
|
- <OrderCard state={{...this.state}}/>
|
|
|
|
|
|
|
+ <div className="addressWrap" onClick={()=>{
|
|
|
|
|
+ this.props.changePage('addressPage')
|
|
|
|
|
+ this.props.triggerAddress(true)
|
|
|
|
|
+ }}>
|
|
|
|
|
+ <span></span>
|
|
|
|
|
+ <span>{address.username+address.telephone+address.province+address.city+address.area+address.address}</span>
|
|
|
|
|
+ <span></span>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ <div className="cardList">{this.renderCardList()}</div>
|
|
|
<div className="payResult">
|
|
<div className="payResult">
|
|
|
- <div>共计{num}件商品;合计 ¥ {num*product.price}</div>
|
|
|
|
|
|
|
+ <div>共计{num}件商品;合计 ¥ {this.state.sumPrice}</div>
|
|
|
<div onClick={this.createOrder}>立即支付</div>
|
|
<div onClick={this.createOrder}>立即支付</div>
|
|
|
</div>
|
|
</div>
|
|
|
</div>
|
|
</div>
|
|
@@ -139,5 +187,9 @@ class OrderPage extends React.Component{
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-export default OrderPage
|
|
|
|
|
|
|
+export default props => (
|
|
|
|
|
+ <PageContext.Consumer>
|
|
|
|
|
+ {(obj)=> <OrderPage {...props} {...obj}/>}
|
|
|
|
|
+ </PageContext.Consumer>
|
|
|
|
|
+);
|
|
|
|
|
|