pay.vue 12 KB

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