微信小程序request请求封装 uni-app

微信小程序request请求封装 uni-app

let requestCount = 0;
// 带loading框的请求,默认post请求
// 取token
const token = () => uni.getStorageSync('accountInfo').token;
// console.log(token);
export const AJAX = async (url, params = {}, method = 'POST') => {
  requestCount++;
  uni.showLoading({ title: '加载中...' });
  const [err, { data }] = await uni.request({
    url:'请求地址'+接口地址,
    data: params,
    method,
    header: {
      'content-type': 'application/json', //自定义请求头信息
      token: token(),
      channelSource: 'largeTerminal',
    },
  });
  requestCount--;
  if (!requestCount) {
    uni.hideLoading();
  }
  if (err) {
    return Promise.reject(err);
  } else {
    return Promise.resolve(data);
  }
};
// 不带loading框的请求 默认post
export const quiet_AJAX = async (url, params = {}, method = 'GET') => {
  const [err, { data }] = await uni.request({
    url:'请求地址'+接口地址,
    data: params,
    method,
    header: {
      'content-type': 'application/json', //自定义请求头信息
      token: token(),
      accessToken: token(),
      channelSource: 'largeTerminal',
    },
  });
  if (err) {
    return Promise.reject(err);
  } else {
    return Promise.resolve(data);
  }
};


你可能感兴趣的:(封装,uni-app,小程序)