index.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. };
  40. export default {
  41. name: "import",
  42. props: {
  43. limit: {
  44. type: Number,
  45. default: 1
  46. },
  47. temp: {
  48. type: String,
  49. required: true
  50. },
  51. action: {
  52. type: String,
  53. required: true
  54. },
  55. accept: {
  56. type: String,
  57. default: ".xls,.xlsm,.xlsx,.et"
  58. },
  59. method: {
  60. type: String,
  61. default: "post"
  62. },
  63. otherParams: {
  64. type: Object,
  65. default() {
  66. return {}
  67. }
  68. }
  69. // url: {
  70. // type: String,
  71. // },
  72. // fileName: {
  73. // type: String,
  74. // },
  75. },
  76. methods: {
  77. download() {
  78. const that = this;
  79. const { temp } = that;
  80. // const { temp, url, fileName } = that;
  81. // if (url) {
  82. // axios
  83. // .request(url, {}, { method: 'post' })
  84. // .then(res => {
  85. // downloadBlob(fileName, res.data);
  86. // })
  87. // .catch(error => {
  88. // console.error('[Error]', error);
  89. // that.$message.error('下载失败');
  90. // });
  91. // return;
  92. // }
  93. commonApi
  94. .downTemplate({ type: temp })
  95. .then((res) => {
  96. downloadBlob(dd[temp] || "模版", res.data);
  97. })
  98. .catch((error) => {
  99. console.error("[Error]", error);
  100. that.$message.error("下载失败");
  101. });
  102. },
  103. beforeUpload(file) {
  104. const that = this;
  105. const isExcel = [/.xls$/, /.xlsm$/, /.xlsx$/, /.et$/].some((item) => item.test(file.name));
  106. const isLt2M = file.size / 1024 / 1024 < 1;
  107. if (!isExcel) {
  108. that.$message.error("只能上传文件只能是 excel 格式文件!");
  109. }
  110. if (!isLt2M) {
  111. that.$message.error("上传文件大小不能超过 1MB!");
  112. }
  113. return isExcel && isLt2M;
  114. },
  115. onClose() {
  116. this.$emit("close");
  117. },
  118. clearFiles() {
  119. this.$refs.upload.clearFiles();
  120. },
  121. handleSuccess({ id }, file, fileList) {
  122. const { action, otherParams } = this;
  123. commonApi.importFile(action,{ fileId: id, ...otherParams}).then(res => {
  124. if(res.data.statusCode == '200') {
  125. this.$message.success(res.data.message);
  126. this.$emit("success");
  127. }else {
  128. this.clearFiles();
  129. this.$message.error(res.data.message);
  130. }
  131. }).catch(err => {
  132. this.clearFiles();
  133. this.$message.error("上传失败");
  134. })
  135. },
  136. handleError(err, file, fileList) {
  137. this.clearFiles();
  138. this.$message.error("上传失败,请重新上传");
  139. },
  140. // 取消
  141. handleCancel() {
  142. this.$emit("close");
  143. },
  144. // 确定
  145. handleSubmit() {
  146. this.$emit("success");
  147. }
  148. }
  149. };
  150. </script>
  151. <style lang="scss" scoped>
  152. /deep/ .el-upload-dragger {
  153. width: 500px;
  154. }
  155. </style>