Taro 封裝request請求


export const request = (url, method = "GET", data) => {
  let _url = `${baseUrlPrefix}${url}`;

  return new Promise((resolve, reject) => {
    Taro.showLoading({
      title: "正在加載"
    });
    Taro.request({
      url: _url,
      data: data,
      method: method,
      header: {
        "content-type": "application/json"
      },
      success: res => {
        console.log("從接口獲取到的數據", res);
        let { code } = res.data;
        Taro.hideLoading();
        if (code === 200) {
          resolve(res.data.data);
          wx.hideLoading();
        } else {
          wx.showToast({
            title: "數據請求錯誤"
          });
        }
      },
      fail() {
        reject("接口有誤,請檢查");
      }
    });
  });
};


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM