import axios from 'axios'
// POST请求数据
// 参数：url：链接地址
//     token: Token值
//     jason：请求参数
//     category：是否加密，true：加密，false：不加密
//     types：请求类型，1：post请求，2：get请求
export const GetDataPost = (url, token, jason, category, types) => {
  const fetchPostOption = {
    method: 'POST',
    headers: {
      Accept: 'application/json',
      'Content-Type': 'application/json',
      token: token
    },
    body: JSON.stringify(jason)
  }
  const fetchGetOption = {
    method: 'GET',
    headers: {
      Accept: 'application/json',
      'Content-Type': 'application/json',
      token: token
    }
  }
  if (types === 1) {
    if (category) {
      return fetch(url, fetchPostOption)
        .then(response => response.json())
        .then(responseJson => {
          return responseJson
        })
        .catch(() => {
          return { code: -1, msg: '服务器故障！' }
        })
    } else {
      return fetch(url, fetchPostOption)
        .then(response => response.json())
        .then(responseJson => {
          return responseJson
        })
        .catch(() => {
          return { code: -1, msg: '服务器故障！' }
        })
    }
  } else {
    // GET请求数据
    if (jason) {
      let paramsArray = []
      // 拼接参数
      Object.keys(jason).forEach(key =>
        paramsArray.push(key + '=' + jason[key])
      )
      if (url.search(/\?/) === -1) {
        url += '?' + paramsArray.join('&')
      } else {
        url += '&' + paramsArray.join('&')
      }
    }
    try {
      return fetch(url, fetchGetOption)
        .then(response => response.json())
        .then(responseJson => {
          return responseJson
        })
    } catch (e) {
      return { code: -1, msg: '服务器故障！' }
    }
  }
}

export const GetData = (url, token, jason, category, types) => {
  const fetchPostOption = {
    method: 'POST',
    headers: {
      Accept: 'application/json',
      'Content-Type': 'application/json',
      token: token
    },
    body: JSON.stringify(jason)
  }
  try {
    axios({
      method: 'post',
      url: '/user/12345',
      data: {
        firstName: 'Fred',
        lastName: 'Flintstone'
      }
    })
    axios({
      fetchPostOption
    }).then(res => {
      console.log('请求结果：', res)
    })
  } catch (e) {
    console.log(1232)
    console.log(e)
  }
}

// Jason序列化
// 参数：jason：jason的字符串
export const JasonSerialize = jason => {
  let times = '201909091222123'
  jason = jason + times
  return jason
}

// POST图片上传
// 参数：url：链接地址
//     token: Token值
//     params：请求参数
//     image：图片信息
//     types：请求类型，1：post请求，2：get请求
export const UploadImage = (url, token, params, image) => {
  return new Promise(function(resolve, reject) {
    let formData = new FormData()
    for (var key in params) {
      formData.append(key, params[key])
    }
    let file = {
      uri: image.path,
      type: 'application/octet-stream',
      name: 'image.jpg'
    }
    formData.append('file', file)
    fetch(url, {
      method: 'POST',
      headers: {
        'Content-Type': 'multipart/form-data;charset=utf-8',
        Authorization: token
      },
      body: formData
    })
      .then(response => response.json())
      .then(responseData => {
        resolve(responseData)
      })
      .catch(err => {
        reject(err)
      })
  })
}
// 通过url获取服务器jason数据
// 参数：url：地址链接
// 返回值：string
// export const GetData = (url, jason) => {
//   const fetchGetOption = {
//     method: 'POST',
//     headers: {
//       Accept: 'application/json',
//       'Content-Type': 'application/json',
//       token: GetToken(),
//     },
//     body: JSON.stringify(jason),
//   };
//   return fetch(url)
//     .then(response => response.json())
//     .then(responseJson => {
//       return responseJson.movies;
//     })
//     .catch(error => {
//       return error;
//     });
// };
