import axios from 'axios'
import { ElMessage } from 'element-plus'
import _ from 'lodash-es'
const instance = axios.create({
  // baseURL: 'http://192.168.16.64:5173',
  baseURL: 'http://localhost:8001',
  timeout: 20000
})
export const useFetch = (url, config = {}) => {
  return new Promise((resolve, reject) => {
    instance({
      url,
      ...config
    }).then(({ data: { data, code, msg } }) => {
      console.log(code, '==code==')
      switch (code) {
        case 0: // success
          resolve({ code, msg, data })
          break
        case 200: // success
          resolve({ code, data, msg: data.message })
          break
        default:
          // other code
          ElMessage.error(msg)
          reject(new Error(msg))
          break
      }
    }).catch((e) => {
      if (_.get(e, 'code', '') !== 'ERR_CANCELED') {
        ElMessage.error(e)
      }
      reject(e)
    })
  })
}
