index.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import { Button, Cell, CellGroup, Icon, RadioGroup, Radio, showConfirmDialog } from 'vant'
  2. import { defineComponent, reactive } from 'vue'
  3. import styles from './index.module.less'
  4. import { browser, moneyFormat } from '@/helpers/utils'
  5. export default defineComponent({
  6. name: 'payment',
  7. props: {
  8. paymentConfig: {
  9. type: Object,
  10. default: {}
  11. }
  12. },
  13. emits: ['backOut', 'close', 'confirm'],
  14. setup(props, { slots, attrs, emit }) {
  15. const state = reactive({
  16. payType: 'wx',
  17. pay_channel: ''
  18. })
  19. const onClose = () => {
  20. // 继续支付则直接关闭弹窗就可
  21. showConfirmDialog({
  22. message: '是否放弃本次付款',
  23. confirmButtonText: '继续付款',
  24. cancelButtonText: '放弃',
  25. showCancelButton: true
  26. }).catch(async () => {
  27. await onCancel()
  28. emit('backOut')
  29. emit('close')
  30. })
  31. }
  32. // 需要关闭订单
  33. const onCancel = async (noBack?: boolean) => {}
  34. const onSubmit = async () => {
  35. // 支付...
  36. const pt = state.payType
  37. // 判断当前浏览器
  38. if (browser().weixin) {
  39. // 微信浏览器
  40. if (pt == 'zfb') {
  41. state.pay_channel = 'alipay_qr'
  42. getCodePay('qrCode')
  43. } else if (pt == 'wx') {
  44. state.pay_channel = 'wx_pub'
  45. getCodePay('pay')
  46. }
  47. } else if (browser().alipay) {
  48. // 支付宝浏览器
  49. if (pt == 'zfb') {
  50. state.pay_channel = 'alipay_wap'
  51. // 支付宝 H5 支付
  52. getCodePay('pay')
  53. } else if (pt == 'wx') {
  54. state.pay_channel = 'wx_pub'
  55. getCodePay('qrCode')
  56. }
  57. } else {
  58. if (pt == 'zfb') {
  59. state.pay_channel = 'alipay_qr'
  60. } else if (pt == 'wx') {
  61. state.pay_channel = 'wx_pub'
  62. }
  63. getCodePay('qrCode')
  64. }
  65. }
  66. const getCodePay = (code) => {
  67. // 二维码页面, 唤起支付页面
  68. const payCode = code == 'qrCode' ? 'payCenter' : 'payResult'
  69. emit('confirm', {
  70. payCode,
  71. pay_channel: state.pay_channel
  72. })
  73. }
  74. return () => (
  75. <div class={styles.payment}>
  76. <Icon onClick={onClose} name="cross" size={20} />
  77. <div class={[styles.title]}>选择支付方式</div>
  78. <div class={styles.payAmount}>
  79. <p>应付金额</p>
  80. <div class={styles.amount}>
  81. <span>¥</span>
  82. {moneyFormat(props.paymentConfig.currentPrice)}
  83. </div>
  84. </div>
  85. <RadioGroup v-model={state.payType}>
  86. <CellGroup border={false}>
  87. <Cell
  88. border={true}
  89. center
  90. onClick={() => {
  91. // wx_lite
  92. state.payType = 'wx'
  93. }}
  94. v-slots={{
  95. icon: () => <Icon name="wechat-pay" color="#15c434" size={22} />,
  96. 'right-icon': () => <Radio name="wx" />,
  97. title: () => (
  98. <div class={styles.payTypeRe}>
  99. 微信支付 <span class={styles.recommend}>推荐</span>
  100. </div>
  101. )
  102. }}
  103. ></Cell>
  104. <Cell
  105. title="支付宝支付"
  106. border={true}
  107. // class="van-hairline--bottom"
  108. center
  109. onClick={() => {
  110. // alipay
  111. state.payType = 'zfb'
  112. }}
  113. v-slots={{
  114. icon: () => <Icon name="alipay" color="#009fe9" size={22} />,
  115. 'right-icon': () => <Radio name="zfb" />
  116. }}
  117. ></Cell>
  118. </CellGroup>
  119. </RadioGroup>
  120. <div class={styles.blank}></div>
  121. <Button type="primary" class={styles.payBtn} block round onClick={onSubmit}>
  122. 确认支付
  123. </Button>
  124. </div>
  125. )
  126. }
  127. })