pay.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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/wechat/pay", obj)
  36. // 区分包月支付和普通支付
  37. if (res.data.needPay && !res.data.monthId) {
  38. //#ifdef H5
  39. $wxApi.JSAPI(res.data.wx, jumpUrl).then(r);
  40. //#endif
  41. } else if (res.data.monthId) {
  42. //#ifdef H5
  43. $wxApi.JSAPI(res.data.wx, jumpUrl).then(r);
  44. //#endif
  45. } else {
  46. uni.showToast({
  47. title: "无需支付",
  48. icon: "none",
  49. duration: 2000
  50. });
  51. setTimeout(() => {
  52. //#ifdef H5
  53. if (jumpUrl) {
  54. window.location.href = jumpUrl;
  55. } else {
  56. window.location.reload();
  57. }
  58. //#endif
  59. }, 1500)
  60. }
  61. // $http.post("/client/pay/wechat", obj)
  62. // .then(res => {
  63. // //#ifdef H5
  64. // $wxApi.JSAPI(res.data).then(r);
  65. // //#endif
  66. // this.payError(res);
  67. // }).catch(err => {
  68. // console.log('pay err', err);
  69. // })
  70. });
  71. },
  72. /**
  73. * 微信支付
  74. * obj 传入对象
  75. * url 提交地址
  76. * jumpUrl 支付成功跳转
  77. */
  78. wechatPay(obj = {}, url = '/client/pay/wechat', jumpUrl) {
  79. uni.showLoading({
  80. title: '支付中'
  81. });
  82. return new Promise(r => {
  83. $http.post(url, obj)
  84. .then(res => {
  85. // 区分包月支付和普通支付
  86. if (res.data.needPay && !res.data.monthId) {
  87. //#ifdef H5
  88. $wxApi.JSAPI(res.data.wx, jumpUrl).then(r);
  89. //#endif
  90. } else if (res.data.monthId) {
  91. //#ifdef H5
  92. $wxApi.JSAPI(res.data.wx, jumpUrl).then(r);
  93. //#endif
  94. } else {
  95. uni.showToast({
  96. title: "无需支付",
  97. icon: "none",
  98. duration: 2000
  99. });
  100. setTimeout(() => {
  101. //#ifdef H5
  102. if (jumpUrl) {
  103. window.location.href = jumpUrl;
  104. } else {
  105. window.location.reload();
  106. }
  107. //#endif
  108. }, 1500)
  109. }
  110. this.payError(res);
  111. }).catch(err => {
  112. // alert(err.msg);
  113. console.log('pay err', err);
  114. })
  115. });
  116. },
  117. //支付错误处理
  118. payError(res) {
  119. uni.hideLoading();
  120. if (res.code == 623) {
  121. setTimeout(i => {
  122. new Vue().$api.goto("/pages/userCenter/myOrder/myOrder");
  123. }, 2000);
  124. uni.showToast({
  125. title: res.msg + " 即将跳转到订单页",
  126. icon: "none",
  127. duration: 2000
  128. });
  129. }
  130. }
  131. }