let app = null

// const app = require('../app');
export let baseUrl = "https://daotian.matripe.com.cn"
// export let baseUrl = "https://test.renminshitang.com.cn/daotian/api_dev"


export let customRequest = (url, options) => {

  return new Promise((resolve, reject) => {
    let recursion = function () {
      console.log(app, ' headerObj');
      // 获取app实例, 如果为空则递归调用
      app = getApp();
      if (app == null) {
        setTimeout(() => {
          recursion()
        }, 100);
      } else {
        console.log('requestApp', app);
        console.log(app, ' headerObj app');
        wx.request({
          url: baseUrl + url,
          method: options.method || 'GET',
          data: options.data || {},
          header: { ...app.globalData[options.header || 'headers2'], agencyId: (wx.getStorageSync("LOGINUSER_ID") != undefined && wx.getStorageSync("LOGINUSER_ID") != null) ? wx.getStorageSync("LOGINUSER_ID") : '' },
          success: (res) => {
            if (res.data.status == 200) {
              resolve(res)
            } else {

              if (res.data.status == 2001) {
                resolve(res)

              } else if (res.data.status == 2002) {
                wx.showToast({
                  title: res.data.msg,
                  icon: 'none',
                  duration: 2000
                })
                setTimeout(() => {
                  app.logout().then(() => {
                    wx.reLaunch({
                      url: "/pages/login/index",
                    });

                  });
                }, 1500);
              } else if (res.data.status == 9999) {
                app.logout().then(() => {
                  wx.reLaunch({
                    url: "/pages/login/index",
                  });
                });
              } else if (res.data.status == 9009) {
                wx.showToast({
                  title: res.data.msg,
                  icon: 'none',
                  duration: 2000
                })
                setTimeout(() => {
                  app.logout().then(() => {
                    wx.reLaunch({
                      url: "/pages/login/index",
                    });
                  });
                }, 2000);

              } else {

                console.log('res.data.status', res.data.status);
                wx.showToast({
                  title: res.data.msg,
                  icon: 'none',
                  duration: 2000
                })
                resolve(res)
              }

            }
          },
        })
      }
    }
    recursion()
  })

  // return

}