code review:优化示例订单的实现

(cherry picked from commit 26cf4ca64a)
This commit is contained in:
YunaiV 2023-08-11 00:02:52 +08:00 committed by shizhong
parent db1e2beaf0
commit 3af92d076e
3 changed files with 205 additions and 371 deletions

View File

@ -116,7 +116,7 @@
<script lang="ts" setup> <script lang="ts" setup>
import QrcodeVue from 'qrcode.vue' import QrcodeVue from 'qrcode.vue'
import { getOrder, submitOrder } from '@/api/pay/order' import * as PayOrderApi from '@/api/pay/order'
import { PayChannelEnum, PayDisplayModeEnum, PayOrderStatusEnum } from '@/utils/constants' import { PayChannelEnum, PayDisplayModeEnum, PayOrderStatusEnum } from '@/utils/constants'
import { formatDate } from '@/utils/formatTime' import { formatDate } from '@/utils/formatTime'
import { useTagsViewStore } from '@/store/modules/tagsView' import { useTagsViewStore } from '@/store/modules/tagsView'
@ -224,14 +224,15 @@ const barCode = ref({
}) })
/** 获得支付信息 */ /** 获得支付信息 */
const getDetail = () => { const getDetail = async () => {
// 1.1 // 1.1
if (!id.value) { if (!id.value) {
message.error('未传递支付单号,无法查看对应的支付信息') message.error('未传递支付单号,无法查看对应的支付信息')
goReturnUrl('cancel') goReturnUrl('cancel')
return return
} }
getOrder(id.value).then((data) => { const data = await PayOrderApi.getOrder(id.value)
payOrder.value = data
// 1.2 // 1.2
if (!data) { if (!data) {
message.error('支付订单不存在,请检查!') message.error('支付订单不存在,请检查!')
@ -248,10 +249,6 @@ const getDetail = () => {
goReturnUrl('close') goReturnUrl('close')
return return
} }
// 2.
payOrder.value = data
})
} }
/** 提交支付 */ /** 提交支付 */
@ -290,15 +287,16 @@ const submit = (channelCode) => {
submit0(channelCode) submit0(channelCode)
} }
const submit0 = (channelCode) => { const submit0 = async (channelCode) => {
submitLoading.value = true submitLoading.value = true
submitOrder({ try {
const formData = {
id: id.value, id: id.value,
channelCode: channelCode, channelCode: channelCode,
returnUrl: location.href, // {@link returnUrl} returnUrl: location.href, // {@link returnUrl}
...buildSubmitParam(channelCode) ...buildSubmitParam(channelCode)
}) }
.then((data) => { const data = await PayOrderApi.submitOrder(formData)
// //
if (data.status === PayOrderStatusEnum.SUCCESS.status) { if (data.status === PayOrderStatusEnum.SUCCESS.status) {
clearQueryInterval() clearQueryInterval()
@ -318,10 +316,9 @@ const submit0 = (channelCode) => {
// //
createQueryInterval() createQueryInterval()
}) } finally {
.catch(() => {
submitLoading.value = false submitLoading.value = false
}) }
} }
/** 构建提交支付的额外参数 */ /** 构建提交支付的额外参数 */
@ -385,8 +382,8 @@ const createQueryInterval = () => {
if (interval.value) { if (interval.value) {
return return
} }
interval.value = setInterval(() => { interval.value = setInterval(async () => {
getOrder(id.value).then((data) => { const data = await PayOrderApi.getOrder(id.value)
// //
if (data.status === PayOrderStatusEnum.SUCCESS.status) { if (data.status === PayOrderStatusEnum.SUCCESS.status) {
clearQueryInterval() clearQueryInterval()
@ -399,7 +396,6 @@ const createQueryInterval = () => {
message.error('支付已关闭!') message.error('支付已关闭!')
goReturnUrl('close') goReturnUrl('close')
} }
})
}, 1000 * 2) }, 1000 * 2)
} }

View File

@ -2,11 +2,12 @@
<!-- 操作工具栏 --> <!-- 操作工具栏 -->
<el-row :gutter="10" class="mb8"> <el-row :gutter="10" class="mb8">
<el-col :span="1.5"> <el-col :span="1.5">
<el-button type="primary" plain @click="handleAdd"><Icon icon="ep:plus" />发起订单</el-button> <el-button type="primary" plain @click="openForm"><Icon icon="ep:plus" />发起订单</el-button>
</el-col> </el-col>
</el-row> </el-row>
<!-- 列表 --> <!-- 列表 -->
<ContentWrap>
<el-table v-loading="loading" :data="list"> <el-table v-loading="loading" :data="list">
<el-table-column label="订单编号" align="center" prop="id" /> <el-table-column label="订单编号" align="center" prop="id" />
<el-table-column label="用户编号" align="center" prop="userId" /> <el-table-column label="用户编号" align="center" prop="userId" />
@ -49,93 +50,115 @@
</el-table-column> </el-table-column>
<el-table-column label="操作" align="center" class-name="small-padding fixed-width"> <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
<template #default="scope"> <template #default="scope">
<el-button link type="primary" @click="handlePay(scope.row)" v-if="!scope.row.payStatus">
前往支付
</el-button>
<el-button <el-button
size="small" link
type="text" type="danger"
icon="el-icon-edit"
@click="handlePay(scope.row)"
v-if="!scope.row.payStatus"
>前往支付</el-button
>
<el-button
size="small"
type="text"
icon="el-icon-delete"
@click="handleRefund(scope.row)" @click="handleRefund(scope.row)"
v-if="scope.row.payStatus && !scope.row.payRefundId" v-if="scope.row.payStatus && !scope.row.payRefundId"
>发起退款</el-button
> >
发起退款
</el-button>
</template> </template>
</el-table-column> </el-table-column>
</el-table> </el-table>
<!-- 分页组件 --> <!-- 分页组件 -->
<pagination <Pagination
v-show="total > 0"
:total="total" :total="total"
v-model:page="queryParams.pageNo" v-model:page="queryParams.pageNo"
v-model:limit="queryParams.pageSize" v-model:limit="queryParams.pageSize"
@pagination="getList" @pagination="getList"
/> />
</ContentWrap>
<!-- 对话框(添加 / 修改) --> <!-- 对话框(添加 / 修改) -->
<el-dialog :title="title" v-model="open" width="500px" append-to-body destroy-on-close> <Dialog title="发起订单" v-model="dialogVisible" width="500px">
<el-form ref="formRef" :model="form" :rules="rules" label-width="80px"> <el-form
ref="formRef"
v-loading="formLoading"
:model="formData"
:rules="formRules"
label-width="80px"
>
<el-form-item label="商品" prop="spuId"> <el-form-item label="商品" prop="spuId">
<el-select <el-select
v-model="form.spuId" v-model="formData.spuId"
placeholder="请输入下单商品" placeholder="请输入下单商品"
clearable clearable
size="small"
style="width: 380px" style="width: 380px"
> >
<el-option v-for="item in spus" :key="item.id" :label="item.name" :value="item.id"> <el-option v-for="item in spus" :key="item.id" :label="item.name" :value="item.id">
<span style="float: left">{{ item.name }}</span> <span style="float: left">{{ item.name }}</span>
<span style="float: right; color: #8492a6; font-size: 13px" <span style="float: right; color: #8492a6; font-size: 13px">
>{{ (item.price / 100.0).toFixed(2) }}</span {{ (item.price / 100.0).toFixed(2) }}
> </span>
</el-option> </el-option>
</el-select> </el-select>
</el-form-item> </el-form-item>
</el-form> </el-form>
<template #footer> <template #footer>
<el-button type="primary" @click="submitForm"> </el-button> <el-button :disabled="formLoading" type="primary" @click="submitForm"> </el-button>
<el-button @click="cancel"> </el-button> <el-button @click="dialogVisible = false"> </el-button>
</template> </template>
</el-dialog> </Dialog>
</template> </template>
<script lang="ts" setup name="PayDemoOrder"> <script lang="ts" setup name="PayDemoOrder">
import { createDemoOrder, getDemoOrderPage, refundDemoOrder } from '@/api/pay/demo' import * as PayDemoApi from '@/api/pay/demo'
import { ref, onMounted } from 'vue'
import { dateFormatter, formatDate } from '@/utils/formatTime' import { dateFormatter, formatDate } from '@/utils/formatTime'
import { DICT_TYPE } from '@/utils/dict' import { DICT_TYPE } from '@/utils/dict'
const { t } = useI18n() //
const router = useRouter() // const router = useRouter() //
const message = useMessage() // const message = useMessage() //
// const loading = ref(true) //
const loading = ref(true) const total = ref(0) //
// const list = ref([]) //
const total = ref(0)
//
const list = ref([])
//
const title = ref('')
//
const open = ref(false)
//
const queryParams = ref({ const queryParams = ref({
pageNo: 1, pageNo: 1,
pageSize: 10 pageSize: 10
}) })
// const formRef = ref()
const form = ref({})
// /** 查询列表 */
const rules = { const getList = async () => {
spuId: [{ required: true, message: '商品编号不能为空', trigger: 'blur' }] loading.value = true
try {
const data = await PayDemoApi.getDemoOrderPage(queryParams)
list.value = data.list
total.value = data.total
} finally {
loading.value = false
}
} }
/** 支付按钮操作 */
const handlePay = (row) => {
router.push({
name: 'PayCashier',
query: {
id: row.payOrderId,
returnUrl: encodeURIComponent('/pay/demo-order?id=' + row.id)
}
})
}
/** 退款按钮操作 */
const handleRefund = async (row: any) => {
const id = row.id
try {
await message.confirm('是否确认退款编号为"' + id + '"的示例订单?')
await PayDemoApi.refundDemoOrder(id)
await getList()
message.success('发起退款成功!')
} catch {}
}
// ========== ==========
// //
const spus = ref([ const spus = ref([
{ {
@ -165,77 +188,45 @@ const spus = ref([
} }
]) ])
const formRef = ref() const dialogVisible = ref(false) //
const formLoading = ref(false) //
/** 查询列表 */ const formData = ref({}) //
const getList = () => { const formRules = {
loading.value = true spuId: [{ required: true, message: '商品编号不能为空', trigger: 'blur' }]
//
getDemoOrderPage(queryParams.value).then((data) => {
list.value = data.list
total.value = data.total
loading.value = false
})
}
/** 取消按钮 */
const cancel = () => {
open.value = false
reset()
} }
/** 表单重置 */ /** 表单重置 */
const reset = () => { const reset = () => {
form.value = { formData.value = {
spuId: undefined spuId: undefined
} }
formRef.value?.resetFields() formRef.value?.resetFields()
} }
/** 新增按钮操作 */ /** 新增按钮操作 */
const handleAdd = () => { const openForm = () => {
reset() reset()
open.value = true dialogVisible.value = true
title.value = '发起订单'
} }
/** 提交按钮 */ /** 提交按钮 */
const submitForm = async () => { const submitForm = async () => {
const valid = await formRef.value?.validate() //
if (!valid) { if (!formRef) return
return const valid = await formRef.value.validate()
} if (!valid) return
// //
createDemoOrder(form.value).then(() => { formLoading.value = true
message.success('新增成功')
open.value = false
getList()
})
}
/** 支付按钮操作 */
const handlePay = (row) => {
router.push({
name: 'PayCashier',
query: {
id: row.payOrderId,
returnUrl: encodeURIComponent('/pay/demo-order?id=' + row.id)
}
})
}
/** 退款按钮操作 */
const handleRefund = async (row: any) => {
const id = row.id
try { try {
await message.confirm('是否确认退款编号为"' + id + '"的示例订单?') await PayDemoApi.createDemoOrder(formData.value)
await refundDemoOrder(id) message.success(t('common.createSuccess'))
getList() dialogVisible.value = false
message.success('发起退款成功!') } finally {
} catch {} formLoading.value = false
}
} }
/** 初始化 **/
onMounted(() => { onMounted(() => {
getList() getList()
}) })

View File

@ -1,153 +0,0 @@
<template>
<ContentWrap>
<!-- 列表 -->
<XTable @register="registerTable">
<template #toolbar_buttons>
<!-- 操作新增 -->
<XButton
type="primary"
preIcon="ep:zoom-in"
:title="t('action.add')"
v-hasPermi="['pay:merchant:create']"
@click="handleCreate()"
/>
<!-- 操作导出 -->
<XButton
type="warning"
preIcon="ep:download"
:title="t('action.export')"
v-hasPermi="['pay:merchant:export']"
@click="exportList('商户列表.xls')"
/>
</template>
<template #actionbtns_default="{ row }">
<!-- 操作修改 -->
<XTextButton
preIcon="ep:edit"
:title="t('action.edit')"
v-hasPermi="['pay:merchant:update']"
@click="handleUpdate(row.id)"
/>
<!-- 操作详情 -->
<XTextButton
preIcon="ep:view"
:title="t('action.detail')"
v-hasPermi="['pay:merchant:query']"
@click="handleDetail(row.id)"
/>
<!-- 操作删除 -->
<XTextButton
preIcon="ep:delete"
:title="t('action.del')"
v-hasPermi="['pay:merchant:delete']"
@click="deleteData(row.id)"
/>
</template>
</XTable>
</ContentWrap>
<XModal v-model="dialogVisible" :title="dialogTitle">
<!-- 对话框(添加 / 修改) -->
<Form
v-if="['create', 'update'].includes(actionType)"
:schema="allSchemas.formSchema"
:rules="rules"
ref="formRef"
/>
<!-- 对话框(详情) -->
<Descriptions
v-if="actionType === 'detail'"
:schema="allSchemas.detailSchema"
:data="detailData"
/>
<!-- 操作按钮 -->
<template #footer>
<!-- 按钮保存 -->
<XButton
v-if="['create', 'update'].includes(actionType)"
type="primary"
:title="t('action.save')"
:loading="actionLoading"
@click="submitForm()"
/>
<!-- 按钮关闭 -->
<XButton :loading="actionLoading" :title="t('dialog.close')" @click="dialogVisible = false" />
</template>
</XModal>
</template>
<script setup lang="ts" name="PayMerchant">
import type { FormExpose } from '@/components/Form'
import { rules, allSchemas } from './merchant.data'
import * as MerchantApi from '@/api/pay/merchant'
const { t } = useI18n() //
const message = useMessage() //
//
const [registerTable, { reload, deleteData, exportList }] = useXTable({
allSchemas: allSchemas,
getListApi: MerchantApi.getMerchantPageApi,
deleteApi: MerchantApi.deleteMerchantApi,
exportListApi: MerchantApi.exportMerchantApi
})
// ========== CRUD ==========
const actionLoading = ref(false) //
const actionType = ref('') //
const dialogVisible = ref(false) //
const dialogTitle = ref('edit') //
const formRef = ref<FormExpose>() // Ref
const detailData = ref() // Ref
//
const setDialogTile = (type: string) => {
dialogTitle.value = t('action.' + type)
actionType.value = type
dialogVisible.value = true
}
//
const handleCreate = () => {
setDialogTile('create')
}
//
const handleUpdate = async (rowId: number) => {
setDialogTile('update')
//
const res = await MerchantApi.getMerchantApi(rowId)
unref(formRef)?.setValues(res)
}
//
const handleDetail = async (rowId: number) => {
setDialogTile('detail')
const res = await MerchantApi.getMerchantApi(rowId)
detailData.value = res
}
//
const submitForm = async () => {
const elForm = unref(formRef)?.getElFormRef()
if (!elForm) return
elForm.validate(async (valid) => {
if (valid) {
actionLoading.value = true
//
try {
const data = unref(formRef)?.formModel as MerchantApi.MerchantVO
if (actionType.value === 'create') {
await MerchantApi.createMerchantApi(data)
message.success(t('common.createSuccess'))
} else {
await MerchantApi.updateMerchantApi(data)
message.success(t('common.updateSuccess'))
}
dialogVisible.value = false
} finally {
actionLoading.value = false
//
await reload()
}
}
})
}
</script>