BatchImportCamera.vue 9.4 KB

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