pay.js 1.8 KB

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