index.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. <template>
  2. <view class="page" v-if="payPrice">
  3. <view class="pay-price">
  4. <view class="price">
  5. <text class="unit">¥</text>
  6. <numberScroll :num='(payPrice / 100.0).toFixed(2)' color="#E93323" width='30' height='50' fontSize='50' />
  7. </view>
  8. <view class="count-down">
  9. 支付剩余时间:
  10. <countDown :is-day="false" :tip-text="' '" :day-text="' '" :hour-text="' : '" :minute-text="' : '" :second-text="' '"
  11. :datatime="invalidTime / 1000" :is-col="true" :bgColor="bgColor" />
  12. </view>
  13. </view>
  14. <view class="payment" >
  15. <view class="title">
  16. 支付方式
  17. </view>
  18. <view class="item acea-row row-between-wrapper" v-for="(item,index) in channels" :key="index" @click="payType(item.code)">
  19. <view class="left acea-row row-between-wrapper">
  20. <view class="iconfont" :class="item.icon"></view>
  21. <view class="text">
  22. <view class=name>{{item.name}}</view>
  23. <view class="info" v-if="item.code === 'wallet'">
  24. {{item.title}} <span class="money">¥{{ fen2yuan(wallet.balance || 0) }}</span>
  25. </view>
  26. <view class="info" v-else>{{item.title}}</view>
  27. </view>
  28. </view>
  29. <view class="iconfont" :class="item.code === channelCode ? 'icon-xuanzhong11 font-num':'icon-weixuan'" />
  30. </view>
  31. </view>
  32. <view class="btn">
  33. <view class="button acea-row row-center-wrapper" @click='goPay(channelCode)'>确认支付</view>
  34. <view class="wait-pay" @click="goReturnUrl('cancel')">暂不支付</view>
  35. </view>
  36. </view>
  37. </template>
  38. <script>
  39. import CountDown from "@/components/countDown";
  40. import numberScroll from '@/components/numberScroll.vue'
  41. import * as PayOrderApi from '@/api/pay/order.js';
  42. import * as PayChannelApi from '@/api/pay/channel.js';
  43. import * as WalletApi from '@/api/pay/wallet.js';
  44. import {fen2yuan} from "../../../utils/util";
  45. import { mapGetters } from "vuex";
  46. import { getDomain } from "@/utils/request.js";
  47. export default {
  48. computed: mapGetters([ 'openid' ]),
  49. components: {
  50. CountDown,
  51. numberScroll
  52. },
  53. data() {
  54. return {
  55. orderId: 0, // 支付单号
  56. returnUrl: '', // 调回地址
  57. payPrice: 0, // 支付金额
  58. invalidTime: 0, // 过期时间
  59. wallet: {},
  60. channelCode: '', // 选中的支付渠道
  61. channels: [{ // 支付方式
  62. name: '微信支付', // 微信公众号
  63. icon: "icon-weixin2",
  64. title: '使用微信快捷支付',
  65. code: "wx_pub"
  66. }, {
  67. name: '微信支付', // 微信小程序
  68. icon: "icon-weixin2",
  69. title: '使用微信快捷支付',
  70. code: "wx_lite"
  71. }, {
  72. name: '支付宝支付',
  73. icon: "icon-zhifubao",
  74. title: '使用支付宝支付',
  75. code: "alipay_wap"
  76. }, {
  77. name: '余额支付',
  78. icon: "icon-yuezhifu",
  79. title: '可用余额',
  80. code: "wallet"
  81. }, {
  82. name: '模拟支付',
  83. icon: "icon-yuezhifu",
  84. title: '使用模拟支付',
  85. code: "mock"
  86. },
  87. // 如下是各种示例接入的示例,默认关闭
  88. {
  89. name: '支付宝支付(PC)', // PC 支付
  90. icon: "icon-zhifubao",
  91. title: '使用支付宝支付',
  92. code: "alipay_pc"
  93. }],
  94. bgColor: {
  95. 'bgColor': '#fff',
  96. 'Color': '#E93323',
  97. 'width': '44rpx',
  98. 'timeTxtwidth': '16rpx',
  99. 'isDay': true
  100. },
  101. }
  102. },
  103. onLoad(options) {
  104. if (options.order_id) {
  105. this.orderId = options.order_id
  106. }
  107. if (options.returnUrl) {
  108. this.returnUrl = decodeURIComponent(options.returnUrl)
  109. }
  110. this.getCashierOrder()
  111. },
  112. methods: {
  113. fen2yuan,
  114. getCashierOrder() {
  115. uni.showLoading({
  116. title: '加载订单中'
  117. });
  118. PayOrderApi.getOrder(this.orderId).then(res => {
  119. // 如果已支付、或者已关闭,则直接跳转
  120. if (!res.data){
  121. this.goReturnUrl('close');
  122. return;
  123. }
  124. if (res.data.status === 10) {
  125. this.goReturnUrl('success');
  126. return;
  127. } else if (res.data.status === 20) {
  128. this.goReturnUrl('close');
  129. return;
  130. }
  131. // 设置属性
  132. this.payPrice = res.data.price
  133. this.invalidTime = res.data.expireTime
  134. console.log(this.channels,'this.channels2222')
  135. // 移除多余的支付渠道;
  136. this.removeDisableChannel(res.data.appId);
  137. uni.hideLoading();
  138. }).catch(err => {
  139. uni.hideLoading();
  140. return this.$util.Tips({
  141. title: err
  142. })
  143. })
  144. },
  145. goPay(channelCode) {
  146. if (!this.orderId) {
  147. return this.$util.Tips({
  148. title: '请选择要支付的订单'
  149. });
  150. }
  151. if (channelCode === 'yue' && parseFloat(number) < parseFloat(this.payPrice)) {
  152. return this.$util.Tips({
  153. title: '余额不足'
  154. });
  155. }
  156. uni.showLoading({
  157. title: '支付中'
  158. });
  159. PayOrderApi.submitOrder({
  160. id: this.orderId,
  161. channelCode: channelCode,
  162. returnUrl: this.getPayReturnUrl(),
  163. channelExtras: { // TODO 芋艿:等登录接入完成,需要改成动态读取
  164. // openid: "ockUAwIZ-0OeMZl9ogcZ4ILrGba0" // wx_pub 微信公众号支付的 openid
  165. // openid: "oLefc4g5GjKWHJjLjMSXB3wX0fD0" // wx_lite 微信小程序支付的 openid
  166. openid: this.openid
  167. }
  168. }).then(res => {
  169. this.handleSubmitOrderResult(res.data);
  170. }).catch(err => {
  171. uni.hideLoading();
  172. this.$util.Tips({
  173. title: err
  174. })
  175. })
  176. },
  177. handleSubmitOrderResult(data) {
  178. // 1. 如果已支付、或者已关闭,则直接跳转
  179. if (data.status === 10) {
  180. this.goReturnUrl('success');
  181. return;
  182. } else if (data.status === 20) {
  183. this.goReturnUrl('close');
  184. return;
  185. }
  186. // 2. 根据 displayMode 模式,进行对应的处理
  187. const displayMode = data.displayMode;
  188. const displayContent = data.displayContent
  189. // 2.1 如果返回的是 URL,则直接跳转
  190. if (displayMode === 'url') {
  191. window.location = displayContent;
  192. return;
  193. }
  194. // 2.2 如果返回的是 APP,则自定义处理
  195. if (displayMode === 'app') {
  196. if (this.channelCode === 'wx_pub') {
  197. this.handleSubmitOrderResultForWxPub(displayContent)
  198. return;
  199. }
  200. if (this.channelCode === 'wx_lite') {
  201. this.handleSubmitOrderResultForWxLite(displayContent)
  202. return;
  203. }
  204. }
  205. },
  206. /**
  207. * 发起微信公众号支付
  208. */
  209. handleSubmitOrderResultForWxPub(displayContent) {
  210. const payConfig = JSON.parse(displayContent);
  211. this.$wechat.pay({
  212. timestamp: payConfig.timeStamp,
  213. nonceStr: payConfig.nonceStr,
  214. package: payConfig.packageValue,
  215. signType: payConfig.signType,
  216. paySign: payConfig.paySign,
  217. }).then(res => {
  218. // 失败的情况
  219. if (res.errMsg === 'chooseWXPay:cancel') {
  220. return this.$util.Tips({
  221. title: '取消微信支付'
  222. });
  223. }
  224. if (res.errMsg) {
  225. return this.$util.Tips({
  226. title: res.errMsg,
  227. icon: 'error'
  228. })
  229. }
  230. // 成功的情况
  231. return this.$util.Tips({
  232. title: '支付成功',
  233. icon: 'success'
  234. }, () => {
  235. this.goReturnUrl('success');
  236. });
  237. }).catch(res => {
  238. return this.$util.Tips({
  239. title: '初始化微信支付失败,请重试或者选择其它支付方式',
  240. icon: 'error'
  241. })
  242. })
  243. },
  244. /**
  245. * 发起微信小程序支付
  246. */
  247. handleSubmitOrderResultForWxLite(displayContent) {
  248. const payConfig = JSON.parse(displayContent);
  249. uni.requestPayment({
  250. timeStamp: payConfig.timeStamp,
  251. nonceStr: payConfig.nonceStr,
  252. package: payConfig.packageValue,
  253. signType: payConfig.signType,
  254. paySign: payConfig.paySign,
  255. success: res => {
  256. uni.hideLoading();
  257. return this.$util.Tips({
  258. title: '支付成功',
  259. icon: 'success'
  260. }, () => {
  261. this.goReturnUrl('success');
  262. });
  263. },
  264. fail: e => {
  265. uni.hideLoading();
  266. // 关闭支付的情况
  267. if (e.errMsg === 'requestPayment:cancel'
  268. || e.errMsg === 'requestPayment:fail cancel') {
  269. return this.$util.Tips({
  270. title: '取消支付'
  271. });
  272. }
  273. return this.$util.Tips({
  274. title: e.errMsg,
  275. icon: 'error'
  276. });
  277. }
  278. })
  279. },
  280. /**
  281. * 移除被禁用的支付渠道
  282. */
  283. removeDisableChannel(appId) {
  284. // 1.1 如果不在小程序里,则移除微信小程序支付
  285. // #ifndef MP
  286. this.channels = this.channels.filter(item => item.code !== 'wx_lite')
  287. // #endif
  288. // #ifdef MP
  289. this.channels = this.channels.filter(item => item.code !== 'wx_pub')
  290. // #endif
  291. // 1.2 如果不是公众号环境,则移除微信公众号支付
  292. // #ifdef MP
  293. if (!this.$wechat.isWeixin()) {
  294. this.channels = this.channels.filter(item => item.code !== 'wx_pub')
  295. }
  296. // #endif
  297. // 2. 读取配置,移除被禁用的支付渠道
  298. PayChannelApi.getEnableChannelCodeList(appId).then(res => {
  299. console.log(res,'我是新写的结果')
  300. this.channels = this.channels.filter(item => res.data.includes(item.code));
  301. console.log(this.channels,'this.channels1111')
  302. // 默认选中第一个
  303. if (this.channels.length > 0) {
  304. this.payType(this.channels[0].code)
  305. }
  306. })
  307. // 3. // 获得钱包
  308. WalletApi.getPayWallet().then(res=>{
  309. this.wallet = res.data;
  310. })
  311. },
  312. /**
  313. * 设置支付方式
  314. */
  315. payType(channelCode) {
  316. this.channelCode = channelCode
  317. },
  318. /**
  319. * 获得支付的 return url
  320. */
  321. getPayReturnUrl() {
  322. // #ifdef H5
  323. return location.href
  324. // #endif
  325. // #ifdef APP-PLUS
  326. const currentDomain = getDomain();
  327. // https://5y413b7433.yicp.fun
  328. return currentDomain + '/pages/goods/order_details/index?order_id=' + this.orderId + '&returnUrl=' + this.returnUrl;
  329. // #endif
  330. return '';
  331. },
  332. /**
  333. * 回到业务的 URL
  334. *
  335. * @param payResult 支付结果
  336. * ① success:支付成功
  337. * ② cancel:取消支付
  338. * ③ close:支付已关闭
  339. */
  340. goReturnUrl(payResult) {
  341. uni.reLaunch({
  342. url: this.returnUrl.indexOf('?') >= 0
  343. ? this.returnUrl + '&payResult=' + payResult
  344. : this.returnUrl + '?payResult=' + payResult
  345. })
  346. },
  347. }
  348. }
  349. </script>
  350. <style lang="scss" scoped>
  351. .page {
  352. .pay-price {
  353. display: flex;
  354. justify-content: center;
  355. flex-direction: column;
  356. align-items: center;
  357. padding: 50rpx 0 40rpx 0;
  358. .price {
  359. color: #E93323;
  360. margin-bottom: 20rpx;
  361. display: flex;
  362. align-items: flex-end;
  363. .unit {
  364. font-size: 34rpx;
  365. font-weight: 500;
  366. line-height: 41rpx;
  367. }
  368. .num {
  369. font-size: 50rpx;
  370. font-weight: 600;
  371. }
  372. }
  373. .count-down {
  374. display: flex;
  375. align-items: center;
  376. background-color: #fff;
  377. padding: 8rpx 28rpx;
  378. border-radius: 40rpx;
  379. font-size: 24rpx;
  380. color: #E93323;
  381. .time {
  382. margin-top: 0 !important;
  383. }
  384. /deep/.red {
  385. margin: 0 !important;
  386. }
  387. }
  388. }
  389. .payment {
  390. width: 690rpx;
  391. border-radius: 14rpx 14rpx;
  392. background-color: #fff;
  393. z-index: 999;
  394. margin: 0 30rpx;
  395. .title {
  396. color: #666666;
  397. font-size: 26rpx;
  398. padding: 30rpx 0 0 30rpx;
  399. }
  400. .payMoney {
  401. font-size: 28rpx;
  402. color: #333333;
  403. text-align: center;
  404. margin-top: 50rpx;
  405. .font-color {
  406. margin-left: 10rpx;
  407. .money {
  408. font-size: 40rpx;
  409. }
  410. }
  411. }
  412. }
  413. .payment.on {
  414. transform: translate3d(0, 0, 0);
  415. }
  416. .icon-xuanzhong11 {
  417. color: #E93323 !important;
  418. }
  419. .payment .item {
  420. border-bottom: 1rpx solid #eee;
  421. height: 130rpx;
  422. margin-left: 30rpx;
  423. padding-right: 30rpx;
  424. }
  425. .payment .item:last-child {
  426. border-bottom: none;
  427. }
  428. .payment .item .left {
  429. flex: 1;
  430. }
  431. .payment .item .left .text {
  432. flex: 1;
  433. }
  434. .payment .item .left .text .name {
  435. font-size: 30rpx;
  436. color: #333;
  437. }
  438. .payment .item .left .text .info {
  439. font-size: 22rpx;
  440. color: #999;
  441. }
  442. .payment .item .left .text .info .money {
  443. color: #ff9900;
  444. }
  445. .payment .item .left .iconfont {
  446. font-size: 50rpx;
  447. color: #09bb07;
  448. margin-right: 28rpx;
  449. }
  450. .payment .item .left .iconfont.icon-zhifubao {
  451. color: #00aaea;
  452. }
  453. .payment .item .left .iconfont.icon-yuezhifu {
  454. color: #ff9900;
  455. }
  456. .payment .item .left .iconfont.icon-yuezhifu1 {
  457. color: #eb6623;
  458. }
  459. .payment .item .left .iconfont.icon-tonglianzhifu1 {
  460. color: #305fd8;
  461. }
  462. .payment .item .iconfont {
  463. font-size: 40rpx;
  464. color: #ccc;
  465. }
  466. .icon-haoyoudaizhifu {
  467. color: #F34C3E !important;
  468. }
  469. .btn {
  470. position: fixed;
  471. left: 30rpx;
  472. display: flex;
  473. flex-direction: column;
  474. align-items: center;
  475. bottom: 30rpx;
  476. bottom: calc(30rpx + constant(safe-area-inset-bottom)); ///兼容 IOS<11.2/
  477. bottom: calc(30rpx + env(safe-area-inset-bottom)); ///兼容 IOS>11.2/
  478. }
  479. .wait-pay {
  480. color: #aaa;
  481. font-size: 24rpx;
  482. padding-top: 20rpx;
  483. }
  484. .button {
  485. width: 690rpx;
  486. height: 90rpx;
  487. border-radius: 45rpx;
  488. color: #FFFFFF;
  489. background-color: #E93323;
  490. }
  491. }
  492. </style>