pay-result.vue 3.7 KB

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