request.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. var authorization = wx.getStorageSync("authorization");
  21. wx.request({
  22. url: app.globalData.serverpath+url,
  23. method: "GET",
  24. header: {
  25. "Content-Type": "json",
  26. "Authorization":authorization
  27. },
  28. data:data,
  29. success: function (res){
  30. console.debug(res);
  31. callback && callback(res);
  32. },
  33. fail: function (error){
  34. failback && failback(error);
  35. },
  36. complete: function () {
  37. reducenum();
  38. }
  39. })
  40. }
  41. export const post=(url,data,header,callback,failback,noauth)=>{
  42. var authorization = wx.getStorageSync("authorization")
  43. if(typeof header=="function"){
  44. noauth = failback;
  45. failback = callback;
  46. callback = header;
  47. header = {};
  48. }
  49. if(authorization){
  50. addnum();
  51. wx.request({
  52. url: app.globalData.serverpath+url,
  53. method: "POST",
  54. header: Object.assign({
  55. "Content-Type": "application/json",
  56. "Authorization":authorization,
  57. "Accept": "application/vnd.vpgame.v1+json"
  58. },header),
  59. data:data,
  60. success: function (res){
  61. //console.debug(res);
  62. callback && callback(res);
  63. },
  64. fail: function (error){
  65. failback && failback(error);
  66. },
  67. complete: function () {
  68. reducenum();
  69. }
  70. })
  71. }else{
  72. if(typeof noauth=="function"){
  73. noauth();
  74. }
  75. }
  76. }