store.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import {defineStore} from 'pinia'
  2. /**
  3. * 表结构信息
  4. */
  5. export interface Structure {
  6. field: string
  7. label: string
  8. form_component: string
  9. list: boolean
  10. form: boolean
  11. search: boolean
  12. search_op: string
  13. validates: string[]
  14. }
  15. /**
  16. * CodeGen
  17. */
  18. export interface CodeGen {
  19. module: string
  20. controller: string
  21. model: string
  22. paginate: true
  23. schema: string
  24. }
  25. /**
  26. * generate
  27. */
  28. interface generate {
  29. schemaId: number
  30. structures: Structure[]
  31. codeGen: CodeGen
  32. }
  33. /**
  34. * useGenerateStore
  35. */
  36. export const useGenerateStore = defineStore('generateStore', {
  37. state(): generate {
  38. return {
  39. // schema id
  40. schemaId: 0,
  41. // structures
  42. structures: [] as Structure[],
  43. // codeGen
  44. codeGen: Object.assign({
  45. module: '',
  46. controller: '',
  47. model: '',
  48. paginate: true,
  49. schema: '',
  50. }),
  51. }
  52. },
  53. // store getters
  54. getters: {
  55. getSchemaId(): any {
  56. return this.schemaId
  57. },
  58. getStructures(): Structure[] {
  59. return this.structures
  60. },
  61. getCodeGen(): CodeGen {
  62. return this.codeGen
  63. },
  64. },
  65. // store actions
  66. actions: {
  67. // set schema
  68. setSchemaId(schemaId: any): void {
  69. this.schemaId = schemaId
  70. },
  71. // reset
  72. resetStructures(): void {
  73. this.structures = []
  74. },
  75. // filter structures
  76. filterStructures(field: string) {
  77. this.structures = this.structures.filter((s: Structure) => {
  78. return !(s.field === field)
  79. })
  80. },
  81. // init structure
  82. initStructures(fields: Array<any>): void {
  83. const unSupportFields = ['deleted_at', 'creator_id']
  84. fields.forEach(field => {
  85. if (!unSupportFields.includes(field.name)) {
  86. this.structures.push(
  87. Object.assign({
  88. field: field.name,
  89. label: '',
  90. form_component: 'input',
  91. list: true,
  92. form: true,
  93. search: false,
  94. search_op: '',
  95. validates: [],
  96. }),
  97. )
  98. }
  99. })
  100. },
  101. },
  102. })