request.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. var authorization = wx.getStorageSync("authorization")
  41. if(typeof header=="function"){
  42. failback = callback;
  43. callback = header;
  44. header = {};
  45. }
  46. addnum();
  47. wx.request({
  48. url: app.globalData.serverpath+url,
  49. method: "POST",
  50. header: Object.assign({
  51. "Content-Type": "json",
  52. "Authorization":authorization
  53. },header),
  54. data:data,
  55. success: function (res){
  56. //console.debug(res);
  57. callback && callback(res);
  58. },
  59. fail: function (error){
  60. failback && failback(error);
  61. },
  62. complete: function () {
  63. reducenum();
  64. }
  65. })
  66. }