// 引入配置
import config from '@/common/config'
// 引入拦截器配置
import {requestInterceptors, responseInterceptors} from './interceptors.js'
// 引入luch-request
import {http} from '@/uni_modules/uview-plus'
import {getToken} from "@/util";
//  初始化请求配置
const initRequest = (vm) => {
  http.setConfig((defaultConfig) => {
    /* defaultConfig 为默认全局配置 */
    defaultConfig.baseURL = config.baseUrl /* 根域名 */
    defaultConfig.sslVerify = false
    defaultConfig.validateStatus = (statusCode) => {
      // statusCode 必存在。此处示例为全局默认配置
      return statusCode >= 200 && statusCode < 300 || statusCode === 401 || statusCode === 500;
    }
    return defaultConfig
  })
  requestInterceptors()
  responseInterceptors()
}
export const postWidthHeader = async (url, data, header = {}) => {
  return await http.post(url, data, {
    header: {
      'Content-Type': 'application/json',
      ...header,
    },
  })
}

export const post = async (url, data, header = {}) => {
  const token = getToken();
  return await postWidthHeader(url, data, {
    'Authorization': token ? `bearer ${token}` : '',
    ...header,
  })
}
export const get = async (url, params = {}, header = {}) => {
  const token = getToken();
  return await http.get(url, {
    params,
    header: {
      'Content-Type': 'application/json',
      'Authorization': token ? `bearer ${token}` : '',
      ...header,
    },
  })
}
export const uploadFilePromise = (url, file, param) => {
  return new Promise((resolve, reject) => {
    let a = uni.uploadFile({
      url: `${config.baseUrl}${url}`, // 仅为示例，非真实的接口地址
      filePath: file,
      name: 'file',
      formData: {
        ...param
      },
      header: {
        'Authorization': `bearer ${getToken()}`
      },
      success: (res) => {
        const data = JSON.parse(res.data)
        if (data.code === 200010) {
          resolve(data.data)
        } else {
          reject(data)
        }
      }
    });
  })
}
export {
  initRequest,

}




