dictionary.js 752 B

1234567891011121314151617181920212223242526
  1. import { useDictionaryStore } from '@/pinia/modules/dictionary'
  2. // 获取字典方法 使用示例 getDict('sex').then(res) 或者 async函数下 const res = await getDict('sex')
  3. export const getDict = async(type) => {
  4. const dictionaryStore = useDictionaryStore()
  5. await dictionaryStore.getDictionary(type)
  6. return dictionaryStore.dictionaryMap[type]
  7. }
  8. // 字典文字展示方法
  9. export const showDictLabel = (
  10. dict,
  11. code,
  12. keyCode = 'value',
  13. valueCode = 'label'
  14. ) => {
  15. if (!dict) {
  16. return ''
  17. }
  18. const dictMap = {}
  19. dict.forEach(item => {
  20. if (Reflect.has(item, keyCode) && Reflect.has(item, valueCode)) {
  21. dictMap[item[keyCode]] = item[valueCode]
  22. }
  23. })
  24. return Reflect.has(dictMap, code) ? dictMap[code] : ''
  25. }