Newer
Older
smart-metering-front / src / components / workFlow / nodeWrap.vue
<script setup lang="ts" name="NodeWrap">
import { computed, getCurrentInstance, onMounted, ref, watch } from 'vue'
import $func from '@/plugins/preload'
import useWorkFlowStore from '@/store/modules/workFlow'
import useUserStore from '@/store/modules/user'
const props = defineProps({
  nodeConfig: {
    type: Object,
    default: () => ({}),
  },
  flowPermission: {
    type: Object,
    default: () => {},
  },
})

const emits = defineEmits(['update:flowPermission', 'update:nodeConfig'])

const userStore = useUserStore()

const workFlowStore = useWorkFlowStore() // store
const proxy = getCurrentInstance() as any
const _uid = proxy.uid
const bgColors = ['87, 106, 149', '255, 148, 62', '50, 150, 250'] // 节点颜色
const placeholderList = ['发起人', '审核人', '抄送人'] // 节点默认名字
const defaultText = computed(() => { // 默认节点名字
  return placeholderList[props.nodeConfig.type]
})

const showText = computed(() => {
  // 设置发起人节点的showText显示文字
  if (props.nodeConfig.type == 0) {
    // return $func.arrToStr(props.flowPermission) || userStore.name
    // return props.flowPermission.name || userStore.name
    return '所有人'
  }
  // 设置审批节点的showText显示文字
  if (props.nodeConfig.type == 1) {
    return $func.setApproverStr(props.nodeConfig)
  }
  // 设置审批节点的showText显示文字
  return $func.copyerStr(props.nodeConfig)
})

// 监听到节点配置的变化,重新设置节点文字内容
watch(() => props.nodeConfig, () => {
  if (props.nodeConfig.type == 1) { // 审批
    return $func.setApproverStr(props.nodeConfig)
  }
  return $func.copyerStr(props.nodeConfig) // 抄送
}, {
  deep: true, // 深度监听的参数
})

// 条件控制节点显示输入框的字段{0: true}
const isInputList = ref<{ [key: number]: boolean }>([])
// 审批和抄送控制节点名称显示输入框的字段
const isInput = ref(false)

// 条件设置提醒(红色感叹号)
const resetConditionNodesErr = () => {
  for (let i = 0; i < props.nodeConfig.conditionNodes.length; i++) {
    props.nodeConfig.conditionNodes[i].error = $func.conditionStr(props.nodeConfig, i) == '请设置条件' && i != props.nodeConfig.conditionNodes.length - 1
  }
}

onMounted(() => {
  // 设置审核节点的error(控制红色感叹号,true表示节点为空)
  if (props.nodeConfig.type == 1) {
    props.nodeConfig.error = !$func.setApproverStr(props.nodeConfig)
  }
  // 抄送
  else if (props.nodeConfig.type == 2) {
    props.nodeConfig.error = !$func.copyerStr(props.nodeConfig)
  }
  // 条件
  else if (props.nodeConfig.type == 4) {
    resetConditionNodesErr()
  }
})

// 审批(监听到审批设置的改变,更新父组件节点)
watch(() => workFlowStore.approverConfigValue, (val: { flag: boolean; id: string; value: any }) => {
  if (val.flag && val.id === _uid) {
    emits('update:nodeConfig', val.value)
  }
})
// 发起人
watch(() => workFlowStore.flowPermissionValue, (val: { flag: boolean; id: string; value: any }) => {
  if (val.flag && val.id === _uid) {
    emits('update:flowPermission', val.value)
  }
})
// 条件
watch(() => workFlowStore.conditionsConfigValue, (val: { flag: boolean; id: string; value: any }) => {
  console.log('val.flag', val.flag, val.id, _uid)
  if (val.flag && val.id === _uid) {
    emits('update:nodeConfig', val.value)
  }
})
// 抄送
// watch(() => workFlowStore.copyerConfigValue, (val: { flag: boolean; id: string; value: any }) => {
//   if (val.flag && val.id === _uid) {
//     emits('update:nodeConfig', val.value)
//   }
// })

const {
  setApprover,
  setCopyer,
  setCondition,
  setApproverConfig,
  setCopyerConfig,
  setConditionsConfig,
} = workFlowStore

// 点击节点名称--显示输入框(条件会有index)
const clickEvent = (index: number | string | null) => {
  if (index || index === 0) { // 条件
    isInputList.value[index as number] = true
  }
  else { // 审批、抄送
    isInput.value = true
  }
}
// 节点名称失去焦点--保存名称
const blurEvent = (index: number | string | null) => {
  if (index || index === 0) { // 条件
    isInputList.value[index as number] = false
    props.nodeConfig.conditionNodes[index].nodeName = props.nodeConfig.conditionNodes[index].nodeName || '条件'
  }
  else { // 审批、抄送
    isInput.value = false
    props.nodeConfig.nodeName = props.nodeConfig.nodeName || defaultText
  }
}

// 删除节点--(审批、抄送)
const delNode = () => {
  emits('update:nodeConfig', props.nodeConfig.childNode) // 更新节点配置
}

// 点击添加条件
const addTerm = () => {
  const len = props.nodeConfig.conditionNodes.length + 1
  props.nodeConfig.conditionNodes.push({
    nodeName: `条件${len}`,
    type: 3,
    priorityLevel: len,
    conditionList: [],
    nodeUserList: [],
    childNode: null,
  })
  resetConditionNodesErr() // 检查空节点
  emits('update:nodeConfig', props.nodeConfig) // 更新节点配置
}

const reData = (data: any, addData: any) => {
  if (!data.childNode) {
    data.childNode = addData
  }
  else {
    reData(data.childNode, addData)
  }
}
// 删除条件设置
const delTerm = (index: number) => {
  props.nodeConfig.conditionNodes.splice(index, 1)
  props.nodeConfig.conditionNodes.map((item: any, index: number) => {
    item.priorityLevel = index + 1
    item.nodeName = `条件${index + 1}`
  })
  resetConditionNodesErr()
  emits('update:nodeConfig', props.nodeConfig)
  // 删除条件如果只剩一个条件,将剩余条件删除,并删除加号
  if (props.nodeConfig.conditionNodes.length == 1) {
    if (props.nodeConfig.childNode) {
      if (props.nodeConfig.conditionNodes[0].childNode) {
        reData(props.nodeConfig.conditionNodes[0].childNode, props.nodeConfig.childNode)
      }
      else {
        props.nodeConfig.conditionNodes[0].childNode = props.nodeConfig.childNode
      }
    }
    emits('update:nodeConfig', props.nodeConfig.conditionNodes[0].childNode)
  }
}

// 点击节点--节点设置
const setPerson = (priorityLevel: any) => {
  const { type } = props.nodeConfig // 节点类型
  if (type == 1) { // 审批
    // 控制审批抽屉显隐
    setApprover(true)
    // 审批节点数据设置
    setApproverConfig({
      value: {
        ...JSON.parse(JSON.stringify(props.nodeConfig)),
        ...{ settype: props.nodeConfig.settype ? props.nodeConfig.settype : 1 },
      },
      flag: false,
      id: _uid,
    })
  }
  else if (type == 2) { // 抄送
    // 控制抄送抽屉显隐
    setCopyer(true)
    // 抄送节点数据设置
    setCopyerConfig({
      value: JSON.parse(JSON.stringify(props.nodeConfig)),
      flag: false,
      id: _uid,
    })
  }
  else { // 条件
    // 控制条件抽屉显隐
    setCondition(true)
    // 条件节点数据设置
    setConditionsConfig({
      value: JSON.parse(JSON.stringify(props.nodeConfig)),
      priorityLevel,
      flag: false,
      id: _uid,
    })
  }
}
// 条件左右切换
const arrTransfer = (index: number, type = 1) => {
  // 向左-1,向右1
  props.nodeConfig.conditionNodes[index] = props.nodeConfig.conditionNodes.splice(
    index + type,
    1,
    props.nodeConfig.conditionNodes[index],
  )[0]
  props.nodeConfig.conditionNodes.map((item: any, index: number) => {
    item.priorityLevel = index + 1
  })
  resetConditionNodesErr() // 检查条件空节点(红色感叹号)
  emits('update:nodeConfig', props.nodeConfig) // 更新节点内容
}
</script>

<template>
  <!-- 非条件 -->
  <div v-if="nodeConfig.type < 3" class="node-wrap">
    <div class="node-wrap-box" :class="(nodeConfig.type === 0 ? 'start-node ' : '') + (nodeConfig.error ? 'active error' : '')">
      <div class="title" :style="`background: rgb(${bgColors[nodeConfig.type]});`">
        <span v-if="nodeConfig.type === 0">{{ nodeConfig.nodeName }}</span>
        <template v-else>
          <span class="iconfont">{{ nodeConfig.type === 1 ? '' : '' }}</span>
          <input
            v-if="isInput"
            v-model="nodeConfig.nodeName"
            v-focus
            type="text"
            class="ant-input editable-title-input"
            :placeholder="defaultText"
            @blur="blurEvent(null)"
          >
          <span v-else class="editable-title" @click="clickEvent(null)">{{ nodeConfig.nodeName }}</span>
          <i class="anticon anticon-close close" @click="delNode" />
        </template>
      </div>
      <div class="content" @click="setPerson">
        <div class="text">
          <span v-if="!showText" class="placeholder">请选择{{ defaultText }}</span>
          {{ showText }}
        </div>
        <i v-if="props.nodeConfig.type !== 0" class="anticon anticon-right arrow" />
      </div>
      <div v-if="nodeConfig.error" class="error_tip">
        <i class="anticon anticon-exclamation-circle" />
      </div>
    </div>
    <add-node v-model:childNodeP="nodeConfig.childNode" />
  </div>
  <!-- 条件 -->
  <div v-if="nodeConfig.type === 4" class="branch-wrap">
    <div class="branch-box-wrap">
      <div class="branch-box">
        <button class="add-branch" @click="addTerm">
          添加条件
        </button>
        <div v-for="(item, index) in nodeConfig.conditionNodes" :key="index" class="col-box">
          <div class="condition-node">
            <div class="condition-node-box">
              <div class="auto-judge" :class="item.error ? 'error active' : ''">
                <div v-if="index !== 0" class="sort-left" @click="arrTransfer(index, -1)">
                  &lt;
                </div>
                <div class="title-wrapper">
                  <input
                    v-if="isInputList[index]"
                    v-model="item.nodeName"
                    v-focus
                    type="text"
                    class="ant-input editable-title-input"
                    @blur="blurEvent(index)"
                  >
                  <span v-else class="editable-title" @click="clickEvent(index)">{{ item.nodeName }}</span>
                  <span class="priority-title" @click="setPerson(item.priorityLevel)">优先级{{ item.priorityLevel }}</span>
                  <i class="anticon anticon-close close" @click="delTerm(index)" />
                </div>
                <div v-if="index !== nodeConfig.conditionNodes.length - 1" class="sort-right" @click="arrTransfer(index)">
                  &gt;
                </div>
                <div class="content" @click="setPerson(item.priorityLevel)">
                  {{ $func.conditionStr(nodeConfig, index) }}
                </div>
                <div v-if="item.error" class="error_tip">
                  <i class="anticon anticon-exclamation-circle" />
                </div>
              </div>
              <add-node v-model:childNodeP="item.childNode" />
            </div>
          </div>
          <node-wrap v-if="item.childNode" v-model:nodeConfig="item.childNode" />
          <template v-if="index === 0">
            <div class="top-left-cover-line" />
            <div class="bottom-left-cover-line" />
          </template>
          <template v-if="index === nodeConfig.conditionNodes.length - 1">
            <div class="top-right-cover-line" />
            <div class="bottom-right-cover-line" />
          </template>
        </div>
      </div>
      <add-node v-model:childNodeP="nodeConfig.childNode" />
    </div>
  </div>
  <node-wrap v-if="nodeConfig.childNode" v-model:nodeConfig="nodeConfig.childNode" />
</template>

<style>
.error_tip {
  position: absolute;
  top: 0;
  right: 0;
  transform: translate(150%, 0);
  font-size: 24px;
}

.promoter_person .el-dialog__body {
  padding: 10px 20px 14px;
}

.selected_list {
  margin-bottom: 20px;
  line-height: 30px;
}

.selected_list span {
  margin-right: 10px;
  padding: 3px 6px 3px 9px;
  line-height: 12px;
  white-space: nowrap;
  border-radius: 2px;
  border: 1px solid rgb(220 220 220 / 100%);
}

.selected_list img {
  margin-left: 5px;
  width: 7px;
  height: 7px;
  cursor: pointer;
}
</style>