pay.vue 11 KB

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