approvalOpinion.js 2.87 KB
import { getBpmSaveOpinionByTeam } from '@/api/flow'
import { mapState } from 'vuex'
const ALIAS_MAP = {
  agree: '同意',
  agreeTrans: '同意',
  oppose: '反对',
  opposeTrans: '反对',
}
import { getValueByPath, setValueByPath } from '@/utils/value.js'
export default {
  data() {
    return {
      oldOpinion: {},
    }
  },
  computed: {
    ...mapState('login', ['currentUser']),
    userName() {
      return this.currentUser.username
    },
    allButtonMap() {
      return this.approvalButtonList?.reduce((pre, cur) => {
        pre[cur.alias] = cur.name
        return pre
      }, {}) || {}
    },
    nodeProps() {
      const { nodeProperties, localProperties } = this.curNodeDef
      if (nodeProperties && nodeProperties.length) {
        return nodeProperties[0] || localProperties || {}
      }
      return {}
    },
    opinionField() {
      return this.nodeProps.opinionField || ''
    },
    appendOpinion() {
      return this.nodeProps.appendOpinion || false
    }
  },
  methods: {
    //通过实例id和任务id获取审批意见
    getApprovalOpinionByInstIdAndTaskId() {
      getBpmSaveOpinionByTeam(this.instId, this.taskId, ({ state, value }) => {
        const opinion = state && value ? value : ALIAS_MAP[this.alias]
        this.setApprovalOpinion(opinion)
      })
    },
    //设置审批意见
    setApprovalOpinion(opinion) {
      if (opinion) {
        this.form.opinion = opinion
      }
    },
    // 审批意见前缀 添加节点名称和当前操作名称
    getOpinionPre() {
      return `${this.bpmTask.name}:${this.allButtonMap[this.currentAlias]}  `
    },
    //审批意见的后缀
    getOpinionTail() {
      return `  审核人:${this.userName}  审核日期:${new Date().format(
        'yyyy-MM-dd HH:mm:ss'
      )}`
    },
    //设置审批意见回填
    setOpinionWrap() {
      if (this.opinionField && this.approvalDialogData.opinion) {
        let opinion4Fill = null
        //是否覆盖审批意见
        if (this.appendOpinion) {
          opinion4Fill = `${this.getOpinionPre()}审核意见:${this.approvalDialogData.opinion
            }${this.getOpinionTail()}\n`
        } else {
          const oldVal = getValueByPath(this.data, this.opinionField)
          this.oldOpinion[this.opinionField] = oldVal
          let preVal = ''
          if (oldVal && oldVal.trim() != '<p><br/></p>') {
            preVal = `${oldVal}\n  `
          }
          if (preVal && preVal.indexOf('\n') == -1) {
            opinion4Fill = `${this.getOpinionPre()}审核意见:${preVal}${this.approvalDialogData.opinion
              }${this.getOpinionTail()}\n`
          } else {
            opinion4Fill = `${preVal} ${this.getOpinionPre()}审核意见:${this.approvalDialogData.opinion
              }${this.getOpinionTail()}\n`
          }
        }
        setValueByPath(this.data, this.opinionField, opinion4Fill)
      }
    },
  },
}