pay.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**
  2. * 订单支付
  3. */
  4. import Vue from "vue";
  5. //#ifdef H5
  6. import $wxApi from "./wxapi.js";
  7. //#endif
  8. // import $http from "./utils/axios.js"
  9. import $http from "./uview-ui/libs/request"
  10. export default {
  11. //微信支付
  12. // wxPay(obj = {}) {
  13. // uni.showLoading({
  14. // title: '支付中'
  15. // });
  16. // return new Promise(r => {
  17. // $http.post("/client/wechat/pay",obj)
  18. // .then(res=>{
  19. // //#ifdef H5
  20. // $wxApi.JSAPI(res.data).then(r);
  21. // //#endif
  22. // this.payError(res);
  23. // }).catch(err =>{
  24. // // alert(err.msg);
  25. // console.log('pay err',err);
  26. // })
  27. // });
  28. // },
  29. // 微信支付
  30. wxPay(obj = {}) {
  31. uni.showLoading({
  32. title: '支付中'
  33. });
  34. return new Promise(r => {
  35. $http.post("/client/pay/wechat", obj)
  36. then(res => {
  37. // 区分包月支付和普通支付
  38. if (res.data.needPay && !res.data.monthId) {
  39. //#ifdef H5
  40. $wxApi.JSAPI(res.data.wx, jumpUrl).then(r);
  41. //#endif
  42. } else if (res.data.monthId) {
  43. //#ifdef H5
  44. $wxApi.JSAPI(res.data.wx, jumpUrl).then(r);
  45. //#endif
  46. } else {
  47. uni.showToast({
  48. title: "无需支付",
  49. icon: "none",
  50. duration: 2000
  51. });
  52. setTimeout(() => {
  53. //#ifdef H5
  54. if (jumpUrl) {
  55. window.location.href = jumpUrl;
  56. } else {
  57. window.location.reload();
  58. }
  59. //#endif
  60. }, 1500)
  61. }
  62. })
  63. // $http.post("/client/pay/wechat", obj)
  64. // .then(res => {
  65. // //#ifdef H5
  66. // $wxApi.JSAPI(res.data).then(r);
  67. // //#endif
  68. // this.payError(res);
  69. // }).catch(err => {
  70. // console.log('pay err', err);
  71. // })
  72. });
  73. },
  74. /**
  75. * 微信支付
  76. * obj 传入对象
  77. * url 提交地址
  78. * jumpUrl 支付成功跳转
  79. */
  80. wechatPay(obj = {}, url = '/client/pay/wechat', jumpUrl) {
  81. uni.showLoading({
  82. title: '支付中'
  83. });
  84. return new Promise(r => {
  85. $http.post(url, obj)
  86. .then(res => {
  87. // 区分包月支付和普通支付
  88. if (res.data.needPay && !res.data.monthId) {
  89. //#ifdef H5
  90. $wxApi.JSAPI(res.data.wx, jumpUrl).then(r);
  91. //#endif
  92. } else if (res.data.monthId) {
  93. //#ifdef H5
  94. $wxApi.JSAPI(res.data.wx, jumpUrl).then(r);
  95. //#endif
  96. } else {
  97. uni.showToast({
  98. title: "无需支付",
  99. icon: "none",
  100. duration: 2000
  101. });
  102. setTimeout(() => {
  103. //#ifdef H5
  104. if (jumpUrl) {
  105. window.location.href = jumpUrl;
  106. } else {
  107. window.location.reload();
  108. }
  109. //#endif
  110. }, 1500)
  111. }
  112. this.payError(res);
  113. }).catch(err => {
  114. // alert(err.msg);
  115. console.log('pay err', err);
  116. })
  117. });
  118. },
  119. //支付错误处理
  120. payError(res) {
  121. uni.hideLoading();
  122. if (res.code == 623) {
  123. setTimeout(i => {
  124. new Vue().$api.goto("/pages/userCenter/myOrder/myOrder");
  125. }, 2000);
  126. uni.showToast({
  127. title: res.msg + " 即将跳转到订单页",
  128. icon: "none",
  129. duration: 2000
  130. });
  131. }
  132. }
  133. }