index.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <template>
  2. <view class="content">
  3. <view>
  4. <view style="padding: 20px;">
  5. <view style="font-size: 16px; padding-bottom: 10px;">选择支付方式</view>
  6. <template v-for="item in payList">
  7. <view v-if="item.enabled" :key="item.name" style="margin-bottom: 10px;">
  8. <button type="default" v-if="item.name === 'alipay'" @click="tradePay(item.name)">支付宝支付</button>
  9. <button type="primary" v-if="item.name === 'wechat'" @click="tradePay(item.name)">微信支付</button>
  10. </view>
  11. </template>
  12. </view>
  13. </view>
  14. </view>
  15. </template>
  16. <script>
  17. import { payConfig } from '@/api/ability/pay/config'
  18. import { tradeJs } from '@/api/ability/pay/wechat'
  19. import { alipayTrade } from '@/api/ability/pay/alipay'
  20. import { checkOrder } from '@/api/order/order'
  21. export default {
  22. name: 'PayDialog',
  23. props: {
  24. orderId: String
  25. },
  26. data() {
  27. return {
  28. // 支付方式
  29. payList: [],
  30. dialogVisible: false,
  31. // 选择支付方式
  32. prepay: true,
  33. // 微信支付
  34. payMethod: '',
  35. // 支付宝相关
  36. aliForm: '',
  37. socket: false
  38. }
  39. },
  40. created() {
  41. // 查找支付方式
  42. payConfig().then(res => {
  43. this.payList = res
  44. })
  45. },
  46. methods: {
  47. tradePay(name) {
  48. // 当前支付方式
  49. this.payMethod = name
  50. if (name === 'wechat') {
  51. this.wechatPay()
  52. }
  53. if (name === 'alipay') {
  54. this.alipayPay()
  55. }
  56. },
  57. // 微信支付
  58. wechatPay() {
  59. let that = this
  60. // 获取当前用户信息
  61. uni.login({
  62. provider: 'weixin',
  63. success: (user) => {
  64. console.log(user)
  65. // 获得JSAPI支付信息
  66. tradeJs({
  67. id: that.orderId,
  68. code: user.code
  69. }).then(res => {
  70. console.log('trade', res)
  71. this.callWechatPay(res)
  72. }).catch(() => {
  73. uni.hideLoading()
  74. })
  75. },
  76. fail: () => {
  77. uni.showToast({
  78. title: "微信登录授权失败",
  79. icon: "none"
  80. });
  81. }
  82. })
  83. },
  84. callWechatPay(data) {
  85. let that = this
  86. uni.hideLoading()
  87. // 仅作为示例,非真实参数信息。
  88. uni.requestPayment({
  89. provider: 'wxpay',
  90. timeStamp: data.timeStamp,
  91. nonceStr: data.nonceStr,
  92. package: data.package,
  93. signType: 'RSA',
  94. paySign: data.paySign,
  95. success: function(res) {
  96. console.log('success:' + JSON.stringify(res));
  97. uni.showToast({
  98. title: "恭喜,支付成功!",
  99. icon: "none"
  100. });
  101. that.$emit('success')
  102. },
  103. fail: function(err) {
  104. console.log('fail:' + JSON.stringify(err));
  105. }
  106. });
  107. },
  108. // 支付宝支付
  109. alipayPay() {
  110. uni.showToast({
  111. title: '抱歉,移动端暂不支持此支付方式!',
  112. icon: "none"
  113. })
  114. },
  115. // 检查订单是否成功
  116. checkState() {
  117. checkOrder(this.orderId).then(res => {
  118. const data = res.data
  119. // 校验订单状态
  120. if (data.orderState === 2) {
  121. this.$message.success('支付成功!')
  122. setTimeout(() => {
  123. window.location.reload()
  124. }, 2000)
  125. } else {
  126. this.$message.warning('订单尚未支付成功,请稍后刷新..')
  127. }
  128. })
  129. },
  130. handleClose() {
  131. this.$emit('update:visible', false)
  132. this.$emit('close')
  133. },
  134. changePay() {
  135. this.prepay = true
  136. this.payMethod = ''
  137. this.weQr = null
  138. this.aliForm = ''
  139. },
  140. // 支付结果回调
  141. onMessage(msg) {
  142. if (msg === 'success' && this.socket) {
  143. this.socket = false
  144. this.$message.success('支付成功!')
  145. setTimeout(() => {
  146. window.location.reload()
  147. }, 2000)
  148. }
  149. }
  150. }
  151. }
  152. </script>