123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- <!-- 支付方式 -->
- <template>
- <v-card elevation="0" :loading="loading" :disabled="loading">
- <!-- 加载样式 -->
- <template v-slot:loader="{ isActive }">
- <v-progress-linear
- :active="isActive"
- color="var(--v-primary-base)"
- height="1"
- indeterminate
- ></v-progress-linear>
- </template>
- <!-- 赏金所需 -->
- <div class="pt-3 pb-5" style="color: var(--v-error-base); font-weight: bold; text-align: center;">
- <span class="font-size-13">¥</span>
- <span class="font-size-30">{{ cost }}</span>
- </div>
- <template v-if="payTypeList?.length">
- <v-chip-group v-model="payType" selected-class="text-primary" column mandatory @update:modelValue="payTypeChange">
- <v-chip filter v-for="k in payTypeList" :key="k.code" :value="k.code" class="mr-3" label>
- {{ k.name }}
- <svg-icon v-if="k.icon" class="ml-1" :name="k.icon" :size="k.size"></svg-icon>
- </v-chip>
- </v-chip-group>
- <div v-if="tip" style="text-align: center;" class="mt-2">{{ tip }}</div>
- <div>
- <!-- 钱包支付 -->
- <div v-if="isWalletPay" class="py-10" style="text-align: center;">
- <div>
- <span >{{ $t('enterprise.account.accountBalances') }}:</span>
- <span style="color: var(--v-primary-base);">{{ balance }}</span>
- </div>
- <div class="my-3" v-if="balanceNotEnough">
- <!-- <v-icon color="warning">mdi-information</v-icon> -->
- <span class="color-warning">{{ props.params?.txt || '当前余额不足,请选择其他支付方式' }}</span>
- </div>
- </div>
- <!-- 二维码支付 -->
- <div v-if="isQrCodePay" style="text-align: center;">
- <QrCode :text="payQrCodeTxt" :width="170" style="margin: 0 auto;" />
- <div class="mb-5" v-if="payQrCodeTxt" style="color: var(--v-error-base);">扫码支付时请勿离开</div>
- </div>
- <!-- 钱包支付确认按钮 -->
- <div v-if="isWalletPay && !balanceNotEnough" class="mt-2" style="text-align: center;">
- <v-btn
- class="buttons" color="primary"
- :loading="payLoading"
- @click="walletPaySubmit"
- >
- 确认
- </v-btn>
- </div>
- </div>
- </template>
- </v-card>
- </template>
- <script setup>
- defineOptions({ name: 'pay-index'})
- import { computed, onUnmounted, ref } from 'vue'
- import QrCode from '@/components/QrCode'
- import { definePayTypeList, qrCodePay, walletPay } from './until/payType'
- import { getEnableCodeList, getUnpaidOrder, payOrderSubmit, getOrderPayStatus } from '@/api/common'
- import { createTradeOrder } from '@/api/position'
- import { useSharedState } from '@/store/sharedState'
- const emit = defineEmits(['payTypeChange', 'paySuccess'])
- const props = defineProps({
- params: {
- type: Object,
- default: () => {}
- },
- cost: {
- type: [String, Number],
- default: 0
- },
- spuId: { // 原始数据id
- type: String,
- default: ''
- },
- spuName: {
- type: String,
- default: ''
- },
- returnUrl: {
- type: String,
- default: ''
- },
- type: {
- type: Number,
- default: 2 // 订单类型 0平台订单| 1发布职位订单| 2发布众聘职位订单,示例值(1)
- },
- })
- const loading = ref(true)
- const tip = ref('')
- // 步骤:
- // 1. 获取支付方式类型列表getCodeList
- // 2. 创建支付订单(先获取有无创建的,没有就创建)getUnpaidOrderList
- // 3. 如果是二维码类型支付(isQrCodePay=true)生成二维码(需要绑定支付订单的订单号)
- const balance = JSON.parse(localStorage.getItem('enterpriseUserAccount'))?.balance || 0
- const balanceNotEnough = computed(() => {
- return (Number(props.cost) > Number(balance))
- })
- // 生成二维码内容
- const timer = ref(null)
- onUnmounted(() => {
- if (timer.value) clearInterval(timer.value); timer.value = null
- })
- const initPayQrCode = async () => { // 生成二维码内容
- if (!payOrder.value?.id || !payOrder.value.notifyUrl || !payType.value) return
- if (timer.value) clearInterval(timer.value); timer.value = null
- try {
- if (payOrder.value) {
- // 提交支付订单
- const params = {
- id: payOrder.value.id, // 支付单编号
- channelCode: payType.value, // 支付渠道
- displayMode: payOrder.value.notifyUrl, // 展示模式 notifyUrl
- // returnUrl: location.href, // 支付成功后,支付渠道跳转回当前页;再由当前页,跳转回 {@link returnUrl} 对应的地址
- }
- const res = await payOrderSubmit(params)
- payQrCodeTxt.value = res?.displayContent || '' // 支付二维码
- timer.value = setInterval(() => { payStatus() }, 1000) // 轮巡查询用户是否支付
- }
- } catch (error) {
- console.log(error)
- }
- }
- // 2.支付订单
- const payOrder = ref({})
- let maxCount = 0
- const getUnpaidOrderList = async () => {
- try {
- const data = await getUnpaidOrder({ spuId: props.spuId, type: props.type }) // 获取待支付的订单 (order:业务订单; payOrder:支付订单)
- if (!data) {
- // 订单超时,重新提交订单
- await createTradeOrder({
- spuId: props.spuId,
- spuName: props.spuName,
- price: props.cost,
- type: props.type, // 发布众聘职位订单
- })
- if (maxCount > 3) return // 避免死循环
- maxCount++
- setTimeout(() => {
- getUnpaidOrderList()
- }, 1000)
- }
- //
- payOrder.value = data?.payOrder || null
- if (isQrCodePay.value) initPayQrCode() // 生成二维码内容
- } catch (error) {
- console.log(error)
- } finally {
- loading.value = false
- }
- }
- // getUnpaidOrderList() // getUnpaidOrder
- // 支付方式
- const isWalletPay = ref(false)
- const isQrCodePay = ref(false)
- const payTypeChange = (value) => {
- payType.value = value
- tip.value = payTypeList.value.find(e => e.code === payType.value)?.tip || ''
- isQrCodePay.value = qrCodePay.includes(payType.value)
- isWalletPay.value = walletPay.includes(payType.value)
- initPayQrCode() // 生成二维码内容
- emit('payTypeChange', value, balanceNotEnough)
- }
- // 1.支付方式
- const payType = ref('')
- const payTypeList = ref([])
- const sharedState = useSharedState()
- const codeList = ref(sharedState.payCodeList || [])
- const getCodeList = async () => {
- try {
- if (!codeList.value?.length) {
- const list = await getEnableCodeList({appId: 10})
- codeList.value = list || []
- sharedState.setPayCodeList(codeList.value)
- } else {
- codeList.value = sharedState.payCodeList
- }
- } catch (error) {
- console.log(error)
- } finally {
- if (definePayTypeList?.length && codeList.value?.length) {
- codeList.value.forEach(code => {
- const item = definePayTypeList.find(p => p.code === code)
- if (item) {
- if (!payType.value) {
- tip.value = item.tip || ''
- payTypeChange(code) // 默认值赋值
- }
- payTypeList.value.push(item)
- }
- })
- }
- getUnpaidOrderList()
- }
- }
- getCodeList()
- const payLoading = ref(false)
- const payQrCodeTxt = ref('')
- // 钱包支付(余额支付)
- const walletPaySubmit = () => {
- // emit('paySubmit', payType.value)
- }
- import Snackbar from '@/plugins/snackbar'
- import { useRoute } from 'vue-router'; const route = useRoute()
- import { useRouter } from 'vue-router'; const router = useRouter()
- const payStatus = async () => {
- // if (timer.value) clearInterval(timer.value); timer.value = null
- // setTimeout(() => {
- // console.log('fullPath1', route.fullPath)
- // console.log('returnUrl2',props.returnUrl)
- // debugger
- // if (route.fullPath === props.returnUrl) router.go(0)
- // else if (props.returnUrl) router.push(props.returnUrl) // 返回指定页面
- // else emit('paySuccess')
- // Snackbar.success('付款成功')
- // }, 2000)
- try {
- const data = await getOrderPayStatus({ id: payOrder.value.id })
- if ((data?.status - 0) === 10) {
- // 支付成功
- if (timer.value) clearInterval(timer.value); timer.value = null
- setTimeout(() => {
- if (route.fullPath === props.returnUrl) router.go(0)
- else if (props.returnUrl) router.push(props.returnUrl) // 返回指定页面
- else emit('paySuccess')
- Snackbar.success('付款成功')
- }, 1000);
- }
- } catch (error) {
- console.log(error)
- }
- }
- </script>
- <style lang="scss" scoped>
- .font-size-30 { font-size: 30px; }
- </style>
|