pay.js 2.7 KB

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