addEdit.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <template>
  2. <el-dialog
  3. v-loading="loading"
  4. title="导入数据"
  5. :center="true"
  6. align="left"
  7. top="20vh"
  8. width="700px"
  9. :close-on-click-modal="false"
  10. :visible.sync="showModelThis"
  11. element-loading-text="拼命加载中"
  12. element-loading-spinner="el-icon-loading"
  13. element-loading-background="rgba(0, 0, 0, 0.8)"
  14. @close="closeModel"
  15. >
  16. <el-card>
  17. <el-row :gutter="10">
  18. <el-col :span="24">
  19. <el-form
  20. ref="ruleForm"
  21. :model="ruleForm"
  22. status-icon
  23. :rules="rules"
  24. label-width="110px"
  25. class="demo-ruleForm"
  26. >
  27. <el-form-item label="表格文件" prop="execl">
  28. <el-card shadow="never" :body-style="{ padding: '0px' }">
  29. <div class="excelUploadBox" v-if="!isfile">
  30. <i class="el-icon-receiving"></i>
  31. <span class="boxM"> 点击此处,上传文件!</span>
  32. <excel-upload
  33. :accept="'.xls'"
  34. class="excelUpload"
  35. :uploadcondition="beforeAvatarUpload"
  36. @UploadErrorEvent="UploadErrorEvent"
  37. @UploadSuccessEvent="UploadSuccessEvent"
  38. />
  39. </div>
  40. <div v-else class="excelUploadRes clear">
  41. <i class="el-icon-document fl"></i>
  42. <span class="fl"> {{ ruleForm.execl.name }}</span>
  43. <i class="el-icon-close fr" @click="fileClose"></i>
  44. </div>
  45. </el-card>
  46. </el-form-item>
  47. </el-form>
  48. </el-col>
  49. <el-col :span="24" style="text-align: right">
  50. <el-button v-if="!isDetail" type="primary" @click="submitForm"
  51. >保 存
  52. </el-button>
  53. <el-button @click="showModelThis = false">{{
  54. isDetail ? "关 闭" : "取 消"
  55. }}</el-button>
  56. </el-col>
  57. </el-row>
  58. </el-card>
  59. </el-dialog>
  60. </template>
  61. <script>
  62. import excelUpload from "@/components/excelUpload";
  63. import urlConfig from "@/apis/url-config";
  64. import { getToken } from "@/utils/auth";
  65. import resToken from "@/mixins/resToken";
  66. export default {
  67. name: "capitalClaim",
  68. props: ["showModel", "id", "isDetail", "sitem"],
  69. components: { excelUpload },
  70. mixins: [resToken],
  71. data() {
  72. return {
  73. imgAPI: urlConfig.baseURL,
  74. loading: false,
  75. showModelThis: this.showModel,
  76. isfile: false,
  77. ruleForm: {
  78. execl: "",
  79. },
  80. rules: {
  81. execl: [
  82. {
  83. required: true,
  84. message: "请选择文件",
  85. trigger: "change",
  86. },
  87. ],
  88. },
  89. };
  90. },
  91. watch: {
  92. showModel: function (val) {
  93. this.showModelThis = val;
  94. if (val) {
  95. this.initForm();
  96. }
  97. },
  98. showModelThis(val) {
  99. if (!val) {
  100. this.$emit("cancel");
  101. }
  102. },
  103. },
  104. methods: {
  105. closeModel() {
  106. console.log("closeModel!!");
  107. },
  108. async initForm() {
  109. console.log(this.id);
  110. this.isfile = false;
  111. this.loading = true;
  112. await this.resetForm();
  113. this.loading = false;
  114. },
  115. async resetForm() {
  116. // 重置
  117. await this.$nextTick(() => {
  118. if (this.$refs.ruleForm) {
  119. this.$refs.ruleForm.resetFields();
  120. this.$refs.ruleForm.clearValidate();
  121. this.ruleForm = {
  122. execl: "",
  123. };
  124. }
  125. });
  126. },
  127. beforeAvatarUpload(file) {
  128. console.log(file.type);
  129. let isJPG = false;
  130. if (file.type === "application/vnd.ms-excel") {
  131. isJPG = true;
  132. }
  133. const isLt2M = file.size / 1024 / 1024 < 3;
  134. if (!isJPG) {
  135. this.$message.error("文件格式不正确!");
  136. }
  137. if (!isLt2M) {
  138. this.$message.error("文件大小不能超过 3MB!");
  139. }
  140. this.isfile = false;
  141. return isJPG && isLt2M;
  142. },
  143. fileClose() {
  144. this.isfile = false;
  145. this.ruleForm.execl = "";
  146. this.$refs.ruleForm.validateField("execl");
  147. },
  148. //图片上传失败
  149. UploadErrorEvent() {
  150. this.isfile = false;
  151. this.$message.error("文件上传失败!");
  152. this.$refs.ruleForm.validateField("execl");
  153. },
  154. //图片上传成功
  155. UploadSuccessEvent(data) {
  156. console.log(data);
  157. this.isfile = true;
  158. this.ruleForm.execl = data.file;
  159. this.$message.success("文件上传成功!");
  160. this.$refs.ruleForm.validateField("execl");
  161. },
  162. async submitForm() {
  163. await this.$refs.ruleForm.validate(async (valid) => {
  164. if (valid) {
  165. this.loading = true;
  166. let that = this;
  167. let form = new FormData();
  168. form.append("execl", this.ruleForm.execl);
  169. form.append("token", getToken());
  170. axios
  171. .post(`${that.imgAPI}Admin/tradecreate`, form)
  172. .then(async (res) => {
  173. this.loading = false;
  174. if (res && res.status === 200 && res.data) {
  175. let data = res.data;
  176. if (data.code === 0) {
  177. this.$notify.success({
  178. title: "数据导入成功!",
  179. message: "",
  180. });
  181. this.showModelThis = false;
  182. this.$emit("refresh");
  183. } else {
  184. this.$message.error(data.message);
  185. }
  186. } else if (res && res.code >= 100 && res.code <= 104) {
  187. await this.logout();
  188. } else {
  189. this.$message.warning(res.message);
  190. }
  191. })
  192. .catch((error) => {
  193. this.loading = false;
  194. console.log(error);
  195. });
  196. } else {
  197. console.log("error submit!!");
  198. return false;
  199. }
  200. });
  201. },
  202. },
  203. };
  204. </script>
  205. <style lang="scss" scoped>
  206. .capitalClaim {
  207. .excelUploadBox {
  208. position: relative;
  209. width: 100%;
  210. height: 120px;
  211. line-height: 120px;
  212. box-sizing: border-box;
  213. &:hover {
  214. cursor: pointer;
  215. }
  216. .el-icon-receiving {
  217. width: 100%;
  218. text-align: center;
  219. height: 50px;
  220. display: block;
  221. font-size: 32px;
  222. line-height: 90px;
  223. color: #d3d4d6;
  224. }
  225. .boxM {
  226. width: 100%;
  227. display: block;
  228. text-align: center;
  229. line-height: 65px;
  230. height: 60px;
  231. color: #909399;
  232. }
  233. }
  234. .excelUpload {
  235. top: 0;
  236. left: 0;
  237. position: absolute;
  238. z-index: 2;
  239. width: 100%;
  240. height: 120px;
  241. line-height: 120px;
  242. box-sizing: border-box;
  243. }
  244. .excelUploadRes {
  245. width: 100%;
  246. height: 120px;
  247. line-height: 120px;
  248. box-sizing: border-box;
  249. i {
  250. width: 55px;
  251. height: 120px;
  252. line-height: 120px;
  253. text-align: center;
  254. font-size: 20px;
  255. &.fl {
  256. padding-left: 16px;
  257. }
  258. &.fr {
  259. padding-right: 16px;
  260. &:hover {
  261. cursor: pointer;
  262. }
  263. }
  264. }
  265. span {
  266. width: 386px;
  267. line-height: 16px;
  268. margin: 52px 0 0 0;
  269. font-size: 16px;
  270. }
  271. }
  272. }
  273. </style>