dictionaries.behavior.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { login } from "../../lib/logic";
  2. import { Get } from "../../lib/request/method";
  3. interface Dictionary {
  4. dictName: string;
  5. dictType: string;
  6. items: { dictLabel: string; dictValue: string }[]
  7. }
  8. export default Behavior({
  9. data: {
  10. $dictionaries: [] as App.Dictionary[],
  11. },
  12. lifetimes: {
  13. attached() {
  14. login()
  15. .then(() => Get<App.Dictionary[], Dictionary[]>(`/dict/getDicts`, {
  16. shareRequest: true,
  17. transform({ data }) {
  18. return data.map(item => {
  19. return {
  20. key: item.dictType,
  21. name: item.dictName,
  22. options: item.items.map(item => ({
  23. label: item.dictLabel,
  24. value: item.dictValue,
  25. mutex: item.dictLabel === '无' && item.dictValue === '0'
  26. }))
  27. }
  28. })
  29. }
  30. }))
  31. .then(dictionaries => { this.setData({ $dictionaries: dictionaries }); })
  32. }
  33. },
  34. methods: {
  35. getDictionaryOptions(key: string) {
  36. const { options } = this.data.$dictionaries.find(item => item.key === key) ?? { options: [] };
  37. return options
  38. },
  39. getDictionaryLabel(key: string, value: string) {
  40. return this.getDictionaryOptions(key).find(item => item.value === value)?.label ?? ''
  41. }
  42. }
  43. })