index.tsx 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import { ActionType, BetaSchemaForm, PageContainer, ProFormInstance, ProTable } from "@ant-design/pro-components"
  2. import { columns } from "./tableConfig"
  3. import { useAjax } from "@/Hook/useAjax"
  4. import { useModel } from "@umijs/max"
  5. import { longBookInfoList, longBookInfoChapterContent, longBookInfoBookConfig, longBookInfoConfig, longBookInfoChapterAllList } from "@/services/miniApp/bookManage"
  6. import { useEffect, useMemo, useRef, useState } from "react"
  7. import { Drawer, message } from "antd"
  8. import ReadText from "../components/readText"
  9. import formConfig from "./formConfig"
  10. import ReadBook from "../components/readBook"
  11. const wordCountRanges: any = {
  12. "": { start: null, end: null }, // 全部
  13. "0-2": { start: 0, end: 2 * 10000 },
  14. "2-5": { start: 2 * 10000, end: 5 * 10000 },
  15. "5-10": { start: 5 * 10000, end: 10 * 10000 },
  16. "10-20": { start: 10 * 10000, end: 20 * 10000 },
  17. "20-40": { start: 20 * 10000, end: 40 * 10000 },
  18. "40-100": { start: 40 * 10000, end: 100 * 10000 },
  19. "100-150": { start: 100 * 10000, end: 150 * 10000 },
  20. "150-200": { start: 150 * 10000, end: 200 * 10000 },
  21. "200-300": { start: 200 * 10000, end: 300 * 10000 },
  22. "300-0": { start: 300 * 10000, end: null }, // 300万以上
  23. };
  24. type DataItem = {
  25. name: string;
  26. state: string;
  27. };
  28. const Page: React.FC = () => {
  29. let { initialState } = useModel("@@initialState")
  30. let { state, getLabelAndClassList } = useModel('global')
  31. let [open, setOpen] = useState<any>(null)//付费配置
  32. let [editValues, setEditValues] = useState<any>({})
  33. let paymentType = useState([])
  34. let [workDirection, setWorkDirection] = useState<any>(null)
  35. const [openBook, setOpneBook] = useState<any>(null)//阅读小说
  36. let getList = useAjax((params) => longBookInfoList(params), { type: 'table' })//获取书列表
  37. let getChapterContent = useAjax((params) => longBookInfoChapterContent(params))//获取章节内容信息
  38. let getChapterAllList = useAjax((params) => longBookInfoChapterAllList(params))//获取全部章节
  39. let add = useAjax((params) => longBookInfoBookConfig(params))//新增配置
  40. let configInfo = useAjax((params) => longBookInfoConfig(params))//获取配置信息
  41. const formRef = useRef<ProFormInstance>();
  42. const actionRef = useRef<ActionType>();
  43. // 获取标签和分类
  44. useEffect(() => {
  45. getLabelAndClassList({ workDirection })
  46. }, [workDirection])
  47. // 接口公共参数
  48. let publicData = useMemo(() => {
  49. return {
  50. miniappId: initialState?.selectApp?.id || "",
  51. distributorId: initialState?.currentUser?.distributorId,
  52. appType: initialState?.selectApp?.appType || ""
  53. }
  54. }, [initialState?.selectApp, initialState?.currentUser?.distributorId])
  55. // 看小说列表
  56. const lookBookList = (params: any) => {
  57. let { id, pageNum, pageSize } = params
  58. return getChapterAllList.run({ bookId: id, ...publicData }).then(res => {
  59. setOpneBook({ list: res.data, ...params })
  60. })
  61. }
  62. // 看小说
  63. const lookBook = (id: any) => {
  64. return getChapterContent.run(id).then(res => {
  65. return res.data
  66. })
  67. }
  68. // 提交表单
  69. const submit = async (values: any) => {
  70. if (editValues?.id) {
  71. values.id = editValues?.id
  72. }
  73. if (editValues?.bookId) {
  74. values.bookId = editValues?.bookId
  75. }
  76. add.run({ ...values, ...publicData }).then(res => {
  77. if (res.code === 200) {
  78. actionRef?.current?.reload()
  79. message.success("付费配置成功!")
  80. closeForm(false)
  81. }
  82. })
  83. }
  84. // 关闭表单弹窗和重置表单内容
  85. const closeForm = (b: boolean, values?: any) => {
  86. if (!b) {
  87. setEditValues({})
  88. paymentType[1]([])
  89. formRef?.current?.resetFields?.()
  90. setOpen(b)
  91. } else {
  92. // 获取书全部章节
  93. getChapterAllList.run({ bookId: values.bookId, ...publicData }).then(res => {
  94. if (res.code === 200) {
  95. setOpen(b)//弹窗开启
  96. if (values) {
  97. // 获取书付费配置
  98. configInfo.run({ bookId: values.bookId, ...publicData }).then(res => {
  99. if (res.code === 200) {
  100. let paymentTypeArr = res.data?.paymentType?.map((i: string) => i + '')
  101. let data = { ...res.data, paymentType: paymentTypeArr }
  102. setEditValues(data)
  103. paymentType[1](paymentTypeArr)
  104. formRef?.current?.setFieldsValue(data)
  105. }
  106. })
  107. }
  108. }
  109. })
  110. }
  111. }
  112. return <PageContainer title={false}
  113. tabProps={{ type: 'card' }}
  114. >
  115. <ProTable<any, any>
  116. params={publicData}
  117. actionRef={actionRef}
  118. headerTitle={"长篇篇小说列表"}
  119. rowKey={(r) => r.bookId}
  120. search={{
  121. labelWidth: 90,
  122. searchGutter: [10, 15],
  123. }}
  124. loading={getChapterAllList?.loading || configInfo?.loading || getList.loading}
  125. // ghost={true}//去除表格的背景一些配置改变ui
  126. beforeSearchSubmit={(params) => {//处理搜索数据
  127. let newParams = Object.entries(params).reduce((acc: any, [key, value]) => {
  128. // 过滤掉空值,包括空字符串和 null
  129. if (value !== '' && value != null) {
  130. acc[key] = value;
  131. }
  132. if (key === 'wordCount' && value) {
  133. let obj = wordCountRanges[value]
  134. acc['startWordCount'] = obj.start
  135. acc['endWordCount'] = obj.end
  136. delete acc[key]
  137. }
  138. return acc;
  139. }, {});
  140. return newParams
  141. }}
  142. request={async (params) => {
  143. return await getList.run(params)
  144. }}
  145. columns={columns({ authList: state?.authList, labelList: state.labelList, categoryList: state.categoryList, enumList: state?.enumList, lookBook:lookBookList, closeForm, setWorkDirection })}
  146. />
  147. {/* 付费配置 */}
  148. <BetaSchemaForm<DataItem>
  149. title={"付费配置"}
  150. formRef={formRef}
  151. open={open}
  152. onOpenChange={(b) => { !b && closeForm(b) }}
  153. layoutType={"ModalForm"}
  154. labelCol={{ span: 6 }}
  155. wrapperCol={{ span: 14 }}
  156. // grid={true}
  157. layout='horizontal'
  158. onFinish={submit}
  159. columns={formConfig({ enumList: state?.enumList, paymentType, paragraphList: getChapterAllList?.data?.data?.map((item: { chapterInfo: any }) => item.chapterInfo) })}
  160. loading={add?.loading}
  161. />
  162. {/* 阅读小说 */}
  163. <Drawer
  164. open={!!openBook}
  165. placement="right"
  166. onClose={() => { setOpneBook(null) }}
  167. footer={null}
  168. width={'65%'}
  169. destroyOnClose={true}
  170. // getContainer={false}
  171. styles={{ body: { padding: 0 } }}
  172. closeIcon={false}
  173. title={<div style={{ fontSize: 20 }}>{openBook?.bookName ? openBook?.bookName : ""}</div>}
  174. >
  175. <ReadBook listData={openBook} next={lookBook} />
  176. </Drawer>
  177. </PageContainer>
  178. }
  179. export default Page