ChannelSelectWithToken.tsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import { useState } from "react";
  2. import { Button, Input, Space, Tooltip, Typography } from "antd";
  3. import {
  4. FolderOpenOutlined,
  5. CheckCircleTwoTone,
  6. LoadingOutlined,
  7. WarningTwoTone,
  8. } from "@ant-design/icons";
  9. import type { TChannelType } from "../../api/Channel";
  10. import { post } from "../../request";
  11. import ChannelTableModal from "./ChannelTableModal";
  12. import type { IChannel } from "./Channel";
  13. import type {
  14. IPayload,
  15. ITokenCreate,
  16. ITokenCreateResponse,
  17. TPower,
  18. } from "../../api/token";
  19. const { Text } = Typography;
  20. interface IData {
  21. value: string;
  22. label: string;
  23. }
  24. interface IWidget {
  25. channelsId?: string[];
  26. type?: TChannelType;
  27. book?: number;
  28. para?: number;
  29. power?: TPower;
  30. onChange?: (channel?: string | null) => void;
  31. }
  32. const ChannelSelectWithToken = ({
  33. type,
  34. book,
  35. para,
  36. power,
  37. onChange,
  38. }: IWidget) => {
  39. const [curr, setCurr] = useState<IData>();
  40. const [access, setAccess] = useState<boolean>();
  41. const [loading, setLoading] = useState(false);
  42. return (
  43. <Space>
  44. <Input
  45. allowClear
  46. value={curr?.label}
  47. placeholder="选择一个版本"
  48. onChange={(event) => {
  49. if (event.target.value.trim().length === 0) {
  50. setCurr(undefined);
  51. setAccess(undefined);
  52. if (onChange) {
  53. onChange(undefined);
  54. }
  55. }
  56. }}
  57. />
  58. <ChannelTableModal
  59. chapter={book && para ? { book: book, paragraph: para } : undefined}
  60. channelType={type}
  61. trigger={<Button icon={<FolderOpenOutlined />} type="text" />}
  62. onSelect={(channel: IChannel) => {
  63. setCurr({ value: channel.id, label: channel.name });
  64. //验证权限
  65. if (power) {
  66. setLoading(true);
  67. const payload: IPayload[] = [];
  68. payload.push({
  69. res_id: channel.id,
  70. res_type: "channel",
  71. power: power,
  72. });
  73. const url = "/v2/access-token";
  74. const values = { payload: payload };
  75. console.info("token api request", url, values);
  76. post<ITokenCreate, ITokenCreateResponse>(url, values)
  77. .then((json) => {
  78. console.info("token api response", json);
  79. if (json.ok) {
  80. if (json.data.count > 0) {
  81. setAccess(true);
  82. }
  83. }
  84. })
  85. .finally(() => setLoading(false));
  86. }
  87. if (onChange) {
  88. onChange(channel.id + (power ? "@" + power : ""));
  89. }
  90. }}
  91. />
  92. <Text type="secondary">{power}</Text>
  93. {loading ? (
  94. <LoadingOutlined />
  95. ) : typeof access !== "undefined" ? (
  96. access ? (
  97. <CheckCircleTwoTone twoToneColor="#52c41a" />
  98. ) : (
  99. <Tooltip title="无法获取指定的权限">
  100. <WarningTwoTone twoToneColor="#eb2f96" />
  101. </Tooltip>
  102. )
  103. ) : (
  104. <></>
  105. )}
  106. </Space>
  107. );
  108. };
  109. export default ChannelSelectWithToken;