index.tsx 6.9 KB

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