pay-qrcode1.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <!-- 纯轮询做法 -->
  2. <template>
  3. <view class="code-container">
  4. <view class="title center">{{isPayment? "向商家付款":"扫一扫,向我付款"}}</view>
  5. <view class="main-content">
  6. <view v-if="isPayment">
  7. <!-- 占位 -->
  8. <view style="height: 50rpx;"></view>
  9. <!-- 条形码 -->
  10. <view class="bar-code center">
  11. <tki-barcode ref="barcode" :val="content" onval=true :opations="barOption" />
  12. </view>
  13. <!-- 付款码 -->
  14. <view class="qr-code center">
  15. <tki-qrcode cid="2" ref="qrcode" :val="content" size="300" :onval="true" :loadMake="true"
  16. :usingComponents="true" />
  17. </view>
  18. </view>
  19. <!-- 收款码 -->
  20. <view v-else class="charge-code center">
  21. <tki-qrcode cid="1" ref="qrcode-charge" :val="id" :icon="avatar" size="440" :onval="true"
  22. :loadMake="true" :usingComponents="true" />
  23. </view>
  24. </view>
  25. <view class="footer center">
  26. <text class="text-xl text-bold">{{nickName}}</text>
  27. </view>
  28. </view>
  29. </template>
  30. <script>
  31. import tkiQrcode from "../../comps/tki-qrcode/tki-qrcode.vue"
  32. import tkiBarcode from "../../comps/tki-barcode/tki-barcode.vue"
  33. import totp from "../../utils/totp.js"
  34. export default {
  35. components: {
  36. tkiBarcode,
  37. tkiQrcode
  38. },
  39. computed: {
  40. content() {
  41. let transformStr = this.id
  42. return transformStr + this.secret
  43. }
  44. },
  45. data() {
  46. return {
  47. id: '',
  48. avatar: "",
  49. nickName:'',
  50. secret: '000000',
  51. isPayment: true,
  52. timer: '',
  53. timerBillRecords:null,
  54. barOption: {
  55. width: 3,
  56. height: 130,
  57. displayValue: false
  58. }
  59. }
  60. },
  61. methods: {
  62. refreshCode() {
  63. this.timer = setInterval(() => {
  64. this.secret = totp.getSecret(this.id);
  65. console.log(this.secret);
  66. }, 1000)
  67. },
  68. async initAvatat(url) {
  69. return new Promise((reslove, reject) => {
  70. uni.downloadFile({
  71. url,
  72. success: (res) => {
  73. reslove(res);
  74. },
  75. fail: (err) => {
  76. reject(err)
  77. }
  78. })
  79. })
  80. },
  81. async queryBillRecords(){
  82. let resp=await this.$api.loginUser.getBillrecordFromRedis({userId:this.vuex_userId})
  83. if (this.$isEmpty(resp.data)) {
  84. return
  85. }
  86. if (resp.data.realPayAmount>0) {
  87. clearInterval(this.timerBillRecords)
  88. //去支付
  89. let params={
  90. orderType:this.$global.orderType.USER_PAY,
  91. orderId:resp.data.id,
  92. payStatus:this.$global.payStatus.IS_WAIT
  93. }
  94. let res=await this.$api.pay.payOrder(params)
  95. if (!this.$isEmpty(res.data.prePayTn)) {
  96. let prePayTn= JSON.parse(res.data.prePayTn)
  97. this.$mpi.requestPayment(prePayTn).then(()=>{
  98. this.$dialog.showModal('支付成功',false).then(()=>{
  99. this.$api.loginUser.clearBillRecordCache({userId:this.vuex_userId})
  100. uni.navigateBack({
  101. delta:1
  102. })
  103. })
  104. }).catch(err=>{
  105. this.$dialog.showModal('支付失败',false).then(()=>{
  106. this.$api.loginUser.clearBillRecordCache({userId:this.vuex_userId})
  107. uni.navigateBack({
  108. delta:1
  109. })
  110. })
  111. })
  112. }
  113. }
  114. },
  115. },
  116. async created(option) {
  117. let res = await this.$api.loginUser.detail({
  118. id: this.vuex_userId
  119. })
  120. this.nickName=res.data.nickName
  121. // this.avatar =(await this.initAvatat(res.data.avatar)).tempFilePath;
  122. //清除缓存
  123. await this.$api.loginUser.clearBillRecordCache({userId:this.vuex_userId})
  124. //轮询
  125. this.timerBillRecords = setInterval(this.queryBillRecords,5000);
  126. },
  127. async mounted(option) {
  128. //获取用户ID
  129. this.id = this.vuex_userId
  130. //生成动态密码
  131. this.refreshCode();
  132. },
  133. beforeDestroy() {
  134. clearInterval(this.timer);
  135. clearInterval(this.timerBillRecords);
  136. uni.hideLoading()
  137. }
  138. }
  139. </script>
  140. <style>
  141. page {
  142. background-color: #18b566;
  143. }
  144. </style>
  145. <style lang="scss" scoped>
  146. .code-container {
  147. background-color: #FFFFFF;
  148. border-radius: 16rpx;
  149. min-height: 800rpx;
  150. margin: 60rpx 20rpx;
  151. .title {
  152. border-radius: 25rpx 25rpx 0 0;
  153. height: 100rpx;
  154. width: 100%;
  155. font-size: 35rpx;
  156. color: #18b566;
  157. background-color: #F7F7F7;
  158. font-weight: 600;
  159. letter-spacing: 3rpx;
  160. }
  161. .footer {
  162. width: 90%;
  163. height: 160rpx;
  164. margin-left: 5%;
  165. border-top: 1rpx dashed #dddddd;
  166. }
  167. .main-content {
  168. width: 100%;
  169. height: 600rpx;
  170. scroll-margin-top: 600rpx;
  171. // overflow: hidden;
  172. .bar-code {
  173. margin-left: 5%;
  174. width: 90%;
  175. height: 150rpx;
  176. }
  177. .qr-code {
  178. width: 100%;
  179. height: 450rpx;
  180. }
  181. .charge-code {
  182. width: 100%;
  183. height: 600rpx;
  184. transition: transform 1s ease-in-out;
  185. }
  186. }
  187. }
  188. </style>