// export const baseURL = 'http://192.168.10.63:8080/jeecg-boot' // 本地
// export const baseURL = 'http://t5qnpc.natappfree.cc/jeecg-boot' // 本地
// export const baseURL = 'http://192.168.12.171:8881/jeecg-boot' // 本地
export const baseURL = 'http://114.115.178.175:8799/jeecg-boot' // 测试
// export const baseURL = 'http://127.0.0.1:4523/m1/3332971-0-default/jeecg-boot' // mock

interface optionsType {
  url: string
  method: "OPTIONS" | "GET" | "HEAD" | "POST" | "PUT" | "DELETE" | "TRACE" | "CONNECT"
  data?: any
  header?: any
  success?: (res: any) => void
  fail?: (res: any) => void
}

export const request = (options: optionsType): Promise<any> => {
  return new Promise((resolve, reject) => {
    uni.request<optionsType>({
      url: baseURL + options.url, //接口地址:前缀+方法中传入的地址
      method: options.method || 'GET', //请求方法:传入的方法或者默认是“GET”
      data: options.data || {}, //传递参数:传入的参数或者默认传递空集合
      header: {
        'X-Access-Token': uni.getStorageSync('token') || '', //自定义请求头信息
        // 'content-type': 'multipart/form-data'
        // 'content-type': 'application/x-www-form-urlencoded'
        'content-type': 'application/json'
      },
      success: (res: any) => {
        // console.log(res, uni.getStorageSync('token'));
        //返回的数据(不固定,看后端接口,这里是做了一个判断,如果不为true,用uni.showToast方法提示获取数据失败)
        if (res.data.code == 200) {
          // console.log(res.data);
          resolve(res.data)
        } else {
          if (res.data.code == 401) {
            uni.reLaunch({ url: '/pages/login/login' })
            uni.clearStorageSync()
            uni.$u.toast('登录失效请重新登录~')
          }
          reject(res.data.message)
        }
        // 如果不满足上述判断就输出数据
      },
      // 这里的接口请求,如果出现问题就输出接口请求失败
      fail: (err: any) => {
        console.log(err, '请求失败')
        reject(err)
      }
    })
  })
}