Skip to content
This repository has been archived by the owner on Jul 15, 2019. It is now read-only.

Commit

Permalink
update post a new order
Browse files Browse the repository at this point in the history
  • Loading branch information
DevilTea committed Jan 7, 2019
1 parent fbfa28b commit de20e1a
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 54 deletions.
17 changes: 13 additions & 4 deletions components/cart/cart-list.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
:data="cartData"
highlight-current-row
class="cart-table"
@current-change="currentChange"
>
<el-table-column
prop="name"
Expand All @@ -26,11 +27,11 @@
prop="quantity"
label="數量"
/>
<el-table-column label="總計">
<!-- <el-table-column label="總計">
<template slot-scope="scope">
{{ scope.row.price * scope.row.quantity + scope.row.shippingFee }}
</template>
</el-table-column>
</el-table-column> -->
<el-table-column
prop="action"
label="操作"
Expand All @@ -39,7 +40,7 @@
<el-button
size="mini"
type="danger"
@click.stop="$emit('delete', scope.$index)"
@click.stop="del(scope.$index)"
>刪除</el-button>
</template>
</el-table-column>
Expand All @@ -54,7 +55,15 @@ export default {
default: () => []
}
},
methods: {}
methods: {
del(index) {
this.$refs.singleTable.setCurrentRow()
this.$emit('delete', index)
},
currentChange(currentRow) {
this.$emit('choose', currentRow ? currentRow.index : -1)
}
}
}
</script>

Expand Down
9 changes: 7 additions & 2 deletions components/good-detail/detail.vue
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
@click="addToCart"
>
<v-icon dark>shopping_cart</v-icon>
加入購物車
{{ detail.goodInfo.state === 0 ? '加入購物車' : detail.goodInfo.state === 1 ? '已遭下架' : detail.goodInfo.state === 2 ? '已遭移除' : '???' }}
</el-button>
</v-layout>
</el-col>
Expand Down Expand Up @@ -221,11 +221,16 @@ export default {
console.log(e)
}
try {
let vm = this
let { data } = await this.$axios.patch('/cart', { goods: cartGoods })
this.$notify({
title: '成功',
message: '成功添加到購物車',
type: 'success'
type: 'success',
onClick: function() {
vm.$router.push('/cart')
this.close()
}
})
} catch (e) {
console.log(e)
Expand Down
94 changes: 46 additions & 48 deletions pages/cart.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,34 @@
<v-container class="m-cart">
<h2 class="title">請選一項訂單提交</h2>
<v-layout v-if="1||cart.length">
<char-list
<cart-list
ref="cartList"
:cart-data="cart"
@delete="deleteCartItem($event)" />
@delete="deleteCartItem"
@choose="choose" />
</v-layout>
<v-layout
justify-end
class="total-price"
>
<p>應付金額:<strong class="money">${{ total }}</strong></p>
<p>總計:<strong class="money">${{ total }}</strong></p>
</v-layout>
<v-layout
justify-end
class="submit"
>
<el-button
:disabled="currentChoise === -1"
type="primary"
class="blue"
@click="submit"
>提交訂單</el-button>
</v-layout>
</v-container>

</template>

<script>
import CharList from '../components/cart/cart-list.vue'
import CartList from '../components/cart/cart-list.vue'
export default {
async asyncData({ $axios, error }) {
let rawCart = []
Expand All @@ -48,6 +51,7 @@ export default {
response = await $axios.get(`/payments/${rawCart[i].payment_id}`)
let payment = response.data
cart.push({
index: cart.length,
id: rawCart[i].id,
name: good.name,
price: good.price,
Expand All @@ -67,67 +71,61 @@ export default {
},
middleware: ['checkUserLogin', 'checkUserRegister'],
components: {
CharList
CartList
},
data() {
return {
currentChoise: -1
}
},
computed: {
total() {
let total = 0
this.cart.forEach(item => {
if (this.currentChoise !== -1) {
let item = this.cart[this.currentChoise]
total += item.price * item.quantity + item.shippingFee
})
}
return total
}
},
methods: {
async deleteCartItem(index) {
this.rawCart.splice(index, 1)
this.cart.splice(index, 1)
let { data } = await this.$axios.patch('/cart', { goods: this.rawCart })
this.$notify({
title: '成功',
message: '成功從購物車移除',
type: 'success'
})
choose(row) {
this.currentChoise = row
},
async updateCartItem() {
this.rawCart = []
this.cart = []
async deleteCartItem(index) {
try {
let { data } = await this.$axios.get('/cart')
this.rawCart = data.goods
} catch (e) {
this.rawCart.splice(index, 1)
this.cart.splice(index, 1)
let { data } = await this.$axios.patch('/cart', { goods: this.rawCart })
this.$notify({
title: '成功',
message: '成功從購物車移除',
type: 'success'
})
} catch(e) {
console.log(e)
}
for (let i in this.rawCart) {
try {
let response = undefined
response = await this.$axios.get(`/goods/${this.rawCart[i].id}`)
let good = response.data
response = await this.$axios.get(`/shippings/${this.rawCart[i].shipping_id}`)
let shipping = response.data
response = await this.$axios.get(`/payments/${this.rawCart[i].payment_id}`)
let payment = response.data
this.cart.push({
id: this.rawCart[i].id,
name: good.name,
price: good.price,
quantity: this.rawCart[i].quantity,
shippingId: shipping.id,
shippingService: shipping.service,
shippingFee: shipping.fee,
shippingInfo: `${shipping.service}: $${shipping.fee}`,
paymentId: payment.id,
paymentService: payment.service
})
} catch (e) {
console.log(e)
}
},
async submit() {
let data = {
good: {
id: this.cart[this.currentChoise].id,
quantity: this.cart[this.currentChoise].quantity
},
shippingId: this.cart[this.currentChoise].shippingId,
paymentId: this.cart[this.currentChoise].paymentId,
}
try {
let orderId = await this.$axios.post('/orders', data)
} catch(e) {
console.log(e)
}
this.$notify({
title: '成功',
message: '成功送出訂單',
type: 'success'
})
await this.$refs.cartList.del(this.currentChoise)
}
}
}
Expand Down

0 comments on commit de20e1a

Please sign in to comment.