request.js 3.5 KB

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