import tools from "@/common/js/tools";
import store from '@/store/index'
let BASE_URL ='https://' + process.uniEnv.baseUrl;


/**
 * post请求封装
 */
export const request = (url, method, data, show = false) => {
  let token=uni.getStorageSync('token')
  // data.token = token;
  // data.m_id = uni.getStorageSync('m_id')
  if (show) {
    tools.showLoading();
  }
  if (data === undefined) {
    data = {};
  }
  return new Promise((resolve, reject) => {
    // 封装主体：网络请求
    console.log(url)
    uni.request({
      url:BASE_URL+  url,
      data: data,
      method: method || 'post',
      header: {
        'content-type': 'application/json',
        'apikey': '01ABA77B8BA6F26243D85B5A50FFB88624951C7B3A4078B3F9971983E667D5B8',
        'hversion': '1.0',
        'Authorization': token
      },
      success: (res) => {
        if (res.statusCode === 200) {
          // console.log('应答信息-----------------------')
          // console.log(res.data); // 控制台显示数据信息
          tools.hideLoading();
          if (res.data.code * 1 === 401) {
            uni.clearStorageSync()
            uni.reLaunch({
              url: '/pages/login/index'
            });
          } else {
            resolve(res.data)
          }
        }else if(res.statusCode === 401){
          uni.clearStorageSync()
          uni.reLaunch({
            url: '/pages/login/index'
          });
        } else{
          tools.error('网络错误！！')
        }
      },
      fail: (err) => {
        tools.error('网络错误！！')
        // 返回错误消息
        reject(err)
        tools.hideLoading();
      }
    })
  })
}

/**
 * 文件上传
 */

export const uploadImage = (filePath, imageName, data) => {
  tools.showLoading();
  return new Promise((resolve, reject) => {
    uni.uploadFile({
      url: BASE_URL + '/api/common/upload',
      // header: {
      // 	'Authorization': JSON.parse(uni.getStorageSync('userInfo')).token
      // },
      filePath: filePath,
      name: imageName,
      formData: data,
      success: (res) => {
        tools.hideLoading();
        resolve(res)
      },
      fail(err) {
        tools.hideLoading();
        reject(err)
      }
    });
  })
}


export const uploadFile = (url, file) => {
  tools.showLoading();
  let token = uni.getStorageSync('token') || '';
  // data.app_version = plus.runtime.version;
  // console.log('请求地址-----------------------')
  // console.log(BASE_URL + url)
  // console.log('请求参数-----------------------')
  // console.log(data)
  console.log(file)
  return new Promise((resolve, reject) => {
    // 封装主体：网络请求
    console.log(BASE_URL + url)
    uni.uploadFile({
      url: BASE_URL + url, //仅为示例，非真实的接口地址
      filePath: file,
      name: 'file',
      header: {
        'content-type': 'application/json',
        'apikey': '01ABA77B8BA6F26243D85B5A50FFB88624951C7B3A4078B3F9971983E667D5B8',
        'hversion': '1.0',
        'Authorization': token
      },
      success: (res) => {
        tools.hideLoading();
        if (res.statusCode === 200) {
          // console.log('应答信息-----------------------')
          res.data = JSON.parse(res.data)
          // console.log(res.data); // 控制台显示数据信息
          if (res.data.code * 1 === 401) {
            uni.reLaunch({
              url: '/pages/login/login'
            });
          } else {
            resolve(res.data)
          }
        } else {
          tools.error('网络错误！！')
        }
      },
      fail: (err) => {
        tools.error('网络错误！！')
        // 返回错误消息
        reject(err)
        tools.hideLoading();
      }
    });

  })

}
