您的当前位置:首页正文

支付宝小程序封装api请求

2024-11-26 来源:个人技术集锦
let app = getApp()
export function request(config) {
  my.showLoading({
    content: '加载中...'
  });
  let host = app.globalData.host;
  // console.log('host',host)
  // 解构赋值
  let {
    url = '',
    data = '',
    method = 'POST',
  } = { ...config }
  //  console.log('data',data)
  return new Promise((resolve, reject) => {
    my.request({
      url: host + url,
      headers:headers,
      method: method,
      data:  {...data} ,
      success: (res) => {
       //这里面判断如果接口返给的不是200
        resolve(res.data)
      },
      fail: (err) => {
        reject(err)
      },
      complete: () => {
        setTimeout(()=> {
          my.hideLoading();
        }, 400)
      }
    });
  })
}
 globalData: {

    host: '基地址'

  },

显示全文