pay.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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(4)" v-if="isVip == 3">
  32. <text class="icon iconfont iconyue"></text>
  33. <view class="con">
  34. <text class="tit">vip积分</text>
  35. <text>可用vip积分 {{ userInfo.vip_point}}</text>
  36. </view>
  37. <label class="radio"><radio value="" color="#FF4C4C" :checked="payType == 4"></radio></label>
  38. </view>
  39. <view class="type-item" @click="changePayType(5)" v-else>
  40. <image class="cash-icon" src="../../static/icon/mygx.png" mode=""></image>
  41. <view class="con">
  42. <text class="tit">普通积分</text>
  43. <text>可用普通积分 {{ userInfo.normal_point }}</text>
  44. </view>
  45. <label class="radio"><radio value="" color="#FF4C4C" :checked="payType == 5"></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. isVip:0,
  63. payType: 1, //支付类型
  64. // #ifdef H5
  65. payName: 'weixin',
  66. // #endif
  67. // #ifdef MP-WEIXIN
  68. payName: 'weixin',
  69. // #endif
  70. orderInfo: {},
  71. money: 0.0, //订单金额
  72. now_money: 0.0, //余额
  73. orderKey: '',
  74. orderId: '', //保存订单id
  75. payLoding: false, //判断是否支付中
  76. type: '', //判断是否从订单中进入
  77. mark:"",//备注
  78. // #ifdef H5
  79. froms: '', //保存h5中数据来源对象
  80. // #endif
  81. pinkid: '', //保存拼团商品id
  82. isP: 0
  83. };
  84. },
  85. computed: {
  86. // #ifdef H5
  87. ...mapState(['weichatObj']),
  88. ...mapState('user', ['userInfo'])
  89. // #endif
  90. },
  91. onLoad(options) {
  92. this.isVip = options.isVip
  93. console.log(options,'9222')
  94. if (options.isP) {
  95. this.isP = options.isP;
  96. }
  97. if (options.type == 1) {
  98. this.type = 1;
  99. this.orderId = options.ordid;
  100. this.money = options.money;
  101. } else {
  102. this.orderKey = options.key;
  103. console.log(this.orderKey,'computedOrderkey');
  104. let prepage = this.$api.prePage();
  105. console.log(prepage,'coupons');
  106. computedOrderkey({
  107. orderkey: this.orderKey,
  108. couponId: prepage.couponChecked.id, //优惠券编号
  109. addressId: prepage.addressData.id, //地址编号
  110. useIntegral: prepage.checkedPoints ? 1 : 0
  111. }).then(({ data }) => {
  112. // 获取支付金额
  113. this.money = data.result.pay_price;
  114. });
  115. }
  116. this.getUserInfo();
  117. // 保存pinkid
  118. if (options.pinkid) {
  119. this.pinkid = options.pinkid;
  120. }
  121. // 载入余额
  122. balance({}).then(({ data }) => {
  123. // 获取余额
  124. this.now_money = data.now_money;
  125. });
  126. },
  127. methods: {
  128. ...mapMutations('user', ['setUserInfo', 'setOrderInfo']),
  129. getUserInfo() {
  130. userinfo({})
  131. .then(({ data }) => {
  132. this.setUserInfo(data);
  133. })
  134. .catch(e => {
  135. console.log(e);
  136. });
  137. },
  138. //选择支付方式
  139. changePayType(type) {
  140. this.payType = type;
  141. if (this.payType == 1) {
  142. this.payName = 'weixin';
  143. }
  144. if (this.payType == 2) {
  145. this.payName = 'ali';
  146. }
  147. if (this.payType == 3) {
  148. this.payName = 'yue';
  149. }
  150. if (this.payType == 4) {
  151. this.payName = 'vip_point';
  152. }
  153. if (this.payType == 5) {
  154. this.payName = 'normal_point';
  155. }
  156. },
  157. // 支付金额
  158. orderMoneyPay() {
  159. let obj = this;
  160. orderPay({
  161. uni: obj.orderId,
  162. // #ifdef H5
  163. from: obj.froms ? 'weixin' : 'H5', //来源
  164. // #endif
  165. // #ifdef MP-WEIXIN
  166. from: 'routine', //来源
  167. // #endif
  168. // #ifdef APP-PLUS
  169. from: 'app', //来源
  170. // #endif
  171. paytype: obj.payName //支付类型 weixin-微信 yue-余额
  172. })
  173. .then(e => {
  174. // 判断是否为余额支付
  175. if (obj.payName == 'yue' && e.data.status == 'SUCCESS') {
  176. if (e.status == 200) {
  177. obj.paySuccessTo();
  178. } else {
  179. obj.$api.msg(msg);
  180. }
  181. }
  182. if (obj.payName == 'vip_point' && e.data.status == 'SUCCESS') {
  183. if (e.status == 200) {
  184. obj.paySuccessTo();
  185. } else {
  186. obj.$api.msg(msg);
  187. }
  188. }
  189. if (obj.payName == 'normal_point' && e.data.status == 'SUCCESS') {
  190. if (e.status == 200) {
  191. obj.paySuccessTo();
  192. } else {
  193. obj.$api.msg(msg);
  194. }
  195. }
  196. if (obj.payName == 'weixin' || obj.payName == 'routine') {
  197. let da = e.data.result.jsConfig;
  198. let data = {
  199. // #ifdef H5
  200. timestamp: da.timestamp,
  201. // #endif
  202. // #ifdef MP
  203. timeStamp: da.timestamp,
  204. // #endif
  205. nonceStr: da.nonceStr,
  206. package: da.package,
  207. signType: da.signType,
  208. paySign: da.paySign,
  209. success: function(res) {
  210. obj.paySuccessTo();
  211. },
  212. fail: () => {
  213. uni.navigateTo({
  214. url: '/pages/order/order?state=0'
  215. });
  216. }
  217. };
  218. // #ifdef H5
  219. if (obj.payName == 'weixin') {
  220. weixinObj.chooseWXPay(data);
  221. }
  222. // #endif
  223. // #ifdef MP-WEIXIN
  224. if (obj.payName == 'routine') {
  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 == 'vip_point' && +obj.userInfo.vip_point < obj.money) {
  255. uni.showModal({
  256. title: '提示',
  257. content: 'vip积分不足!',
  258. showCancel: false,
  259. success: res => {},
  260. fail: () => {},
  261. complete: () => {}
  262. });
  263. uni.hideLoading();
  264. return;
  265. }
  266. // 判断是否拼团积分不足
  267. if (obj.payName == 'normal_point' && +obj.userInfo.normal_point < 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 mark=prepage.desc
  313. console.log(prepage.desc,'备注')
  314. let data = {
  315. real_name: prepage.addressData.real_name, //联系人名称
  316. phone: prepage.addressData.phone, //联系人号码
  317. couponId: prepage.couponChecked.id, //优惠券编号
  318. addressId: prepage.addressData.id, //支付地址id
  319. useIntegral: prepage.checkedPoints ? 1 : 0, //是否积分抵扣1为是0为否
  320. payType: obj.payName, //支付类型 weixin-微信 yue-余额
  321. mark:prepage.desc, //备注
  322. // #ifdef H5
  323. from: obj.froms ? 'weixin' : 'H5', //来源
  324. // #endif
  325. // #ifdef MP-WEIXIN
  326. from: 'routine', //来源
  327. // #endif
  328. // #ifdef APP-PLUS
  329. from: 'app', //来源
  330. // #endif
  331. shipping_type: prepage.tabCurrentIndex + 1, //提货方式 1 快递 2自提
  332. store_id: prepage.tabCurrentIndex == 1 ? prepage.shopAddress.id : '',
  333. };
  334. // 判断是否拼团商品
  335. if (obj.pinkid) {
  336. data.pinkId = obj.pinkid;
  337. }
  338. // 生成订单
  339. createOrderkey(data, obj.orderKey)
  340. .then(({ data, status, msg }) => {
  341. // 判断是否支付失败
  342. if (data.status == 'ORDER_EXIST') {
  343. uni.showModal({
  344. title: '提示',
  345. content: msg,
  346. showCancel: false
  347. });
  348. uni.hideLoading();
  349. obj.payLoding = false;
  350. return;
  351. }
  352. // 保存订单号
  353. obj.orderId = data.result.orderId;
  354. // 判断是否为余额支付
  355. if (obj.payName == 'yue') {
  356. console.log()
  357. if (status == 200 && data.status == 'SUCCESS') {
  358. obj.paySuccessTo();
  359. } else {
  360. obj.$api.msg(msg);
  361. }
  362. } else if (obj.payName == 'normal_point') {
  363. if (status == 200 && data.status == 'SUCCESS') {
  364. obj.paySuccessTo();
  365. } else {
  366. obj.$api.msg(msg);
  367. }
  368. } else if (obj.payName == 'vip_point') {
  369. if (status == 200 && data.status == 'SUCCESS') {
  370. obj.paySuccessTo();
  371. } else {
  372. obj.$api.msg(msg);
  373. }
  374. }else {
  375. // 立即支付
  376. obj.orderMoneyPay();
  377. }
  378. })
  379. .catch(e => {
  380. uni.hideLoading();
  381. obj.payLoding = false;
  382. console.log(e);
  383. });
  384. }
  385. }
  386. };
  387. </script>
  388. <style lang="scss">
  389. .app {
  390. width: 100%;
  391. }
  392. .price-box {
  393. background-color: #fff;
  394. height: 265upx;
  395. display: flex;
  396. flex-direction: column;
  397. justify-content: center;
  398. align-items: center;
  399. font-size: 28upx;
  400. color: #909399;
  401. .price {
  402. font-size: 50upx;
  403. color: #303133;
  404. margin-top: 12upx;
  405. &:before {
  406. content: '¥';
  407. font-size: 40upx;
  408. }
  409. }
  410. }
  411. .pay-type-list {
  412. margin-top: 20upx;
  413. background-color: #fff;
  414. padding-left: 60upx;
  415. .type-item {
  416. height: 120upx;
  417. padding: 20upx 0;
  418. display: flex;
  419. justify-content: space-between;
  420. align-items: center;
  421. padding-right: 60upx;
  422. font-size: 30upx;
  423. position: relative;
  424. }
  425. .cash-icon {
  426. width: 100rpx;
  427. height: 52rpx;
  428. padding-right: 50rpx;
  429. }
  430. .icon {
  431. width: 100upx;
  432. font-size: 52upx;
  433. }
  434. .iconyue {
  435. color: #fe8e2e;
  436. }
  437. .iconweixin {
  438. color: #36cb59;
  439. }
  440. .iconzhifubao {
  441. color: #01aaef;
  442. }
  443. .tit {
  444. font-size: $font-lg;
  445. color: $font-color-dark;
  446. margin-bottom: 4upx;
  447. }
  448. .con {
  449. flex: 1;
  450. display: flex;
  451. flex-direction: column;
  452. font-size: $font-sm;
  453. color: $font-color-light;
  454. }
  455. }
  456. .mix-btn {
  457. display: flex;
  458. align-items: center;
  459. justify-content: center;
  460. width: 630upx;
  461. height: 80upx;
  462. margin: 80upx auto 30upx;
  463. font-size: $font-lg;
  464. color: #F8DABA;
  465. background-color: #303030;
  466. border-radius: 10upx;
  467. /* box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4); */
  468. }
  469. .clickbg {
  470. background-color: $color-gray !important;
  471. }
  472. </style>