request.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. "Accept": "application/vnd.vpgame.v1+json"
  28. },
  29. data:data,
  30. success: function (res){
  31. if(res.statusCode!=200){
  32. if(res.data.message){
  33. wx.showToast({
  34. title:res.data.message,
  35. icon:"none"
  36. })
  37. return;
  38. }
  39. }
  40. callback && callback(res);
  41. },
  42. fail: function (error){
  43. failback && failback(error);
  44. },
  45. complete: function () {
  46. reducenum();
  47. }
  48. })
  49. }
  50. export const postwithoutahth=(url,data,callback,failback)=>{
  51. addnum();
  52. var authorization = wx.getStorageSync("authorization");
  53. wx.request({
  54. url: app.globalData.serverpath+url,
  55. method: "POST",
  56. header: {
  57. "Content-Type": "application/json",
  58. "Authorization":authorization,
  59. "Accept": "application/vnd.vpgame.v1+json"
  60. },
  61. data:data,
  62. success: function (res){
  63. if(res.statusCode!=200){
  64. if(res.data.message){
  65. wx.showToast({
  66. title:res.data.message,
  67. icon:"none"
  68. })
  69. return;
  70. }
  71. }
  72. callback && callback(res);
  73. },
  74. fail: function (error){
  75. failback && failback(error);
  76. },
  77. complete: function () {
  78. reducenum();
  79. }
  80. })
  81. }
  82. export const post=(url,data,header,callback,failback,noauth)=>{
  83. var authorization = wx.getStorageSync("authorization")
  84. if(typeof header=="function"){
  85. noauth = failback;
  86. failback = callback;
  87. callback = header;
  88. header = {};
  89. }
  90. if(authorization){
  91. addnum();
  92. wx.request({
  93. url: app.globalData.serverpath+url,
  94. method: "POST",
  95. header: Object.assign({
  96. "Content-Type": "application/json",
  97. "Authorization":authorization,
  98. "Accept": "application/vnd.vpgame.v1+json"
  99. },header),
  100. data:data,
  101. success: function (res){
  102. if(res.statusCode!=200){
  103. if(res.data.message){
  104. wx.showToast({
  105. title:res.data.message,
  106. icon:"none"
  107. })
  108. return;
  109. }
  110. }
  111. callback && callback(res);
  112. },
  113. fail: function (error){
  114. failback && failback(error);
  115. },
  116. complete: function () {
  117. reducenum();
  118. }
  119. })
  120. }else{
  121. if(typeof noauth=="function"){
  122. noauth();
  123. }
  124. }
  125. }
  126. export const upload =(url,path,callback)=>{
  127. addnum();
  128. var authorization = wx.getStorageSync("authorization")
  129. wx.uploadFile({
  130. url: app.globalData.serverpath+url, //仅为示例,非真实的接口地址
  131. filePath: path,
  132. name: 'file',
  133. formData:{},
  134. header:{
  135. "Authorization":authorization,
  136. "Accept": "application/vnd.vpgame.v1+json"
  137. },
  138. success: function(res){
  139. callback && callback(res);
  140. },
  141. complete: function () {
  142. reducenum();
  143. }
  144. })
  145. }