index.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <!-- 支付方式 -->
  2. <template>
  3. <v-card elevation="0" :loading="loading" :disabled="loading">
  4. <!-- 加载样式 -->
  5. <template v-slot:loader="{ isActive }">
  6. <v-progress-linear
  7. :active="isActive"
  8. color="var(--v-primary-base)"
  9. height="1"
  10. indeterminate
  11. ></v-progress-linear>
  12. </template>
  13. <!-- 赏金所需 -->
  14. <div class="pt-3 pb-5" style="color: var(--v-error-base); font-weight: bold; text-align: center;">
  15. <span class="font-size-13">¥</span>
  16. <span class="font-size-30">{{ cost }}</span>
  17. </div>
  18. <template v-if="payTypeList?.length">
  19. <v-chip-group v-model="payType" selected-class="text-primary" mandatory @update:modelValue="payTypeChange">
  20. <v-chip filter v-for="k in payTypeList" :key="k.code" :value="k.code" class="mr-3" label>
  21. {{ k.name }}
  22. <svg-icon v-if="k.icon" class="ml-1" :name="k.icon" :size="k.size"></svg-icon>
  23. </v-chip>
  24. </v-chip-group>
  25. <div v-if="tip" style="text-align: center;" class="mt-2">{{ tip }}</div>
  26. <div>
  27. <!-- 支付 -->
  28. <div v-if="isWalletPay" class="py-10" style="text-align: center;">
  29. <div>
  30. <span >{{ $t('enterprise.account.accountBalances') }}:</span>
  31. <span style="color: var(--v-primary-base);">{{ balance }}</span>
  32. </div>
  33. <div class="my-3" v-if="balanceNotEnough">
  34. <!-- <v-icon color="warning">mdi-information</v-icon> -->
  35. <span class="color-warning">{{ props.params?.txt || '当前余额不足,请选择其他支付方式' }}</span>
  36. </div>
  37. </div>
  38. <div v-if="isQrCodePay" style="text-align: center;">
  39. <QrCode :text="payQrCodeTet" :width="170" style="margin: 0 auto;" />
  40. <div v-if="isQrCodePay && payQrCodeTet" style="color: var(--v-error-base);">扫码支付时请勿离开</div>
  41. </div>
  42. <div class="mt-2" style="text-align: center;">
  43. <!-- v-if="(isQrCodePay && payQrCodeTet) || isWalletPay" -->
  44. <v-btn
  45. v-if="isWalletPay && !balanceNotEnough"
  46. class="buttons" color="primary"
  47. :loading="payLoading"
  48. @click="walletPaySubmit"
  49. >
  50. <!-- {{ isWalletPay ? '确认' : '支付完成' }} -->
  51. 确认
  52. </v-btn>
  53. </div>
  54. </div>
  55. </template>
  56. </v-card>
  57. </template>
  58. <script setup>
  59. defineOptions({ name: 'pay-index'})
  60. import { computed, onUnmounted, ref } from 'vue'
  61. import QrCode from '@/components/QrCode'
  62. import { definePayTypeList, qrCodePay, walletPay } from './until/payType'
  63. import { getEnableCodeList, getUnpaidOrder, payOrderSubmit, getOrderPayStatus } from '@/api/common'
  64. import { createTradeOrder } from '@/api/position'
  65. import { useSharedState } from '@/store/sharedState'
  66. const emit = defineEmits(['payTypeChange', 'paySuccess'])
  67. const props = defineProps({
  68. params: {
  69. type: Object,
  70. default: () => {}
  71. },
  72. cost: {
  73. type: [String, Number],
  74. default: 0
  75. },
  76. spuId: { // 原始数据id
  77. type: String,
  78. default: ''
  79. },
  80. spuName: {
  81. type: String,
  82. default: ''
  83. },
  84. returnUrl: {
  85. type: String,
  86. default: ''
  87. },
  88. type: {
  89. type: Number,
  90. default: 2 // 订单类型 0平台订单| 1发布职位订单| 2发布众聘职位订单,示例值(1)
  91. },
  92. })
  93. const loading = ref(true)
  94. const tip = ref('')
  95. const balance = JSON.parse(localStorage.getItem('enterpriseUserAccount'))?.balance || 0
  96. const balanceNotEnough = computed(() => {
  97. return (Number(props.cost) > Number(balance))
  98. })
  99. // 支付方式
  100. const isWalletPay = ref(true)
  101. const isQrCodePay = ref(false)
  102. const payTypeChange = (value) => {
  103. payType.value = value
  104. tip.value = payTypeList.value.find(e => e.code === payType.value)?.tip || ''
  105. isQrCodePay.value = qrCodePay.includes(payType.value)
  106. isWalletPay.value = walletPay.includes(payType.value)
  107. if (isQrCodePay.value) initPayQrCode() // 生成二维码内容
  108. emit('payTypeChange', value, balanceNotEnough)
  109. }
  110. // 支付方式
  111. const payType = ref('')
  112. const payTypeList = ref([])
  113. const sharedState = useSharedState()
  114. const codeList = ref(sharedState.payCodeList || [])
  115. const getCodeList = async () => {
  116. try {
  117. if (!codeList.value?.length) {
  118. const list = await getEnableCodeList({appId: 10})
  119. codeList.value = list || []
  120. sharedState.setPayCodeList(codeList.value)
  121. } else {
  122. codeList.value = sharedState.payCodeList
  123. }
  124. } catch (error) {
  125. console.log(error)
  126. } finally {
  127. if (definePayTypeList?.length && codeList.value?.length) {
  128. codeList.value.forEach(code => {
  129. const item = definePayTypeList.find(p => p.code === code)
  130. if (item) {
  131. if (!payType.value) {
  132. tip.value = item.tip || ''
  133. payTypeChange(code) // 默认值赋值
  134. }
  135. payTypeList.value.push(item)
  136. }
  137. })
  138. }
  139. }
  140. }
  141. getCodeList()
  142. const payOrder = ref({})
  143. let maxCount = 0
  144. const getUnpaidOrderList = async () => {
  145. try {
  146. const data = await getUnpaidOrder({ spuId: props.spuId, type: props.type }) // 获取待支付的订单
  147. // order:业务订单; payOrder:支付订单
  148. if (!data) {
  149. // 订单超时,重新提交订单
  150. await createTradeOrder({
  151. spuId: props.spuId,
  152. spuName: props.spuName,
  153. price: props.cost,
  154. type: props.type, // 发布众聘职位订单
  155. })
  156. if (maxCount > 3) return // 避免死循环
  157. maxCount++
  158. setTimeout(() => {
  159. getUnpaidOrderList()
  160. }, 1000)
  161. }
  162. //
  163. if (data?.payOrder) {
  164. payOrder.value = data.payOrder
  165. // loading.value = false
  166. }
  167. } catch (error) {
  168. console.log(error)
  169. } finally {
  170. loading.value = false
  171. }
  172. }
  173. getUnpaidOrderList() // getUnpaidOrder
  174. const payLoading = ref(false)
  175. const payQrCodeTet = ref('')
  176. // 钱包支付(余额支付)
  177. const walletPaySubmit = () => {
  178. // emit('paySubmit', payType.value)
  179. }
  180. // 生成二维码内容
  181. const timer = ref(null)
  182. onUnmounted(() => {
  183. if (timer.value) clearInterval(timer.value); timer.value = null
  184. })
  185. const initPayQrCode = async () => {
  186. if (timer.value) clearInterval(timer.value); timer.value = null
  187. try {
  188. if (payOrder.value) {
  189. // 提交支付订单
  190. const params = {
  191. id: payOrder.value.id, // 支付单编号
  192. channelCode: payType.value, // 支付渠道
  193. displayMode: payOrder.value.notifyUrl, // 展示模式 notifyUrl
  194. // returnUrl: location.href, // 支付成功后,支付渠道跳转回当前页;再由当前页,跳转回 {@link returnUrl} 对应的地址
  195. }
  196. const res = await payOrderSubmit(params)
  197. payQrCodeTet.value = res?.displayContent || '' // 支付二维码
  198. timer.value = setInterval(() => { payStatus() }, 1000) // 轮巡查询用户是否支付
  199. }
  200. } catch (error) {
  201. console.log(error)
  202. }
  203. }
  204. import Snackbar from '@/plugins/snackbar'
  205. import { useRouter } from 'vue-router'; const router = useRouter()
  206. const payStatus = async () => {
  207. try {
  208. const data = await getOrderPayStatus({ id: payOrder.value.id })
  209. if ((data?.status - 0) === 10) {
  210. // 支付成功
  211. if (timer.value) clearInterval(timer.value); timer.value = null
  212. setTimeout(() => {
  213. if (props.returnUrl) router.push(props.returnUrl) // 返回指定页面
  214. else emit('paySuccess')
  215. Snackbar.success('付款成功')
  216. }, 1000);
  217. }
  218. } catch (error) {
  219. console.log(error)
  220. }
  221. }
  222. </script>
  223. <style lang="scss" scoped>
  224. .font-size-30 { font-size: 30px; }
  225. </style>