pay.vue 14 KB

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