pay.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. <template>
  2. <view class="app">
  3. <view class="price-box">
  4. <text>支付金额</text>
  5. <text class="price">{{ money }}</text>
  6. </view>
  7. <view class="pay-type-list">
  8. <view class="type-item b-b" @click="changePayType(1)">
  9. <text class="icon iconfont iconweixin"></text>
  10. <view class="con">
  11. <text class="tit">微信支付</text>
  12. <text>推荐使用微信支付</text>
  13. </view>
  14. <label class="radio">
  15. <radio value="" color="#5dbc7c" :checked="payType == 1"></radio>
  16. </label>
  17. </view>
  18. <!-- #ifdef APP-PLUS -->
  19. <view class="type-item b-b" @click="changePayType(2)">
  20. <text class="icon iconfont iconzhifubao"></text>
  21. <view class="con"><text class="tit">支付宝支付</text></view>
  22. <label class="radio">
  23. <radio value="" color="#5dbc7c" :checked="payType == 2"></radio>
  24. </label>
  25. </view>
  26. <!-- #endif -->
  27. <view class="type-item" @click="changePayType(3)">
  28. <text class="icon iconfont iconyue"></text>
  29. <view class="con">
  30. <text class="tit">余额支付</text>
  31. <text>可用余额{{ now_money }}</text>
  32. </view>
  33. <label class="radio">
  34. <radio value="" color="#5dbc7c" :checked="payType == 3"></radio>
  35. </label>
  36. </view>
  37. <!-- <view class="type-item" @click="changePayType(4)">
  38. <image src="../../static/icon/i8.png" mode=""
  39. style="width: 32rpx; height: 32rpx;margin-right: 70rpx;margin-left: 10rpx;"></image>
  40. <view class="con">
  41. <text class="tit">代付</text>
  42. </view>
  43. <label class="radio">
  44. <radio value="" color="#5dbc7c" :checked="payType == 4"></radio>
  45. </label>
  46. </view> -->
  47. <!-- <view class="type-item" @click="changePayType(5)">
  48. <image src="../../static/icon/i9.png" mode=""
  49. style="width: 32rpx; height: 32rpx;margin-right: 58rpx;margin-left: 10rpx;"></image>
  50. <view class="con">
  51. <text class="tit">第三方支付</text>
  52. </view>
  53. <label class="radio">
  54. <radio value="" color="#5dbc7c" :checked="payType == 5"></radio>
  55. </label>
  56. </view> -->
  57. </view>
  58. <text class="mix-btn" :class="{ clickbg: payLoding }" @click="!payLoding ? confirm() : ''">确认支付</text>
  59. <uni-popup ref="popupdf" type="center" :maskClick="false">
  60. <view class="df-wrap">
  61. <view class="ewm">
  62. <!-- <image :src="erweimasrc" mode=""></image> -->
  63. <tki-qrcode cid="tki-qrcode-canvass" ref="qrcodes" :val="val" :size="size" :unit="unit" :background="background"
  64. :foreground="foreground" :pdground="pdground" :icon="icon" :iconSize="iconSize" :lv="lv" :onval="onval"
  65. :showLoading="showLoading" :loadMake="loadMake" :usingComponents="usingComponents" @result="qrRs"/>
  66. </view>
  67. <view class="dftit" @click="comfirm(val)">
  68. 代付码:{{val}}
  69. </view>
  70. <view class="dftip">
  71. 扫一扫为他代付
  72. </view>
  73. <view class="btn" @click="closeDf">
  74. 代付完成,返回首页
  75. </view>
  76. </view>
  77. </uni-popup>
  78. <view class="qrimg">
  79. <tki-qrcode :cid="cid" ref="qrcode" :val="val" :size="size" :unit="unit" :background="background"
  80. :foreground="foreground" :pdground="pdground" :icon="icon" :iconSize="iconSize" :lv="lv" :onval="onval"
  81. :showLoading="showLoading" :loadMake="loadMake" :usingComponents="usingComponents" @result="qrR" />
  82. </view>
  83. </view>
  84. </template>
  85. <script>
  86. import {
  87. balance
  88. } from '@/api/wallet.js';
  89. import {
  90. createOrderkey,
  91. computedOrderkey,
  92. orderPay
  93. } from '@/api/order.js';
  94. import {
  95. mapState
  96. } from 'vuex';
  97. // #ifdef H5
  98. import weichatObj from '@/plugin/jweixin-module/index.js';
  99. // #endif
  100. export default {
  101. data() {
  102. return {
  103. cid: 'tki-qrcode-canvas', //canvasId,页面存在多个二维码组件时需设置不同的ID
  104. size: 300, //生成的二维码大小
  105. unit: 'upx', //大小单位尺寸
  106. val: '', //要生成的内容
  107. background: '#ffffff', //二维码背景色
  108. foreground: '#333333', //二维码前景色
  109. pdground: '#333333', //二维码角标色
  110. icon: '', //二维码图标URL(必须是本地图片,网络图需要先下载至本地)
  111. iconSize: 40, //二维码图标大小
  112. lv: 3, //容错级别
  113. onval: true, //监听val值变化自动重新生成二维码
  114. loadMake: true, //组件初始化完成后自动生成二维码,val需要有值
  115. usingComponents: false, //是否使用了自定义组件模式(主要是为了修复非自定义组件模式时 v-if 无法生成二维码的问题)
  116. showLoading: false, //是否显示loading
  117. payType: 3, //支付类型
  118. // #ifdef H5
  119. payName: 'yue',
  120. // #endif
  121. // #ifdef APP-PLUS
  122. payName: 'yue',
  123. // #endif
  124. // #ifdef MP-WEIXIN
  125. payName: 'yue',
  126. // #endif
  127. orderInfo: {},
  128. money: 0.0, //订单金额
  129. now_money: 0.0, //余额
  130. orderKey: '',
  131. orderId: '', //保存订单id
  132. payLoding: false, //判断是否支付中
  133. type: '', //判断是否从订单中进入
  134. // #ifdef H5
  135. froms: '', //保存h5中数据来源对象
  136. // #endif
  137. pinkid: '', //保存拼团商品id
  138. is_mark: 0,
  139. erweimasrc:'',
  140. };
  141. },
  142. computed: {
  143. // #ifdef H5
  144. ...mapState(['weichatObj']),
  145. // #endif
  146. ...mapState('user', ['userInfo'])
  147. },
  148. onLoad(options) {
  149. if (options.is_mark) {
  150. this.is_mark = options.is_mark
  151. }
  152. if (options.type == 1) {
  153. this.type = 1;
  154. this.orderId = options.ordid;
  155. this.money = options.money;
  156. } else {
  157. this.orderKey = options.key;
  158. let prepage = this.$api.prePage();
  159. computedOrderkey({
  160. orderkey: this.orderKey,
  161. couponId: prepage.couponChecked.id, //优惠券编号
  162. addressId: prepage.addressData.id, //地址编号
  163. useIntegral: prepage.checkedPoints ? 1 : 0
  164. }).then(({
  165. data
  166. }) => {
  167. // 获取支付金额
  168. this.money = data.result.pay_price;
  169. });
  170. }
  171. // 保存pinkid
  172. if (options.pinkid) {
  173. this.pinkid = options.pinkid;
  174. }
  175. // 载入余额
  176. balance({}).then(({
  177. data
  178. }) => {
  179. // 获取余额
  180. this.now_money = data.now_money;
  181. });
  182. },
  183. methods: {
  184. comfirm(text) {
  185. console.log(text);
  186. const result = this.uniCopy(text);
  187. if (result === false) {
  188. uni.showToast({
  189. title: '不支持'
  190. });
  191. } else {
  192. uni.showToast({
  193. title: '复制成功',
  194. icon: 'none'
  195. });
  196. }
  197. // this.$refs.popupkf.close();
  198. },
  199. uniCopy(content) {
  200. /**
  201. * 小程序端 和 app端的复制逻辑
  202. */
  203. //#ifndef H5
  204. uni.setClipboardData({
  205. data: content,
  206. success: function() {
  207. console.log('success');
  208. return true;
  209. }
  210. });
  211. //#endif
  212. /**
  213. * H5端的复制逻辑
  214. */
  215. // #ifdef H5
  216. if (!document.queryCommandSupported('copy')) {
  217. //为了兼容有些浏览器 queryCommandSupported 的判断
  218. // 不支持
  219. return false;
  220. }
  221. let textarea = document.createElement('textarea');
  222. textarea.value = content;
  223. textarea.readOnly = 'readOnly';
  224. document.body.appendChild(textarea);
  225. textarea.select(); // 选择对象
  226. textarea.setSelectionRange(0, content.length); //核心
  227. let result = document.execCommand('copy'); // 执行浏览器复制命令
  228. textarea.remove();
  229. return result;
  230. // #endif
  231. },
  232. qrRs() {
  233. },
  234. closeDf() {
  235. this.$refs.popupdf.close()
  236. uni.switchTab({
  237. url:'/pages/index/index'
  238. })
  239. },
  240. qrR(res) {
  241. console.log('zheli');
  242. // this.erweimasrc = res
  243. try{
  244. uni.hideLoading()
  245. this.$refs.popupdf.open()
  246. }catch(e){
  247. //TODO handle the exception
  248. console.log(e);
  249. }
  250. },
  251. //选择支付方式
  252. changePayType(type) {
  253. this.payType = type;
  254. if (this.payType == 1) {
  255. this.payName = 'weixin';
  256. }
  257. if (this.payType == 2) {
  258. this.payName = 'ali';
  259. }
  260. if (this.payType == 3) {
  261. this.payName = 'yue';
  262. }
  263. if (this.payType == 4) {
  264. this.payName = 'paying';
  265. }
  266. if (this.payType == 5) {
  267. this.payName = 'lakala';
  268. }
  269. },
  270. // 支付金额
  271. orderMoneyPay() {
  272. let obj = this;
  273. console.log('支付金额')
  274. orderPay({
  275. uni: obj.orderId,
  276. // #ifdef H5
  277. from: obj.froms ? 'weixin' : 'H5', //来源
  278. // #endif
  279. // #ifdef MP-WEIXIN
  280. from: 'routine', //来源
  281. // #endif
  282. // #ifdef APP-PLUS
  283. from: 'app', //来源
  284. // #endif
  285. paytype: obj.payName //支付类型 weixin-微信 yue-余额
  286. })
  287. .then(e => {
  288. console.log(obj.payName, '支付类型')
  289. console.log(e, '支付金额')
  290. // 判断是否为余额支付
  291. if (obj.payName == 'yue' && e.data.status == 'SUCCESS') {
  292. if (e.status == 200) {
  293. obj.paySuccessTo();
  294. } else {
  295. obj.$api.msg(msg);
  296. }
  297. }
  298. if (obj.payName == 'paying' && e.data.status == 'SUCCESS') {
  299. if (e.status == 200) {
  300. console.log(e.data.result.order_id);
  301. // obj.paySuccessTo();
  302. obj.val = e.data.result.order_id
  303. // console.log(obj.val,'obj.val');
  304. uni.showLoading({
  305. title: '代付码生成中...'
  306. })
  307. } else {
  308. obj.$api.msg(msg);
  309. }
  310. }
  311. if (obj.payName == 'lakala' && e.data.status == 'LAKALA_PAY') {
  312. if (e.status == 200) {
  313. // console.log(e.data.result.order_id);
  314. // obj.paySuccessTo();
  315. let url = e.data.result.jsConfig.counter_url
  316. // console.log(obj.val,'obj.val');
  317. // uni.showLoading({
  318. // title: '代付码生成中...'
  319. // })
  320. // #ifdef APP
  321. plus.runtime.openURL(url);
  322. // #endif
  323. // #ifdef H5
  324. window.open(url)
  325. // #endif
  326. setTimeout(()=>{
  327. uni.switchTab({
  328. url:'/pages/index/index'
  329. })
  330. },1500)
  331. } else {
  332. obj.$api.msg(msg);
  333. }
  334. }
  335. if (obj.payName == 'weixin' || obj.payName == 'routine') {
  336. let da = e.data.result.jsConfig;
  337. let data = {
  338. // #ifdef H5
  339. timestamp: da.timestamp,
  340. // #endif
  341. // #ifdef MP
  342. timeStamp: da.timestamp,
  343. // #endif
  344. nonceStr: da.nonceStr,
  345. package: da.package,
  346. signType: da.signType,
  347. paySign: da.paySign,
  348. success: function(res) {
  349. obj.paySuccessTo();
  350. },
  351. fail: () => {
  352. uni.navigateTo({
  353. url: '/pages/order/order?state=0'
  354. });
  355. }
  356. };
  357. // #ifdef H5
  358. if (obj.payName == 'weixin') {
  359. weichatObj.chooseWXPay(data);
  360. }
  361. // #endif
  362. // #ifdef MP-WEIXIN
  363. if (obj.payName == 'weixin' || obj.payName == 'routine') {
  364. console.log('开始支付')
  365. wx.requestPayment(data);
  366. }
  367. // #endif
  368. }
  369. uni.hideLoading();
  370. obj.payLoding = false;
  371. })
  372. .catch(e => {
  373. // 支付完成
  374. uni.hideLoading();
  375. obj.payLoding = false;
  376. console.log(e);
  377. });
  378. },
  379. // 支付成功跳转
  380. paySuccessTo() {
  381. let obj = this
  382. uni.hideLoading();
  383. uni.redirectTo({
  384. url: '/pages/money/paySuccess?orderid=' + this.orderId
  385. });
  386. },
  387. //确认支付
  388. confirm: async function() {
  389. let obj = this;
  390. // 判断是否余额不足
  391. if (obj.payName == 'yue' && +obj.now_money < obj.money) {
  392. uni.showModal({
  393. title: '提示',
  394. content: '账户余额不足!',
  395. showCancel: false,
  396. success: res => {},
  397. fail: () => {},
  398. complete: () => {}
  399. });
  400. return;
  401. }
  402. if (obj.payName == 'againjifen' && +obj.userInfo.again_integral < obj.money) {
  403. uni.showModal({
  404. title: '提示',
  405. content: '账户复购积分不足!',
  406. showCancel: false,
  407. success: res => {},
  408. fail: () => {},
  409. complete: () => {}
  410. });
  411. return;
  412. }
  413. uni.showLoading({
  414. title: '支付中',
  415. mask: true
  416. })
  417. // 支付中
  418. obj.payLoding = true;
  419. // #ifdef H5
  420. // 获取当前是否为微信浏览器
  421. obj.froms = uni.getStorageSync('weichatBrowser') || '';
  422. // #endif
  423. // 判断是否为未支付订单中跳转进入
  424. if (obj.type != 1) {
  425. // 初次生成订单
  426. obj.firstCreateOrder();
  427. } else {
  428. // 已经生成订单未支付
  429. obj.orderMoneyPay();
  430. }
  431. },
  432. // 初次订单创建
  433. firstCreateOrder() {
  434. let obj = this;
  435. // 获取下单页面数据
  436. let prepage = obj.$api.prePage();
  437. let data = {
  438. is_agent:prepage.is_agent,
  439. is_level: prepage.is_level,
  440. is_pack: prepage.is_pack,
  441. real_name: prepage.addressData.real_name, //联系人名称
  442. phone: prepage.addressData.phone, //联系人号码
  443. couponId: prepage.couponChecked.id, //优惠券编号
  444. addressId: prepage.addressData.id, //支付地址id
  445. useIntegral: prepage.checkedPoints ? 1 : 0, //是否积分抵扣1为是0为否
  446. payType: obj.payName, //支付类型 weixin-微信 yue-余额
  447. mark: prepage.desc, //备注
  448. // #ifdef H5
  449. from: obj.froms ? 'weixin' : 'H5', //来源
  450. // #endif
  451. // #ifdef MP-WEIXIN
  452. from: 'routine', //来源
  453. // #endif
  454. // #ifdef APP-PLUS
  455. from: 'app', //来源
  456. // #endif
  457. shipping_type: prepage.tabCurrentIndex == 0?1:2,//提货方式 1 快递 2自提
  458. store_id: prepage.tabCurrentIndex == 0?'':prepage.checkedPoint.id
  459. };
  460. // 判断是否拼团商品
  461. if (obj.pinkid) {
  462. data.pinkId = obj.pinkid;
  463. }
  464. // 生成订单
  465. createOrderkey(data, obj.orderKey)
  466. .then(({
  467. data,
  468. status,
  469. msg
  470. }) => {
  471. console.log('生成订单')
  472. // 判断是否支付失败
  473. if (data.status == 'ORDER_EXIST') {
  474. uni.showModal({
  475. title: '提示',
  476. content: msg,
  477. showCancel: false
  478. });
  479. uni.hideLoading();
  480. obj.payLoding = false;
  481. return;
  482. }
  483. // 保存订单号
  484. obj.orderId = data.result.orderId;
  485. // 判断是否为余额支付
  486. if (obj.payName == 'yue') {
  487. if (status == 200 && data.status == 'SUCCESS') {
  488. obj.paySuccessTo();
  489. } else {
  490. obj.$api.msg(msg);
  491. }
  492. } else if (obj.payName == 'paying') {
  493. if (status == 200 && data.status == 'SUCCESS') {
  494. // obj.paySuccessTo();
  495. // console.log(data.result.orderId);
  496. obj.val = data.result.orderId
  497. console.log(obj.val,'obj.val');
  498. uni.showLoading({
  499. title: '代付码生成中...'
  500. })
  501. } else {
  502. obj.$api.msg(msg);
  503. }
  504. } else if(obj.payName == 'lakala'){
  505. if (status == 200 && data.status == 'LAKALA_PAY') {
  506. // obj.paySuccessTo();
  507. // console.log(data.result.orderId);
  508. let url = data.result.jsConfig.counter_url
  509. // window.open()
  510. // console.log(obj.val,'obj.val');
  511. // uni.showLoading({
  512. // title: '代付码生成中...'
  513. // })
  514. // #ifdef APP
  515. plus.runtime.openURL(url);
  516. // #endif
  517. // #ifdef H5
  518. window.open(url)
  519. // #endif
  520. setTimeout(()=>{
  521. uni.switchTab({
  522. url:'/pages/index/index'
  523. })
  524. },1500)
  525. } else {
  526. obj.$api.msg(msg);
  527. }
  528. } else {
  529. // 立即支付
  530. obj.orderMoneyPay();
  531. }
  532. })
  533. .catch(e => {
  534. uni.hideLoading();
  535. obj.payLoding = false;
  536. console.log(e);
  537. });
  538. }
  539. }
  540. };
  541. </script>
  542. <style lang="scss">
  543. .app {
  544. width: 100%;
  545. }
  546. .price-box {
  547. background-color: #fff;
  548. height: 265upx;
  549. display: flex;
  550. flex-direction: column;
  551. justify-content: center;
  552. align-items: center;
  553. font-size: 28upx;
  554. color: #909399;
  555. .price {
  556. font-size: 50upx;
  557. color: #303133;
  558. margin-top: 12upx;
  559. &:before {
  560. content: '¥';
  561. font-size: 40upx;
  562. }
  563. }
  564. }
  565. .pay-type-list {
  566. margin-top: 20upx;
  567. background-color: #fff;
  568. padding-left: 60upx;
  569. .type-item {
  570. height: 120upx;
  571. padding: 20upx 0;
  572. display: flex;
  573. justify-content: space-between;
  574. align-items: center;
  575. padding-right: 60upx;
  576. font-size: 30upx;
  577. position: relative;
  578. }
  579. .icon {
  580. width: 100upx;
  581. font-size: 52upx;
  582. }
  583. .iconyue {
  584. color: #fe8e2e;
  585. }
  586. .iconweixin {
  587. color: #36cb59;
  588. }
  589. .iconzhifubao {
  590. color: #01aaef;
  591. }
  592. .tit {
  593. font-size: $font-lg;
  594. color: $font-color-dark;
  595. margin-bottom: 4upx;
  596. }
  597. .con {
  598. flex: 1;
  599. display: flex;
  600. flex-direction: column;
  601. font-size: $font-sm;
  602. color: $font-color-light;
  603. }
  604. }
  605. .mix-btn {
  606. display: flex;
  607. align-items: center;
  608. justify-content: center;
  609. width: 630upx;
  610. height: 80upx;
  611. margin: 80upx auto 30upx;
  612. font-size: $font-lg;
  613. color: #fff;
  614. background-color: $base-color;
  615. border-radius: 10upx;
  616. /* box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4); */
  617. }
  618. .clickbg {
  619. background-color: $color-gray !important;
  620. }
  621. .df-wrap {
  622. text-align: center;
  623. padding:60rpx 40rpx;
  624. background-color: #fff;
  625. border-radius: 20rpx;
  626. position: relative;
  627. .ewm {
  628. // width: 377rpx;
  629. // height: 377rpx;
  630. // border: 3px solid #FF6F0F;
  631. margin: auto;
  632. padding: 23rpx;
  633. border-radius: 15rpx;
  634. image {
  635. width: 100%;
  636. height: 100%;
  637. margin: auto;
  638. }
  639. }
  640. .dftit {
  641. padding: 30rpx 0 15rpx;
  642. font-size: 36rpx;
  643. color: #333333;
  644. }
  645. .dftip {
  646. font-size: 30rpx;
  647. color: #333333;
  648. }
  649. .btn {
  650. width: 600rpx;
  651. line-height: 87rpx;
  652. background: $base-color;
  653. color: #fff;
  654. border-radius: 20rpx;
  655. font-weight: 500;
  656. font-size: 35rpx;
  657. position: absolute;
  658. bottom: -120rpx;
  659. left: 0;
  660. right: 0;
  661. margin: auto;
  662. }
  663. }
  664. .qrimg {
  665. position: absolute;
  666. left: -9999rpx;
  667. top: -9999rpx;
  668. }
  669. </style>