TemplateTab.tsx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. import { useRef, useState } from "react";
  2. import { ActionType, ProColumns } from "@ant-design/pro-components";
  3. import { ProTable } from "@ant-design/pro-components";
  4. import { Space, Tag, message, Popconfirm } from "antd";
  5. import AddTemplateDrawer from "./AddTemplateDrawer";
  6. import {
  7. GetTemplateList,
  8. DeleteTemplate,
  9. OnMarketTemplate,
  10. OffMarketTemplate,
  11. } from "@/api/templateStore";
  12. import { history } from "umi";
  13. import { MODULE_TEMPLATE_TYPE } from "@/constants";
  14. export default () => {
  15. const actionRef = useRef<ActionType>();
  16. const [editData, setEditData] = useState<any>();
  17. const handleToDetail = (id?: string) => {
  18. history.push(`/detail/template/${id}`);
  19. };
  20. const handleOffMarket = async (id?: string) => {
  21. if (id) {
  22. await OffMarketTemplate({ ids: [id] });
  23. actionRef.current?.reload();
  24. message.success("下架成功");
  25. }
  26. };
  27. const handleOnMarket = async (id?: string) => {
  28. if (id) {
  29. await OnMarketTemplate({ ids: [id] });
  30. actionRef.current?.reload();
  31. message.success("上架成功");
  32. }
  33. };
  34. const handleDelete = async (id?: string) => {
  35. if (id) {
  36. await DeleteTemplate({ ids: [id] });
  37. actionRef.current?.reload();
  38. message.success("删除成功");
  39. }
  40. };
  41. const columns: ProColumns<any>[] = [
  42. {
  43. title: "图标",
  44. dataIndex: "icon",
  45. search: false,
  46. render: (_, record) => (
  47. <img
  48. src={`/api/File/Download?fileId=${record.icon}`}
  49. alt=""
  50. style={{
  51. width: 30,
  52. height: 30,
  53. borderRadius: "50%",
  54. }}
  55. />
  56. ),
  57. },
  58. {
  59. title: "模版名称",
  60. dataIndex: "name",
  61. copyable: true,
  62. ellipsis: true,
  63. },
  64. {
  65. title: "作者",
  66. dataIndex: "createByName",
  67. ellipsis: true,
  68. search: false,
  69. },
  70. {
  71. title: "模版分类",
  72. dataIndex: "type",
  73. copyable: true,
  74. ellipsis: true,
  75. valueType: "select",
  76. fieldProps: {
  77. options: MODULE_TEMPLATE_TYPE,
  78. },
  79. },
  80. {
  81. disable: true,
  82. title: "标签",
  83. dataIndex: "labels",
  84. search: false,
  85. renderFormItem: (_, { defaultRender }) => {
  86. return defaultRender(_);
  87. },
  88. render: (_, record) => (
  89. <Space>
  90. {record.tags?.split(",").map((tag: string) => (
  91. <Tag color="green" key={tag}>
  92. {tag}
  93. </Tag>
  94. ))}
  95. </Space>
  96. ),
  97. },
  98. {
  99. title: "上架状态",
  100. dataIndex: "isOnMarket",
  101. ellipsis: true,
  102. valueType: "select",
  103. valueEnum: {
  104. on: {
  105. text: "已上架",
  106. status: "Error",
  107. },
  108. off: {
  109. text: "未上架",
  110. status: "Success",
  111. },
  112. },
  113. render: (_, record) => (
  114. <Tag color={record.isOnMarket ? "green" : "red"}>
  115. {record.isOnMarket ? "已上架" : "未上架"}
  116. </Tag>
  117. ),
  118. },
  119. {
  120. title: "使用量",
  121. dataIndex: "useNum",
  122. search: false,
  123. },
  124. {
  125. title: "价格",
  126. dataIndex: "price",
  127. search: false,
  128. renderText: (text) => {
  129. return text ? `¥${text}` : "免费";
  130. },
  131. },
  132. {
  133. title: "更新时间",
  134. dataIndex: "createTime",
  135. search: false,
  136. },
  137. {
  138. title: "更新时间",
  139. dataIndex: "updateTime",
  140. search: false,
  141. },
  142. {
  143. title: "操作",
  144. valueType: "option",
  145. key: "option",
  146. width: 180,
  147. render: (text, record, _, action) => [
  148. <a key="edit" onClick={() => setEditData(record)}>
  149. 编辑
  150. </a>,
  151. <a
  152. target="_blank"
  153. rel="noopener noreferrer"
  154. key="view"
  155. onClick={() => handleToDetail(record.id)}
  156. >
  157. 详情
  158. </a>,
  159. record.isOnMarket ? (
  160. <a
  161. target="_blank"
  162. rel="noopener noreferrer"
  163. key="off"
  164. onClick={() => handleOffMarket(record.id)}
  165. >
  166. 下架
  167. </a>
  168. ) : (
  169. <a
  170. target="_blank"
  171. rel="noopener noreferrer"
  172. key="on"
  173. onClick={() => handleOnMarket(record.id)}
  174. >
  175. 上架
  176. </a>
  177. ),
  178. <Popconfirm
  179. key="delete"
  180. title="确定删除吗?"
  181. onConfirm={() => handleDelete(record.id)}
  182. >
  183. <a className="text-red-500" target="_blank" rel="noopener noreferrer">
  184. 删除
  185. </a>
  186. </Popconfirm>,
  187. ],
  188. },
  189. ];
  190. return (
  191. <ProTable
  192. columns={columns}
  193. actionRef={actionRef}
  194. cardBordered
  195. request={async (params, sort, filter) => {
  196. const isOnMarket =
  197. params?.isOnMarket === "on"
  198. ? 1
  199. : params?.isOnMarket === "off"
  200. ? 0
  201. : undefined;
  202. const res = await GetTemplateList({
  203. currentPage: params.current || 1,
  204. pageSize: params.pageSize || 10,
  205. filters: [
  206. { name: "name", value: params?.name },
  207. { name: "isFree", value: params?.isFree },
  208. { name: "isOnMarket", value: isOnMarket },
  209. { name: "isDel", value: 0 },
  210. { name: "type", value: params?.type },
  211. ],
  212. });
  213. const data = res?.result || {};
  214. return {
  215. success: true,
  216. data: data.model || [],
  217. total: data.totalCount || 0,
  218. };
  219. }}
  220. columnsState={{
  221. persistenceKey: "shalu-marketplace",
  222. persistenceType: "localStorage",
  223. defaultValue: {
  224. option: { fixed: "right", disable: true },
  225. },
  226. }}
  227. rowKey="id"
  228. search={{
  229. labelWidth: "auto",
  230. }}
  231. options={{
  232. setting: {
  233. listsHeight: 400,
  234. },
  235. }}
  236. pagination={{
  237. pageSize: 10,
  238. }}
  239. dateFormatter="string"
  240. headerTitle={
  241. <AddTemplateDrawer
  242. onClose={() => setEditData(undefined)}
  243. onSuccess={() => {
  244. actionRef.current?.reload();
  245. }}
  246. editData={editData}
  247. />
  248. }
  249. />
  250. );
  251. };