pay.vue 11 KB

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