index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. <span class="font-size-15 ml-1">{{ unit }}</span>
  18. </div>
  19. <template v-if="payTypeList?.length">
  20. <v-chip-group v-model="payType" selected-class="text-primary" column mandatory @update:modelValue="payTypeChange">
  21. <v-chip filter v-for="k in payTypeList" :key="k.code" :value="k.code" class="mr-3" label>
  22. {{ k.name }}
  23. <svg-icon v-if="k.icon" class="ml-1" :name="k.icon" :size="k.size"></svg-icon>
  24. </v-chip>
  25. </v-chip-group>
  26. <div v-if="tip" style="text-align: center;" class="mt-2">{{ tip }}</div>
  27. <div>
  28. <!-- 钱包支付 -->
  29. <div v-if="isWalletPay" class="py-10" style="text-align: center;">
  30. <div>
  31. <span>剩余点数:</span>
  32. <span style="color: var(--v-primary-base);">{{ balance }}</span>
  33. </div>
  34. <div class="my-3" v-if="balanceNotEnough">
  35. <!-- <v-icon color="warning">mdi-information</v-icon> -->
  36. <!-- <span class="color-warning">{{ props.params?.txt || '当前余额不足,请选择其他支付方式' }}</span> -->
  37. <span class="color-error">
  38. 当前剩余点数不足,<span class="text-decoration-underline cursor-pointer" @click="showRecharge = true">去充值</span>
  39. </span>
  40. </div>
  41. </div>
  42. <!-- 二维码支付 -->
  43. <div v-if="isQrCodePay" style="text-align: center;">
  44. <QrCode :text="payQrCodeTxt" :width="170" style="margin: 0 auto;" />
  45. <div class="mb-5" v-if="payQrCodeTxt" style="color: var(--v-error-base);">扫码支付时请勿离开</div>
  46. </div>
  47. <!-- 钱包支付确认按钮 -->
  48. <div v-if="isWalletPay && !balanceNotEnough" class="mt-2" style="text-align: center;">
  49. <v-btn
  50. class="buttons" color="primary"
  51. :loading="payLoading"
  52. @click="walletPaySubmit"
  53. >
  54. 确认
  55. </v-btn>
  56. </div>
  57. </div>
  58. </template>
  59. </v-card>
  60. <CtDialog :visible="showRecharge" :widthType="1" titleClass="text-h6" title="点数充值" :footer="false" submitText="确认" @close="handleClose">
  61. <Recharge @finish="handleFinish"></Recharge>
  62. </CtDialog>
  63. </template>
  64. <script setup>
  65. defineOptions({ name: 'pay-index'})
  66. import { computed, nextTick, onUnmounted, ref } from 'vue'
  67. import QrCode from '@/components/QrCode'
  68. import { definePayTypeList, qrCodePay, walletPay } from './until/payType'
  69. import { getEnableCodeList, getUnpaidOrder, payOrderSubmit, getOrderPayStatus } from '@/api/common'
  70. import { createTradeOrder } from '@/api/position'
  71. import { useSharedState } from '@/store/sharedState'
  72. import { rechargeOrderCreate } from '@/api/recruit/enterprise/member/points'
  73. // import { payCalculation } from '@/utils/position'
  74. import Recharge from '@/views/recruit/enterprise/memberCenter/myMembers/components/pointsAndBalance.vue'
  75. const emit = defineEmits(['payTypeChange', 'paySuccess'])
  76. const props = defineProps({
  77. params: {
  78. type: Object,
  79. default: () => {}
  80. },
  81. cost: {
  82. type: [String, Number],
  83. default: 0
  84. },
  85. spuId: { // 原始数据id
  86. type: String,
  87. default: ''
  88. },
  89. spuName: {
  90. type: String,
  91. default: ''
  92. },
  93. returnUrl: {
  94. type: String,
  95. default: ''
  96. },
  97. rechargeInfo: {
  98. type: Object,
  99. default: () => {}
  100. },
  101. appId: {
  102. type: Number,
  103. default: 10 // 10为一般情况下支付,11为充值支付
  104. },
  105. orderType: {
  106. type: Number,
  107. default: 2 // 订单类型 0平台订单| 1发布职位订单| 2发布众聘职位订单,示例值(1)
  108. },
  109. unit: {
  110. type: String,
  111. default: '点数'
  112. }
  113. })
  114. const showRecharge = ref(false)
  115. const loading = ref(true)
  116. const tip = ref('')
  117. // 步骤:
  118. // 1. 获取支付方式类型列表getCodeList (appId不同时数据返回有区别)
  119. // 2. 创建支付订单(先获取有无创建的,没有就创建)getUnpaidOrderList (appId=11时只有创建,没有获取)
  120. // 3. 如果是二维码类型支付(isQrCodePay=true)生成二维码(需要绑定支付订单的订单号)
  121. // 4.轮询用户是否支付成功
  122. // 如果是点数支付也要走上面的步骤,只是不生成二维码
  123. const balance = ref(JSON.parse(localStorage.getItem('enterpriseUserAccount'))?.balance || 0)
  124. const balanceNotEnough = computed(() => {
  125. console.log(props.cost, balance.value, 'balanceNotEnough')
  126. return (Number(props.cost) > Number(balance))
  127. })
  128. // 生成二维码内容
  129. const timer = ref(null)
  130. onUnmounted(() => {
  131. if (timer.value) clearInterval(timer.value); timer.value = null
  132. })
  133. // 如果是点数支付的话走完payOrderSubmit之后即扣点数,如果是二维码支付的话只是生成二维码,这一步以后是轮询是否支付成功
  134. const paySubmit = async () => {
  135. if (!payType.value) return
  136. try {
  137. if (payOrder.value) {
  138. // 提交支付订单
  139. const params = {
  140. channelCode: payType.value, // 支付渠道
  141. // returnUrl: , // 支付成功后,支付渠道跳转回当前页;再由当前页,跳转回 {@link returnUrl} 对应的地址
  142. }
  143. if ((props.appId - 0) === 10) { // 众聘
  144. if (!payOrder.value?.id || !payOrder.value.notifyUrl) return
  145. params.id = payOrder.value.id // 支付单编号
  146. params.displayMode = payOrder.value.notifyUrl // 展示模式 notifyUrl
  147. }
  148. if ((props.appId - 0) === 11) { // 充值
  149. if (!payOrder.value?.payOrderId) return
  150. params.id = payOrder.value.payOrderId // 支付单编号
  151. }
  152. const res = await payOrderSubmit(params)
  153. payQrCodeTxt.value = res?.displayContent || '' // 二维码
  154. if (timer.value) clearInterval(timer.value); timer.value = null
  155. timer.value = setInterval(() => { payStatus() }, 1000) // 轮巡查询用户是否支付
  156. // if (isQrCodePay.value) {
  157. // }
  158. }
  159. } catch (error) {
  160. console.log(error)
  161. }
  162. }
  163. // 2.支付订单
  164. const payOrder = ref({})
  165. let maxCount = 0
  166. const getUnpaidOrderList = async () => {
  167. try {
  168. if ((props.appId - 0) === 11) {
  169. //* 充值
  170. if (props.rechargeInfo.payPrice === undefined && props.rechargeInfo.packageId === undefined) return
  171. const params = {
  172. // payPrice: payCalculation(props.cost, 'emit'),
  173. payPrice: props.rechargeInfo.payPrice - 0,
  174. packageId: props.rechargeInfo.id,
  175. }
  176. const data = await rechargeOrderCreate(params)
  177. payOrder.value = data || {}
  178. } else {
  179. //* 发布职位
  180. const data = await getUnpaidOrder({ spuId: props.spuId, type: props.orderType }) // 获取待支付的订单 (order:业务订单; payOrder:支付订单)
  181. if (!data) {
  182. // 订单超时,重新提交订单
  183. await createTradeOrder({
  184. spuId: props.spuId,
  185. spuName: props.spuName,
  186. price: props.cost,
  187. type: props.orderType, // 发布众聘职位订单
  188. })
  189. if (maxCount > 3) return // 避免死循环
  190. maxCount++
  191. setTimeout(() => {
  192. getUnpaidOrderList()
  193. }, 1000)
  194. }
  195. //
  196. payOrder.value = data?.payOrder || null
  197. }
  198. if (isQrCodePay.value) paySubmit() // 生成二维码内容
  199. } catch (error) {
  200. console.log(error)
  201. } finally {
  202. nextTick(() => {
  203. loading.value = false
  204. })
  205. }
  206. }
  207. // 1.支付方式
  208. const isWalletPay = ref(false)
  209. const isQrCodePay = ref(false)
  210. const payTypeChange = (value) => {
  211. payType.value = value
  212. tip.value = payTypeList.value.find(e => e.code === payType.value)?.tip || ''
  213. isQrCodePay.value = qrCodePay.includes(payType.value)
  214. isWalletPay.value = walletPay.includes(payType.value)
  215. paySubmit() // 生成二维码内容
  216. // emit('payTypeChange', value, balanceNotEnough)
  217. }
  218. // 1.支付方式
  219. const payType = ref('')
  220. const payTypeList = ref([])
  221. const sharedState = useSharedState()
  222. const codeList = ref(sharedState.payCodeList || [])
  223. const getCodeList = async () => {
  224. try {
  225. if (!codeList.value?.length) {
  226. const list = await getEnableCodeList({appId: props.appId})
  227. codeList.value = list || []
  228. // sharedState.setPayCodeList(codeList.value) // 返回的支付方式列表会变
  229. } else {
  230. codeList.value = sharedState.payCodeList
  231. }
  232. } catch (error) {
  233. console.log(error)
  234. } finally {
  235. if (definePayTypeList?.length && codeList.value?.length) {
  236. codeList.value.forEach(code => {
  237. const item = definePayTypeList.find(p => p.code === code)
  238. if (item) {
  239. if (!payType.value) {
  240. tip.value = item.tip || ''
  241. payTypeChange(code) // 默认值赋值
  242. }
  243. payTypeList.value.push(item)
  244. }
  245. })
  246. }
  247. getUnpaidOrderList()
  248. }
  249. }
  250. getCodeList()
  251. const payLoading = ref(false)
  252. const payQrCodeTxt = ref('')
  253. // 钱包支付(余额支付)
  254. const walletPaySubmit = () => {
  255. payLoading.value = true
  256. paySubmit() // 使用点数
  257. }
  258. import Snackbar from '@/plugins/snackbar'
  259. import { useRoute } from 'vue-router'; const route = useRoute()
  260. import { useRouter } from 'vue-router'; const router = useRouter()
  261. const payStatus = async () => {
  262. // if (timer.value) clearInterval(timer.value); timer.value = null
  263. // setTimeout(() => { // 测试代码
  264. // }, 2000)
  265. try {
  266. const data = await getOrderPayStatus({ id: (props.appId - 0) === 11 ? payOrder.value.payOrderId : payOrder.value.id })
  267. if ((data?.status - 0) === 10) {
  268. // 支付成功
  269. if (timer.value) clearInterval(timer.value); timer.value = null
  270. setTimeout(() => {
  271. if (route.fullPath === props.returnUrl) router.go(0)
  272. else if (props.returnUrl) router.push(props.returnUrl) // 返回指定页面
  273. emit('paySuccess', isWalletPay.value)
  274. Snackbar.success('支付成功')
  275. }, 2000);
  276. }
  277. } catch (error) {
  278. console.log(error)
  279. }
  280. }
  281. const handleClose = () => {
  282. showRecharge.value = false
  283. }
  284. const handleFinish = () => {
  285. showRecharge.value = false
  286. console.log(localStorage.getItem('enterpriseUserAccount'), '------')
  287. balance.value = JSON.parse(localStorage.getItem('enterpriseUserAccount'))?.balance || 0
  288. console.log(balance.value, 'balance.value')
  289. }
  290. </script>
  291. <style lang="scss" scoped>
  292. .font-size-30 { font-size: 30px; }
  293. </style>