pay-qrcode.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. import socket from "@/utils/socket.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. barOption: {
  54. width: 3,
  55. height: 130,
  56. displayValue: false
  57. },
  58. //webSocket
  59. webSocket: {},
  60. receiveId: '',
  61. payResult: {
  62. isSuccess: false,
  63. msg: "交易失败",
  64. //总价
  65. totalPrice: 0,
  66. //消耗积分值
  67. pointsNum:0,
  68. //消耗现金值
  69. amountNum:0,
  70. }
  71. }
  72. },
  73. async onLoad(options) {
  74. let res = await this.$api.loginUser.detail({
  75. id: this.vuex_userId
  76. })
  77. this.nickName = res.data.nickName
  78. // this.avatar =(await this.initAvatat(res.data.avatar)).tempFilePath;
  79. //连接websocket
  80. this.onSocketOpen()
  81. },
  82. async mounted() {
  83. //获取用户ID
  84. this.id = this.vuex_userId
  85. //生成动态密码
  86. this.refreshCode();
  87. },
  88. onUnload() {
  89. this.handelOut()
  90. },
  91. onHide() {
  92. this.handelOut()
  93. },
  94. methods: {
  95. handelOut() {
  96. uni.hideLoading()
  97. this.webSocket.closeSocket()
  98. clearInterval(this.timer);
  99. },
  100. // 启动webSocket
  101. onSocketOpen() {
  102. this.webSocket = new socket({
  103. sid: this.$global.socket_prefix + this.vuex_userId
  104. })
  105. this.webSocket.init(() => {
  106. console.log("启动成功");
  107. });
  108. this.webSocket.error = () => {
  109. this.webSocket.closeSocket();
  110. this.$dialog.showModalAndBack('网络异常')
  111. }
  112. this.webSocket.acceptMessage = (res) => {
  113. uni.hideLoading()
  114. if (!this.$isEmpty(res.price)) {
  115. this.receiveId = res.receiveId
  116. this.payResult.totalPrice = res.cost
  117. this.payResult.amountNum = res.price
  118. this.payResult.pointsNum =this.$digital.floatSub(res.cost,res.price)
  119. //调起微信支付
  120. this.toPay(res.id)
  121. }
  122. if (res.isSuccess) {
  123. this.payResult=res
  124. this.payResult.pointsNum=res.totalPrice
  125. this.payResult.amountNum = 0
  126. uni.navigateTo({
  127. url: "/pages/checkstand/pay-result?payResult=" + JSON.stringify(res)
  128. })
  129. }
  130. }
  131. },
  132. refreshCode() {
  133. this.timer = setInterval(() => {
  134. this.secret = totp.getSecret(this.id);
  135. console.log(this.secret);
  136. }, 1000)
  137. },
  138. async initAvatat(url) {
  139. return new Promise((reslove, reject) => {
  140. uni.downloadFile({
  141. url,
  142. success: (res) => {
  143. reslove(res);
  144. },
  145. fail: (err) => {
  146. reject(err)
  147. }
  148. })
  149. })
  150. },
  151. async toPay(orderId) {
  152. //去支付
  153. let params = {
  154. orderType: this.$global.orderType.USER_PAY,
  155. orderId,
  156. payStatus: this.$global.payStatus.IS_WAIT
  157. }
  158. let res = await this.$api.pay.payOrder(params)
  159. if (!this.$isEmpty(res.data.prePayTn)) {
  160. let prePayTn = JSON.parse(res.data.prePayTn)
  161. this.$mpi.requestPayment(prePayTn).then(() => {
  162. this.handelResult(true)
  163. }).catch(err => {
  164. this.handelResult(false)
  165. })
  166. return
  167. }
  168. this.handelResult(false)
  169. },
  170. handelResult(flag) {
  171. if (flag) {
  172. this.payResult.msg = '交易成功'
  173. this.payResult.isSuccess = true
  174. } else {
  175. this.payResult.msg = '交易失败'
  176. this.payResult.isSuccess = false
  177. }
  178. this.sendInfo()
  179. uni.navigateTo({
  180. url: "/pages/checkstand/pay-result?payResult=" + JSON.stringify(this.payResult)
  181. })
  182. },
  183. sendInfo() {
  184. let msg = {
  185. sid: this.$global.socket_prefix_SHOP + this.receiveId,
  186. content: JSON.stringify(this.payResult),
  187. }
  188. this.$api.webSocket.sendInfo(msg)
  189. },
  190. },
  191. }
  192. </script>
  193. <style>
  194. page {
  195. background-color: #18b566;
  196. }
  197. </style>
  198. <style lang="scss" scoped>
  199. .code-container {
  200. background-color: #FFFFFF;
  201. border-radius: 16rpx;
  202. min-height: 800rpx;
  203. margin: 60rpx 20rpx;
  204. .title {
  205. border-radius: 25rpx 25rpx 0 0;
  206. height: 100rpx;
  207. width: 100%;
  208. font-size: 35rpx;
  209. color: #18b566;
  210. background-color: #F7F7F7;
  211. font-weight: 600;
  212. letter-spacing: 3rpx;
  213. }
  214. .footer {
  215. width: 90%;
  216. height: 160rpx;
  217. margin-left: 5%;
  218. border-top: 1rpx dashed #dddddd;
  219. }
  220. .main-content {
  221. width: 100%;
  222. height: 600rpx;
  223. scroll-margin-top: 600rpx;
  224. // overflow: hidden;
  225. .bar-code {
  226. margin-left: 5%;
  227. width: 90%;
  228. height: 150rpx;
  229. }
  230. .qr-code {
  231. width: 100%;
  232. height: 450rpx;
  233. }
  234. .charge-code {
  235. width: 100%;
  236. height: 600rpx;
  237. transition: transform 1s ease-in-out;
  238. }
  239. }
  240. }
  241. </style>