pay-result.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <template>
  2. <view>
  3. <u-navbar :is-back="false" title="收款结果"></u-navbar>
  4. <view class="content">
  5. <image :src="payData.icon" mode=""></image>
  6. <view class="tips">
  7. <text >{{payData.title}}</text>
  8. <text >{{payData.desc}}</text>
  9. </view>
  10. <view class="cu-btn btn radius line-gray" @click="confirm">
  11. 确认
  12. </view>
  13. </view>
  14. </view>
  15. </template>
  16. <script>
  17. import socket from "@/utils/socket.js"
  18. export default {
  19. data() {
  20. return {
  21. //webSocket
  22. webSocket:{},
  23. count: 0,
  24. //支付参数
  25. payParams:{},
  26. //支付数据
  27. payData:{
  28. payStatus:0,
  29. title:'订单处理中',
  30. desc:'正在处理中,请等待',
  31. icon:"/static/icon/pay-handel.png"
  32. },
  33. payAmount:0,
  34. failReason:'订单收款失败'
  35. }
  36. },
  37. beforeDestroy() {
  38. this.handelClose()
  39. },
  40. onUnload() {
  41. this.handelClose()
  42. },
  43. onHide() {
  44. this.handelClose()
  45. },
  46. onLoad(options) {
  47. if (this.$isEmpty(options.payParams)) {
  48. this.$dialog.showModalAndBack('支付参数不能为空')
  49. return
  50. }
  51. this.payParams=JSON.parse(options.payParams)
  52. this.openSocket()
  53. },
  54. methods: {
  55. confirm(){
  56. uni.reLaunch({
  57. url:'/pages/index/index'
  58. })
  59. },
  60. //开启socket begin
  61. openSocket() {
  62. this.webSocket=new socket({
  63. sid:this.$global.SOCKET_PRE + this.vuex_shopId
  64. })
  65. this.webSocket.init(() => {
  66. console.log("启动成功,120s后关闭");
  67. this.payBefore()
  68. this.timeToClose()
  69. });
  70. this.webSocket.acceptMessage = (res) => {
  71. if (res.isSuccess==true) {
  72. //支付成功
  73. this.payAmount=res.totalPrice
  74. this.changePayData(1)
  75. }
  76. if (res.isSuccess==false) {
  77. this.failReason=res.msg
  78. this.changePayData(2)
  79. }
  80. }
  81. this.webSocket.error = (err) => {
  82. this.$dialog.showModalAndBack('网络错误')
  83. }
  84. },
  85. //定时关闭连接
  86. timeToClose() {
  87. let _this = this
  88. this.timerSocket = setInterval(() => {
  89. _this.count++
  90. if (_this.count == 120) {
  91. _this.handelClose()
  92. }
  93. }, 1000)
  94. },
  95. handelClose() {
  96. this.count = 0
  97. clearTimeout(this.timerSocket)
  98. this.timerSocket = null
  99. if (!this.$isEmpty(this.webSocket)) {
  100. this.webSocket.closeSocket()
  101. }
  102. },
  103. //开启socket end
  104. async payBefore(){
  105. this.$api.shop.scanPay(this.payParams).then(res=>{
  106. if (!res.success) {
  107. this.failReason=res.msg
  108. this.changePayData(2)
  109. }
  110. }).catch(err=>{
  111. this.changePayData(2)
  112. })
  113. },
  114. changePayData(type){
  115. if (type==1) {
  116. this.payData={
  117. payStatus:1,
  118. title:'收款成功',
  119. desc:'已收款'+this.payAmount+'元',
  120. icon:"/static/icon/pay-success.png"
  121. }
  122. }else if (type==2) {
  123. this.payData={
  124. payStatus:2,
  125. title:'收款失败',
  126. desc:this.failReason,
  127. icon:"/static/icon/pay-fail.png"
  128. }
  129. }
  130. this.handelClose()
  131. }
  132. }
  133. }
  134. </script>
  135. <style>
  136. page{
  137. background-color: #FFFFFF;
  138. }
  139. </style>
  140. <style lang="scss" scoped>
  141. .content{
  142. margin-top: 180rpx;
  143. display: flex;
  144. justify-content: center;
  145. align-items: center;
  146. flex-direction: column;
  147. image{
  148. width: 150rpx;
  149. height: 150rpx;
  150. }
  151. .tips{
  152. margin-top: 50rpx;
  153. display: flex;
  154. flex-direction: column;
  155. text-align: center;
  156. text:first-child{
  157. font-size: 38rpx;
  158. }
  159. text:last-child{
  160. margin-top: 20rpx;
  161. font-size: 28rpx;
  162. color: #969696;
  163. }
  164. }
  165. .btn{
  166. margin-top: 180rpx;
  167. width: 60%;
  168. padding: 42rpx;
  169. }
  170. }
  171. </style>