compConfig.tsx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import { BetaSchemaForm, ProFormInstance } from "@ant-design/pro-components";
  2. import { useEffect, useMemo, useRef } from "react";
  3. import { bannersConfig, hotBooksConfig, hotCategoryConfig } from "./compFormConfig";
  4. import { useModel } from "@umijs/max";
  5. import { Button, Popconfirm } from "antd";
  6. import { useAjax } from "@/Hook/useAjax";
  7. import { queryCategoryList } from "@/services/global";
  8. import { Config } from "@/models/appPageConifg";
  9. type Props = {
  10. pageList?: any[]
  11. compName: "banners" | "hot_books" | "hot_category",
  12. }
  13. export function CompConfig(props: Props) {
  14. let { pageList = [], compName } = props
  15. const formRef = useRef<ProFormInstance>();
  16. let { state, dispatch } = useModel('appPageConifg')
  17. let QueryCategoryList = useAjax((params) => queryCategoryList(params))
  18. // 获取分类列表
  19. useEffect(() => {
  20. if (compName === 'hot_category') {
  21. QueryCategoryList.run({ workDirection: state.tabs })
  22. }
  23. }, [state.tabs, compName])
  24. // 获取对应from设置
  25. let columns = useMemo(() => {
  26. switch (compName) {
  27. case 'banners':
  28. return bannersConfig(pageList);
  29. case "hot_books":
  30. return hotBooksConfig();
  31. case "hot_category":
  32. return hotCategoryConfig(QueryCategoryList?.data?.data,formRef);
  33. }
  34. }, [compName, pageList, QueryCategoryList?.data?.data,formRef])
  35. // 获取当前页面配置表
  36. const list = useMemo(() => {
  37. let pageConfig = state.pageConfigList.find(page => page.pageUrl === state.activePage)
  38. let list: {
  39. appComponentId: number | string;
  40. componentType: string;
  41. showRightButton?: boolean;
  42. componentName?: string;
  43. remark?: string;
  44. configs?: Config[];
  45. }[] = []
  46. if (state.isWorkDirection) {
  47. let thePage = pageConfig?.workDirectionList?.find(page => page.workDirection == state.tabs)
  48. list = thePage?.componentConfigList || []
  49. } else {
  50. list = pageConfig?.workDirectionList?.[0].componentConfigList || []
  51. }
  52. return list
  53. }, [state])
  54. // 提交表单
  55. const submit = async (values: any) => {
  56. }
  57. // 初始化内容
  58. useEffect(() => {
  59. let thatConfig = list.find(item => item.componentType === compName)
  60. console.log("初始化内容",thatConfig)
  61. formRef.current?.setFieldsValue(thatConfig)
  62. }, [list])
  63. // 值变化
  64. const handelValues = (values: any, isDel?: boolean) => {
  65. // 处理分类 书籍 需要的完整数据
  66. if(values){
  67. for (let config of values?.configs) {
  68. if (config?.bookId && typeof config.bookId !== "number") {
  69. config.bookInfo = config.bookId
  70. config.bookId = config.bookInfo.id
  71. }
  72. if (config?.categoryId) {
  73. config.categoryInfo = QueryCategoryList?.data?.data?.find((item: { id: any; })=>item.id == config.categoryId)
  74. config.categoryId = config.categoryInfo.id
  75. }
  76. if (config?.bookIds) {
  77. config.bookInfos = config.bookIds
  78. config.bookIds = config.bookInfos.map((item: { id: any; }) => item.id)
  79. }
  80. }
  81. }
  82. // 找到当前的页面
  83. let pageConfig = state.pageConfigList.find(page => page.pageUrl === state.activePage)
  84. //找到当前选中的组件
  85. let thatConfig = list.find(item => item.componentType === compName)
  86. // 修改组件的内容
  87. if (thatConfig) {
  88. thatConfig.configs = values ? values.configs : []
  89. if(values?.componentName){
  90. thatConfig.componentName = values?.componentName
  91. thatConfig.showRightButton = values?.showRightButton
  92. }
  93. }
  94. // 判断当前页面是否存在男女频,获取对应的页面配置列表
  95. if (state.isWorkDirection) {
  96. let thePage = pageConfig?.workDirectionList?.find(page => page.workDirection == state.tabs)
  97. if (thePage) {
  98. // 删除的判断
  99. if (isDel) {
  100. thePage.componentConfigList = list?.filter(item => item.componentType !== compName)
  101. } else {
  102. thePage.componentConfigList = list
  103. }
  104. }
  105. } else {
  106. if (pageConfig) {
  107. if (isDel) {
  108. pageConfig.workDirectionList[0].componentConfigList = list?.filter(item => item.componentType !== compName)
  109. } else {
  110. pageConfig.workDirectionList[0].componentConfigList = list
  111. }
  112. }
  113. pageConfig && (pageConfig.workDirectionList[0].componentConfigList = list)
  114. }
  115. // 修改保存数据到状态管理
  116. dispatch({
  117. type: 'setAll', params: {
  118. pageConfigList: state.pageConfigList,
  119. index: state.index + 1,
  120. compAc: isDel ? "" : state.compAc
  121. }
  122. })
  123. }
  124. return <BetaSchemaForm<any>
  125. formRef={formRef}
  126. layoutType={"Form"}
  127. size="small"
  128. colProps={{
  129. span: 12,
  130. }}
  131. grid={true}
  132. submitter={{
  133. render: (props, doms) => {
  134. return [<Popconfirm key={compName + "del"} title="确定要删除此组件?" onConfirm={() => {
  135. handelValues(null, true)
  136. }}><Button danger>删除</Button></Popconfirm>, ...doms];
  137. },
  138. }}
  139. onFinish={submit}
  140. onReset={() => {
  141. handelValues(null)
  142. }}
  143. columns={columns}
  144. onValuesChange={(changedValues: any, values: any) => {
  145. handelValues(values)
  146. }}
  147. />
  148. }