pay-qrcode.vue 8.5 KB

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