common.js 1.5 KB
import axios from 'axios'
export const Common = {
  // 提示框
  confirmDialog: (tipData) => {
    tipData.that.$confirm(tipData.tipText, tipData.tipTitle, {
      confirmButtonText: tipData.confirmButtonText || '确定',
      cancelButtonText: tipData.cancelButtonText || '取消',
      type: tipData.type,
      showCancelButton: tipData.showCancelButton
    }).then(() => {
      tipData.callBack ? tipData.callBack() : ''
    }).catch(() => {
      tipData.errorCallBack ? tipData.errorCallBack() : ''
    })
  },
  /**
	 * 导出excel
	 * */
  exportExcel: function (options) {
    if (!options) {
      return false
    }
    console.log(options.data)
    axios({
      method: options.method || 'POST',
      url: options.url,
      params: options.data,
      responseType: 'blob',
      headers: {
        Authorization: localStorage.getItem('token') || ''
      }
    }).then(response => {
      if (!response) {
        return
      }
      let url = window.URL.createObjectURL(new Blob([response.data], {
        type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8'
      }))
      let link = document.createElement('a')
      link.style.display = 'none'
      link.href = url
      link.setAttribute('download', options.fileName || 'excel.xlsx')
      document.body.appendChild(link)
      link.click()
      if (options.success) {
        (options.success)()
      }
    }).catch((error) => {
      if (options.errorFunc) {
        (options.errorFunc)(error)
      }
    })
  }
}