spay.vue 13 KB

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