zhengnaiwen_citu 4 месяцев назад
Родитель
Сommit
bec8c9c830

+ 93 - 9
src/utils/dict.js

@@ -1,4 +1,4 @@
-
+import { getDictionariesDetails } from '@/api/system'
 // 菜单类型字典
 export const MENU_TYPE = {
   DIRECTORY: 0,
@@ -23,14 +23,14 @@ export const APPROVAL_STATUS = [
 ]
 
 // 员工状态
-export const EMPLOYEE_STATUS = [
-  { text: '在职', color: 'primary', value: 0 },
-  { text: '退休', color: 'info', value: 1 },
-  { text: '离职', color: 'danger', value: 2 },
-  { text: '借调', color: 'warning', value: 3 },
-  { text: '病休', color: 'info', value: 4 },
-  { text: '产假', color: 'info', value: 5 }
-]
+// export const EMPLOYEE_STATUS = [
+//   { text: '在职', color: 'primary', value: 0 },
+//   { text: '退休', color: 'info', value: 1 },
+//   { text: '离职', color: 'danger', value: 2 },
+//   { text: '借调', color: 'warning', value: 3 },
+//   { text: '病休', color: 'info', value: 4 },
+//   { text: '产假', color: 'info', value: 5 }
+// ]
 
 export const BONUS_ALLOCATION_STATUS = {
   0: {
@@ -46,3 +46,87 @@ export const BONUS_ALLOCATION_STATUS = {
     color: 'info'
   }
 }
+
+// const EMPLOYEE_STATUS = {}
+
+// 字典缓存对象
+const dictCache = {}
+
+// 字典配置
+const DICT_CONFIG = {
+  EMPLOYEE_STATUS: {
+    code: 'employeeStatus',
+    data: null
+  }
+}
+
+/**
+ * 获取字典数据
+ * @param {string} dictKey 字典键名
+ * @returns {Promise} 返回字典数据
+ */
+async function getDict (dictKey) {
+  // 如果缓存中有数据,直接返回
+  if (dictCache[dictKey]) {
+    return dictCache[dictKey]
+  }
+
+  // 如果没有配置,抛出错误
+  if (!DICT_CONFIG[dictKey]) {
+    throw new Error(`字典 ${dictKey} 未配置`)
+  }
+
+  // 如果已有数据但未缓存,先缓存
+  if (DICT_CONFIG[dictKey].data) {
+    dictCache[dictKey] = DICT_CONFIG[dictKey].data
+    return dictCache[dictKey]
+  }
+  // 从API获取数据
+  try {
+    const { data } = await getDictionariesDetails({ dictCode: DICT_CONFIG[dictKey].code })
+    const _data = data.map(item => {
+      return {
+        label: item.dictTitle,
+        value: item.dictValue,
+        color: item.webContent?.dictColor
+      }
+    })
+    DICT_CONFIG[dictKey].data = _data
+    dictCache[dictKey] = _data
+    return _data
+  } catch (error) {
+    console.error(`获取字典 ${dictKey} 失败:`, error)
+    throw error
+  }
+}
+
+// 默认导出字典获取方法
+export default {
+  /**
+   * 获取单个字典
+   * @param {string} dictKey 字典键名
+   * @returns {Promise} 返回字典数据
+   */
+  get: getDict,
+
+  /**
+   * 获取多个字典
+   * @param {Array} dictKeys 字典键名数组
+   * @returns {Promise} 返回字典对象 {dictKey1: data, dictKey2: data}
+   */
+  async getMultiple (dictKeys) {
+    const result = {}
+    await Promise.all(dictKeys.map(async key => {
+      result[key] = await getDict(key)
+    }))
+    return result
+  },
+
+  /**
+   * 预加载字典
+   * @param {Array} dictKeys 需要预加载的字典键名数组
+   */
+  preload (dictKeys) {
+    return this.getMultiple(dictKeys)
+  }
+}

+ 12 - 10
src/views/humanResources/roster/index.vue

@@ -15,8 +15,8 @@
       @sort-change="sortChange"
     >
       <template #employeeStatus="{ row }">
-        <el-tag size="small" :type="EMPLOYEE_STATUS.find(e => e.value === row.employeeStatus)?.color">
-          {{ EMPLOYEE_STATUS.find(e => e.value === row.employeeStatus)?.text }}
+        <el-tag size="small" :type="employeeStatus.find(e => +e.value === row.employeeStatus)?.color">
+          {{ employeeStatus.find(e => +e.value === row.employeeStatus)?.label }}
         </el-tag>
       </template>
       <template #card-tools v-if="!$attrs.panorama">
@@ -51,19 +51,15 @@ import {
   exportRoster,
   downloadRosterTemplate
 } from '@/api/system'
-import {
-  dateFormat
-} from '@/utils/date'
-import {
-  EMPLOYEE_STATUS
-} from '@/utils/dict'
+import { dateFormat } from '@/utils/date'
+import DICT from '@/utils/dict'
 import { mapGetters } from 'vuex'
 export default {
   name: 'sys-roster',
   components: { RosterEdit, RosterVersion, RosterHistory },
   data () {
     return {
-      EMPLOYEE_STATUS,
+      employeeStatus: [],
       importLoading: false,
       exportLoading: false,
       downloadLoading: false,
@@ -137,7 +133,13 @@ export default {
       return header
     }
   },
-  created () {
+  async created () {
+    try {
+      const data = await DICT.get('EMPLOYEE_STATUS')
+      this.employeeStatus = data
+    } catch (error) {
+      this.$message.error(error)
+    }
     // 全景视图终止自动调用
     if (this.$attrs.panorama) {
       return

+ 19 - 5
src/views/humanResources/roster/rosterEdit.vue

@@ -18,9 +18,8 @@ import {
   getJobName
 } from '@/api/dictionary'
 
-import {
-  EMPLOYEE_STATUS
-} from '@/utils/dict'
+import DICT from '@/utils/dict'
+
 export default {
   name: 'roster-edit',
   props: {
@@ -34,7 +33,8 @@ export default {
       query: {},
       isEdit: false,
       loading: false,
-      dict: {}
+      dict: {},
+      employeeStatus: []
     }
   },
   computed: {
@@ -103,7 +103,12 @@ export default {
           options: {
             placeholder: '请选择员工状态',
             filterable: true,
-            items: EMPLOYEE_STATUS.map(e => ({ label: e.text, value: e.value }))
+            items: this.employeeStatus.map(e => {
+              return {
+                ...e,
+                value: +e.value
+              }
+            })
           },
           rules: { required: true, message: '请输入人员类别', trigger: 'change' }
         },
@@ -190,6 +195,7 @@ export default {
     async open (item) {
       this.$refs.editDialog.open()
       this.loading = true
+      await this.getEmployeeStatus()
       this.query = this.items.reduce((res, item) => {
         res[item.prop] = null
         return res
@@ -208,6 +214,14 @@ export default {
       }
       this.loading = false
     },
+    async getEmployeeStatus () {
+      try {
+        const data = await DICT.get('EMPLOYEE_STATUS')
+        this.employeeStatus = data
+      } catch (error) {
+        this.$snackbar.error(error)
+      }
+    },
     handleSaveEdit () {
       this.$refs.formRef.validate(async valid => {
         if (!valid) {