Token.tsx 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import { Button, message, Segmented, Typography } from "antd";
  2. import type { SegmentedValue } from "antd/lib/segmented";
  3. import { useEffect, useState } from "react";
  4. import { CopyOutlined } from "@ant-design/icons";
  5. import { useIntl } from "react-intl";
  6. import type { ArticleType } from "./Article";
  7. import { post } from "../../request";
  8. import type {
  9. IPayload,
  10. ITokenCreate,
  11. ITokenCreateResponse,
  12. TPower,
  13. } from "../../api/token";
  14. const { Text } = Typography;
  15. interface IWidget {
  16. channelId?: string;
  17. articleId?: string;
  18. type?: ArticleType;
  19. }
  20. const DictInfoCopyRef = ({ channelId, articleId, type }: IWidget) => {
  21. const [text, setText] = useState("");
  22. const [power, setPower] = useState<TPower>("readonly");
  23. const intl = useIntl();
  24. useEffect(() => {
  25. if (!channelId || !articleId || !type) {
  26. console.error("token", channelId, articleId, type);
  27. return;
  28. }
  29. const id = articleId.split("-");
  30. if (!channelId || !id || id.length < 2) {
  31. console.error("channels or book or para is undefined", channelId, id);
  32. return;
  33. }
  34. const _book = id[0];
  35. const _para = id[1];
  36. const payload: IPayload[] = [];
  37. payload.push({
  38. res_id: channelId,
  39. res_type: "channel",
  40. book: parseInt(_book),
  41. para_start: parseInt(_para),
  42. para_end: parseInt(_para) + 100,
  43. power: power,
  44. });
  45. const url = "/v2/access-token";
  46. const values = { payload: payload };
  47. console.info("token api request", url, values);
  48. post<ITokenCreate, ITokenCreateResponse>(url, values).then((json) => {
  49. console.info("token api response", json);
  50. if (json.ok) {
  51. setText(json.data.rows[0].token);
  52. }
  53. });
  54. }, [articleId, channelId, power, type]);
  55. return (
  56. <div>
  57. <div style={{ textAlign: "center", padding: 20 }}>
  58. <Segmented
  59. options={["readonly", "edit"]}
  60. onChange={(value: SegmentedValue) => {
  61. setPower(value as TPower);
  62. }}
  63. />
  64. </div>
  65. <div>
  66. <Text>{text}</Text>
  67. </div>
  68. <div style={{ textAlign: "center", padding: 20 }}>
  69. <Button
  70. type="primary"
  71. style={{ width: 200 }}
  72. icon={<CopyOutlined />}
  73. onClick={() => {
  74. navigator.clipboard.writeText(text).then(() => {
  75. message.success("链接地址已经拷贝到剪贴板");
  76. });
  77. }}
  78. >
  79. {intl.formatMessage({
  80. id: "buttons.copy",
  81. })}
  82. </Button>
  83. </div>
  84. </div>
  85. );
  86. };
  87. export default DictInfoCopyRef;