AddUser.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <template>
  2. <div v-if="props.modelValue">
  3. <el-card v-if="cardVisible" class="pop-card">
  4. <template #header>
  5. <div class="flex justify-between items-center pop-head">
  6. <div style="font-size: 16px">批量导入</div>
  7. <el-icon :size="16" class="mr-3" @click="updateValue(false)"><Close /></el-icon
  8. ></div>
  9. </template>
  10. <div class="upload-content">
  11. <el-upload
  12. ref="upload"
  13. class="upload-demo"
  14. :multiple="false"
  15. :limit="1"
  16. drag
  17. action="/skyeye-admin-api/user/import"
  18. :headers="headers"
  19. :with-credentials="true"
  20. :auto-upload="false"
  21. :on-exceed="handleExceed"
  22. :before-upload="beforeUpload"
  23. :on-success="handleUploadSuccess"
  24. style="width: 384px; height: 192px; border-radius: 8px"
  25. >
  26. <el-icon class="el-icon--upload" style="width: 33px; height: 42px"><Document /></el-icon>
  27. <div class="el-upload__text">
  28. <div style="font-size: 16px">点击或将文件拖拽到这里上传</div>
  29. <div style="font-size: 14px; color: rgba(0, 0, 0, 0.45)"
  30. >文件只支持:.xlsx .xls .仅支持上传一个文件</div
  31. ></div
  32. >
  33. </el-upload>
  34. <div style="margin-top: 52px; margin-left: 288px; display: flex">
  35. <el-button @click="handleDownload" style="margin-right: 10px">下载模板</el-button>
  36. <el-button type="primary" @click="handleImport">导入</el-button></div
  37. ></div
  38. >
  39. </el-card>
  40. <!-- //上传成功 -->
  41. <el-dialog
  42. v-model="DialogVisibleSuc"
  43. title="Warning"
  44. width="30%"
  45. @close="
  46. () => {
  47. emits('update:modelValue', false);
  48. }
  49. "
  50. align-center
  51. >
  52. <template #header="{ titleId, titleClass }">
  53. <div class="my-header">
  54. <h4 :id="titleId" :class="titleClass" style="display: flex">
  55. <el-icon style="margin-top: 2px" color="#52C41A"><CircleCheck /></el-icon
  56. ><div style="margin-left: 14px">导入成功</div></h4
  57. >
  58. </div>
  59. </template>
  60. <span style="margin-left: 30px"> 已成功添加{{ sucCount }}条用户信息</span>
  61. <template #footer>
  62. <span class="dialog-footer">
  63. <el-button type="primary" @click="handleRightComfirm"> 确定 </el-button>
  64. </span>
  65. </template>
  66. </el-dialog>
  67. <!-- 上传失败 -->
  68. <el-dialog
  69. v-model="DialogVisibleErr"
  70. title="Warning"
  71. width="30%"
  72. @close="
  73. () => {
  74. emits('update:modelValue', false);
  75. }
  76. "
  77. align-center
  78. >
  79. <template #header="{ titleId, titleClass }">
  80. <div class="my-header">
  81. <h4 :id="titleId" :class="titleClass" style="display: flex">
  82. <el-icon style="margin-top: 2px" color="#FF4D4F "><Warning /></el-icon>
  83. <div style="margin-left: 14px">导入失败</div></h4
  84. >
  85. </div>
  86. </template>
  87. <ul v-for="(item, index) in errDetail" :key="index">
  88. <li v-if="index < 3">{{ item }} </li>
  89. <li v-else v-show="showMore">{{ item }}</li>
  90. </ul>
  91. <div v-if="errDetail.length > 3 && !showMore" @click="showMore = true" class="more-link"
  92. >更多</div
  93. >
  94. <template #footer>
  95. <span class="dialog-footer">
  96. <el-button type="primary" @click="handleErrComfirm"> 确定 </el-button>
  97. </span>
  98. </template>
  99. </el-dialog></div
  100. >
  101. </template>
  102. <script setup lang="ts">
  103. import { Close, Document, CircleCheck, Warning } from '@element-plus/icons-vue';
  104. import { ref } from 'vue';
  105. import { genFileId, ElMessage } from 'element-plus';
  106. import type { UploadInstance, UploadProps, UploadRawFile } from 'element-plus';
  107. import { useUserStore } from '@/store/modules/user';
  108. import { onMounted } from 'vue';
  109. import axios, { AxiosRequestConfig } from 'axios';
  110. const userStore = useUserStore();
  111. onMounted(() => {
  112. console.log('111');
  113. console.log('props.modelValue', props.modelValue);
  114. cardVisible.value = props.modelValue;
  115. });
  116. const headers = {
  117. Satoken: userStore.getToken,
  118. Tenantid: userStore.getTenantId,
  119. };
  120. const cardVisible = ref<boolean>(true);
  121. //对话框
  122. const DialogVisibleSuc = ref<boolean>(false);
  123. const DialogVisibleErr = ref<boolean>(false);
  124. //更多
  125. const showMore = ref(false);
  126. const isSuc = ref<boolean>(true);
  127. const errDetail = ref<string[]>([]);
  128. const sucCount = ref<number>(0);
  129. const props = defineProps<{ modelValue: boolean }>();
  130. const emits = defineEmits(['update:modelValue', 'change']);
  131. const updateValue = (value) => {
  132. emits('update:modelValue', value);
  133. };
  134. const upload = ref<UploadInstance>();
  135. const handleDownload = async () => {
  136. //调用后端接口
  137. try {
  138. const config: AxiosRequestConfig = {
  139. headers,
  140. responseType: 'blob',
  141. };
  142. const response = await axios.get('/skyeye-admin-api/user/downloadExcel', config);
  143. const blob = new Blob([response.data], {
  144. type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  145. });
  146. // 创建下载链接
  147. let downloadLink: HTMLAnchorElement | null = document.createElement('a');
  148. const url = window.URL.createObjectURL(blob);
  149. downloadLink.href = url;
  150. downloadLink.download = '批量导入模板(重命名请加后缀.xlsm).xlsm';
  151. downloadLink.click();
  152. // 移除下载链接
  153. window.URL.revokeObjectURL(url);
  154. downloadLink = null;
  155. } catch (error) {
  156. console.error('Error downloading file:', error);
  157. }
  158. };
  159. const handleImport = async () => {
  160. upload.value!.submit();
  161. };
  162. const beforeUpload = (file) => {
  163. const isExcel = /\.(xlsx|xls|xlsm)$/.test(file.name.toLowerCase());
  164. if (!isExcel) {
  165. // 提示用户选择正确的文件类型
  166. ElMessage({
  167. message: '仅支持上传.xlsx .xls格式文件',
  168. type: 'error',
  169. });
  170. return false; // 阻止上传
  171. }
  172. return true; // 允许上传
  173. };
  174. const handleUploadSuccess = (response, _file, _fileList) => {
  175. isSuc.value = response.data.isSuc;
  176. errDetail.value = response.data.failedInfo;
  177. sucCount.value = response.data.sucCount || 0;
  178. if (isSuc.value) {
  179. DialogVisibleSuc.value = true;
  180. DialogVisibleErr.value = false;
  181. } else {
  182. DialogVisibleSuc.value = false;
  183. DialogVisibleErr.value = true;
  184. }
  185. cardVisible.value = false;
  186. };
  187. const handleRightComfirm = () => {
  188. DialogVisibleSuc.value = false;
  189. emits('update:modelValue', false);
  190. emits('change');
  191. };
  192. const handleErrComfirm = () => {
  193. DialogVisibleErr.value = false;
  194. emits('update:modelValue', false);
  195. emits('change');
  196. };
  197. const handleExceed: UploadProps['onExceed'] = (files) => {
  198. upload.value!.clearFiles();
  199. const file = files[0] as UploadRawFile;
  200. file.uid = genFileId();
  201. upload.value!.handleStart(file);
  202. };
  203. //对话框
  204. </script>
  205. <style scoped>
  206. .upload-content {
  207. margin-left: 96px;
  208. margin-top: 36px;
  209. }
  210. .more-link {
  211. color: #1890ff;
  212. margin-left: 30px;
  213. }
  214. li:before {
  215. content: '';
  216. width: 6px;
  217. height: 6px;
  218. display: inline-block;
  219. border-radius: 50%;
  220. background: #ff4d4f;
  221. vertical-align: middle;
  222. margin-left: 30px;
  223. margin-right: 8px;
  224. }
  225. li {
  226. font-size: 14px;
  227. margin-bottom: 2px;
  228. }
  229. </style>