request.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. var app = getApp();
  2. var requestnum = 0;
  3. const addnum = function(){
  4. if (requestnum==0){
  5. wx.showLoading({
  6. title:"加载中...",
  7. // mask:true
  8. })
  9. }
  10. requestnum++;
  11. }
  12. const reducenum = function(){
  13. requestnum--;
  14. if (requestnum==0){
  15. wx.hideLoading();
  16. }
  17. }
  18. export const get=(url,data,callback,failback)=>{
  19. addnum();
  20. wx.request({
  21. url: app.globalData.serverpath+url,
  22. method: "GET",
  23. header: {
  24. "Content-Type": "json"
  25. },
  26. data:data,
  27. success: function (res){
  28. console.debug(res);
  29. callback && callback(res);
  30. },
  31. fail: function (error){
  32. failback && failback(error);
  33. },
  34. complete: function () {
  35. reducenum();
  36. }
  37. })
  38. }
  39. export const post=(url,data,header,callback,failback)=>{
  40. addnum();
  41. wx.request({
  42. url: app.globalData.serverpath+url,
  43. method: "POST",
  44. header: Object.assign({
  45. "Content-Type": "json"
  46. },header),
  47. data:data,
  48. success: function (res){
  49. console.debug(res);
  50. callback && callback(res);
  51. },
  52. fail: function (error){
  53. failback && failback(error);
  54. },
  55. complete: function () {
  56. reducenum();
  57. }
  58. })
  59. }