ChannelSelect.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import { ProFormCascader } from "@ant-design/pro-components";
  2. import { message } from "antd";
  3. import { useAppSelector } from "../../hooks";
  4. import { currentUser } from "../../reducers/current-user";
  5. import { get } from "../../request";
  6. import type { IApiResponseChannelList } from "../../api/Channel";
  7. import type { IStudio } from "../auth/Studio";
  8. import { useIntl } from "react-intl";
  9. interface IOption {
  10. value: string;
  11. label?: string;
  12. lang?: string;
  13. children?: IOption[];
  14. }
  15. interface IWidget {
  16. width?: number | "md" | "sm" | "xl" | "xs" | "lg";
  17. channelId?: string;
  18. name?: string;
  19. tooltip?: string;
  20. label?: string;
  21. allowClear?: boolean;
  22. parentChannelId?: string;
  23. parentStudioId?: string;
  24. placeholder?: string;
  25. onSelect?: (selected: string[]) => void;
  26. }
  27. const ChannelSelectWidget = ({
  28. width = "md",
  29. name = "channel",
  30. tooltip,
  31. label,
  32. parentChannelId,
  33. parentStudioId,
  34. placeholder,
  35. allowClear = true,
  36. }: IWidget) => {
  37. const user = useAppSelector(currentUser);
  38. const intl = useIntl();
  39. return (
  40. <ProFormCascader
  41. width={width}
  42. name={name}
  43. tooltip={tooltip}
  44. label={label}
  45. allowClear={allowClear}
  46. placeholder={placeholder}
  47. request={async ({ keyWords }) => {
  48. console.debug("keyWord", keyWords);
  49. const url = `/v2/channel?view=user-edit&key=${keyWords}`;
  50. console.info("ChannelSelect api request", url);
  51. const json = await get<IApiResponseChannelList>(url);
  52. console.debug("ChannelSelect api response", json);
  53. if (json.ok) {
  54. //获取studio list
  55. const studio = new Map<string, string>();
  56. for (const iterator of json.data.rows) {
  57. studio.set(
  58. iterator.studio.id,
  59. iterator.studio.nickName ? iterator.studio.nickName : ""
  60. );
  61. }
  62. let channels: IOption[] = [];
  63. if (user && user.id === parentStudioId) {
  64. if (!user.roles?.includes("basic")) {
  65. channels.push({
  66. value: "",
  67. label: intl.formatMessage({
  68. id: "term.general-in-studio",
  69. }),
  70. });
  71. }
  72. }
  73. if (typeof parentChannelId === "string") {
  74. channels.push({ value: parentChannelId, label: "仅此版本" });
  75. }
  76. if (user) {
  77. //自己的 studio
  78. channels.push({
  79. value: user.id,
  80. label: user.realName,
  81. children: json.data.rows
  82. .filter((value) => value.studio.id === user.id)
  83. .map((item) => {
  84. return { value: item.uid, label: item.name, lang: item.lang };
  85. }),
  86. });
  87. }
  88. const arrStudio: IStudio[] = [];
  89. studio.forEach((value, key) => {
  90. arrStudio.push({ id: key, nickName: value });
  91. });
  92. const others: IOption[] = arrStudio
  93. .sort((a, b) =>
  94. a.nickName && b.nickName ? (a.nickName > b.nickName ? 1 : -1) : 0
  95. )
  96. .filter((value) => value.id !== user?.id)
  97. .map((item) => {
  98. const node = {
  99. value: item.id,
  100. label: item.nickName,
  101. children: json.data.rows
  102. .filter((value) => value.studio.id === item.id)
  103. .map((item) => {
  104. return {
  105. value: item.uid,
  106. label: item.name,
  107. lang: item.lang,
  108. };
  109. }),
  110. };
  111. return node;
  112. });
  113. channels = [...channels, ...others];
  114. console.debug("ChannelSelect json", channels);
  115. return channels;
  116. } else {
  117. message.error(json.message);
  118. return [];
  119. }
  120. }}
  121. fieldProps={{}}
  122. />
  123. );
  124. };
  125. export default ChannelSelectWidget;