index.tsx 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. import request from '@/helpers/request';
  2. import { browser, getUrlCode, moneyFormat } from '@/helpers/utils';
  3. import { goAliAuth, goWechatAuth } from '@/state';
  4. import numeral from 'numeral';
  5. import { Button, Cell, CellGroup, Icon, showToast } from 'vant';
  6. import { defineComponent, onMounted, reactive } from 'vue';
  7. import { useRoute } from 'vue-router';
  8. import styles from './index.module.less';
  9. export default defineComponent({
  10. name: 'pay-define',
  11. setup() {
  12. const route = useRoute();
  13. const state = reactive({
  14. browserStatus: false,
  15. errorText: '',
  16. code: null as any,
  17. pay_channel: route.query.pay_channel as any,
  18. wxAppId: route.query.wxAppId as any,
  19. paymentType: route.query.paymentType as any,
  20. alipayAppId: route.query.alipayAppId as any,
  21. body: route.query.body as any,
  22. price: route.query.price as any,
  23. orderNo: route.query.orderNo as any,
  24. userId: route.query.userId as any,
  25. payInfo: {} as any,
  26. isYeePay: false // 是否为易宝支付
  27. });
  28. const getPayment = async () => {
  29. try {
  30. if (parseFloat(state.price) <= 0) {
  31. showToast('支付金额异常');
  32. return;
  33. }
  34. const payMap: any = {
  35. merOrderNo: state.orderNo,
  36. paymentChannel: state.pay_channel, // 支付渠道
  37. userId: state.userId,
  38. code: state.code
  39. };
  40. // 判断是否是微信公众号支付
  41. // if (state.pay_channel == 'wx_pub') {
  42. // payMap.code = state.code;
  43. // }
  44. const { data } = await request.post(
  45. '/edu-app/open/userOrder/executePayment',
  46. {
  47. hideLoading: false,
  48. data: {
  49. ...payMap
  50. }
  51. }
  52. );
  53. state.isYeePay = data.paymentVender?.indexOf('yeepay') !== -1;
  54. scanCodePay(data.reqParams);
  55. } catch (e) {
  56. //
  57. console.log(e);
  58. }
  59. };
  60. const scanCodePay = (data: any) => {
  61. // 判断支付方式 如果是 test 模式 支付用测试url 否则用生产url
  62. if (state.pay_channel == 'alipay_qr') {
  63. if (state.isYeePay) {
  64. tradePay(data.prePayTn);
  65. } else {
  66. const url =
  67. data.prod_mode === 'false'
  68. ? data.expend.qrcode_url +
  69. '?payment_id=' +
  70. data.id +
  71. '&pay_channel=' +
  72. data.pay_channel
  73. : data.expend.qrcode_url;
  74. window.location.href = url;
  75. }
  76. } else if (state.pay_channel == 'wx_pub') {
  77. const tempPayInfo = state.isYeePay
  78. ? JSON.parse(data.prePayTn)
  79. : JSON.parse(data.expend.pay_info);
  80. state.payInfo = tempPayInfo;
  81. if (typeof (window as any).WeixinJSBridge == 'undefined') {
  82. if (document.addEventListener) {
  83. document.addEventListener(
  84. 'WeixinJSBridgeReady',
  85. onBridgeReady,
  86. false
  87. );
  88. } else if ((document as any).attachEvent) {
  89. (document as any)
  90. .attachEvent(
  91. 'WeixinJSBridgeReady',
  92. onBridgeReady
  93. )(document as any)
  94. .attachEvent('onWeixinJSBridgeReady', onBridgeReady);
  95. }
  96. } else {
  97. onBridgeReady();
  98. }
  99. }
  100. };
  101. // 由于js的载入是异步的,所以可以通过该方法,当AlipayJSBridgeReady事件发生后,再执行callback方法
  102. const ready = (callback: any) => {
  103. if ((window as any).AlipayJSBridge) {
  104. callback && callback();
  105. } else {
  106. document.addEventListener('AlipayJSBridgeReady', callback, false);
  107. }
  108. };
  109. const tradePay = (tradeNO: any) => {
  110. ready(function () {
  111. // 通过传入交易号唤起快捷调用方式(注意tradeNO大小写严格)
  112. (window as any).AlipayJSBridge.call(
  113. 'tradePay',
  114. {
  115. tradeNO: tradeNO
  116. },
  117. function (data: any) {
  118. if ('9000' == data.resultCode) {
  119. window.location.replace(
  120. location.origin +
  121. '/classroom-app/#/payment-result?orderNo=' +
  122. state.orderNo
  123. );
  124. } else {
  125. window.location.replace(
  126. location.origin +
  127. '/classroom-app/#/payment-result?orderNo=' +
  128. state.orderNo
  129. );
  130. }
  131. //使用的支付宝内置api实现关闭H5
  132. (window as any).AlipayJSBridge.call('closeWebview');
  133. }
  134. );
  135. });
  136. };
  137. const onBridgeReady = () => {
  138. const payInfo = state.payInfo;
  139. // let orderNo = state.orderNo
  140. (window as any).WeixinJSBridge.invoke(
  141. 'getBrandWCPayRequest',
  142. {
  143. appId: payInfo.appId, //公众号名称,由商户传入
  144. timeStamp: payInfo.timeStamp, //时间戳,自1970年以来的秒数
  145. nonceStr: payInfo.nonceStr, //随机串
  146. package: payInfo.package,
  147. signType: payInfo.signType, //微信签名方式:
  148. paySign: payInfo.paySign //微信签名
  149. },
  150. (res: any) => {
  151. // if(res.err_msg == "get_brand_wcpay_request:ok" ){
  152. // 使用以上方式判断前端返回,微信团队郑重提示:
  153. //res.err_msg将在用户支付成功后返回ok,但并不保证它绝对可靠。
  154. // } else
  155. if (
  156. res.err_msg == 'get_brand_wcpay_request:cancel' ||
  157. res.err_msg == 'get_brand_wcpay_request:fail'
  158. ) {
  159. window.location.replace(
  160. location.origin +
  161. '/classroom-app/#/payment-result?orderNo=' +
  162. state.orderNo
  163. );
  164. } else {
  165. // 使用以上方式判断前端返回,微信团队郑重提示:
  166. //res.err_msg将在用户支付成功后返回ok,但并不保证它绝对可靠。
  167. // alert('支付成功')
  168. window.location.replace(
  169. location.origin +
  170. '/classroom-app/#/payment-result?orderNo=' +
  171. state.orderNo
  172. );
  173. }
  174. }
  175. );
  176. };
  177. // const goAuth = () => {
  178. // // 用户授权
  179. // const urlNow = encodeURIComponent(window.location.href)
  180. // const scope = 'snsapi_base' //snsapi_userinfo //静默授权 用户无感知
  181. // const appid = state.wxAppId || 'wx8654c671631cfade'
  182. // 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`
  183. // window.location.replace(url)
  184. // }
  185. const init = () => {
  186. const pay_channel = state.pay_channel;
  187. const isYeePay = state.paymentType.indexOf('yeepay') !== -1;
  188. if (browser().weixin) {
  189. if (pay_channel === 'wx_pub') {
  190. //授权
  191. const code = getUrlCode();
  192. if (code) {
  193. state.code = code; // 赋值code码
  194. } else {
  195. goWechatAuth(state.wxAppId);
  196. }
  197. state.browserStatus = true;
  198. document.title = '微信支付';
  199. } else if (pay_channel == 'alipay_qr') {
  200. state.errorText = '请使用支付宝扫码';
  201. }
  202. } else if (browser().alipay) {
  203. if (pay_channel === 'wx_pub') {
  204. state.errorText = '请使用微信扫码';
  205. } else if (pay_channel == 'alipay_qr') {
  206. // 支付宝支付
  207. // 易宝才需要
  208. if (isYeePay) {
  209. //授权
  210. const code = getUrlCode('auth_code');
  211. if (code) {
  212. state.code = code; // 赋值code码
  213. } else {
  214. goAliAuth(state.alipayAppId);
  215. }
  216. }
  217. state.browserStatus = true;
  218. document.title = '支付宝支付';
  219. }
  220. } else {
  221. state.errorText = '请在微信或支付宝客户端打开';
  222. }
  223. state.errorText && (document.title = 'ERROR');
  224. };
  225. onMounted(() => {
  226. console.log(state);
  227. init();
  228. });
  229. return () => (
  230. <div class={styles.paydefine}>
  231. {state.browserStatus && (
  232. <div class={styles.container}>
  233. <div class={styles.amount}>
  234. <span>¥ </span>
  235. {moneyFormat(state.price)}
  236. </div>
  237. <CellGroup inset>
  238. <Cell
  239. title="订单信息"
  240. value={state.body}
  241. valueClass={styles.values}></Cell>
  242. <Cell
  243. title="支付方式"
  244. value={
  245. state.pay_channel === 'wx_pub' ? '微信' : '支付宝'
  246. }></Cell>
  247. <Cell
  248. title="实付金额"
  249. value={`¥ ${moneyFormat(state.price)}元`}></Cell>
  250. </CellGroup>
  251. <Button
  252. type="primary"
  253. block
  254. size="large"
  255. onClick={getPayment}
  256. round>
  257. 立即支付
  258. </Button>
  259. </div>
  260. )}
  261. {!state.browserStatus && (
  262. <div class={styles.container}>
  263. <div class={styles['error-text']}>
  264. {state.errorText && (
  265. <Icon class={styles['error-icon']} name="warning-o" />
  266. )}
  267. {state.errorText}
  268. </div>
  269. </div>
  270. )}
  271. </div>
  272. );
  273. }
  274. });