index.tsx 6.9 KB

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