BatchImportCamera.vue 8.4 KB

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