pay.vue 12 KB

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