index.tsx 6.7 KB

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