图文问诊 {{ item.payment }} 元
{{ item.statusValue }}服务医生信息
目标:配置路由,分析问诊订单组件结构
代码:
1)分析问诊记录页面
User/ConsultOrder.vue
<script setup lang="ts">
import ConsultList from './components/ConsultList.vue'
// import { ConsultType } from '@/enums'
</script>
<template>
<div class="consult-page">
<cp-nav-bar title="问诊记录" />
<van-tabs sticky>
<van-tab title="找医生"><consult-list :type="1" /></van-tab>
<van-tab title="极速问诊"><consult-list :type="2" /></van-tab>
<van-tab title="开药问诊"><consult-list :type="3" /></van-tab>
</van-tabs>
</div>
</template>
<style lang="scss" scoped>
.consult-page {
padding-top: 46px;
background-color: var(--cp-bg);
min-height: calc(100vh - 46px);
}
</style>
2)配置路由
{
path: '/user/consult',
component: () => import('@/views/user/ConsultOrder.vue'),
meta: { title: '问诊记录' }
},
目标:定义接口参数类型和api函数
步骤:
代码:
1)定义接口参数类型 types/consult.d.ts
// 根据通用分页类型定义
export type ConsultOrderListParams = PageParams & {
type: ConsultType
}
2)订单状态枚举 enums/index.ts
(已定义)
// 问诊订单状态
export enum OrderType {
// ----------------问诊订单------------------
// 待支付
ConsultPay = 1,
// 待接诊
ConsultWait = 2,
// 咨询中
ConsultChat = 3,
// 已完成
ConsultComplete = 4,
// 已取消
ConsultCancel = 5,
// ----------------药品订单------------------
// 待支付
MedicinePay = 10,
// 待发货
MedicineSend = 11,
// 待收货
MedicineTake = 12,
// 已完成
MedicineComplete = 13,
// 已取消
MedicineCancel = 14
}
3)单个问诊订单类型 types/consult.d.ts
(已定义)
// 问诊订单单项信息
export type ConsultOrderItem = Consult & {
createTime: string
docInfo?: Doctor
patientInfo: Patient
orderNo: string
statusValue: string
typeValue: string
status: OrderType
countdown: number
prescriptionId?: string
evaluateId: number
payment: number
couponDeduction: number
pointDeduction: number
actualPayment: number
}
4)带分页问诊订单类型 types/consult.d.ts
export type ConsultOrderPage = {
pageTotal: number
total: number
rows: ConsultOrderItem[]
}
5)定义查询API函数 api/consult.ts
import type { ConsultOrderListParams, ConsultOrderPage } from '@/types/consult'
// 获取问诊订单记录列表
export const getConsultOrderList = (params: ConsultOrderListParams) =>
request.get<ConsultOrderPage>('/patient/consult/order/list', { params })
实现:获取问诊订单数据,支持上拉加载更多
1)加载数据逻辑 User/components/ConsultList.vue
2)渲染 User/components/ConsultItem.vue
{{ item.docInfo?.name || '暂未分配医生' }}
{{ item.statusValue }}
病情描述
{{ item.illnessDesc }}
价格
¥ {{ item.payment }}
创建时间
{{ item.createTime }}
取消订单
去支付
目标:了解问诊订单状态和对应业务功能
状态梳理:
待支付:取消问诊+去支付
待接诊:取消问诊+继续沟通
咨询中:查看处方(如果开了) + 继续沟通
已完成:更多菜单=> 查看处方(如果开了)+删除订单 + 问诊记录+去评价
已取消:删除订单+咨询其他医生
代码实现:
User/components/ConsultItem.vue
import { computed, ref } from 'vue'
// == 已完成订单使用 ==
// 控制更多操作显示
const showPopover = ref(false)
// 操作项
const actions = computed(() => [
{ text: '查看处方', disabled: !props.item.prescriptionId }, // 没有开处方不能查看
{ text: '删除订单' }
])
// 操作项的点击回调
const onSelect = () => {
//
}
<div class="foot" v-if="item.status === OrderType.ConsultPay">
<van-button class="gray" plain size="small" round>取消问诊van-button>
<van-button type="primary" plain size="small" round :to="`/user/consult/${item.id}`">
去支付
van-button>
div>
<div class="foot" v-if="item.status === OrderType.ConsultWait">
<van-button class="gray" plain size="small" round>取消问诊van-button>
<van-button type="primary" plain size="small" round :to="`/room?orderId=${item.id}`">
继续沟通
van-button>
div>
<div class="foot" v-if="item.status === OrderType.ConsultChat">
<van-button v-if="item.prescriptionId" class="gray" plain size="small" round>
查看处方
van-button>
<van-button type="primary" plain size="small" round :to="`/room?orderId=${item.id}`">
继续沟通
van-button>
div>
<div class="foot" v-if="item.status === OrderType.ConsultComplete">
<div class="more">
<van-popover
placement="top-start"
v-model:show="showPopover"
:actions="actions"
@select="onSelect"
>
<template #reference> 更多 template>
van-popover>
div>
<van-button class="gray" plain size="small" round :to="`/room?orderId=${item.id}`">
问诊记录
van-button>
<van-button v-if="!item.evaluateId" type="primary" plain size="small" round>
去评价
van-button>
<van-button v-else class="gray" plain size="small" round> 查看评价 van-button>
div>
<div class="foot" v-if="item.status === OrderType.ConsultCancel">
<van-button class="gray" plain size="small" round>删除订单van-button>
<van-button type="primary" plain size="small" round to="/">咨询其他医生van-button>
div>
实现:取消问诊订单功能
步骤:
代码:
1)API接口 api/consult.ts
// 取消订单
export const cancelOrder = (id: string) => request.put(`/patient/order/cancel/${id}`)
2)取消订单逻辑函数 User/components/ConsultItem.vue
import { cancelOrder } from '@/api/consult'
import { Toast } from 'vant'
// 取消订单
const loading = ref(false)
const cancelConsultOrder = async (item: ConsultOrderItem) => {
loading.value = true
try {
await cancelOrder(item.id)
// 修改订单的状态
item.status = OrderType.ConsultCancel
item.statusValue = '已取消'
Toast.success('取消成功')
} catch (e) {
Toast.fail('取消失败')
} finally {
loading.value = false
}
}
3)使用逻辑
-
取消问诊
去支付
-
取消问诊
继续沟通
实现:删除订单功能
步骤:
代码:
1)API接口 api/consult.ts
// 删除订单
export const deleteOrder = (id: string) => request.delete(`/patient/order/${id}`)
2)删除订单逻辑函数 User/components/ConsultItem.vue
import { deleteOrder } from '@/api/consult'
// 删除订单
const emit = defineEmits<{
(e: 'on-delete', id: string): void
}>()
const deleteLoading = ref(false)
const deleteConsultOrder = (item: ConsultOrderItem) => {
deleteLoading.value = true
deleteOrder(item.id)
.then(() => {
// 通知父组件更新列表
emit('on-delete', item.id)
Toast.success('删除成功')
})
.catch(() => {
Toast.fail('删除失败')
})
.finally(() => {
deleteLoading.value = false
})
}
3)使用逻辑
const onSelect = (action: { text: string }, i: number) => {
if (i === 1) {
// 删除
deleteConsultOrder(props.item)
}
}
-
删除订单
咨询其他医生
4)父组件进行删除数据 User/components/ConsultList.vue
const onDelete = (id: string) => {
list.value = list.value.filter((item) => item.id !== id)
}
实现:查看处方逻辑复用,提取一个hook函数
步骤:
代码:
1)在Room/components/RoomMessage.vue
提取hook函数 composable/index.ts
import { getPrescriptionPic } from '@/api/consult'
import { ImagePreview } from 'vant'
// 封装查看处方逻辑
export const useShowPrescription = () => {
const showPrescription = async (id?: string) => {
if (id) {
const res = await getPrescriptionPic(id)
ImagePreview([res.data.url])
}
}
return { showPrescription }
}
2)使用hook函数
Room/components/RoomMessage.vue
import { useShowPrescription } from '@/composable'
const { showPrescription } = useShowPrescription()
电子处方
+
原始处方
User/components/ConsultItem.vue
+ import { useShowPrescription } from '@/composable'
+ const { showPrescription } = useShowPrescription()
const onSelect = (action: { text: string }, i: number) => {
+ if (i === 0) {
+ showPrescription(props.item.prescriptionId)
+ }
if (i === 1) {
// 删除
deleteConsultOrder(props.item)
}
}
小结:
目标:配置详情路由,分析组件结构,渲染详情数据
代码:
1)分析结构和路由配置
{
path: '/user/consult/:id',
component: () => import('@/views/user/ConsultDetail.vue'),
meta: { title: '问诊详情' }
}
2)骨架效果
<div class="consult-detail-page" v-if="item">
// ...
div>
<div class="consult-detail-page" v-else>
<cp-nav-bar title="问诊详情" />
<van-skeleton title :row="4" style="margin-top: 30px" />
<van-skeleton title :row="4" style="margin-top: 30px" />
div>
3)基本渲染
说明❓:api接口已定义
图文问诊 {{ item.payment }} 元
{{ item.statusValue }}
服务医生信息
极速问诊
{{ item.docInfo?.name }}
订单信息
复制
{{ item.orderNo }}
实现:使用useClipboard复制订单号
步骤:
代码:
参考代码-api
1. copy(拷贝函数)
2. copied 是否拷贝成功,默认1.5s恢复状态
3. isSupported 浏览器是否支持,需要授权读取粘贴板和写入粘贴板权限
实现逻辑 User/ConsultDetail.vue
import { useClipboard } from '@vueuse/core'
import { Toast } from 'vant'
const { copy, copied, isSupported } = useClipboard()
// 1. 复制回调
const onCopy = () => {
if (!isSupported.value) Toast('未授权,不支持')
copy(item.value?.orderNo || '')
}
// 2. 复制后提示
watch(copied, () => {
if (copied.value) Toast('已复制')
})
+ 复制
{{ item.orderNo }}
实现:封装可复用的支付抽屉组件
思考❓:抽离Consult/ConsultPay.vue
中支付抽屉部分
需求:
代码:
1)封装组件 components/CpPaySheet.vue
¥{{ actualPayment.toFixed(2) }}
立即支付
import CpNavBar from '@/components/CpNavBar.vue'
import CpIcon from '@/components/CpIcon.vue'
import CpRadioBtn from '@/components/CpRadioBtn.vue'
+import CpPaySheet from '@/components/CpPaySheet.vue'
import { RouterLink, RouterView } from 'vue-router'
declare module 'vue' {
interface GlobalComponents {
CpNavBar: typeof CpNavBar
CpIcon: typeof CpIcon
CpRadioBtn: typeof CpRadioBtn
+ CpPaySheet: typeof CpPaySheet
}
}
2)使用组件
问诊支付Consult/ConsultPay.vue
<cp-pay-sheet
v-model:show="show"
:order-id="orderId"
:actualPayment="payInfo.actualPayment"
:onClose="onClose"
/>
问诊订单页支付User/components/ConsultItem.vue
// 4. 支付
const show = ref(false)
...
+
注意❓:
父传子订单ID:子组件解构orderId失去响应式解决
vite.config.ts
export default defineConfig({
plugins: [
vue({
+ reactivityTransform: true
}),
Components({
dts: false, // 关闭自动生成类型声明文件
resolvers: [VantResolver()]
}),
createSvgIconsPlugin({
// 指定图标文件夹,绝对路径(NODE代码)
iconDirs: [path.resolve(process.cwd(), 'src/icons')]
})
],
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url))
}
}
})
pnpm dev