index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. <template>
  2. <view class="" :style="{'height':height}">
  3. <!-- #ifdef MP-WEIXIN-->
  4. <view class="">
  5. <u-navbar :isBack="isBack" :title="title"></u-navbar>
  6. </view>
  7. <!-- #endif -->
  8. <view class="" v-show="!payShow">
  9. <view class="shopInfo">
  10. <image :src="shopDetail.cover" mode=""></image>
  11. <text>{{shopDetail.name}}</text>
  12. </view>
  13. <view class="data">
  14. <text style="color: #000;">付款金额</text>
  15. <view class="price">
  16. <view class="input-bar center" @tap="show">
  17. <view class="icon center">¥</view>
  18. <view class="input" style="display: flex; align-items: center;">
  19. <view style="font-size: 80rpx;">{{filterMoney(orderAmount)}}</view>
  20. <view class="cusor"></view>
  21. </view>
  22. </view>
  23. </view>
  24. <view class="text-center text-base padding-top-20">
  25. <text>*使用联兑通付款有优惠打折</text>
  26. </view>
  27. </view>
  28. <!-- #ifdef MP-WEIXIN-->
  29. <view style="margin-top: -20rpx;">
  30. <channel-list ></channel-list>
  31. </view>
  32. <!-- #endif -->
  33. <amountInput ref="amountInput" confirmText="付款" btnColor="#ff9900" placeholder="请输入交易金额" @change="change"
  34. @confirm="debouncePay"></amountInput>
  35. <u-modal v-model="modalShow" :mask-close-able="true" :showCancelButton="true" @cancel="payOfh5"
  36. @confirm="jumpLDT" cancel-text="直接付款" confirm-color="#FF9447" confirmText="去联兑通支付"
  37. content="去联兑通小程序付款有更多优惠哟~"></u-modal>
  38. </view>
  39. <toast ref="toast" ></toast>
  40. <pay-confirm confirmText="确认" :params="params" cancelText="继续支付" :type="payConfirmType" v-show="payShow" :info="info" @confirm="confirmClick" @cancel="cancelClick"></pay-confirm>
  41. </view>
  42. </template>
  43. <script>
  44. import payConfirm from '@/components/pay-confirm.vue'
  45. import channelList from '@/components/channel-list.vue'
  46. import amountInput from '../../comps/amountInput/amountInput.vue';
  47. import crypto from "@/utils/crypto.js"
  48. export default {
  49. components: {
  50. payConfirm,
  51. channelList,
  52. amountInput
  53. },
  54. data() {
  55. return {
  56. height:'',
  57. isH5:false,
  58. //navbar
  59. title:"收银台",
  60. isBack:true,
  61. //第一次点击支付不需要防抖操作
  62. isFirstPay: true,
  63. orderAmount: '',
  64. //支付bills信息
  65. payShow:false,
  66. payConfirmType:'',
  67. info:{},
  68. bills:{},
  69. modalShow: false,
  70. shopId: '',
  71. shopDetail: {},
  72. //支付参数
  73. payParams: {},
  74. //支付结果
  75. payResult: {
  76. isSuccess: false,
  77. msg: "交易失败",
  78. //总价
  79. totalPrice: 0,
  80. //消耗积分值
  81. pointsNum: 0,
  82. //消耗现金值
  83. amountNum: 0,
  84. },
  85. // 是否主动点击取消支付,防止主动调一次取消支付,退出页面也调一次取消支付
  86. isCancelTrade:false,
  87. }
  88. },
  89. async onLoad(options) {
  90. this.height=this.$u.sys().windowHeight + 'px';
  91. if (!this.$isEmpty(options.query)) {
  92. //h5跳转回小程序后重新赋值
  93. let params = options.query.split(";")
  94. this.shopId = params[0]
  95. this.orderAmount = params[1]
  96. this.$refs.amountInput.initialMoney(this.orderAmount)
  97. this.isH5=true
  98. } else {
  99. this.isH5=false
  100. this.shopId = crypto.decrypt(options.data)
  101. }
  102. // #ifdef MP-WEIXIN
  103. //如果是新用户通过h5微信支付扫码进入此页面,跳回首页
  104. if (this.$isEmpty(this.vuex_userId)) {
  105. uni.reLaunch({
  106. url: "/pages/mine/mine?query=" + this.shopId + ";" + this.orderAmount
  107. })
  108. return
  109. }
  110. //如果用户信息为空,阻断
  111. let flag = await this.fetchUserDetail()
  112. if (!flag) {
  113. this.$dialog.showModal('用户信息获取失败', false).then(() => {
  114. uni.reLaunch({
  115. url: "/pages/mine/mine"
  116. })
  117. })
  118. return
  119. }
  120. // #endif
  121. if (this.$isEmpty(this.shopId)) {
  122. this.$dialog.showModalAndBack('商户id不能为空')
  123. return
  124. }
  125. this.fetchShopDetail()
  126. },
  127. onShow() {
  128. console.log(this.orderAmount);
  129. },
  130. onUnload() {
  131. if (!this.isCancelTrade) {
  132. this.cancelTrade()
  133. }
  134. },
  135. onBackPress() {
  136. if (!this.isCancelTrade) {
  137. this.cancelTrade()
  138. }
  139. },
  140. methods: {
  141. handelScanResult(res){
  142. let result=res.result
  143. /**
  144. * urlParams
  145. * url和加密的参数 ["/pagesC/pages/checkstand/index","cA8f31EnoYduLe52YELaV109iQpDEOxcDQYxiizK6BA="]
  146. */
  147. let urlParams=result.split("#")[1].split("?")
  148. //解密参数
  149. let decryptParams=crypto.decrypt(urlParams[1])
  150. if (this.$isEmpty(decryptParams)) {
  151. this.$refs.toast.error('参数异常')
  152. return
  153. }
  154. //重新拼接url
  155. let url=urlParams[0]+"?"+decryptParams
  156. this.jump(url)
  157. },
  158. isWxBrowser(){
  159. let na = navigator.userAgent.toLowerCase()
  160. return na.indexOf('micromessenger') !== -1
  161. },
  162. //防抖支付
  163. debouncePay() {
  164. if (this.isFirstPay) {
  165. this.pay()
  166. this.isFirstPay = false
  167. } else {
  168. this.$u.debounce(this.pay, 500)
  169. }
  170. },
  171. //获取商户信息
  172. fetchShopDetail() {
  173. this.$api.shop.detail({
  174. id: this.shopId
  175. }).then(res => {
  176. if (this.$isEmpty(res.data)) {
  177. this.$dialog.showModalAndBack('获取不到商户信息')
  178. return
  179. }
  180. this.shopDetail = res.data
  181. if (this.$isNotEmpty(this.orderAmount)&&this.isH5) {
  182. //从h5跳转回来后,直接付款
  183. this.pay()
  184. }
  185. }).catch(err => {
  186. this.$dialog.showModalAndBack('获取不到商户信息')
  187. })
  188. },
  189. //获取用户信息
  190. async fetchUserDetail() {
  191. let res = await this.$api.loginUser.detail({
  192. id: this.vuex_userId
  193. })
  194. if (this.$isEmpty(res.data)) {
  195. return false
  196. } else {
  197. return true
  198. }
  199. },
  200. show() {
  201. this.$refs.amountInput.show()
  202. },
  203. change(e) {
  204. if (!e) {
  205. this.orderAmount = 0
  206. } else {
  207. this.orderAmount = e
  208. }
  209. },
  210. filterMoney(value) {
  211. if (!value) {
  212. return ''
  213. } else {
  214. value = value.replace(/\$\s?|(,*)/g, '')
  215. return value.replace(/\B(?=(\d{3})+(?!\d))/g, ',')
  216. }
  217. },
  218. async jumpLDT() {
  219. this.$dialog.showLoading('打开中..')
  220. let params = {
  221. path: "/pagesC/pages/checkstand/index",
  222. query: "query=" + this.shopId + ";" + this.orderAmount
  223. }
  224. let res = await this.$api.wxApp.getGenerateScheme(params)
  225. if (!this.$isEmpty(res.data.openlink)) {
  226. location.href = res.data.openlink
  227. } else {
  228. location.href = "weixin://dl/business/?t=X2CyIQDbgtm"
  229. }
  230. uni.hideLoading()
  231. },
  232. async payOfh5() {
  233. let expireTime = this.$dateTime.getExpireTime(5)
  234. let params = {
  235. expireTime,
  236. money: this.orderAmount,
  237. shopId: this.shopId,
  238. }
  239. let resp = await this.$api.pay.getPayOrderPamamsForH5(params)
  240. let payObj = {
  241. orderType: this.$global.orderType.USER_PAY,
  242. orderId: resp.data.id
  243. }
  244. let res = await this.$api.pay.payOrderOfscanCode(payObj)
  245. let qrCodeUrl=JSON.parse(res.data.result).qrCodeUrl
  246. if (this.$isNotEmpty(qrCodeUrl)) {
  247. location.href = qrCodeUrl
  248. } else {
  249. this.$refs.toast.error('支付失败')
  250. }
  251. },
  252. async pay() {
  253. if (!this.orderAmount.length || this.orderAmount == 0) {
  254. this.$refs.toast.warn('请输入交易金额')
  255. return
  256. }
  257. // #ifdef H5
  258. if (this.$isWxBrowser()) {
  259. //如果是微信浏览器打开
  260. this.modalShow = true
  261. }else{
  262. //直接支付
  263. this.payOfh5()
  264. }
  265. // #endif
  266. // #ifdef MP-WEIXIN
  267. this.toWXPay()
  268. // #endif
  269. },
  270. async toWXPay() {
  271. let expireTime = this.$dateTime.getExpireTime(5)
  272. let params = {
  273. userId: this.vuex_userId,
  274. appId: this.$global.wxParams.APPID,
  275. billsTitle: '用户扫码支付',
  276. money: this.orderAmount,
  277. expireTime,
  278. shopId: this.shopId,
  279. openId: this.$cache.get('userInfo').openId,
  280. }
  281. if (this.$isNotEmpty(this.vuex_channel)) {
  282. params.channelId=this.vuex_channel.id
  283. }
  284. let resp = await this.$api.pay.getPayOrderPamams(params)
  285. this.payParams = resp.data
  286. this.bills=resp.data.bills
  287. if (this.bills.discount==-1) {
  288. this.bills.discount=0
  289. }
  290. if (resp.data.status == '付款成功') {
  291. this.handelResult(true)
  292. } else if (resp.data.status == '待付款') {
  293. if (this.bills.cost==this.bills.price) {
  294. this.doWxPay(this.bills.id)
  295. }else{
  296. this.info={
  297. shop:this.shopDetail,
  298. bills:this.bills,
  299. }
  300. this.payConfirmType=''
  301. this.title="支付确认"
  302. this.isBack=false
  303. this.payShow=true
  304. }
  305. } else {
  306. this.handelResult(false)
  307. }
  308. },
  309. async doWxPay(orderId) {
  310. let obj = {
  311. orderType: this.$global.orderType.USER_PAY,
  312. orderId,
  313. payStatus: this.$global.payStatus.IS_WAIT
  314. }
  315. let res = await this.$api.pay.payOrder(obj)
  316. let prePayTn = JSON.parse(res.data.prePayTn)
  317. this.$mpi.requestPayment(prePayTn).then((res) => {
  318. this.handelResult(true)
  319. }).catch(err => {
  320. this.handelResult(false)
  321. })
  322. },
  323. handelResult(flag) {
  324. if (flag) {
  325. //跳转倒支付结果页
  326. // this.payResult = {
  327. // isSuccess: true,
  328. // msg: "交易成功",
  329. // totalPrice: this.payParams.totalPrice,
  330. // amountNum: this.payParams.bills.price,
  331. // pointsNum: this.$digital.floatSub(this.payParams.totalPrice, this.payParams.bills.price),
  332. // }
  333. // uni.navigateTo({
  334. // url: "/pagesC/pages/checkstand/pay-result?payResult=" + JSON.stringify(this.payResult)
  335. // })
  336. // return
  337. //本页显示支付结果+抵扣详情
  338. this.info={
  339. shop:this.shopDetail,
  340. bills:this.bills,
  341. }
  342. this.payConfirmType='paySuccess'
  343. this.title="支付结果"
  344. this.isBack=false
  345. this.payShow=true
  346. return
  347. }
  348. //交易失败,把冻结的积分重新解冻
  349. this.isCancelTrade=true
  350. this.cancelTrade()
  351. this.payConfirmType=''
  352. this.payResult.msg = '交易失败'
  353. this.payResult.isSuccess = false
  354. uni.navigateTo({
  355. url: "/pagesC/pages/checkstand/pay-result?payResult=" + JSON.stringify(this.payResult)
  356. })
  357. },
  358. confirmClick(){
  359. if (this.payConfirmType=='paySuccess') {
  360. uni.reLaunch({
  361. url:"/pages/mine/mine"
  362. })
  363. return
  364. }
  365. this.doWxPay(this.bills.id)
  366. },
  367. async cancelClick(){
  368. this.isCancelTrade=true
  369. await this.cancelTrade()
  370. this.title="收银台"
  371. this.isBack=true
  372. this.payShow=false
  373. },
  374. async cancelTrade(){
  375. if (this.payConfirmType!='paySuccess') {
  376. let res=await this.$api.pay.cancelTrade(this.payParams)
  377. }
  378. }
  379. }
  380. }
  381. </script>
  382. <style>
  383. page {
  384. background-color: #ffff;
  385. }
  386. </style>
  387. <style lang="scss" scoped>
  388. .shopInfo {
  389. margin: 60rpx 0 20rpx;
  390. display: flex;
  391. justify-content: center;
  392. align-items: center;
  393. flex-direction: column;
  394. image {
  395. border-radius: 50%;
  396. width: 140rpx;
  397. height: 140rpx;
  398. }
  399. text {
  400. margin-top: 30rpx;
  401. font-size: 32rpx;
  402. }
  403. }
  404. .data {
  405. padding: 50rpx;
  406. display: flex;
  407. justify-content: flex-start;
  408. flex-direction: column;
  409. .price {
  410. &-icon {
  411. font-size: 40rpx;
  412. font-weight: 800;
  413. }
  414. }
  415. }
  416. .center {
  417. display: flex;
  418. justify-content: center;
  419. align-items: center;
  420. }
  421. .input-bar {
  422. height: 150rpx;
  423. border-bottom: 1rpx solid #DDDDDD;
  424. .icon {
  425. width: 15%;
  426. font-size: 70rpx;
  427. font-weight: 900;
  428. height: 100%;
  429. }
  430. .input {
  431. width: 85%;
  432. height: 100%;
  433. overflow: hidden;
  434. font-size: 70rpx;
  435. }
  436. .cusor {
  437. margin-left: 10rpx;
  438. width: 6rpx;
  439. border-radius: 20rpx;
  440. height: 60%;
  441. background-color: #ff9900;
  442. animation: blink 1200ms infinite ease-in-out;
  443. }
  444. }
  445. @keyframes blink {
  446. from {
  447. opacity: 0;
  448. }
  449. }
  450. </style>