index.js 9.11 KB
import Api from '~/service/Api';
import Utils from "~/utils/index"
import commonFunctions from '~/mixin/commonFunctions.js';
import operationFunc from "~/mixin/commonDepend";

export default {
  mixins: [commonFunctions, operationFunc],
  methods: {
    file2XLSX(file) {
			return new Promise(function (resolve, reject) {
				// 通过FileReader对象读取文件
				const reader = new FileReader()
				// 读取为二进制字符串
				reader.readAsBinaryString(file)
				reader.onload = function (e) {
					console.log(e, '读取文件成功的e');
					// 获取读取文件成功的结果值
					const data = e.target.result
					// XLSX.read解析数据,按照type 的类型解析
					const XLSX = require('xlsx')
					let wb = XLSX.read(data, {
						type: 'binary' // 二进制
					})
					console.log(wb, '---->解析后的数据')
					// 存储获取到的数据
					const result = []
					// 工作表名称的有序列表
					wb.SheetNames.forEach(sheetName => {
						result.push({
							// 工作表名称
							sheetName: sheetName,
							// 利用 sheet_to_json 方法将 excel 转成 json 数据
							sheet: XLSX.utils.sheet_to_json(wb.Sheets[sheetName])
						})
					})
					resolve(result)
				}
			})
    },
    
    queryOwnref() {
      Api.post(`/Financing/dbeblc/getRef`, this.model).then(res => {
        if (res.respCode == "AAAAAA") {
          this.model.recgrp.rec.ownref = res.data;
        }
      });
    },

    async edit() {
      // 前端检验
      this.$refs['modelForm'].clearValidate();
      this.$refs['modelForm'].validate(async (validStatic) => {
        if (validStatic) {
          const loading = this.loading();
          this.$store.commit('setLoadingSubmit', true);
          if (this.model.recpan.danlst.length == 0) {
            this.$alert('至少输入一条关单信息!', '提示');
          } else {
            const res = await Api.post(`/Financing/dbeblc/edit`, this.model);
            if (res.respCode === "AAAAAA") {
              loading.close();
              if (!res.fieldErrors || (res.fieldErrors && Object.keys(res.fieldErrors).length == 0)) {
                this.$notify({
                  title: '成功',
                  message: '提交成功',
                  type: 'success',
                });
                this.$nextTick(() => {
                  this.$store.commit('delTagsArry', this.$route.path);
									this.$router.back()
                })
              } else {
                const tab = this.showBackendErrors(res.fieldErrors);
                if (tab) {
                  // 判断校验失败的表单不属于当前的tab,则切换tab到对应报错的tab页面
                  if (tab.name !== this.tabVal) {
                    this.tabClick(tab, '1');
                  }
                }
              }
            } else {
              this.$notify({
                title: "错误",
                message: res.respMsg,
                type: "error",
              });
            }
          }
          loading.close();
          this.$store.commit('setLoadingSubmit', false)
        } else {
          // 前端校验失败
          this.$notify({
            title: '失败',
            message: '校验失败',
            type: 'error',
          });
          this.showFrontendErrors()
        }
      })
    },

    async init(params) {
      const loading = this.loading();
      const rtnmsg = await Api.post(`/${this.moduleRouter()}/${this.trnName}/init`, params);
      if (rtnmsg.respCode === SUCCESS) {
        loading.close();
        this.model = rtnmsg.data;
      } else if (rtnmsg.respCode === '400001') {
        loading.close();
        this.$store.commit('delTagsArry', this.$route.path);
        this.$router.back()
      }
    },

    exit() {
      this.$confirm("确认退出?", "提示", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        Api.post(`/Financing/dbeblc/exit`, this.model);
        this.$store.commit('delTagsArry', this.$route.path)
        this.$router.back()
      })
    },

    pend() {
      const loading = this.loading('正在暂存数据');
      Api.post(`/Financing/dbeblc/pend`, this.model).then((res) => {
        if (res.respCode == "AAAAAA") {
          loading.close();
          this.$notify({
            title: '成功',
            message: '暂存成功',
            type: 'success',
          });
          this.$store.commit('delTagsArry', this.$route.path)
					this.$router.back()
        } else {
          this.$message.info(res.respMsg)
        }
        loading.close();
      })
    },

    async getdbCode(codeType, uil, codeNam) {
      let params = {
        codeType: codeType,
        uil: uil ? uil : "EN"
      };
      let rtnmsg = await Api.post("/manager/dic/listDicInfo", params);
      if (rtnmsg.respCode === SUCCESS) {
        let curList = rtnmsg.data.map(item => ({
          value: item.codeValue,
          label: item.codeName
        }));
        this.dbCodes[codeNam] = curList;
      }
    },

    // 检核
    async check() {
      // 前端检验
      this.$refs['modelForm'].clearValidate()
      this.$refs['modelForm'].validate(async (validStatic) => {
        if (validStatic) {
          const loading = this.loading('正在校验数据');
          this.$store.commit('setLoadingCheck', true);
          if (this.model.recpan.danlst.length == 0) {
            this.$alert('至少输入一条关单信息!', '提示');
          } else {
            const res = await Api.post(`/Financing/dbeblc/checkAll`, this.model);
            if (res.respCode === SUCCESS) {
              loading.close();
              if (!res.fieldErrors || (res.fieldErrors && Object.keys(res.fieldErrors).length == 0)) {
                // 清除之前的校验状态
                this.$refs.modelForm.clearValidate();
                this.$notify({
                  title: "成功",
                  message: "校验成功",
                  type: "success",
                });
              } else {
                const tab = this.showBackendErrors(res.fieldErrors);
                if (tab) {
                  // 判断校验失败的表单不属于当前的tab,则切换tab到对应报错的tab页面
                  if (tab.name !== this.tabVal) {
                    this.tabClick(tab, '1');
                  }
                }
              }
            } else {
              this.$notify({
                title: "错误",
                message: res.respMsg,
                type: "error",
              });
            }
          }
          loading.close();
          this.$store.commit('setLoadingCheck', false)
        } else {
          // 前端校验失败
          this.$notify({
            title: '失败',
            message: '校验失败',
            type: 'error',
          });
          this.showFrontendErrors()
        }
      })
    },

    // 前端校验失败时候,tab和Collapse组件效果处理
    showFrontendErrors() {
      this.$nextTick(() => {
        let fields = this.$refs['modelForm'].fields;
        let parentVC = null;
        for (let i = 0; i < fields.length; i++) {
          let fieldItem = fields[i];
          if (fieldItem.validateState === 'error') {
            parentVC = fieldItem;
            break;
          }
        }
        let firstErrorTab = null;
        let collapsePanel = null;
        // while循环找出哪个tab和Collapse中报出的校验失败
        while (true) {
          const vcName = parentVC.$options.componentName;
          // 没有Tabs的表单
          if (vcName === "ElForm") {
            break;
          }
          if (vcName === "ElTabPane") {
            firstErrorTab = parentVC;
            break;
          }
          if (vcName === "ElCollapseItem") {
            collapsePanel = parentVC;
          }
          parentVC = parentVC.$parent;
        }
        if (firstErrorTab && firstErrorTab.name !== this.tabVal) {
          const tabs = firstErrorTab.$parent;
          tabs.currentName = firstErrorTab.name;
        }
        if (collapsePanel && collapsePanel.collapse.activeNames.indexOf(collapsePanel.name) < 0) {
          collapsePanel.collapse.activeNames.push(collapsePanel.name)
        }
        setTimeout(() => {
          let isError = document.querySelectorAll('.is-error');
          isError[0].scrollIntoView({
            block: 'center',
            behavior: 'smooth'
          })
        }, 0);
      })
    },

    refreshLnelstAndCuslst() {
      Api.post(`/Financing/dbeblc/calDanlst`, this.model).then((res) => {
        if (res.respCode == "AAAAAA") {
          if (res.data != null) {
            this.model.recgrp.rec.inenum = res.data.recgrp.inelst.length;
            this.model.recgrp.rec.cusnum = res.data.recgrp.cuslst.length;
            this.model.recpan.totamt = res.data.recpan.totamt;
            this.model.recpan.bgdamt = res.data.recpan.bgdamt;
            this.model.recgrp.rec.rcvtotamt = res.data.recpan.totamt;
            this.model.recgrp.rec.amt = res.data.recpan.bgdamt;
            this.model.recgrp.inelst = res.data.recgrp.inelst;
            this.model.recgrp.cuslst = res.data.recgrp.cuslst;
          }
        }
      })
    },
  },
};