pay-qrcode.vue 7.0 KB

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