干燥机配套车间生产管理系统/云平台服务端
baoshiwei
2023-03-10 1fb197352b6a263646e4ccd3ed1c7854ede031dd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<template>
  <a-button type="primary" v-if="hasExportAuth() && config.export" preIcon="ant-design:export-outlined" @click="onExportXls()"> 导出</a-button>
  <a-upload name="file" :showUploadList="false" v-if="hasImportAuth() && config.import" :customRequest="(file) => onImportXls(file)">
    <a-button type="primary" preIcon="ant-design:import-outlined">导入</a-button>
  </a-upload>
</template>
 
<script lang="ts" setup name="ExcelButton">
  import { PropType } from 'vue';
  import { usePermission } from '/@/hooks/web/usePermission';
  import { useMethods } from '/@/hooks/system/useMethods';
  import { useMessage } from '/@/hooks/web/useMessage';
 
  // 定义 excel 方法所需参数
  interface ExcelConfig {
    // 导出配置
    exportConfig: {
      url: string;
      // 导出文件名
      name?: string | (() => string);
      //按钮权限
      auth?: string | string[];
    };
    // 导入配置
    importConfig: {
      url: string;
      // 导出成功后的回调
      success?: (fileInfo?: any) => void;
      //按钮权限
      auth?: string | string[];
    };
  }
  /**
   * 定义组件参数
   */
  const props = defineProps({
    config: {
      type: Object as PropType<ExcelConfig>,
      default: null,
    },
  });
  //按钮权限问题
  const { hasPermission } = usePermission();
  //导入导出方法
  const { handleExportXls, handleImportXls } = useMethods();
 
  const $message = useMessage();
  // 导出 excel
  function onExportXls() {
    let { url, name } = props.config?.export ?? {};
    if (url) {
      let title = typeof name === 'function' ? name() : name;
      return handleExportXls(title as string, url);
    } else {
      $message.createMessage.warn('没有传递 export.url 参数');
      return Promise.reject();
    }
  }
 
  // 导入 excel
  function onImportXls(file) {
    let { url, success } = props.config?.import ?? {};
    if (url) {
      return handleImportXls(file, url, success);
    } else {
      $message.createMessage.warn('没有传递 import.url 参数');
      return Promise.reject();
    }
  }
 
  // 导入按钮权限
  function hasImportAuth() {
    let auth = props.config?.import?.auth;
    return auth && auth.length > 0 ? hasPermission(auth) : true;
  }
 
  // 导出按钮权限
  function hasExportAuth() {
    let auth = props.config?.export?.auth;
    return auth && auth.length > 0 ? hasPermission(auth) : true;
  }
</script>
 
<style scoped lang="less"></style>