当前位置:首页 > VUE

vue 实现审批流程

2026-01-18 18:55:18VUE

实现审批流程的基本思路

审批流程通常涉及多个步骤,如提交申请、部门审批、上级审批等。Vue.js 适合构建此类动态流程,结合状态管理和组件化设计可以实现灵活的审批系统。

使用 Vuex 管理审批状态

审批流程的状态管理是关键。Vuex 提供集中式存储,适合管理多步骤审批的状态。

// store/modules/approval.js
const state = {
  currentStep: 1,
  steps: [
    { id: 1, name: '提交申请', status: 'pending' },
    { id: 2, name: '部门审批', status: 'waiting' },
    { id: 3, name: '上级审批', status: 'waiting' }
  ]
}

const mutations = {
  APPROVE_STEP(state, stepId) {
    state.steps.find(step => step.id === stepId).status = 'approved'
    state.currentStep += 1
  },
  REJECT_STEP(state, stepId) {
    state.steps.find(step => step.id === stepId).status = 'rejected'
  }
}

动态渲染审批步骤组件

根据当前步骤动态渲染不同审批组件,保持界面与状态同步。

<template>
  <div class="approval-flow">
    <div v-for="step in steps" :key="step.id" 
         :class="['step', step.status]">
      {{ step.name }}
    </div>

    <component :is="currentComponent" @approve="handleApprove" @reject="handleReject"/>
  </div>
</template>

<script>
export default {
  computed: {
    steps() {
      return this.$store.state.approval.steps
    },
    currentComponent() {
      const step = this.$store.state.approval.currentStep
      return () => import(`@/components/Step${step}`)
    }
  },
  methods: {
    handleApprove() {
      this.$store.commit('APPROVE_STEP', this.$store.state.approval.currentStep)
    },
    handleReject() {
      this.$store.commit('REJECT_STEP', this.$store.state.approval.currentStep)
    }
  }
}
</script>

审批流程可视化

使用进度条或流程图增强用户体验,清晰展示审批进度。

<template>
  <div class="progress-container">
    <div v-for="(step, index) in steps" :key="step.id" class="step-node">
      <div class="connector" v-if="index > 0" :class="{ active: step.status !== 'waiting' }"></div>
      <div class="node" :class="step.status"></div>
      <div class="step-label">{{ step.name }}</div>
    </div>
  </div>
</template>

<style scoped>
.progress-container {
  display: flex;
  justify-content: space-between;
}
.node {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #ccc;
}
.node.approved {
  background: #4CAF50;
}
.node.rejected {
  background: #F44336;
}
.connector {
  flex-grow: 1;
  height: 2px;
  background: #ccc;
  margin-top: 9px;
}
.connector.active {
  background: #4CAF50;
}
</style>

与后端API集成

审批流程通常需要持久化数据,通过axios与后端API交互。

// api/approval.js
import axios from 'axios'

export default {
  submitApplication(data) {
    return axios.post('/api/approval/submit', data)
  },
  approveStep(stepId) {
    return axios.post(`/api/approval/${stepId}/approve`)
  },
  getApprovalStatus(approvalId) {
    return axios.get(`/api/approval/${approvalId}/status`)
  }
}

权限控制实现

不同审批步骤可能需要不同权限,使用路由守卫控制访问。

// router.js
router.beforeEach((to, from, next) => {
  const requiresApprover = to.matched.some(record => record.meta.requiresApprover)
  const userRole = store.state.user.role

  if (requiresApprover && userRole !== 'approver') {
    next('/forbidden')
  } else {
    next()
  }
})

审批历史记录

记录审批操作历史,方便追踪和审计。

<template>
  <div class="approval-history">
    <div v-for="(log, index) in history" :key="index" class="log-entry">
      <span class="timestamp">{{ log.timestamp }}</span>
      <span class="action">{{ log.action }}</span>
      <span class="actor">{{ log.actor }}</span>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      history: []
    }
  },
  created() {
    this.fetchHistory()
  },
  methods: {
    async fetchHistory() {
      const response = await this.$api.approval.getHistory(this.approvalId)
      this.history = response.data
    }
  }
}
</script>

响应式设计考虑

确保审批流程在不同设备上都能良好显示。

vue 实现审批流程

/* 响应式布局 */
@media (max-width: 768px) {
  .progress-container {
    flex-direction: column;
    align-items: flex-start;
  }
  .step-node {
    display: flex;
    align-items: center;
    margin-bottom: 10px;
  }
  .connector {
    width: 2px;
    height: 20px;
    margin: 0 9px;
  }
}

标签: 流程vue
分享给朋友:

相关文章

vue实现分屏

vue实现分屏

Vue 实现分屏的方法 分屏功能通常用于展示多个独立内容区域,以下是几种常见的实现方式: 使用 CSS Grid 布局 通过 CSS Grid 可以轻松创建分屏布局,在 Vue 组件中直接应用样式:…

vue 实现关闭

vue 实现关闭

Vue 实现关闭功能的方法 在Vue中实现关闭功能通常涉及组件销毁、模态框隐藏或路由跳转等场景。以下是几种常见实现方式: 销毁当前组件 使用v-if或v-show控制组件显示/隐藏,适用于局部组件:…

vue实现增删

vue实现增删

Vue 实现增删功能 在 Vue 中实现增删功能通常涉及数据绑定、事件处理和列表渲染。以下是基于 Vue 2 或 Vue 3 的实现方法: 数据定义 初始化一个数组用于存储列表数据,并在 Vue 实…

vue实现滚屏

vue实现滚屏

实现滚屏的基本方法 在Vue中实现滚屏效果可以通过多种方式完成,常见的有原生JavaScript滚动方法、第三方库或CSS动画。以下是几种常见实现方式: 使用window.scrollTo或Elem…

vue实现treetable

vue实现treetable

Vue实现TreeTable的方法 使用第三方组件库(如Element UI) Element UI的el-table组件支持树形表格展示,通过设置row-key和tree-props属性即可实现。…

vue实现数据

vue实现数据

Vue 实现数据绑定的方法 Vue.js 提供了多种方式实现数据绑定,包括双向绑定、单向绑定以及动态数据绑定。以下是几种常见的实现方式: 双向数据绑定(v-model) 双向数据绑定通常用于表单元素…