import { domShowFu, progressDomFu } from '@/utils/domShow'
import { ResetPassType } from '@/types'
import http from '@/utils/http'
import axios from 'axios'
import store from '..'

/**
 * 用户登录接口
 */
export const userLoginAPI = (data: any) => {
  return http.post('museum/login', { ...data })
}

/**
 * 判断用户是否登录
 */
export const isLoginAPI = () => {
  return http.post('museum/isLogin')
}

/**
 * 获取用户信息接口
 */
export const getUserInfoAPI = () => {
  return http.post('museum/getUserInfo')
}

/**
 * 获取左侧菜单接口
 */
export const getLeftMenuAPI = () => {
  return http.post('museum/menu/treeselect')
}

/**
 * 修改密码接口
 */
export const passWordEditAPI = (data: ResetPassType) => {
  return http.post('museum/user/changePwd', { ...data })
}

const CancelToken = axios.CancelToken
/**
 * 上传封面图和附件
 */
export const API_upFile = (data: any, url: string) => {
  domShowFu('#UpAsyncLoding', true)

  return http.post(url, data, {
    timeout: 0,
    // 显示进度条
    onUploadProgress: (e: any) => {
      const complete = (e.loaded / e.total) * 100 || 0
      progressDomFu(complete + '%')
    },
    // 取消上传
    cancelToken: new CancelToken(function executor(c) {
      store.dispatch({
        type: 'layout/closeUpFile',
        payload: { fu: c, state: true }
      })
    })
  })
}

// 点击表格的预览操作，打开/关闭弹窗
export const editPreview = (data: { isOpenPreview: boolean; src?: string }) => {
  return {
    type: 'layout/editPreview',
    payload: data
  }
}
