index.tsx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. import {
  2. Button,
  3. Cell,
  4. CellGroup,
  5. Icon,
  6. RadioGroup,
  7. Radio,
  8. showConfirmDialog,
  9. showLoadingToast,
  10. closeToast,
  11. showDialog
  12. } from 'vant';
  13. import { computed, defineComponent, reactive } from 'vue';
  14. import styles from './index.module.less';
  15. import { browser, moneyFormat } from '@/helpers/utils';
  16. import request from '@/helpers/request';
  17. import { listenerMessage, postMessage } from '@/helpers/native-message';
  18. import { useRouter } from 'vue-router';
  19. export default defineComponent({
  20. name: 'payment',
  21. props: {
  22. paymentConfig: {
  23. type: Object,
  24. default: {}
  25. },
  26. config: {
  27. type: Object,
  28. default: {}
  29. }
  30. },
  31. emits: ['backOut', 'close', 'confirm'],
  32. setup(props, { slots, attrs, emit }) {
  33. const router = useRouter();
  34. const state = reactive({
  35. payType: 'wx',
  36. pay_channel: ''
  37. });
  38. const onClose = () => {
  39. // 继续支付则直接关闭弹窗就可
  40. showConfirmDialog({
  41. message: '是否放弃本次付款',
  42. confirmButtonText: '继续付款',
  43. cancelButtonText: '放弃',
  44. showCancelButton: true
  45. }).catch(async () => {
  46. await onCancel();
  47. emit('backOut');
  48. emit('close');
  49. });
  50. };
  51. const isWxPay = computed(() => {
  52. const paymentChannels = props.config.paymentChannel || '';
  53. if (paymentChannels) {
  54. if (paymentChannels.toUpperCase().indexOf('WX') !== -1) {
  55. return true;
  56. } else {
  57. return false;
  58. }
  59. } else {
  60. return true;
  61. }
  62. });
  63. const isAliPay = computed(() => {
  64. const paymentChannels = props.config.paymentChannel || '';
  65. if (paymentChannels) {
  66. if (paymentChannels.toUpperCase().indexOf('ALI') !== -1) {
  67. return true;
  68. } else {
  69. return false;
  70. }
  71. } else {
  72. return true;
  73. }
  74. });
  75. // 需要关闭订单
  76. const onCancel = async (noBack?: boolean) => {};
  77. const onSubmit = async () => {
  78. // 支付...
  79. if (props.config.paymentType === 'original') {
  80. submitNativePay();
  81. } else {
  82. submitQrCodePay();
  83. }
  84. };
  85. const submitNativePay = async () => {
  86. // 支付...
  87. try {
  88. // const payChannel = state.payType === 'zfb' ? 'alipay-app' : 'wxpay-app';
  89. let paymentChannels = props.config.paymentChannel || '';
  90. paymentChannels = paymentChannels.split(',');
  91. // const payChannel = paymentChannels.map((item: any) => item.indexOf(''))
  92. let payChannel = '';
  93. paymentChannels.forEach((item: any) => {
  94. if (
  95. state.payType === 'zfb' &&
  96. item.toUpperCase().indexOf('ALI') !== -1
  97. ) {
  98. payChannel = item;
  99. } else if (
  100. state.payType === 'wx' &&
  101. item.toUpperCase().indexOf('WX') !== -1
  102. ) {
  103. payChannel = item;
  104. }
  105. });
  106. console.log(state.payType, 'state.payType');
  107. let paymentType = props.config.paymentType;
  108. const payMap: any = {
  109. merOrderNo: props.paymentConfig.orderNo,
  110. paymentChannel: payChannel, // 支付渠道
  111. paymentType: paymentType,
  112. userId: props.paymentConfig.userId
  113. };
  114. const { data } = await request.post(
  115. '/edu-app/open/userOrder/executePayment',
  116. {
  117. data: {
  118. ...payMap
  119. }
  120. }
  121. );
  122. postMessage({
  123. api: 'paymentOrder',
  124. content: {
  125. orderNo: props.paymentConfig.orderNo,
  126. payChannel: state.payType === 'zfb' ? 'ali_app' : 'wx_app',
  127. payInfo: data.reqParams.body || JSON.stringify(data.reqParams)
  128. }
  129. });
  130. showLoadingToast({
  131. message: '支付中...',
  132. forbidClick: true,
  133. duration: 3000,
  134. loadingType: 'spinner'
  135. });
  136. emit('close');
  137. // 唤起支付时状态
  138. listenerMessage('paymentOperation', result => {
  139. console.log(result, 'init paymentOperation');
  140. paymentOperation(result?.content);
  141. });
  142. } catch (e: any) {
  143. console.log(e);
  144. }
  145. };
  146. const paymentOperation = (res: any) => {
  147. console.log(res, 'paymentOperation');
  148. // 支付状态
  149. // paymentOperation 支付成功:success 支付失败:error 支付取消:cancel 未安装:fail
  150. // error 只有安卓端有
  151. closeToast();
  152. if (res.status === 'success' || res.status === 'error') {
  153. emit('close');
  154. router.replace({
  155. path: '/payment-result',
  156. query: {
  157. orderNo: props.paymentConfig.orderNo
  158. }
  159. });
  160. } else if (res.status === 'cancel') {
  161. emit('close');
  162. } else if (res.status === 'fail') {
  163. const message =
  164. state.payType === 'zfb' ? '您尚未安装支付宝' : '您尚未安装微信';
  165. showDialog({
  166. title: '提示',
  167. message
  168. }).then(() => {
  169. emit('close');
  170. });
  171. }
  172. };
  173. const submitQrCodePay = () => {
  174. const pt = state.payType;
  175. // 判断当前浏览器
  176. if (browser().weixin) {
  177. // 微信浏览器
  178. if (pt == 'zfb') {
  179. state.pay_channel = 'alipay_qr';
  180. getCodePay('qrCode');
  181. } else if (pt == 'wx') {
  182. state.pay_channel = 'wx_pub';
  183. getCodePay('pay');
  184. }
  185. } else if (browser().alipay) {
  186. // 支付宝浏览器
  187. if (pt == 'zfb') {
  188. state.pay_channel = 'alipay_wap';
  189. // 支付宝 H5 支付
  190. getCodePay('pay');
  191. } else if (pt == 'wx') {
  192. state.pay_channel = 'wx_pub';
  193. getCodePay('qrCode');
  194. }
  195. } else {
  196. if (pt == 'zfb') {
  197. state.pay_channel = 'alipay_qr';
  198. } else if (pt == 'wx') {
  199. state.pay_channel = 'wx_pub';
  200. }
  201. getCodePay('qrCode');
  202. }
  203. };
  204. const getCodePay = (code: any) => {
  205. // 二维码页面, 唤起支付页面
  206. const payCode = code == 'qrCode' ? 'payCenter' : 'payResult';
  207. emit('confirm', {
  208. payCode,
  209. pay_channel: state.pay_channel
  210. });
  211. };
  212. return () => (
  213. <div class={styles.payment}>
  214. <Icon onClick={onClose} name="cross" size={20} />
  215. <div class={[styles.title]}>选择支付方式</div>
  216. <div class={styles.payAmount}>
  217. <p>应付金额</p>
  218. <div class={styles.amount}>
  219. <span>¥ </span>
  220. {moneyFormat(props.paymentConfig.currentPrice)}
  221. </div>
  222. </div>
  223. <RadioGroup v-model={state.payType}>
  224. <CellGroup border={false}>
  225. {isWxPay.value && (
  226. <Cell
  227. border={true}
  228. center
  229. onClick={() => {
  230. state.payType = 'wx';
  231. }}
  232. v-slots={{
  233. icon: () => (
  234. <Icon name="wechat-pay" color="#15c434" size={22} />
  235. ),
  236. 'right-icon': () => <Radio name="wx" />,
  237. title: () => (
  238. <div class={styles.payTypeRe}>
  239. 微信支付 <span class={styles.recommend}>推荐</span>
  240. </div>
  241. )
  242. }}></Cell>
  243. )}
  244. {isAliPay.value && (
  245. <Cell
  246. title="支付宝支付"
  247. border={true}
  248. center
  249. onClick={() => {
  250. state.payType = 'zfb';
  251. }}
  252. v-slots={{
  253. icon: () => <Icon name="alipay" color="#009fe9" size={22} />,
  254. 'right-icon': () => <Radio name="zfb" />
  255. }}></Cell>
  256. )}
  257. </CellGroup>
  258. </RadioGroup>
  259. <div class={styles.blank}></div>
  260. <Button
  261. type="primary"
  262. class={styles.payBtn}
  263. block
  264. round
  265. onClick={onSubmit}>
  266. 确认支付
  267. </Button>
  268. </div>
  269. );
  270. }
  271. });