123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277 |
- <template>
- <el-dialog
- v-loading="loading"
- title="导入数据"
- :center="true"
- align="left"
- top="20vh"
- width="700px"
- :close-on-click-modal="false"
- :visible.sync="showModelThis"
- element-loading-text="拼命加载中"
- element-loading-spinner="el-icon-loading"
- element-loading-background="rgba(0, 0, 0, 0.8)"
- @close="closeModel"
- >
- <el-card>
- <el-row :gutter="10">
- <el-col :span="24">
- <el-form
- ref="ruleForm"
- :model="ruleForm"
- status-icon
- :rules="rules"
- label-width="110px"
- class="demo-ruleForm"
- >
- <el-form-item label="表格文件" prop="execl">
- <el-card shadow="never" :body-style="{ padding: '0px' }">
- <div class="excelUploadBox" v-if="!isfile">
- <i class="el-icon-receiving"></i>
- <span class="boxM"> 点击此处,上传文件!</span>
- <excel-upload
- :accept="'.xls'"
- class="excelUpload"
- :uploadcondition="beforeAvatarUpload"
- @UploadErrorEvent="UploadErrorEvent"
- @UploadSuccessEvent="UploadSuccessEvent"
- />
- </div>
- <div v-else class="excelUploadRes clear">
- <i class="el-icon-document fl"></i>
- <span class="fl"> {{ ruleForm.execl.name }}</span>
- <i class="el-icon-close fr" @click="fileClose"></i>
- </div>
- </el-card>
- </el-form-item>
- </el-form>
- </el-col>
- <el-col :span="24" style="text-align: right">
- <el-button v-if="!isDetail" type="primary" @click="submitForm"
- >保 存
- </el-button>
- <el-button @click="showModelThis = false">{{
- isDetail ? "关 闭" : "取 消"
- }}</el-button>
- </el-col>
- </el-row>
- </el-card>
- </el-dialog>
- </template>
- <script>
- import excelUpload from "@/components/excelUpload";
- import urlConfig from "@/apis/url-config";
- import { getToken } from "@/utils/auth";
- import resToken from "@/mixins/resToken";
- export default {
- name: "capitalClaim",
- props: ["showModel", "id", "isDetail", "sitem"],
- components: { excelUpload },
- mixins: [resToken],
- data() {
- return {
- imgAPI: urlConfig.baseURL,
- loading: false,
- showModelThis: this.showModel,
- isfile: false,
- ruleForm: {
- execl: "",
- },
- rules: {
- execl: [
- {
- required: true,
- message: "请选择文件",
- trigger: "change",
- },
- ],
- },
- };
- },
- watch: {
- showModel: function (val) {
- this.showModelThis = val;
- if (val) {
- this.initForm();
- }
- },
- showModelThis(val) {
- if (!val) {
- this.$emit("cancel");
- }
- },
- },
- methods: {
- closeModel() {
- console.log("closeModel!!");
- },
- async initForm() {
- console.log(this.id);
- this.isfile = false;
- this.loading = true;
- await this.resetForm();
- this.loading = false;
- },
- async resetForm() {
- // 重置
- await this.$nextTick(() => {
- if (this.$refs.ruleForm) {
- this.$refs.ruleForm.resetFields();
- this.$refs.ruleForm.clearValidate();
- this.ruleForm = {
- execl: "",
- };
- }
- });
- },
- beforeAvatarUpload(file) {
- console.log(file.type);
- let isJPG = false;
- if (file.type === "application/vnd.ms-excel") {
- isJPG = true;
- }
- const isLt2M = file.size / 1024 / 1024 < 3;
- if (!isJPG) {
- this.$message.error("文件格式不正确!");
- }
- if (!isLt2M) {
- this.$message.error("文件大小不能超过 3MB!");
- }
- this.isfile = false;
- return isJPG && isLt2M;
- },
- fileClose() {
- this.isfile = false;
- this.ruleForm.execl = "";
- this.$refs.ruleForm.validateField("execl");
- },
- //图片上传失败
- UploadErrorEvent() {
- this.isfile = false;
- this.$message.error("文件上传失败!");
- this.$refs.ruleForm.validateField("execl");
- },
- //图片上传成功
- UploadSuccessEvent(data) {
- console.log(data);
- this.isfile = true;
- this.ruleForm.execl = data.file;
- this.$message.success("文件上传成功!");
- this.$refs.ruleForm.validateField("execl");
- },
- async submitForm() {
- await this.$refs.ruleForm.validate(async (valid) => {
- if (valid) {
- this.loading = true;
- let that = this;
- let form = new FormData();
- form.append("execl", this.ruleForm.execl);
- form.append("token", getToken());
- axios
- .post(`${that.imgAPI}Admin/tradecreate`, form)
- .then(async (res) => {
- this.loading = false;
- if (res && res.status === 200 && res.data) {
- let data = res.data;
- if (data.code === 0) {
- this.$notify.success({
- title: "数据导入成功!",
- message: "",
- });
- this.showModelThis = false;
- this.$emit("refresh");
- } else {
- this.$message.error(data.message);
- }
- } else if (res && res.code >= 100 && res.code <= 104) {
- await this.logout();
- } else {
- this.$message.warning(res.message);
- }
- })
- .catch((error) => {
- this.loading = false;
- console.log(error);
- });
- } else {
- console.log("error submit!!");
- return false;
- }
- });
- },
- },
- };
- </script>
- <style lang="scss" scoped>
- .capitalClaim {
- .excelUploadBox {
- position: relative;
- width: 100%;
- height: 120px;
- line-height: 120px;
- box-sizing: border-box;
- &:hover {
- cursor: pointer;
- }
- .el-icon-receiving {
- width: 100%;
- text-align: center;
- height: 50px;
- display: block;
- font-size: 32px;
- line-height: 90px;
- color: #d3d4d6;
- }
- .boxM {
- width: 100%;
- display: block;
- text-align: center;
- line-height: 65px;
- height: 60px;
- color: #909399;
- }
- }
- .excelUpload {
- top: 0;
- left: 0;
- position: absolute;
- z-index: 2;
- width: 100%;
- height: 120px;
- line-height: 120px;
- box-sizing: border-box;
- }
- .excelUploadRes {
- width: 100%;
- height: 120px;
- line-height: 120px;
- box-sizing: border-box;
- i {
- width: 55px;
- height: 120px;
- line-height: 120px;
- text-align: center;
- font-size: 20px;
- &.fl {
- padding-left: 16px;
- }
- &.fr {
- padding-right: 16px;
- &:hover {
- cursor: pointer;
- }
- }
- }
- span {
- width: 386px;
- line-height: 16px;
- margin: 52px 0 0 0;
- font-size: 16px;
- }
- }
- }
- </style>
|