11-订单管理-取消订单
目的:完成取消订单操作
大致步骤:
- 准备取消原因的常量数据
- 准备取消订单的API接口函数
- 订单列表准备一个取消订单组件
- 提取取消订单组件
- 取消订单逻辑封装
- 单条订单组件触发取消订单事件
- 立即付款,查看详情,给上链接即可。
src/api/order.js
/**
* 取消订单
* @param {String} orderId - 订单ID
* @param {String} cancelReason - 取消原因
* @returns Promise
*/
export const cancelOrder = (orderId, cancelReason) => {
return request(`/member/order/${orderId}/cancel`, 'put', { cancelReason })
}
src/api/constants.js
export const cancelReason = [
'配送信息有误',
'商品买错了',
'重复下单/误下单',
'忘记使用优惠券、兔币等',
'其他渠道价格更低',
'不想买了'
]
views/member/order/components/order-cancel.vue
<template>
<XtxDialog title="取消订单" v-model="visibleDialog">
<!-- 组件内容 -->
<div class="cancel-info">
<p>取消订单后,本单享有的优惠可能会一并取消,是否继续?</p>
<p class="tip">请选择取消订单的原因(必选):</p>
<div class="btn">
<a
@click="curText = item"
v-for="item in cancelReason"
:key="item"
href="javascript:;"
:class="{ active: curText === item }"
>
{{ item }}
</a>
</div>
</div>
<!-- 按钮操作 -->
<template #footer>
<XtxButton type="gray" @click="visibleDialog=false" style="margin-right:20px">取消</XtxButton>
<XtxButton type="primary" @click="submit">确认</XtxButton>
</template>
</XtxDialog>
</template>
<script>
import { ref } from 'vue'
import { cancelReason } from '@/api/constants'
import Message from '@/components/library/Message'
import { cancelOrder } from '@/api/order'
export default {
name: 'OrderCancel',
setup () {
const visibleDialog = ref(false)
// 打开对话框,提供给其他组件调用
const order = ref(null)
const open = (item) => {
visibleDialog.value = true
// 记录订单ID
order.value = item
// 清除之前选中原因
curText.value = ''
}
// 选中的取消原因
const curText = ref('')
// 确认函数
const submit = () => {
// 1. 根据订单ID和取消原因发请求
if (!curText.value) return Message({ text: '请选择取消订单的原因' })
cancelOrder(order.value.id, curText.value).then(() => {
// 取消订单成功
Message({ text: '取消订单成功', type: 'success' })
// 更新订单状态
order.value.orderState = 6
// 关闭对话框
visibleDialog.value = false
})
}
return { visibleDialog, submit, cancelReason, curText, open }
}
}
</script>
<style scoped lang="less">
.xtx-dialog ::v-deep .wrapper {
width: 620px;
}
.cancel-info {
p {
font-size: 16px;
line-height: 35px;
&.tip {
color: #999;
}
}
.btn {
padding-top: 21px;
display: flex;
flex-wrap: wrap;
a {
width: 256px;
height: 45px;
line-height: 45px;
text-align: center;
background-color: #ffffff;
border: 1px solid #e4e4e4;
margin-right: 20px;
margin-bottom: 20px;
color: #666;
&:nth-child(2n){
margin-right: 0;
}
&:hover,&.active {
background-color: #e3f9f4;
border-color: @xtxColor;
}
}
}
}
</style>
views/member/order/index.vue
封装取消订单逻辑,使用取消订单组件,利用teleport组件将结构放在#app容器外
+ // setup中的return
return {
activeName,
orderStatus,
tabClick,
requestParams,
orderList,
loading,
total,
changePager,
+ ...useCancelOrder()
}
// 封装逻辑-取消订单
const useCancelOrder = () => {
const orderCancelCom = ref(null)
const onCancelOrder = (item) => {
// item 就是你要取消的订单
orderCancelCom.value.open(item)
}
return { onCancelOrder, orderCancelCom }
}
<!-- 对于像:对话框,消息提示,确认提示,通知组件 适合使用传送门 Teleport -->
<Teleport to="#dailog">
<OrderCancel ref="orderCancelCom" />
</Teleport>
<OrderItem
v-for="item in orderList"
:key="item.id"
:order="item"
+ @on-cancel-order="onCancelOrder(item)"
/>
public/index.html
<div id="app"></div>
<div id="dailog"></div>
views/member/order/components/order-item.vue
触发自定义事件,申明自定义事件
<!-- 待支付:立即付款,查看详情,取消订单 -->
<!-- 待发货:查看详情,再次购买 -->
<!-- 待收货:确认收货,查看详情,再次购买 -->
<!-- 待评价:查看详情,再次购买,申请售后 -->
<!-- 已完成:查看详情,再次购买,申请售后 -->
<!-- 已取消:查看详情 -->
+ <XtxButton @click="$router.push(`/member/pay?orderId=${order.id}`)" v-if="order.orderState===1" type="primary" size="small">立即付款</XtxButton>
<XtxButton v-if="order.orderState===3" type="primary" size="small">确认收货</XtxButton>
+ <p><a @click="$router.push(`/member/order/${order.id}`)" href="javascript:;">查看详情</a></p>
+ <p @click="$emit('on-cancel-order')" v-if="order.orderState===1"><a href="javascript:;">取消订单</a></p>
<p v-if="[2,3,4,5].includes(order.orderState)"><a href="javascript:;">再次购买</a></p>
<p v-if="[4,5].includes(order.orderState)"><a href="javascript:;">申请售后</a></p>
// 组件本身触发的自定义事件可以在这里申明
emits: ['on-cancel-order'],
12-订单管理-删除订单
目的:完成删除订单操作
大致步骤:
order-item
组件触发自定义时间通知父组件- 再父组件实现删除逻辑,需要提前定义API函数
src/api/order.js
/**
* 删除订单
* @param {Array<string>} ids - 删除订单,id集合
* @returns
*/
export const delteOrder = (ids) => {
return request('/member/order', 'delete', { ids })
}
src/views/member/order/components/order-item.vue
</span>
<!-- 已完成 已取消 -->
+ <a @click="$emit('on-delete-order')" v-if="[5,6].includes(order.orderState)" href="javascript:;" class="del">删除</a>
</div>
// 组件本身触发的自定义事件可以在这里申明
+ emits: ['on-cancel-order', 'on-delete-order'],
src/views/member/order/index.vue
<OrderItem
v-for="item in orderList"
:key="item.id"
:order="item"
@on-cancel-order="onCancelOrder(item)"
+ @on-delete-order="onDeleteOrder(item)"
/>
import { reactive, ref, watch } from 'vue'
import { orderStatus } from '@/api/constants'
import { delteOrder, findOrderList } from '@/api/order'
import OrderItem from './components/order-item'
import OrderCancel from './components/order-cancel'
import Confirm from '@/components/library/Confirm'
import Message from '@/components/library/Message'
export default {
name: 'MemberOrderPage',
components: {
OrderItem,
OrderCancel
},
setup () {
const activeName = ref('all')
const tabClick = (tab) => {
// 此时:tab.index 就是订单的状态
requestParams.orderState = tab.index
requestParams.page = 1
}
// 筛选条件
const requestParams = reactive({
page: 1,
pageSize: 5,
orderState: 0
})
// 发请求获取数据
const orderList = ref([])
const loading = ref(false)
const total = ref(0)
// 使用侦听器,监听 requestParams 的改变
+ const findOrderListFn = () => {
+ loading.value = true
+ findOrderList(requestParams).then(data => {
+ orderList.value = data.result.items
+ total.value = data.result.counts
+ loading.value = false
+ })
+ }
watch(requestParams, () => {
+ findOrderListFn()
}, { immediate: true })
// 分页事件
const changePager = (np) => {
requestParams.page = np
}
+ // 删除订单
+ const onDeleteOrder = (item) => {
+ Confirm({ text: '您确认删除该条订单吗?' }).then(() => {
+ delteOrder([item.id]).then(() => {
+ Message({ text: '删除订单成功', type: 'success' })
+ findOrderListFn()
+ })
+ }).catch(e => {})
+ }
return {
activeName,
orderStatus,
tabClick,
requestParams,
orderList,
loading,
total,
changePager,
+ onDeleteOrder,
...useCancelOrder()
}
}
}
13-订单管理-确认收货
温馨提示:
- 接口服务器地址 + member/order/consignment/ + 订单ID
- 改成已发货状态,后面订单号改成,自己的订单编号。
http://pcapi-xiaotuxian-front-devtest.itheima.net//member/order/consignment/1394116427400613890
大致步骤:
- 定义确认收货API接口
- 完成确认收货业务逻辑
落的代码:
src/views/member/order/index.vue
// 封装逻辑-确认收货
const useConfirmOrder = () => {
const onConfirmOrder = (item) => {
// item 就是你要确认收货的订单
Confirm({ text: '您确认收到货吗?确认后货款将会打给卖家。' }).then(() => {
confirmOrder(item.id).then(() => {
Message({ text: '确认收货成功', type: 'success' })
// 确认收货后状态变成 待评价
item.orderState = 4
})
})
}
return { onConfirmOrder }
}
return {
activeName,
orderStatus,
tabClick,
requestParams,
orderList,
loading,
total,
changePager,
onDeleteOrder,
...useCancelOrder(),
+ ...useConfirmOrder()
}
<OrderItem
v-for="item in orderList"
:key="item.id"
:order="item"
@on-cancel-order="onCancelOrder(item)"
@on-delete-order="onDeleteOrder(item)"
+ @on-confirm-order="onConfirmOrder(item)"
/>
src/views/member/order/components/order-item.vue
<p @click="$emit('on-cancel-order')" v-if="order.orderState===1"><a href="javascript:;">取消订单</a></p>
// 组件本身触发的自定义事件可以在这里申明
emits: ['on-cancel-order', 'on-delete-order', 'on-confirm-order'],