BatchEditCamera.vue 7.3 KB

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