index.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <template>
  2. <div>
  3. <dg-button @click="download" style="margin-bottom: 16px">模版下载</dg-button>
  4. <dg-upload
  5. ref="upload"
  6. :limit="limit"
  7. :accept="accept"
  8. :on-error="handleError"
  9. :on-success="handleSuccess"
  10. :before-upload="beforeUpload"
  11. action="/dcucauth/duceap/v2/upload/file"
  12. drag
  13. >
  14. <i class="el-icon-upload" />
  15. <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
  16. <div class="el-upload__tip" slot="tip">只能上传excel(.xls,.xlsm,.xlsx,.et)文件,且不超过1M</div>
  17. </dg-upload>
  18. <!-- <div v-footer>
  19. <el-button @click="handleCancel">取消</el-button>
  20. <el-button type="primary" @click="handleSubmit">确定</el-button>
  21. </div> -->
  22. </div>
  23. </template>
  24. <script>
  25. import { downloadBlob } from "@/utils/download";
  26. import * as commonApi from "@/api/common";
  27. // import axios from "@/utils/req";
  28. const dd = {
  29. police: "警员模版.xls",
  30. auxiliaryPolice: "辅警模版.xls",
  31. externalPersonnel: "外部人员.xls",
  32. manager: "管理员模版.xls",
  33. govUser: "政府人员模版.xls",
  34. apply: "应用资源模板.xls",
  35. appFun: "功能资源模板.xls",
  36. serviceResource: "服务资源模板.xls",
  37. org: "机构模板.xls",
  38. user: "人员模板.xls",
  39. dataLevel: "数据分级模板.xls",
  40. dataSec: "数据安全级别模板.xls",
  41. };
  42. export default {
  43. name: "import",
  44. props: {
  45. limit: {
  46. type: Number,
  47. default: 1
  48. },
  49. temp: {
  50. type: String,
  51. required: true
  52. },
  53. action: {
  54. type: String,
  55. required: true
  56. },
  57. accept: {
  58. type: String,
  59. default: ".xls,.xlsm,.xlsx,.et"
  60. },
  61. method: {
  62. type: String,
  63. default: "post"
  64. },
  65. otherParams: {
  66. type: Object,
  67. default() {
  68. return {}
  69. }
  70. }
  71. // url: {
  72. // type: String,
  73. // },
  74. // fileName: {
  75. // type: String,
  76. // },
  77. },
  78. methods: {
  79. download() {
  80. const that = this;
  81. const { temp } = that;
  82. // const { temp, url, fileName } = that;
  83. // if (url) {
  84. // axios
  85. // .request(url, {}, { method: 'post' })
  86. // .then(res => {
  87. // downloadBlob(fileName, res.data);
  88. // })
  89. // .catch(error => {
  90. // console.error('[Error]', error);
  91. // that.$message.error('下载失败');
  92. // });
  93. // return;
  94. // }
  95. commonApi
  96. .downTemplate({ type: temp })
  97. .then((res) => {
  98. downloadBlob(dd[temp] || "模版", res.data);
  99. })
  100. .catch((error) => {
  101. console.error("[Error]", error);
  102. that.$message.error("下载失败");
  103. });
  104. },
  105. beforeUpload(file) {
  106. const that = this;
  107. const isExcel = [/.xls$/, /.xlsm$/, /.xlsx$/, /.et$/].some((item) => item.test(file.name));
  108. const isLt2M = file.size / 1024 / 1024 < 1;
  109. if (!isExcel) {
  110. that.$message.error("只能上传文件只能是 excel 格式文件!");
  111. }
  112. if (!isLt2M) {
  113. that.$message.error("上传文件大小不能超过 1MB!");
  114. }
  115. return isExcel && isLt2M;
  116. },
  117. onClose() {
  118. this.$emit("close");
  119. },
  120. clearFiles() {
  121. this.$refs.upload.clearFiles();
  122. },
  123. handleSuccess({ id }, file, fileList) {
  124. const { action, otherParams } = this;
  125. commonApi.importFile(action,{ fileId: id, ...otherParams}).then(res => {
  126. if(res.data.statusCode == '200') {
  127. this.$message.success(res.data.message);
  128. this.$emit("success");
  129. }else {
  130. this.clearFiles();
  131. this.$message.error(res.data.message);
  132. }
  133. }).catch(err => {
  134. this.clearFiles();
  135. this.$message.error(err.response.data.message);
  136. })
  137. },
  138. handleError(err, file, fileList) {
  139. this.clearFiles();
  140. this.$message.error("上传失败,请重新上传");
  141. },
  142. // 取消
  143. handleCancel() {
  144. this.$emit("close");
  145. },
  146. // 确定
  147. handleSubmit() {
  148. this.$emit("success");
  149. }
  150. }
  151. };
  152. </script>
  153. <style lang="scss" scoped>
  154. /deep/ .el-upload-dragger {
  155. width: 500px;
  156. }
  157. </style>