import Vue from "vue"
import vueResource from "vue-resource"
Vue.use(vueResource)
import {
  MessageBox,
  Message,
  Loading
} from 'element-ui'


// 请求接口通用方法
export function request(api, params, callback) {
  let url = api + "?t=" + new Date();
  Vue.http.post(url, params, {
    header: {
      "Content-Type": "application/x-www-form-urlencoded"
    }
  }).then((response) => {
    let res = response.body
    if (res.retCode === 0) {
      if (typeof callback === "function") {
        callback(res)
      }
    } else {
      Message({
        message: res.msg + '，重新获取中' || 'Error',
        type: 'error',
        duration: 5 * 1000
      })
    }
  }).catch(err => {

  })
}


export function get_isbn(params, callback) {
  const api = "/query"
  requestGet(api, {
    params: {
      key: "417d45266d502c8f67235e96f1e891e2",
      ...params
    }
  }, callback)
}

// 请求接口通用方法
export function requestGet(api, params, callback) {
  let url = api + "?t=" + new Date();
  Vue.http.get(url, params).then((response) => {
    console.log(response)
    let res = response.body
    if (res.error_code === 0) {
      if (typeof callback === "function") {
        callback(res)
      }
    } else {
      Message({
        message: res.msg + '，重新获取中' || 'Error',
        type: 'error',
        duration: 5 * 1000
      })
    }
  }).catch(err => {

  })
}
