pay.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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("/wechat/pay",obj)
  18. // this.$u.post("/wechat/h5/pay",obj)
  19. .then(res=>{
  20. //#ifdef H5
  21. $wxApi.JSAPI(res.data).then(r);
  22. //#endif
  23. this.payError(res);
  24. }).catch(err =>{
  25. // alert(err.msg);
  26. console.log('pay err',err);
  27. })
  28. });
  29. },
  30. //微信支付
  31. wechatPay(obj = {}) {
  32. uni.showLoading({
  33. title: '支付中'
  34. });
  35. return new Promise(r => {
  36. $http.post("/pay/wechat",obj)
  37. .then(res=>{
  38. if (res.data.needPay) {
  39. //#ifdef H5
  40. $wxApi.JSAPI(res.data.wx).then(r);
  41. //#endif
  42. } else {
  43. uni.showToast({
  44. title: "无需支付",
  45. icon: "none",
  46. duration: 2000
  47. });
  48. setTimeout(() =>{
  49. //#ifdef H5
  50. window.location.reload();
  51. //#endif
  52. }, 1500)
  53. }
  54. this.payError(res);
  55. }).catch(err =>{
  56. // alert(err.msg);
  57. console.log('pay err',err);
  58. })
  59. });
  60. },
  61. //支付错误处理
  62. payError(res){
  63. uni.hideLoading();
  64. if(res.code == 623){
  65. setTimeout(i=>{
  66. new Vue().$api.goto("/pages/userCenter/myOrder/myOrder");
  67. },2000);
  68. uni.showToast({
  69. title:res.msg + " 即将跳转到订单页",
  70. icon:"none",
  71. duration:2000
  72. });
  73. }
  74. }
  75. }