pay-qrcode.vue 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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="320" :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 flex-direction">
  25. <text class="text-xl text-bold">{{nickName}}</text>
  26. <!-- #ifdef MP-WEIXIN-->
  27. <view style="width: 100%;padding: 0;">
  28. <channel-list></channel-list>
  29. </view>
  30. <!-- #endif -->
  31. </view>
  32. </view>
  33. </template>
  34. <script>
  35. import channelList from '@/components/channel-list.vue'
  36. import tkiQrcode from "../../comps/tki-qrcode/tki-qrcode.vue"
  37. import tkiBarcode from "../../comps/tki-barcode/tki-barcode.vue"
  38. import totp from "../../utils/totp.js"
  39. import socket from "../../utils/socket.js"
  40. export default {
  41. components: {
  42. channelList,
  43. tkiBarcode,
  44. tkiQrcode
  45. },
  46. computed: {
  47. content() {
  48. let transformStr = this.id
  49. return transformStr + this.secret + ";" + (this.vuex_channel ? this.vuex_channel.id : 0)
  50. }
  51. },
  52. data() {
  53. return {
  54. id: '',
  55. avatar: "",
  56. nickName: '',
  57. secret: '000000',
  58. isPayment: true,
  59. timer: '',
  60. barOption: {
  61. width: 3,
  62. height: 130,
  63. displayValue: false
  64. },
  65. //webSocket
  66. webSocket: {},
  67. receiveId: '',
  68. payResult: {
  69. isSuccess: false,
  70. msg: "交易失败",
  71. //总价
  72. totalPrice: 0,
  73. //消耗积分值
  74. pointsNum: 0,
  75. //消耗现金值
  76. amountNum: 0,
  77. }
  78. }
  79. },
  80. async onLoad(options) {
  81. let res = await this.$api.loginUser.detail({
  82. id: this.vuex_userId
  83. })
  84. this.nickName = res.data.nickName
  85. // this.avatar =(await this.initAvatat(res.data.avatar)).tempFilePath;
  86. //连接websocket
  87. this.onSocketOpen()
  88. },
  89. async mounted() {
  90. //获取用户ID
  91. this.id = this.vuex_userId
  92. //生成动态密码
  93. this.refreshCode();
  94. },
  95. onUnload() {
  96. this.handelOut()
  97. },
  98. onHide() {
  99. this.handelOut()
  100. },
  101. onBackPress() {
  102. this.handelOut()
  103. },
  104. methods: {
  105. handelOut() {
  106. uni.hideLoading()
  107. this.webSocket.closeSocket()
  108. clearInterval(this.timer);
  109. },
  110. // 启动webSocket
  111. onSocketOpen() {
  112. this.webSocket = new socket({
  113. sid: this.$global.socket_prefix + this.vuex_userId
  114. })
  115. this.webSocket.init(() => {
  116. console.log("启动成功");
  117. });
  118. this.webSocket.error = () => {
  119. this.webSocket.closeSocket();
  120. this.$dialog.showModalAndBack('网络异常')
  121. }
  122. this.webSocket.acceptMessage = (res) => {
  123. uni.hideLoading()
  124. if (!this.$isEmpty(res.price)) {
  125. this.receiveId = res.receiveId
  126. this.payResult.totalPrice = res.cost
  127. this.payResult.amountNum = res.price
  128. this.payResult.pointsNum = this.$digital.floatSub(res.cost, res.price)
  129. //调起微信支付
  130. this.toPay(res.id)
  131. }
  132. if (res.isSuccess) {
  133. console.log(res);
  134. this.payResult = res
  135. this.payResult.pointsNum = res.totalPrice
  136. this.payResult.amountNum = 0
  137. uni.navigateTo({
  138. url: "/pagesC/pages/checkstand/pay-result?payResult=" + JSON.stringify(res),
  139. })
  140. }
  141. }
  142. },
  143. refreshCode() {
  144. this.timer = setInterval(() => {
  145. this.secret = totp.getSecret(this.id);
  146. console.log(this.secret);
  147. }, 1000)
  148. },
  149. async initAvatat(url) {
  150. return new Promise((reslove, reject) => {
  151. uni.downloadFile({
  152. url,
  153. success: (res) => {
  154. reslove(res);
  155. },
  156. fail: (err) => {
  157. reject(err)
  158. }
  159. })
  160. })
  161. },
  162. async toPay(orderId) {
  163. //去支付
  164. let params = {
  165. orderType: this.$global.orderType.USER_PAY,
  166. orderId,
  167. payStatus: this.$global.payStatus.IS_WAIT
  168. }
  169. let res = await this.$api.pay.payOrder(params)
  170. if (!this.$isEmpty(res.data.prePayTn)) {
  171. let prePayTn = JSON.parse(res.data.prePayTn)
  172. this.$mpi.requestPayment(prePayTn).then(() => {
  173. this.handelResult(true)
  174. }).catch(err => {
  175. this.handelResult(false)
  176. })
  177. return
  178. }
  179. this.handelResult(false)
  180. },
  181. handelResult(flag) {
  182. if (flag) {
  183. this.payResult.msg = '交易成功'
  184. this.payResult.isSuccess = true
  185. } else {
  186. this.payResult.msg = '交易失败'
  187. this.payResult.isSuccess = false
  188. }
  189. this.sendInfo()
  190. uni.navigateTo({
  191. url: "/pagesC/pages/checkstand/pay-result?payResult=" + JSON.stringify(this.payResult)
  192. })
  193. },
  194. sendInfo() {
  195. let msg = {
  196. sid: this.$global.socket_prefix_SHOP + this.receiveId,
  197. content: JSON.stringify(this.payResult),
  198. }
  199. this.$api.webSocket.sendInfo(msg)
  200. },
  201. },
  202. }
  203. </script>
  204. <style>
  205. page {
  206. background-color: #18b566;
  207. }
  208. </style>
  209. <style lang="scss" scoped>
  210. .popup-content {
  211. display: flex;
  212. flex-direction: column;
  213. justify-content: space-between;
  214. padding: 40rpx 0;
  215. height: 100%;
  216. .channelbtn {
  217. background-color: #18b566;
  218. color: #FFFFFF;
  219. padding: 36rpx 150rpx;
  220. }
  221. .channel-item {
  222. position: relative;
  223. display: flex;
  224. padding: 30rpx;
  225. border-bottom: 1rpx solid #eee;
  226. image {
  227. width: 40rpx;
  228. height: 40rpx;
  229. margin-right: 10rpx;
  230. }
  231. .checked {
  232. color: #18b566;
  233. font-size: 34rpx;
  234. position: absolute;
  235. font-weight: 800;
  236. right: 40rpx;
  237. bottom: 30rpx;
  238. }
  239. }
  240. }
  241. .channel {
  242. display: flex;
  243. justify-content: space-between;
  244. margin-top: 40rpx;
  245. .left {
  246. display: flex;
  247. image {
  248. width: 60rpx;
  249. height: 60rpx;
  250. display: flex;
  251. justify-content: center;
  252. align-items: center;
  253. }
  254. view {
  255. display: flex;
  256. flex-direction: column;
  257. }
  258. .content {
  259. margin-left: 16rpx;
  260. text:first-child {
  261. font-size: 34rpx;
  262. margin-bottom: 10rpx;
  263. }
  264. text:last-child {
  265. font-size: 28rpx;
  266. color: #989898;
  267. }
  268. }
  269. }
  270. .right {
  271. display: flex;
  272. justify-content: center;
  273. align-items: center;
  274. }
  275. }
  276. .code-container {
  277. background-color: #FFFFFF;
  278. border-radius: 16rpx;
  279. min-height: 800rpx;
  280. margin: 60rpx 20rpx;
  281. .title {
  282. border-radius: 25rpx 25rpx 0 0;
  283. height: 100rpx;
  284. width: 100%;
  285. font-size: 35rpx;
  286. color: #18b566;
  287. background-color: #F7F7F7;
  288. font-weight: 600;
  289. letter-spacing: 3rpx;
  290. }
  291. .footer {
  292. width: 100%;
  293. padding: 30rpx;
  294. border-top: 1rpx dashed #dddddd;
  295. }
  296. .main-content {
  297. width: 100%;
  298. height: 600rpx;
  299. scroll-margin-top: 600rpx;
  300. // overflow: hidden;
  301. .bar-code {
  302. margin-left: 5%;
  303. width: 90%;
  304. height: 150rpx;
  305. }
  306. .qr-code {
  307. width: 100%;
  308. height: 450rpx;
  309. }
  310. .charge-code {
  311. width: 100%;
  312. height: 600rpx;
  313. transition: transform 1s ease-in-out;
  314. }
  315. }
  316. }
  317. </style>