pay.js 2.2 KB

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