index.tsx 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import { BetaSchemaForm, PageContainer, ProFormInstance, useToken } from "@ant-design/pro-components"
  2. import { useAjax } from "@/Hook/useAjax"
  3. import { useModel } from "@umijs/max"
  4. import React, { JSXElementConstructor, Key, ReactElement, ReactNode, ReactPortal, useEffect, useMemo, useRef, useState } from "react"
  5. import { appVipRechargeTemplateInfo, appVipRechargeTemplateList, appVipRechargeTemplateRemove, appVipRechargeTemplateSave, appVipRechargeTemplateSwitch, appVipRechargeTemplateUpdate } from "@/services/miniApp/moduleConfig"
  6. import { Button, Card, Col, Empty, message, Modal, Row, Space } from "antd"
  7. import { DeleteOutlined, EditOutlined, ExclamationCircleFilled, PlusOutlined } from "@ant-design/icons"
  8. import formConfig from "./formConfig"
  9. import { Template } from "./template"
  10. import { createStyles } from "antd-style";
  11. const useStyles = createStyles(({ token }) => {
  12. return {
  13. active: {
  14. position: 'relative',
  15. "&::after": {
  16. content: '""', // 修正了这里的拼写错误
  17. display: "block",
  18. opacity: 1,
  19. border: '15px solid #1890ff',
  20. borderBlockEnd: '15px solid transparent',
  21. borderInlineStart: '15px solid transparent', // 修正了多余的分号
  22. borderStartEndRadius: '6px',
  23. position: 'absolute',
  24. top: 0,
  25. right: 0
  26. }
  27. }
  28. }
  29. })
  30. type DataItem = {
  31. name: string;
  32. state: string;
  33. };
  34. const VipPage: React.FC = () => {
  35. let { token } = useToken()
  36. let { initialState } = useModel("@@initialState")
  37. let [open, setOpen] = useState<any>(null)
  38. let [editValues, setEditValues] = useState<any>({})
  39. let [activeT, setActiveT] = useState<any>()
  40. let { styles } = useStyles()
  41. let getList = useAjax((params) => appVipRechargeTemplateList(params))//获取模板列表
  42. let add = useAjax((params) => appVipRechargeTemplateSave(params))//新增模板
  43. let switchT = useAjax((params) => appVipRechargeTemplateSwitch(params))//切换模板
  44. let editT = useAjax((params) => appVipRechargeTemplateUpdate(params))//编辑模板
  45. let delT = useAjax((params) => appVipRechargeTemplateRemove(params))//删除模板
  46. let infoT = useAjax((id) => appVipRechargeTemplateInfo(id))//模板详情
  47. const formRef = useRef<ProFormInstance>();
  48. let publicData = useMemo(() => {
  49. return {
  50. appId: initialState?.selectApp?.id || "",
  51. appType: initialState?.selectApp?.appType || ""
  52. }
  53. }, [initialState?.selectApp, initialState?.currentUser?.distributorId])
  54. // 获取表单数据
  55. useEffect(() => {
  56. getList.run(publicData).then(res => {
  57. if (res.data) {
  58. let activeObj = res?.data?.find((item: { activateTemplate: any }) => item.activateTemplate)
  59. activeObj && setActiveT(activeObj.id)
  60. }
  61. })
  62. }, [publicData])
  63. // 提交表单
  64. const submit = async (values: any) => {
  65. let api = editValues?.id ? editT : add
  66. if (!values.activateTemplate) {
  67. values.activateTemplate = false
  68. }
  69. if (editValues?.id) {
  70. values.id = editValues?.id
  71. }
  72. values.rechargeConfigs = []
  73. if (values.rechargeConfigList?.length > 0) {//首充
  74. values.rechargeConfigs.push({
  75. firstRecharge: true,//是否首充档位;true:首充 false:非首充
  76. rechargeConfigList: values.rechargeConfigList,
  77. })
  78. }
  79. if (values.rechargeConfigList1?.length > 0) {//非首充
  80. values.rechargeConfigs.push(
  81. {
  82. firstRecharge: false,//是否首充档位;true:首充 false:非首充
  83. rechargeConfigList: values.rechargeConfigList1,
  84. }
  85. )
  86. }
  87. delete values.rechargeConfigList
  88. delete values.rechargeConfigList1
  89. api.run({ ...values, ...publicData }).then(res => {
  90. if (res.code === 200) {
  91. getList.refresh()
  92. message.success(values.id ? "编辑模板成功" : "新建模板成功!")
  93. closeForm(false)
  94. }
  95. })
  96. }
  97. // 关闭表单弹窗和重置表单内容
  98. const closeForm = (b: boolean, values?: any) => {
  99. if (!b) {
  100. setEditValues({})
  101. formRef?.current?.resetFields?.()
  102. setOpen(b)
  103. } else {
  104. setOpen(b)
  105. if (values) {
  106. infoT.run(values.id).then(res => {
  107. if (res.code === 200) {
  108. let data = res.data
  109. for (let item of data.rechargeConfigs) {
  110. if (item.firstRecharge) {
  111. data.rechargeConfigList = item.rechargeConfigList
  112. } else {
  113. data.rechargeConfigList1 = item.rechargeConfigList
  114. }
  115. }
  116. setEditValues(data)
  117. formRef?.current?.setFieldsValue(data)
  118. }
  119. })
  120. }
  121. }
  122. }
  123. // 模板切换
  124. const switchTemplate = (id: any) => {
  125. let params = { ...publicData, id }
  126. Modal.confirm({
  127. title: '切换充值模板',
  128. content: '是否要切换为当前充值模板?',
  129. icon: <ExclamationCircleFilled />,
  130. okText: "切换",
  131. onOk: () => {
  132. switchT.run(params).then(res => {
  133. if (res.code === 200) {
  134. setActiveT(id)
  135. getList.refresh()
  136. message.success("切换成功")
  137. }
  138. })
  139. }
  140. });
  141. }
  142. // 模板删除
  143. const delTemplate = (id: any) => {
  144. Modal.confirm({
  145. title: '删除充值模板',
  146. content: '是否要删除当前充值模板?',
  147. icon: <ExclamationCircleFilled />,
  148. okType: 'danger',
  149. okText: "删除",
  150. onOk: () => {
  151. delT.run(id).then(res => {
  152. if (res.code === 200) {
  153. getList.refresh()
  154. message.success("删除成功")
  155. }
  156. })
  157. }
  158. });
  159. }
  160. return <PageContainer
  161. title={false}
  162. extra={getList?.data?.data?.length > 0 && <Button type="primary" onClick={() => { closeForm(true) }}><PlusOutlined />新增模板</Button>}
  163. >
  164. {/* 模板列表 */}
  165. <Row gutter={[20, 20]}>
  166. {
  167. getList?.data?.data?.map((item: { id: Key | null | undefined; templateName: string | number | boolean | ReactElement<any, string | JSXElementConstructor<any>> | Iterable<ReactNode> | ReactPortal | null | undefined; rechargeConfigs: any[]; templateType: number; templateDescription: string | number | boolean | ReactElement<any, string | JSXElementConstructor<any>> | Iterable<ReactNode> | ReactPortal | null | undefined }) => {
  168. return <Col key={item.id} style={{ cursor: 'pointer' }} onClick={() => { switchTemplate(item.id) }} xs={{span:24}} sm={{span:8}}>
  169. <Card className={activeT === item.id ? styles.active : ""} style={{ background: activeT === item.id ? token.colorPrimaryBgHover : token.colorFillAlter }} hoverable>
  170. <h3 style={{ fontSize: 16, fontWeight: 500, color: token.colorText, fontFamily: 'PingFang SC' }}>{item.templateName}</h3>
  171. {
  172. item.rechargeConfigs?.map((config, index) => {
  173. return <React.Fragment key={index}>
  174. <Template data={config} />
  175. </React.Fragment>
  176. })
  177. }
  178. <p style={{ marginTop: 20, color: token.colorTextSecondary, fontSize: 12 }}>{item.templateDescription}</p>
  179. <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'end' }}>
  180. <Space>
  181. <Button size='small' type='primary' onClick={(e) => { e.stopPropagation(); closeForm(true, item) }}><EditOutlined /></Button>
  182. <Button size='small' type="primary" danger onClick={(e) => { e.stopPropagation(); delTemplate(item.id) }}><DeleteOutlined /></Button>
  183. </Space>
  184. </div>
  185. </Card>
  186. </Col>
  187. })
  188. }
  189. {
  190. getList?.data?.data?.length === 0 && <div style={{ width: "100%", height: "100%", marginTop: "10%" }}><Empty description={<Space direction='vertical'><div>暂无模板数据</div><Button type="primary" onClick={() => { closeForm(true) }}><PlusOutlined />新增模板</Button></Space>} /></div>
  191. }
  192. </Row>
  193. {/* 模板弹窗 */}
  194. <BetaSchemaForm<DataItem>
  195. title={editValues?.id ? "编辑模板" : '新建模板'}
  196. formRef={formRef}
  197. open={open}
  198. onOpenChange={(b) => { !b && closeForm(b) }}
  199. layoutType={"ModalForm"}
  200. rowProps={{
  201. gutter: [16, 16],
  202. }}
  203. colProps={{
  204. span: 12,
  205. }}
  206. width={1000}
  207. grid={true}
  208. onFinish={submit}
  209. columns={formConfig()}
  210. loading={add?.loading}
  211. />
  212. </PageContainer>
  213. }
  214. export default VipPage