pay.vue 13 KB

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