pay.js 2.3 KB

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