request.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. }
  77. export const upload =(url,path,callback)=>{
  78. addnum();
  79. var authorization = wx.getStorageSync("authorization")
  80. wx.uploadFile({
  81. url: app.globalData.serverpath+url, //仅为示例,非真实的接口地址
  82. filePath: path,
  83. name: 'file',
  84. formData:{},
  85. header:{
  86. "Authorization":authorization,
  87. "Accept": "application/vnd.vpgame.v1+json"
  88. },
  89. success: function(res){
  90. callback && callback(res);
  91. },
  92. complete: function () {
  93. reducenum();
  94. }
  95. })
  96. }