BatchImportCamera.vue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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, `/admin/cameraConfig/uploadCameraList`);
  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.post(
  130. urlPrefix + '/admin/cameraConfig/exportSceneCodeInfo',
  131. null,
  132. config,
  133. );
  134. const blob = new Blob([response.data], {
  135. type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  136. });
  137. // 创建下载链接
  138. let downloadLink: HTMLAnchorElement | null = document.createElement('a');
  139. const url = window.URL.createObjectURL(blob);
  140. downloadLink.href = url;
  141. downloadLink.download = '场景code信息查询.xlsx';
  142. downloadLink.click();
  143. // 移除下载链接
  144. window.URL.revokeObjectURL(url);
  145. downloadLink = null;
  146. } catch (error) {
  147. console.error('Error downloading file:', error);
  148. }
  149. };
  150. // 下载模板
  151. const handleDownloadTemplate = async () => {
  152. try {
  153. const config: AxiosRequestConfig = {
  154. headers: getHeaders(),
  155. responseType: 'blob',
  156. };
  157. const response = await axios.get(
  158. './skyeye-file-upload/skyeye-v4/CAMERALIST_TEMPLATE/camera-upload-template.xlsx',
  159. config,
  160. );
  161. const blob = new Blob([response.data], {
  162. type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  163. });
  164. // 创建下载链接
  165. let downloadLink: HTMLAnchorElement | null = document.createElement('a');
  166. const url = window.URL.createObjectURL(blob);
  167. downloadLink.href = url;
  168. downloadLink.download = '相机批量添加.xlsx';
  169. downloadLink.click();
  170. // 移除下载链接
  171. window.URL.revokeObjectURL(url);
  172. downloadLink = null;
  173. } catch (error) {
  174. console.error('Error downloading file:', error);
  175. }
  176. };
  177. // 导入
  178. const handleImport = async () => {
  179. upload.value!.submit();
  180. };
  181. // 上传文件之前的钩子,参数为上传的文件。即上传之前验证文件类型/后缀
  182. const beforeUpload = (file) => {
  183. const isExcel = /\.(xlsx|xls)$/.test(file.name.toLowerCase());
  184. if (!isExcel) {
  185. // 提示用户选择正确的文件类型
  186. ElMessage({
  187. message: '仅支持上传.xlsx .xls格式文件',
  188. type: 'error',
  189. });
  190. return false; // 阻止上传
  191. }
  192. return true; // 允许上传
  193. };
  194. const handleUploadSuccess = (response, _file, _fileList) => {
  195. console.log(response);
  196. sucCount.value = response.data.successCount;
  197. errCount.value = response.data.failCount;
  198. errDetail.value = response.data.errorList;
  199. try {
  200. if (errDetail.value.length > 0) {
  201. errDetail.value.forEach((item, index) => {
  202. if (item.indexOf('【添加失败】') >= 0) {
  203. errDetail.value[index] = item.replace(
  204. '【添加失败】',
  205. '<span style="color: #ff4d4f">【添加失败】</span>',
  206. );
  207. } else if (item.indexOf('【添加成功】') >= 0) {
  208. errDetail.value[index] = item.replace(
  209. '【添加成功】',
  210. '<span style="color: #52c41a">【添加成功】</span>',
  211. );
  212. }
  213. });
  214. }
  215. if (sucCount.value != 0 && errCount.value === 0 && errDetail.value.length === 0) {
  216. ElMessage({
  217. message: '添加成功', // 1.全部添加成功 —— failCount === 0
  218. type: 'success',
  219. });
  220. emits('update');
  221. } else {
  222. DialogVisibleErr.value = true; // 2.有错误 —— 显示错误dialog
  223. }
  224. cardVisible.value = false;
  225. } catch (error) {
  226. ElMessage({
  227. message: '系统错误',
  228. type: 'error',
  229. });
  230. emits('update');
  231. }
  232. };
  233. const handleErrComfirm = () => {
  234. DialogVisibleErr.value = false;
  235. emits('update');
  236. };
  237. // 当超出只能上传一个文件的限制时,自动替换上一个文件
  238. const handleExceed: UploadProps['onExceed'] = (files) => {
  239. upload.value!.clearFiles();
  240. const file = files[0] as UploadRawFile;
  241. file.uid = genFileId();
  242. upload.value!.handleStart(file);
  243. };
  244. const handleChange = () => {
  245. isImportEnable.value = false;
  246. };
  247. const handleRemove = () => {
  248. isImportEnable.value = true;
  249. };
  250. </script>
  251. <style scoped>
  252. .upload-content {
  253. margin-left: 90px;
  254. margin-top: 36px;
  255. }
  256. :deep(.el-dialog) {
  257. padding: 0px;
  258. border-radius: 5px;
  259. .el-dialog__header {
  260. display: flex;
  261. align-items: flex-end;
  262. height: 70px;
  263. padding: 0px 0px 10px 10px;
  264. border-bottom: 1px solid #e7e7e7;
  265. .header-text {
  266. font-size: 20px;
  267. }
  268. }
  269. .el-dialog__headerbtn {
  270. top: 22px;
  271. .el-dialog__close {
  272. color: black;
  273. }
  274. }
  275. .el-dialog__body {
  276. padding: 20px;
  277. .sum-count {
  278. margin: 10px 0 20px 20px;
  279. font-size: 20px;
  280. .succ-sum {
  281. color: #52c41a;
  282. }
  283. .err-sum {
  284. color: #ff4d4f;
  285. }
  286. }
  287. .err-info {
  288. height: 200px;
  289. margin-left: 20px;
  290. overflow: auto;
  291. }
  292. }
  293. .el-dialog__footer {
  294. margin: 0 20px 20px 0;
  295. }
  296. }
  297. li {
  298. font-size: 14px;
  299. margin-bottom: 2px;
  300. }
  301. </style>