dictionary.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import { defineStore } from 'pinia';
  2. import { DictionaryStates } from './interface';
  3. import { request } from '../utils/service';
  4. export const urlPrefix = '/api/init/dictionary/';
  5. export const BUTTON_VALUE_TO_COLOR_MAPPING: any = {
  6. 1: 'success',
  7. true: 'success',
  8. 0: 'danger',
  9. false: 'danger',
  10. Search: 'warning', // 查询
  11. Update: 'primary', // 编辑
  12. Create: 'success', // 新增
  13. Retrieve: 'info', // 单例
  14. Delete: 'danger', // 删除
  15. };
  16. export function getButtonSettings(objectSettings: any) {
  17. return objectSettings.map((item: any) => ({
  18. label: item.label,
  19. value: item.value,
  20. color: item.color || BUTTON_VALUE_TO_COLOR_MAPPING[item.value],
  21. }));
  22. }
  23. /**
  24. * 字典管理数据
  25. * @methods getSystemDictionarys 获取系统字典数据
  26. */
  27. export const DictionaryStore = defineStore('Dictionary', {
  28. state: (): DictionaryStates => ({
  29. data: {},
  30. }),
  31. actions: {
  32. async getSystemDictionarys() {
  33. request({
  34. url: '/api/init/dictionary/?dictionary_key=all',
  35. method: 'get',
  36. }).then((ret: { data: [] }) => {
  37. // 转换数据格式并保存到pinia
  38. let dataList = ret.data;
  39. dataList.forEach((item: any) => {
  40. let childrens = item.children;
  41. // console.log(item);
  42. // this.data[item.value] = childrens;
  43. childrens.forEach((children:any, index:any) => {
  44. switch (children.type) {
  45. case 1:
  46. children.value = Number(children.value)
  47. break
  48. case 6:
  49. children.value = children.value === 'true'
  50. break
  51. }
  52. })
  53. this.data[item.value]=childrens
  54. });
  55. });
  56. },
  57. },
  58. persist: {
  59. enabled: true,
  60. },
  61. });