index.tsx 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. import request from '@/helpers/request';
  2. import { browser, getUrlCode, moneyFormat } from '@/helpers/utils';
  3. import { goWechatAuth } from '@/state';
  4. import {
  5. Cell,
  6. CellGroup,
  7. Icon,
  8. Loading,
  9. showConfirmDialog,
  10. showToast
  11. } from 'vant';
  12. import { defineComponent, onMounted, reactive } from 'vue';
  13. import { useRouter, useRoute } from 'vue-router';
  14. import styles from './index.module.less';
  15. export default defineComponent({
  16. name: 'pay-result',
  17. setup() {
  18. const router = useRouter();
  19. const route = useRoute();
  20. const state = reactive({
  21. errorText: '',
  22. browserStatus: false,
  23. code: null as any,
  24. pay_channel: route.query.pay_channel as any,
  25. wxAppId: route.query.wxAppId as any,
  26. body: route.query.body as any,
  27. price: route.query.price as any,
  28. orderNo: route.query.orderNo as any,
  29. userId: route.query.userId as any,
  30. payInfo: {} as any
  31. });
  32. console.log(state.errorText);
  33. const init = () => {
  34. const query = route.query;
  35. console.log(query);
  36. // 判断是否有支付对象
  37. if (!query.orderNo || !query.pay_channel) {
  38. showConfirmDialog({
  39. message: '支付订单信息错误请重新支付',
  40. showCancelButton: false
  41. }).then(() => {
  42. router.back();
  43. });
  44. } else {
  45. // 判断当前浏览器
  46. if (browser().weixin) {
  47. state.browserStatus = true;
  48. getWxPay();
  49. } else if (browser().alipay) {
  50. state.browserStatus = true;
  51. getPayment();
  52. } else {
  53. state.errorText = '请在微信或支付宝客户端打开';
  54. document.title = 'ERROR';
  55. }
  56. }
  57. };
  58. const getPayment = async () => {
  59. try {
  60. if (parseFloat(state.price) <= 0) {
  61. showToast('支付金额异常');
  62. return;
  63. }
  64. const payMap: any = {
  65. merOrderNo: state.orderNo,
  66. paymentChannel: state.pay_channel, // 支付渠道
  67. userId: state.userId
  68. };
  69. // // 判断是否是微信公众号支付
  70. if (state.pay_channel == 'wx_pub') {
  71. payMap.code = state.code;
  72. }
  73. const { data } = await request.post(
  74. '/edu-app/open/userOrder/executePayment',
  75. {
  76. data: {
  77. ...payMap
  78. }
  79. }
  80. );
  81. scanCodePay(data.reqParams);
  82. } catch (e) {
  83. //
  84. console.log(e);
  85. // 接口报错也跳转到支付回调页
  86. window.location.replace(
  87. location.origin +
  88. '/classroom-app/#/payment-result?orderNo=' +
  89. state.orderNo
  90. );
  91. }
  92. };
  93. const scanCodePay = (data: any) => {
  94. // 判断支付方式 如果是 test 模式 支付用测试url 否则用生产url
  95. if (state.pay_channel == 'alipay_qr') {
  96. const url =
  97. data.prod_mode === 'false'
  98. ? data?.expend?.qrcode_url +
  99. '?payment_id=' +
  100. data.id +
  101. '&pay_channel=' +
  102. data.pay_channel
  103. : data?.expend?.qrcode_url;
  104. window.location.href = url;
  105. } else if (state.pay_channel == 'alipay_wap') {
  106. window.location.href = data?.expend?.pay_info;
  107. } else if (state.pay_channel == 'wx_pub') {
  108. const tempPayInfo = data?.expend
  109. ? JSON.parse(data?.expend?.pay_info)
  110. : data;
  111. state.payInfo = tempPayInfo;
  112. if (typeof (window as any).WeixinJSBridge == 'undefined') {
  113. if (document.addEventListener) {
  114. document.addEventListener(
  115. 'WeixinJSBridgeReady',
  116. onBridgeReady,
  117. false
  118. );
  119. } else if ((document as any).attachEvent) {
  120. (document as any)
  121. .attachEvent(
  122. 'WeixinJSBridgeReady',
  123. onBridgeReady
  124. )(document as any)
  125. .attachEvent('onWeixinJSBridgeReady', onBridgeReady);
  126. }
  127. } else {
  128. onBridgeReady();
  129. }
  130. }
  131. };
  132. const onBridgeReady = () => {
  133. const payInfo = state.payInfo;
  134. // let orderNo = state.orderNo
  135. (window as any).WeixinJSBridge.invoke(
  136. 'getBrandWCPayRequest',
  137. {
  138. appId: payInfo.appId, //公众号名称,由商户传入
  139. timeStamp: payInfo.timeStamp, //时间戳,自1970年以来的秒数
  140. nonceStr: payInfo.nonceStr, //随机串
  141. package: payInfo.package || payInfo.packageValue,
  142. signType: payInfo.signType, //微信签名方式:
  143. paySign: payInfo.paySign //微信签名
  144. },
  145. (res: any) => {
  146. // if(res.err_msg == "get_brand_wcpay_request:ok" ){
  147. // 使用以上方式判断前端返回,微信团队郑重提示:
  148. //res.err_msg将在用户支付成功后返回ok,但并不保证它绝对可靠。
  149. // } else
  150. if (
  151. res.err_msg == 'get_brand_wcpay_request:cancel' ||
  152. res.err_msg == 'get_brand_wcpay_request:fail'
  153. ) {
  154. window.location.replace(
  155. location.origin +
  156. '/classroom-app/#/payment-result?orderNo=' +
  157. state.orderNo
  158. );
  159. } else {
  160. // 使用以上方式判断前端返回,微信团队郑重提示:
  161. //res.err_msg将在用户支付成功后返回ok,但并不保证它绝对可靠。
  162. // alert('支付成功')
  163. window.location.replace(
  164. location.origin +
  165. '/classroom-app/#/payment-result?orderNo=' +
  166. state.orderNo
  167. );
  168. }
  169. }
  170. );
  171. };
  172. const getWxPay = () => {
  173. // 微信公众号支付
  174. //授权
  175. const code = getUrlCode();
  176. if (!code) {
  177. // goAuth()
  178. goWechatAuth(state.wxAppId);
  179. } else {
  180. state.code = code;
  181. getPayment();
  182. }
  183. };
  184. // const goAuth = () => {
  185. // // 用户授权
  186. // const urlNow = encodeURIComponent(window.location.href)
  187. // const scope = 'snsapi_base' //snsapi_userinfo //静默授权 用户无感知
  188. // const appid = state.wxAppId || 'wx8654c671631cfade'
  189. // const url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${urlNow}&response_type=code&scope=${scope}&state=STATE&connect_redirect=1#wechat_redirect`
  190. // window.location.replace(url)
  191. // }
  192. onMounted(() => {
  193. init();
  194. });
  195. return () => (
  196. <div class={styles.paywxresult}>
  197. {state.browserStatus && (
  198. <>
  199. <div class={styles.container}>
  200. <CellGroup border={false}>
  201. <Cell
  202. title={'订单金额'}
  203. value={'¥ ' + moneyFormat(state.price)}></Cell>
  204. <Cell title={'订单信息'} value={state.body}></Cell>
  205. </CellGroup>
  206. </div>
  207. <div class={styles['order-loading']}>
  208. <p>{state.pay_channel == 'wx_pub' ? '微信支付' : '支付宝支付'}</p>
  209. <Loading type="spinner" color="#01C1B5" />
  210. </div>
  211. </>
  212. )}
  213. {!state.browserStatus && (
  214. <div class={styles['error-text']}>
  215. {state.errorText && (
  216. <Icon class={styles['error-icon']} name="warning-o" />
  217. )}
  218. {state.errorText}
  219. </div>
  220. )}
  221. </div>
  222. );
  223. }
  224. });