payt.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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>
  38. <text class="mix-btn" :class="{ clickbg: payLoding }" @click="!payLoding ? confirm() : ''">确认支付</text>
  39. <uni-popup ref="popupdf" type="center" maskClick="false">
  40. <view class="df-wrap">
  41. <view class="ewm">
  42. <image :src="erweimasrc" mode=""></image>
  43. </view>
  44. <view class="dftit">
  45. 代付码:{{val}}
  46. </view>
  47. <view class="dftip">
  48. 扫一扫为他代付
  49. </view>
  50. <view class="btn" @click="closeDf">
  51. 代付完成,返回首页
  52. </view>
  53. </view>
  54. </uni-popup>
  55. <tki-qrcode :cid="cid" ref="qrcode" :val="val" :size="size" :unit="unit" :background="background"
  56. :foreground="foreground" :pdground="pdground" :icon="icon" :iconSize="iconSize" :lv="lv" :onval="onval"
  57. :showLoading="showLoading" :loadMake="loadMake" :usingComponents="usingComponents" @result="qrR" style="z-index: -1;"/>
  58. </view>
  59. </template>
  60. <script>
  61. import {
  62. balance
  63. } from '@/api/wallet.js';
  64. import {
  65. createOrderkey,
  66. computedOrderkey,
  67. orderPay,
  68. payingInfo,
  69. payingPay
  70. } from '@/api/order.js';
  71. import {
  72. mapState
  73. } from 'vuex';
  74. // #ifdef H5
  75. import weichatObj from '@/plugin/jweixin-module/index.js';
  76. // #endif
  77. export default {
  78. data() {
  79. return {
  80. cid: 'tki-qrcode-canvas', //canvasId,页面存在多个二维码组件时需设置不同的ID
  81. size: 300, //生成的二维码大小
  82. unit: 'upx', //大小单位尺寸
  83. val: '', //要生成的内容
  84. background: '#ffffff', //二维码背景色
  85. foreground: '#333333', //二维码前景色
  86. pdground: '#333333', //二维码角标色
  87. icon: '', //二维码图标URL(必须是本地图片,网络图需要先下载至本地)
  88. iconSize: 40, //二维码图标大小
  89. lv: 3, //容错级别
  90. onval: true, //监听val值变化自动重新生成二维码
  91. loadMake: true, //组件初始化完成后自动生成二维码,val需要有值
  92. usingComponents: false, //是否使用了自定义组件模式(主要是为了修复非自定义组件模式时 v-if 无法生成二维码的问题)
  93. showLoading: false, //是否显示loading
  94. payType: 1, //支付类型
  95. // #ifdef H5
  96. payName: 'weixin',
  97. // #endif
  98. // #ifdef MP-WEIXIN
  99. payName: 'weixin',
  100. // #endif
  101. orderInfo: {},
  102. money: 0.0, //订单金额
  103. now_money: 0.0, //余额
  104. orderKey: '',
  105. orderId: '', //保存订单id
  106. payLoding: false, //判断是否支付中
  107. type: '', //判断是否从订单中进入
  108. // #ifdef H5
  109. froms: '', //保存h5中数据来源对象
  110. // #endif
  111. pinkid: '', //保存拼团商品id
  112. is_mark: 0,
  113. erweimasrc:'',
  114. uni: '',
  115. };
  116. },
  117. computed: {
  118. // #ifdef H5
  119. ...mapState(['weichatObj']),
  120. // #endif
  121. ...mapState('user', ['userInfo'])
  122. },
  123. onLoad(options) {
  124. this.getBaseInfo(options.uni)
  125. // 载入余额
  126. balance({}).then(({
  127. data
  128. }) => {
  129. // 获取余额
  130. this.now_money = data.now_money;
  131. });
  132. },
  133. methods: {
  134. getBaseInfo(uni) {
  135. this.orderId = uni
  136. payingInfo({
  137. uni
  138. }).then(res => {
  139. this.money = res.data.total_price
  140. })
  141. },
  142. closeDf() {
  143. this.$refs.popupdf.close()
  144. uni.switchTab({
  145. url:'/pages/index/index'
  146. })
  147. },
  148. qrR(res) {
  149. console.log(res);
  150. this.erweimasrc = res
  151. uni.hideLoading()
  152. this.$refs.popupdf.open()
  153. },
  154. //选择支付方式
  155. changePayType(type) {
  156. this.payType = type;
  157. if (this.payType == 1) {
  158. this.payName = 'weixin';
  159. }
  160. if (this.payType == 2) {
  161. this.payName = 'ali';
  162. }
  163. if (this.payType == 3) {
  164. this.payName = 'yue';
  165. }
  166. if (this.payType == 4) {
  167. this.payName = 'paying';
  168. }
  169. },
  170. // 支付金额
  171. orderMoneyPay() {
  172. let obj = this;
  173. // console.log('支付金额')
  174. payingPay({
  175. uni: obj.orderId,
  176. // #ifdef H5
  177. from: obj.froms ? 'weixin' : 'H5', //来源
  178. // #endif
  179. // #ifdef MP-WEIXIN
  180. from: 'routine', //来源
  181. // #endif
  182. // #ifdef APP-PLUS
  183. from: 'app', //来源
  184. // #endif
  185. paytype: obj.payName //支付类型 weixin-微信 yue-余额
  186. })
  187. .then(e => {
  188. console.log(obj.payName, '支付类型')
  189. console.log(e, '支付金额')
  190. // 判断是否为余额支付
  191. if (obj.payName == 'yue' && e.data.status == 'SUCCESS') {
  192. if (e.status == 200) {
  193. obj.paySuccessTo();
  194. } else {
  195. obj.$api.msg(msg);
  196. }
  197. }
  198. if (obj.payName == 'weixin' || obj.payName == 'routine') {
  199. let da = e.data.result.jsConfig;
  200. let data = {
  201. // #ifdef H5
  202. timestamp: da.timestamp,
  203. // #endif
  204. // #ifdef MP
  205. timeStamp: da.timestamp,
  206. // #endif
  207. nonceStr: da.nonceStr,
  208. package: da.package,
  209. signType: da.signType,
  210. paySign: da.paySign,
  211. success: function(res) {
  212. obj.paySuccessTo();
  213. },
  214. fail: () => {
  215. uni.navigateTo({
  216. url: '/pages/order/order?state=0'
  217. });
  218. }
  219. };
  220. // #ifdef H5
  221. if (obj.payName == 'weixin') {
  222. weichatObj.chooseWXPay(data);
  223. }
  224. // #endif
  225. // #ifdef MP-WEIXIN
  226. if (obj.payName == 'weixin' || obj.payName == 'routine') {
  227. console.log('开始支付')
  228. wx.requestPayment(data);
  229. }
  230. // #endif
  231. }
  232. uni.hideLoading();
  233. obj.payLoding = false;
  234. })
  235. .catch(e => {
  236. // 支付完成
  237. uni.hideLoading();
  238. obj.payLoding = false;
  239. console.log(e);
  240. });
  241. },
  242. // 支付成功跳转
  243. paySuccessTo() {
  244. let obj = this
  245. uni.hideLoading();
  246. uni.redirectTo({
  247. url: '/pages/money/paySuccess?orderid=' + this.orderId + '&type=1'
  248. });
  249. },
  250. //确认支付
  251. confirm: async function() {
  252. let obj = this;
  253. // 判断是否余额不足
  254. if (obj.payName == 'yue' && +obj.now_money < obj.money) {
  255. uni.showModal({
  256. title: '提示',
  257. content: '账户余额不足!',
  258. showCancel: false,
  259. success: res => {},
  260. fail: () => {},
  261. complete: () => {}
  262. });
  263. return;
  264. }
  265. uni.showLoading({
  266. title: '支付中',
  267. mask: true
  268. })
  269. // 支付中
  270. obj.payLoding = true;
  271. // #ifdef H5
  272. // 获取当前是否为微信浏览器
  273. obj.froms = uni.getStorageSync('weichatBrowser') || '';
  274. // #endif
  275. // 判断是否为未支付订单中跳转进入
  276. // if (obj.type != 1) {
  277. // // 初次生成订单
  278. // obj.firstCreateOrder();
  279. // } else {
  280. // // 已经生成订单未支付
  281. // obj.orderMoneyPay();
  282. // }
  283. obj.orderMoneyPay()
  284. },
  285. // 初次订单创建
  286. firstCreateOrder() {
  287. let obj = this;
  288. // 获取下单页面数据
  289. let prepage = obj.$api.prePage();
  290. let data = {
  291. is_pack: prepage.is_pack,
  292. real_name: prepage.addressData.real_name, //联系人名称
  293. phone: prepage.addressData.phone, //联系人号码
  294. couponId: prepage.couponChecked.id, //优惠券编号
  295. addressId: prepage.addressData.id, //支付地址id
  296. useIntegral: prepage.checkedPoints ? 1 : 0, //是否积分抵扣1为是0为否
  297. payType: obj.payName, //支付类型 weixin-微信 yue-余额
  298. mark: prepage.desc, //备注
  299. // #ifdef H5
  300. from: obj.froms ? 'weixin' : 'H5', //来源
  301. // #endif
  302. // #ifdef MP-WEIXIN
  303. from: 'routine', //来源
  304. // #endif
  305. // #ifdef APP-PLUS
  306. from: 'app', //来源
  307. // #endif
  308. shipping_type: 1 //提货方式 1 快递 2自提
  309. };
  310. // 判断是否拼团商品
  311. if (obj.pinkid) {
  312. data.pinkId = obj.pinkid;
  313. }
  314. // 生成订单
  315. createOrderkey(data, obj.orderKey)
  316. .then(({
  317. data,
  318. status,
  319. msg
  320. }) => {
  321. console.log('生成订单')
  322. // 判断是否支付失败
  323. if (data.status == 'ORDER_EXIST') {
  324. uni.showModal({
  325. title: '提示',
  326. content: msg,
  327. showCancel: false
  328. });
  329. uni.hideLoading();
  330. obj.payLoding = false;
  331. return;
  332. }
  333. // 保存订单号
  334. obj.orderId = data.result.orderId;
  335. // 判断是否为余额支付
  336. if (obj.payName == 'yue') {
  337. if (status == 200 && data.status == 'SUCCESS') {
  338. obj.paySuccessTo();
  339. } else {
  340. obj.$api.msg(msg);
  341. }
  342. } else if (obj.payName == 'paying') {
  343. if (status == 200 && data.status == 'SUCCESS') {
  344. // obj.paySuccessTo();
  345. // console.log(data.result.orderId);
  346. obj.val = data.result.orderId
  347. console.log(obj.val,'obj.val');
  348. uni.showLoading({
  349. title: '代付码生成中...'
  350. })
  351. } else {
  352. obj.$api.msg(msg);
  353. }
  354. } else {
  355. // 立即支付
  356. obj.orderMoneyPay();
  357. }
  358. })
  359. .catch(e => {
  360. uni.hideLoading();
  361. obj.payLoding = false;
  362. console.log(e);
  363. });
  364. }
  365. }
  366. };
  367. </script>
  368. <style lang="scss">
  369. .app {
  370. width: 100%;
  371. }
  372. .price-box {
  373. background-color: #fff;
  374. height: 265upx;
  375. display: flex;
  376. flex-direction: column;
  377. justify-content: center;
  378. align-items: center;
  379. font-size: 28upx;
  380. color: #909399;
  381. .price {
  382. font-size: 50upx;
  383. color: #303133;
  384. margin-top: 12upx;
  385. &:before {
  386. content: '¥';
  387. font-size: 40upx;
  388. }
  389. }
  390. }
  391. .pay-type-list {
  392. margin-top: 20upx;
  393. background-color: #fff;
  394. padding-left: 60upx;
  395. .type-item {
  396. height: 120upx;
  397. padding: 20upx 0;
  398. display: flex;
  399. justify-content: space-between;
  400. align-items: center;
  401. padding-right: 60upx;
  402. font-size: 30upx;
  403. position: relative;
  404. }
  405. .icon {
  406. width: 100upx;
  407. font-size: 52upx;
  408. }
  409. .iconyue {
  410. color: #fe8e2e;
  411. }
  412. .iconweixin {
  413. color: #36cb59;
  414. }
  415. .iconzhifubao {
  416. color: #01aaef;
  417. }
  418. .tit {
  419. font-size: $font-lg;
  420. color: $font-color-dark;
  421. margin-bottom: 4upx;
  422. }
  423. .con {
  424. flex: 1;
  425. display: flex;
  426. flex-direction: column;
  427. font-size: $font-sm;
  428. color: $font-color-light;
  429. }
  430. }
  431. .mix-btn {
  432. display: flex;
  433. align-items: center;
  434. justify-content: center;
  435. width: 630upx;
  436. height: 80upx;
  437. margin: 80upx auto 30upx;
  438. font-size: $font-lg;
  439. color: #fff;
  440. background-color: $base-color;
  441. border-radius: 10upx;
  442. /* box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4); */
  443. }
  444. .clickbg {
  445. background-color: $color-gray !important;
  446. }
  447. .df-wrap {
  448. text-align: center;
  449. padding:60rpx 40rpx;
  450. background-color: #fff;
  451. border-radius: 20rpx;
  452. position: relative;
  453. .ewm {
  454. width: 377rpx;
  455. height: 377rpx;
  456. border: 3px solid #FF6F0F;
  457. margin: auto;
  458. padding: 23rpx;
  459. border-radius: 15rpx;
  460. image {
  461. width: 100%;
  462. height: 100%;
  463. margin: auto;
  464. }
  465. }
  466. .dftit {
  467. padding: 30rpx 0 15rpx;
  468. font-size: 36rpx;
  469. color: #333333;
  470. }
  471. .dftip {
  472. font-size: 30rpx;
  473. color: #333333;
  474. }
  475. .btn {
  476. width: 600rpx;
  477. line-height: 87rpx;
  478. background: $base-color;
  479. color: #fff;
  480. border-radius: 20rpx;
  481. font-weight: 500;
  482. font-size: 35rpx;
  483. position: absolute;
  484. bottom: -120rpx;
  485. left: 0;
  486. right: 0;
  487. margin: auto;
  488. }
  489. }
  490. </style>