// export const baseUrl = 'https://www.chrivc.com/api';
// export const baseUrl = 'https://app.pc.seemore.club';
export const baseUrl = 'http://office.xianci.info:19092';
// export const baseUrl = 'http://192.168.168.189:9090';
// export const baseUrl = 'http://120.77.82.246:18085';
// export const baseUrl = 'http://192.168.168.234:18085';
const requestRecords = <any>{}
// const app = getApp<IAppOption>()
export function request(config: WechatMiniprogram.RequestOption) {
  const key = JSON.stringify(config)
  // params
  const { url, header } = config
  return new Promise((resolve, reject) => {
    if (requestRecords[key]) {
      wx.showToast({
        icon: 'none',
        title: '请勿重复操作!'
      })
      reject()
    }
    requestRecords[key] = true
    wx.showLoading({
      title: '加载中...'
    })
    wx.request({
      ...config,
      url:  baseUrl + url,
      header: {
        ...header,
        Authorization: wx.getStorageSync('Authorization')
      },
      timeout: 60000,
      success: (res:any) => {
        const { data } = res
        if (data.c !== 200) {
          wx.nextTick(() => {
            const app = getApp<IAppOption>()
            // && app.globalData.ready
            const msg = data.c === 500 ? '请求异常' : (data.m || '请求异常')
            if (data.c === 501017) {
              wx.removeStorageSync('Authorization')
              const source = wx.getStorageSync('QSource')
              wx.reLaunch({
                url: `/pages/login/login?source=${source}`
              })
            }
            if (data.c === 405 && app.globalData.ready) {
              app.login().then(() => {
                app.reload()
              })
            }
            if (data.c !== 400002) {
              wx.showToast({
                duration: 1500,
                icon: 'none',
                title: msg
              })
            }
            reject(data)
          })
        } else {
          resolve(data)
        }
      },
      fail: (err) => {
        console.log(err)
        reject(err)
      },
      complete: () => {
        wx.hideLoading()
        requestRecords[key] = false
      }
    })
  })
}