index.tsx 3.8 KB

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