pay-qrcode.vue 4.4 KB

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