123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- var app = getApp();
- var requestnum = 0;
- const addnum = function(){
- if (requestnum==0){
- wx.showLoading({
- title:"加载中...",
- // mask:true
- })
- }
- requestnum++;
- }
- const reducenum = function(){
- requestnum--;
- if (requestnum==0){
- wx.hideLoading();
- }
- }
- export const get=(url,data,callback,failback)=>{
- addnum();
- var authorization = wx.getStorageSync("authorization");
- wx.request({
- url: app.globalData.serverpath+url,
- method: "GET",
- header: {
- "Content-Type": "json",
- "Authorization":authorization,
- "Accept": "application/vnd.vpgame.v1+json"
- },
- data:data,
- success: function (res){
- if(res.statusCode!=200){
- if(res.data.message){
- wx.showToast({
- title:res.data.message,
- icon:"none"
- })
- return;
- }
- }
- setTimeout(function(){
- callback && callback(res);
- },0);
- },
- fail: function (error){
- failback && failback(error);
- },
- complete: function () {
- reducenum();
- }
- })
- }
- export const postwithoutahth=(url,data,callback,failback)=>{
- addnum();
- var authorization = wx.getStorageSync("authorization");
- wx.request({
- url: app.globalData.serverpath+url,
- method: "POST",
- header: {
- "Content-Type": "application/json",
- "Authorization":authorization,
- "Accept": "application/vnd.vpgame.v1+json"
- },
- data:data,
- success: function (res){
- if(res.statusCode!=200){
- if(res.data.message){
- setTimeout(function(){
- wx.showToast({
- title:res.data.message,
- icon:"none"
- })
- },0);
- return;
- }
- }
- setTimeout(function(){
- callback && callback(res);
- },0);
- },
- fail: function (error){
- failback && failback(error);
- },
- complete: function () {
- reducenum();
- }
- })
- }
- export const post=(url,data,header,callback,failback,noauth)=>{
- var authorization = wx.getStorageSync("authorization")
- if(typeof header=="function"){
- noauth = failback;
- failback = callback;
- callback = header;
- header = {};
- }
- if(authorization){
- addnum();
- wx.request({
- url: app.globalData.serverpath+url,
- method: "POST",
- header: Object.assign({
- "Content-Type": "application/json",
- "Authorization":authorization,
- "Accept": "application/vnd.vpgame.v1+json"
- },header),
- data:data,
- success: function (res){
- if(res.statusCode!=200){
- if(res.data.message){
- wx.showToast({
- title:res.data.message,
- icon:"none"
- })
- return;
- }
- }
- setTimeout(function(){
- callback && callback(res);
- },0);
- },
- fail: function (error){
- failback && failback(error);
- },
- complete: function () {
- reducenum();
- }
- })
- }else{
- if(typeof noauth=="function"){
- noauth();
- }
- }
- }
- export const upload =(url,path,callback)=>{
- addnum();
- var authorization = wx.getStorageSync("authorization")
- wx.uploadFile({
- url: app.globalData.serverpath+url, //仅为示例,非真实的接口地址
- filePath: path,
- name: 'file',
- formData:{},
- header:{
- "Authorization":authorization,
- "Accept": "application/vnd.vpgame.v1+json"
- },
- success: function(res){
- callback && callback(res);
- },
- complete: function () {
- reducenum();
- }
- })
- }
|