BatchEditCamera.vue 7.1 KB

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