ExcelButton.vue 2.4 KB

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