batchImportDialog.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <template>
  2. <div>
  3. <Base-dialog
  4. title="导入"
  5. width="660px"
  6. :isShow.sync="isShow"
  7. :isShowFooter="false"
  8. >
  9. <div>
  10. <div class="swq">
  11. <img
  12. style="width: 182px; height: 168px"
  13. src="@/assets/images/dr.png"
  14. alt=""
  15. />
  16. </div>
  17. <div style="padding-left: 100px">
  18. <p>第一步:下载导入模板</p>
  19. <p style="padding-left: 50px">
  20. <i class="el-icon-upload"></i
  21. ><span class="dowmStys" @click="getDowm">下载模板</span>
  22. </p>
  23. <p>第二步:(批量新增):点击“上传{{ type }}”完成导入</p>
  24. <div
  25. v-loading="loading"
  26. element-loading-text="正在处理中"
  27. style="margin: 0 0 20px 50px; width: 130px; height: 60px"
  28. >
  29. <label
  30. v-show="!loading"
  31. for="mobles"
  32. class="el-button el-button--primary"
  33. >上传 {{ type }}</label
  34. ><input
  35. style="display: none"
  36. type="file"
  37. id="mobles"
  38. ref="input1"
  39. @change="importMobleadd"
  40. />
  41. </div>
  42. </div>
  43. <span slot="footer" class="dialog-footer">
  44. <el-button @click="isShow = false">取消</el-button>
  45. </span>
  46. </div>
  47. </Base-dialog>
  48. <Base-dialog
  49. title="提示"
  50. width="660px"
  51. :isShow.sync="isShowErr"
  52. :isShowFooter="false"
  53. >
  54. <div>
  55. <div>
  56. <h4 style="margin-top: 0px; font-weight: bold; text-align: center">
  57. 导入失败原因
  58. </h4>
  59. <el-input
  60. readonly
  61. type="textarea"
  62. :autosize="{ minRows: 6, maxRows: 24 }"
  63. v-model="errorData"
  64. >
  65. </el-input>
  66. </div>
  67. <span slot="footer" class="dialog-footer">
  68. <el-button @click="isShowErr = false">确定</el-button>
  69. </span>
  70. </div>
  71. </Base-dialog>
  72. </div>
  73. </template>
  74. <script>
  75. import * as baseUrls from "@/utils/request.js";
  76. export default {
  77. props: {
  78. dialogVisible: {
  79. type: Boolean,
  80. default: false,
  81. },
  82. temUrl: {
  83. type: String,
  84. },
  85. apiKey: {
  86. type: String,
  87. },
  88. checkKey: {
  89. type: String,
  90. },
  91. newSujectApis: {
  92. type: Array,
  93. default: () => {
  94. return [];
  95. },
  96. },
  97. isCheck: {
  98. type: Boolean,
  99. default: true,
  100. },
  101. isShowTip: {
  102. type: Boolean,
  103. default: true,
  104. },
  105. param: {
  106. type: Object,
  107. default: () => {
  108. return {};
  109. },
  110. },
  111. type: {
  112. type: String,
  113. default: "Excel",
  114. },
  115. isSuccessBack: {
  116. type: Boolean,
  117. default: false,
  118. },
  119. },
  120. data() {
  121. return {
  122. loading: false,
  123. errorData: [],
  124. isShowErr: false,
  125. };
  126. },
  127. methods: {
  128. getDowm() {
  129. let url =
  130. baseUrls.BASE_IMG_URL +
  131. this.temUrl +
  132. `?time=${this.$methodsTools.getNewTime()}`;
  133. let link = document.createElement("a");
  134. let fileName = "导入模板" + ".xlsx";
  135. document.body.appendChild(link);
  136. link.href = url;
  137. link.dowmload = fileName;
  138. link.click();
  139. link.remove();
  140. },
  141. importMobleadd(e) {
  142. var file = e.target.files[0];
  143. if (file === undefined) {
  144. e.target.value = "";
  145. return;
  146. }
  147. var type = e.target.value.toLowerCase().split(".").splice(-1);
  148. if (this.type == "Excel") {
  149. if (type[0] != "xlsx" && type[0] != "xls") {
  150. e.target.value = "";
  151. this.$message.error("请上传excel文件,且上传格式需为:.xlsx、.xls");
  152. return;
  153. }
  154. } else {
  155. if (type[0] != "docx") {
  156. e.target.value = "";
  157. this.$message.error("请上传word文件,且上传格式需为:.docx");
  158. return;
  159. }
  160. }
  161. this.loading = true;
  162. let formData = new FormData();
  163. formData.append("file", file);
  164. for (const key in this.param) {
  165. formData.append(key, this.param[key]);
  166. }
  167. this.$api[this.apiKey](formData)
  168. .then((res) => {
  169. if (res.code === 200) {
  170. this.isCheck && this.getFestivalList(res.data);
  171. this.getErrorData(res.data || res);
  172. }
  173. })
  174. .finally(() => {
  175. e.target.value = "";
  176. this.loading = false;
  177. });
  178. },
  179. /**
  180. *
  181. * @param {Strings} ids 查询编码
  182. * @param {Number} type 1为成功2为失败
  183. * @remards 失败时也需查询是否有成功的数据导入数据库,如存在 则加列队列同时提示
  184. */
  185. getFestivalList({ importNo, errorLog }) {
  186. this.$api[this.checkKey]({ importNo })
  187. .then((res) => {
  188. if (res.rows.length) {
  189. errorLog &&
  190. this.$message({
  191. type: "success",
  192. message: `成功导入${res.rows.length}条数据,`,
  193. customClass: "myMessageClass",
  194. });
  195. this.$emit("success", res.rows);
  196. }
  197. })
  198. .catch(() => {});
  199. },
  200. getErrorData(data) {
  201. this.isShow = false;
  202. let { errorLog } = data;
  203. if (errorLog) {
  204. let ary = errorLog.split("\r\n");
  205. ary = ary
  206. .filter((item) => {
  207. return item.length > 0;
  208. })
  209. .reverse();
  210. this.$message({
  211. message: `${ary.length}条数据导入失败,请查看失败原因`,
  212. customClass: "myMessageClass",
  213. });
  214. ary = ary.join("\r\n");
  215. this.errorData = ary;
  216. this.isShowErr = true;
  217. } else {
  218. this.isShowTip &&
  219. this.$message({
  220. type: "success",
  221. message: `添加成功`,
  222. customClass: "myMessageClass",
  223. });
  224. }
  225. if (!errorLog || this.isSuccessBack) {
  226. this.$emit("success", data);
  227. }
  228. },
  229. },
  230. computed: {
  231. isShow: {
  232. get() {
  233. return this.dialogVisible;
  234. },
  235. set(val) {
  236. this.$emit("update:dialogVisible", false);
  237. },
  238. },
  239. },
  240. };
  241. </script>
  242. <style lang="less" scoped>
  243. .swq {
  244. text-align: center;
  245. border-bottom: 1px solid #eee;
  246. }
  247. .dowmStys {
  248. color: blue;
  249. cursor: pointer;
  250. }
  251. /deep/.el-dialog {
  252. .el-dialog__body {
  253. padding: 30px 20px 0;
  254. }
  255. .dialog-footer {
  256. height: 80px;
  257. border-top: 1px solid #e2e2e2;
  258. display: flex;
  259. align-items: center;
  260. justify-content: flex-end;
  261. }
  262. }
  263. </style>