BatchImportCamera.vue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <template>
  2. <div>
  3. <el-card v-if="cardVisible">
  4. <template #header>
  5. <div class="flex justify-between items-center pop-head">
  6. <div style="font-size: 16px; font-weight: 600">批量导入</div>
  7. <el-icon :size="18" class="mr-3" @click="() => { emits('close'); }" style="cursor: pointer;">
  8. <Close />
  9. </el-icon>
  10. </div>
  11. </template>
  12. <div class="upload-content">
  13. <el-upload ref="upload" style="width: 384px; height: 192px; border-radius: 8px" :headers="headers"
  14. :multiple="false" :limit="1" drag action="/skyeye-admin-api/addCameraList/uploadForm" :with-credentials="true"
  15. :auto-upload="false" :before-upload="beforeUpload" :on-success="handleUploadSuccess" :on-exceed="handleExceed"
  16. :on-change="handleChange" :on-remove="handleRemove">
  17. <el-icon class="el-icon--upload" style="width: 33px; height: 42px; color: #409efc;">
  18. <Document />
  19. </el-icon>
  20. <div class="el-upload__text">
  21. <div style="font-size: 12px; color: red; margin-bottom: 5px;">请下载模板并按要求填写后上传</div>
  22. <div style="font-size: 16px">点击或将文件拖拽到这里上传</div>
  23. <div style="font-size: 12px; color: rgba(0, 0, 0, 0.45); margin-top: 5px;">文件支持.xlsx .xls格式,仅支持上传一个文件</div>
  24. </div>
  25. </el-upload>
  26. <div style="margin-top: 72px; margin-left: 128px; display: flex">
  27. <el-icon :size="18" style="margin-top: 7px;">
  28. <Download />
  29. </el-icon>
  30. <el-tooltip content="点击下载场景字段对应的code信息" placement="top" effect="light">
  31. <span style="color:#409efc; margin-top: 6px; margin-right: 12px; cursor: pointer;"
  32. @click="handleDownloadSceneCode">场景code信息查询</span>
  33. </el-tooltip>
  34. <el-button @click="handleDownloadTemplate">下载模板</el-button>
  35. <el-button type="primary" @click="handleImport" :disabled="isImportEnable">导入</el-button>
  36. </div>
  37. </div>
  38. </el-card>
  39. <el-dialog v-model="DialogVisibleErr" title="Warning" width="50%" align-center @close="() => { emits('update'); }">
  40. <template #header>
  41. <el-icon :size="24" color="#f2b20a" style="margin: 0 5px 2px">
  42. <WarnTriangleFilled />
  43. </el-icon>
  44. <div class="header-text">添加提示</div>
  45. </template>
  46. <div class="sum-count">
  47. 成功上传 <span class="succ-sum">{{ sucCount }}</span> 条,
  48. 失败 <span class="err-sum">{{ errCount }}</span> 条
  49. </div>
  50. <div class="err-info">
  51. <ul v-for="(item, index) in errDetail" :key="index">
  52. <li v-html="item"></li>
  53. </ul>
  54. </div>
  55. <template #footer>
  56. <el-button type="primary" @click="handleErrComfirm"> 确定 </el-button>
  57. </template>
  58. </el-dialog>
  59. </div>
  60. </template>
  61. <script setup lang="ts">
  62. import { ref } from 'vue';
  63. import axios, { AxiosRequestConfig } from 'axios';
  64. import { useUserStore } from '@/store/modules/user';
  65. import { genFileId, ElMessage } from 'element-plus';
  66. import type { UploadInstance, UploadProps, UploadRawFile } from 'element-plus';
  67. import { Close, Document, WarnTriangleFilled, Download } from '@element-plus/icons-vue';
  68. import { useGlobSetting } from '@/hooks/setting';
  69. const emits = defineEmits(['close', 'update']);
  70. const userStore = useUserStore();
  71. const headers = {
  72. Satoken: userStore.getToken,
  73. Tenantid: userStore.getTenantId,
  74. };
  75. const upload = ref<UploadInstance>();
  76. const cardVisible = ref<boolean>(true);
  77. const isImportEnable = ref<boolean>(true);
  78. const DialogVisibleErr = ref<boolean>(false);
  79. const sucCount = ref<number>(0);
  80. const errCount = ref<number>(0);
  81. const errDetail = ref<string[]>([]);
  82. const { urlPrefix } = useGlobSetting()
  83. // 下载场景code信息查询表
  84. const handleDownloadSceneCode = async () => {
  85. try {
  86. const config: AxiosRequestConfig = {
  87. headers,
  88. responseType: 'blob',
  89. };
  90. const response = await axios.get(urlPrefix + '/addCameraList/downloadWorkspaceCodeForm', config);
  91. const blob = new Blob([response.data], {
  92. type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  93. });
  94. // 创建下载链接
  95. let downloadLink: HTMLAnchorElement | null = document.createElement('a');
  96. const url = window.URL.createObjectURL(blob);
  97. downloadLink.href = url;
  98. downloadLink.download = '场景code信息查询.xlsx';
  99. downloadLink.click();
  100. // 移除下载链接
  101. window.URL.revokeObjectURL(url);
  102. downloadLink = null;
  103. } catch (error) {
  104. console.error('Error downloading file:', error);
  105. }
  106. };
  107. // 下载模板
  108. const handleDownloadTemplate = async () => {
  109. try {
  110. const config: AxiosRequestConfig = {
  111. headers,
  112. responseType: 'blob',
  113. };
  114. const response = await axios.get('/skyeye-file-upload/skyeye/CAMERALIST_TEMPLATE/camera-upload-template.xlsx', config);
  115. const blob = new Blob([response.data], {
  116. type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  117. });
  118. // 创建下载链接
  119. let downloadLink: HTMLAnchorElement | null = document.createElement('a');
  120. const url = window.URL.createObjectURL(blob);
  121. downloadLink.href = url;
  122. downloadLink.download = '相机批量添加.xlsx';
  123. downloadLink.click();
  124. // 移除下载链接
  125. window.URL.revokeObjectURL(url);
  126. downloadLink = null;
  127. } catch (error) {
  128. console.error('Error downloading file:', error);
  129. }
  130. };
  131. // 导入
  132. const handleImport = async () => {
  133. upload.value!.submit();
  134. };
  135. // 上传文件之前的钩子,参数为上传的文件。即上传之前验证文件类型/后缀
  136. const beforeUpload = (file) => {
  137. const isExcel = /\.(xlsx|xls)$/.test(file.name.toLowerCase());
  138. if (!isExcel) {
  139. // 提示用户选择正确的文件类型
  140. ElMessage({
  141. message: '仅支持上传.xlsx .xls格式文件',
  142. type: 'error',
  143. });
  144. return false; // 阻止上传
  145. };
  146. return true; // 允许上传
  147. };
  148. const handleUploadSuccess = (response, _file, _fileList) => {
  149. console.log(response);
  150. sucCount.value = response.data.successCount;
  151. errCount.value = response.data.failCount;
  152. errDetail.value = response.data.errorList;
  153. try {
  154. if (errDetail.value.length > 0) {
  155. errDetail.value.forEach((item, index) => {
  156. if (item.indexOf('【添加失败】') >= 0) {
  157. errDetail.value[index] = item.replace('【添加失败】', '<span style="color: #ff4d4f">【添加失败】</span>')
  158. } else if (item.indexOf('【添加成功】') >= 0) {
  159. errDetail.value[index] = item.replace('【添加成功】', '<span style="color: #52c41a">【添加成功】</span>')
  160. }
  161. })
  162. };
  163. if (sucCount.value != 0 && errCount.value === 0 && errDetail.value.length === 0) {
  164. ElMessage({
  165. message: '添加成功', // 1.全部添加成功 —— failCount === 0
  166. type: 'success',
  167. });
  168. emits('update');
  169. } else {
  170. DialogVisibleErr.value = true; // 2.有错误 —— 显示错误dialog
  171. };
  172. cardVisible.value = false;
  173. } catch (error) {
  174. ElMessage({
  175. message: '系统错误',
  176. type: 'error',
  177. });
  178. emits('update');
  179. };
  180. };
  181. const handleErrComfirm = () => {
  182. DialogVisibleErr.value = false;
  183. emits('update');
  184. };
  185. // 当超出只能上传一个文件的限制时,自动替换上一个文件
  186. const handleExceed: UploadProps['onExceed'] = (files) => {
  187. upload.value!.clearFiles();
  188. const file = files[0] as UploadRawFile;
  189. file.uid = genFileId();
  190. upload.value!.handleStart(file);
  191. };
  192. const handleChange = () => {
  193. isImportEnable.value = false;
  194. };
  195. const handleRemove = () => {
  196. isImportEnable.value = true;
  197. };
  198. </script>
  199. <style scoped>
  200. .upload-content {
  201. margin-left: 90px;
  202. margin-top: 36px;
  203. }
  204. :deep(.el-dialog) {
  205. padding: 0px;
  206. border-radius: 5px;
  207. .el-dialog__header {
  208. display: flex;
  209. align-items: flex-end;
  210. height: 70px;
  211. padding: 0px 0px 10px 10px;
  212. border-bottom: 1px solid #e7e7e7;
  213. .header-text {
  214. font-size: 20px;
  215. }
  216. }
  217. .el-dialog__headerbtn {
  218. top: 22px;
  219. .el-dialog__close {
  220. color: black;
  221. }
  222. }
  223. .el-dialog__body {
  224. padding: 20px;
  225. .sum-count {
  226. margin: 10px 0 20px 20px;
  227. font-size: 20px;
  228. .succ-sum {
  229. color: #52c41a;
  230. }
  231. .err-sum {
  232. color: #ff4d4f;
  233. }
  234. }
  235. .err-info {
  236. height: 200px;
  237. margin-left: 20px;
  238. overflow: auto;
  239. }
  240. }
  241. .el-dialog__footer {
  242. margin: 0 20px 20px 0;
  243. }
  244. }
  245. li {
  246. font-size: 14px;
  247. margin-bottom: 2px;
  248. }
  249. </style>