@tsingroc/tsingroc-components 5.0.0-alpha.11 → 5.0.0-alpha.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (140) hide show
  1. package/dist/components/Auth.d.ts +310 -0
  2. package/dist/components/Auth.js +267 -0
  3. package/dist/components/AutoResizedECharts.d.ts +21 -0
  4. package/dist/components/AutoResizedECharts.js +98 -0
  5. package/dist/components/Calendar.d.ts +50 -0
  6. package/dist/components/Calendar.js +130 -0
  7. package/dist/components/CircularProgress.d.ts +21 -0
  8. package/dist/components/CircularProgress.js +34 -0
  9. package/dist/components/ConnectedECharts.d.ts +31 -0
  10. package/dist/components/ConnectedECharts.js +100 -0
  11. package/dist/components/ECharts.d.ts +57 -0
  12. package/dist/components/ECharts.js +255 -0
  13. package/dist/components/Header.d.ts +67 -0
  14. package/dist/components/Header.js +171 -0
  15. package/dist/components/ImageBackground.d.ts +32 -0
  16. package/dist/components/ImageBackground.js +76 -0
  17. package/dist/components/IndicatorLight.d.ts +44 -0
  18. package/dist/components/IndicatorLight.js +124 -0
  19. package/dist/components/LeftAlignedECharts.d.ts +42 -0
  20. package/dist/components/LeftAlignedECharts.js +270 -0
  21. package/dist/components/LineChartEditor.d.ts +74 -0
  22. package/dist/components/LineChartEditor.js +458 -0
  23. package/dist/components/LineChartTable.d.ts +38 -0
  24. package/dist/components/LineChartTable.js +245 -0
  25. package/dist/components/LinkedLineChart.d.ts +45 -0
  26. package/dist/components/LinkedLineChart.js +159 -0
  27. package/dist/components/QuickDateRangePicker.d.ts +30 -0
  28. package/dist/components/QuickDateRangePicker.js +58 -0
  29. package/dist/components/SegmentedButtons.d.ts +22 -0
  30. package/dist/components/SegmentedButtons.js +86 -0
  31. package/dist/components/Sidebar.d.ts +79 -0
  32. package/dist/components/Sidebar.js +178 -0
  33. package/dist/components/TsingrocDatePicker.d.ts +38 -0
  34. package/dist/components/TsingrocDatePicker.js +64 -0
  35. package/dist/components/TsingrocTheme.d.ts +15 -0
  36. package/dist/components/TsingrocTheme.js +72 -0
  37. package/dist/components/UserButton.d.ts +42 -0
  38. package/dist/components/UserButton.js +105 -0
  39. package/dist/components/VerticalColorLegend.d.ts +7 -0
  40. package/dist/components/VerticalColorLegend.js +208 -0
  41. package/dist/components/WeatherMap.d.ts +18 -0
  42. package/dist/components/WeatherMap.js +658 -0
  43. package/dist/deckgl/TiandituLayer.d.ts +13 -0
  44. package/dist/deckgl/TiandituLayer.js +44 -0
  45. package/dist/deckgl/WeatherData.d.ts +53 -0
  46. package/dist/deckgl/WeatherData.js +94 -0
  47. package/dist/deckgl/index.d.ts +1 -0
  48. package/dist/deckgl/index.js +1 -0
  49. package/dist/echarts/coordinateSystems/grid.d.ts +43 -0
  50. package/dist/echarts/coordinateSystems/grid.js +107 -0
  51. package/dist/echarts/coordinateSystems/index.js +2 -0
  52. package/dist/echarts/coordinateSystems/polar.d.ts +45 -0
  53. package/dist/echarts/coordinateSystems/polar.js +96 -0
  54. package/dist/echarts/gl-types.d.js +0 -0
  55. package/dist/echarts/gl.d.ts +115 -0
  56. package/dist/echarts/gl.js +47 -0
  57. package/dist/echarts/index.d.ts +46 -0
  58. package/dist/echarts/index.js +46 -0
  59. package/dist/echarts/legend.d.ts +17 -0
  60. package/dist/echarts/legend.js +15 -0
  61. package/dist/echarts/radar.d.ts +24 -0
  62. package/dist/echarts/radar.js +22 -0
  63. package/dist/echarts/series/barSeries.d.ts +23 -0
  64. package/dist/echarts/series/barSeries.js +18 -0
  65. package/dist/echarts/series/boxplotSeries.d.ts +21 -0
  66. package/dist/echarts/series/boxplotSeries.js +40 -0
  67. package/dist/echarts/series/index.js +7 -0
  68. package/dist/echarts/series/intervalSeries.d.ts +32 -0
  69. package/dist/echarts/series/intervalSeries.js +55 -0
  70. package/dist/echarts/series/lineSeries.d.ts +36 -0
  71. package/dist/echarts/series/lineSeries.js +45 -0
  72. package/dist/echarts/series/maxBarSeries.d.ts +18 -0
  73. package/dist/echarts/series/maxBarSeries.js +39 -0
  74. package/dist/echarts/series/pieSeries.d.ts +31 -0
  75. package/dist/echarts/series/pieSeries.js +47 -0
  76. package/dist/echarts/series/windLineSeries.d.ts +47 -0
  77. package/dist/echarts/series/windLineSeries.js +51 -0
  78. package/{src/echarts/tooltip.ts → dist/echarts/tooltip.d.ts} +1 -5
  79. package/dist/echarts/tooltip.js +18 -0
  80. package/dist/env.d.js +0 -0
  81. package/dist/index.d.ts +20 -0
  82. package/dist/index.js +20 -0
  83. package/dist/utils/debug.d.ts +1 -0
  84. package/dist/utils/debug.js +25 -0
  85. package/dist/utils/index.d.ts +4 -0
  86. package/dist/utils/index.js +4 -0
  87. package/dist/utils/math.d.ts +9 -0
  88. package/{src/utils/math.ts → dist/utils/math.js} +2 -2
  89. package/dist/utils/mock.d.ts +8 -0
  90. package/dist/utils/mock.js +40 -0
  91. package/dist/utils/startOfQuarter.d.ts +2 -0
  92. package/dist/utils/startOfQuarter.js +4 -0
  93. package/package.json +26 -14
  94. package/src/components/Auth.tsx +0 -623
  95. package/src/components/AutoResizedECharts.tsx +0 -70
  96. package/src/components/Calendar.tsx +0 -182
  97. package/src/components/CircularProgress.tsx +0 -38
  98. package/src/components/ConnectedECharts.tsx +0 -62
  99. package/src/components/ECharts.tsx +0 -206
  100. package/src/components/Header.tsx +0 -136
  101. package/src/components/ImageBackground.tsx +0 -58
  102. package/src/components/IndicatorLight.tsx +0 -106
  103. package/src/components/LeftAlignedECharts.tsx +0 -190
  104. package/src/components/LineChartEditor.tsx +0 -558
  105. package/src/components/LineChartTable.tsx +0 -286
  106. package/src/components/LinkedECharts.tsx +0 -51
  107. package/src/components/LinkedLineChart.tsx +0 -144
  108. package/src/components/QuickDateRangePicker.tsx +0 -84
  109. package/src/components/SegmentedButtons.tsx +0 -46
  110. package/src/components/Sidebar.tsx +0 -271
  111. package/src/components/StrictECharts.d.ts +0 -47
  112. package/src/components/StrictECharts.js +0 -1
  113. package/src/components/TsingrocDatePicker.tsx +0 -103
  114. package/src/components/TsingrocTheme.tsx +0 -48
  115. package/src/components/UserButton.tsx +0 -165
  116. package/src/components/VerticalColorLegend.tsx +0 -73
  117. package/src/components/WeatherMap.tsx +0 -522
  118. package/src/deckgl/TiandituLayer.ts +0 -56
  119. package/src/deckgl/WeatherData.ts +0 -157
  120. package/src/deckgl/index.ts +0 -4
  121. package/src/echarts/coordinateSystems/grid.ts +0 -143
  122. package/src/echarts/coordinateSystems/polar.ts +0 -148
  123. package/src/echarts/gl.ts +0 -159
  124. package/src/echarts/index.ts +0 -129
  125. package/src/echarts/legend.ts +0 -36
  126. package/src/echarts/radar.ts +0 -46
  127. package/src/echarts/series/barSeries.ts +0 -37
  128. package/src/echarts/series/boxplotSeries.ts +0 -62
  129. package/src/echarts/series/intervalSeries.ts +0 -70
  130. package/src/echarts/series/lineSeries.ts +0 -78
  131. package/src/echarts/series/maxBarSeries.ts +0 -55
  132. package/src/echarts/series/pieSeries.ts +0 -76
  133. package/src/echarts/series/windLineSeries.ts +0 -113
  134. package/src/index.ts +0 -120
  135. package/src/types.d.ts +0 -5
  136. package/src/utils/debug.ts +0 -39
  137. package/src/utils/mock.ts +0 -69
  138. package/src/utils/startOfQuarter.ts +0 -6
  139. /package/{src/echarts/coordinateSystems/index.ts → dist/echarts/coordinateSystems/index.d.ts} +0 -0
  140. /package/{src/echarts/series/index.ts → dist/echarts/series/index.d.ts} +0 -0
@@ -0,0 +1,310 @@
1
+ import Sdk from "casdoor-js-sdk";
2
+ import type { SdkConfig } from "casdoor-js-sdk/lib/esm/sdk";
3
+ import { type JwtPayload } from "jwt-decode";
4
+ import { type ReactNode } from "react";
5
+ export type AccessTokenPayload = CasdoorAccessTokenPayload | LocalAccessTokenPayload;
6
+ interface CasdoorAccessTokenPayload extends JwtPayload {
7
+ /**
8
+ * Token 的发布者。见 [JWT 标准][1]。
9
+ *
10
+ * [1]: https://datatracker.ietf.org/doc/html/rfc7519#section-4.1
11
+ */
12
+ iss: string;
13
+ /**
14
+ * Token 的主题。见 [JWT 标准][1]。
15
+ *
16
+ * [1]: https://datatracker.ietf.org/doc/html/rfc7519#section-4.1
17
+ */
18
+ sub: string;
19
+ /**
20
+ * Token 的目标应用 ID。见 [JWT 标准][1]。
21
+ *
22
+ * [1]: https://datatracker.ietf.org/doc/html/rfc7519#section-4.1
23
+ */
24
+ aud: string[];
25
+ /**
26
+ * 使用 Token 的应用 ID。见 [JWT 标准][1]。
27
+ *
28
+ * [1]: https://datatracker.ietf.org/doc/html/rfc7519#section-4.1
29
+ */
30
+ azp: string;
31
+ /**
32
+ * Token 的唯一标识符。见 [JWT 标准][1]。
33
+ *
34
+ * [1]: https://datatracker.ietf.org/doc/html/rfc7519#section-4.1
35
+ */
36
+ jti: string;
37
+ /** Token 的过期时间(Unix 时间戳)。*/
38
+ exp: number;
39
+ /** Token 的生效时间(Unix 时间戳)。*/
40
+ nbf: number;
41
+ /** Token 的签发时间(Unix 时间戳)。*/
42
+ iat: number;
43
+ /** 用户的登录名。*/
44
+ name: string;
45
+ /** 用户的 UUID。*/
46
+ id: string;
47
+ /** 用户的显示名。*/
48
+ displayName: string;
49
+ /** 用户的头像 URL。*/
50
+ avatar: string;
51
+ /** 用户的邮件地址。*/
52
+ email: string;
53
+ /** 用户的手机号。*/
54
+ phone: string;
55
+ owner: "tsingroc";
56
+ tokenType: "access-token";
57
+ scope: "profile";
58
+ }
59
+ interface LocalAccessTokenPayload extends JwtPayload {
60
+ /** Token 的过期时间(Unix 时间戳)。*/
61
+ exp: number;
62
+ /** Token 的生效时间(Unix 时间戳)。*/
63
+ nbf: number;
64
+ /** Token 的签发时间(Unix 时间戳)。*/
65
+ iat: number;
66
+ /** 用户信息 */
67
+ User: {
68
+ /** 注册时间。*/
69
+ createTime: string;
70
+ /** 用户的 UUID。*/
71
+ id?: string;
72
+ /** 用户名。*/
73
+ username?: string;
74
+ /** 是否已删除。*/
75
+ deleted?: boolean;
76
+ /** 用户的角色。*/
77
+ role: {
78
+ /** 角色的 UUID。*/
79
+ id: string;
80
+ /** 角色名。*/
81
+ name: string;
82
+ /** 角色的显示名。*/
83
+ display_name: string;
84
+ /** 角色拥有的权限。*/
85
+ functions: string[];
86
+ /** 角色的优先级。*/
87
+ priority: number;
88
+ };
89
+ /** 用户的角色的 UUID。*/
90
+ roleId: string;
91
+ };
92
+ }
93
+ export type UserInfo = CasdoorUserInfo | LocalUserInfo;
94
+ interface CasdoorUserInfo {
95
+ /**
96
+ * Token 的发布者。见 [JWT 标准][1]。
97
+ *
98
+ * [1]: https://datatracker.ietf.org/doc/html/rfc7519#section-4.1
99
+ */
100
+ iss: string;
101
+ /**
102
+ * Token 的主题。见 [JWT 标准][1]。
103
+ *
104
+ * [1]: https://datatracker.ietf.org/doc/html/rfc7519#section-4.1
105
+ */
106
+ sub: string;
107
+ /**
108
+ * Token 的目标应用 ID。见 [JWT 标准][1]。
109
+ *
110
+ * [1]: https://datatracker.ietf.org/doc/html/rfc7519#section-4.1
111
+ */
112
+ aud: string;
113
+ /** 用户的全名(显示名)。*/
114
+ name: string;
115
+ /** 短用户名(登录名)。*/
116
+ prefered_username?: string;
117
+ /** 用户的邮件地址。*/
118
+ email?: string;
119
+ /** 用户的邮件地址是否已确认。*/
120
+ email_verified?: boolean;
121
+ /** 用户的手机号。*/
122
+ phone?: string;
123
+ /** 用户的头像 URL。*/
124
+ picture: string;
125
+ /** 用户的地址。*/
126
+ address?: string;
127
+ /** 用户所属的用户组。*/
128
+ groups: string[];
129
+ /** 用户的角色。*/
130
+ roles: string[];
131
+ }
132
+ interface LocalUserInfo {
133
+ /** 用户的 UUID。*/
134
+ id?: string;
135
+ /** 用户名。*/
136
+ username?: string;
137
+ /** 显示名。*/
138
+ displayName?: string;
139
+ /** 创建时间。*/
140
+ createdAt: string;
141
+ /** 更新时间。*/
142
+ updatedAt: string;
143
+ }
144
+ export type Auth = CasdoorAuth | LocalAuth;
145
+ interface CommonAuth {
146
+ /**
147
+ * 当前使用的登录机制。
148
+ *
149
+ * - `"casdoor"`:基于 Casdoor 的登录系统。
150
+ * - `"local"`:场站端使用的内网登录系统。
151
+ */
152
+ mode: "casdoor" | "local";
153
+ /**
154
+ * 跳转到登录页面。
155
+ *
156
+ * 参数 `redirect` 可以填入登录完成后要跳转到的地址。如果省略,那么跳转目标将由
157
+ * {@linkcode AuthCallback} 的 {@linkcode AuthCallbackProps.onRedirect | onRedirect}
158
+ * 属性决定,默认为跳转到主页。
159
+ */
160
+ login(redirect?: string): void;
161
+ /**
162
+ * 从登录页面返回时应该调用的回调。当登录状态无效时会**抛出异常**。
163
+ *
164
+ * 当 {@linkcode mode | mode === "local"} 时,该函数无效。
165
+ */
166
+ callback(): Promise<void>;
167
+ /** 在线检查用户是否登录。用户未登录时会**抛出异常**。*/
168
+ validate(): Promise<void>;
169
+ /**
170
+ * 登出。
171
+ *
172
+ * 参数 `redirect` 可以填入登出后跳转到的地址。如果省略,默认为**不做任何跳转**。
173
+ * 你也可以在登出后手动加一行跳转操作,比如使用 React Router 的 `navigate`。
174
+ */
175
+ logout(redirect?: string): void;
176
+ /**
177
+ * 以当前用户的 access token 作为 bearer token 发起请求。
178
+ * 用法与正常的 {@linkcode fetch} 完全一致。未登录时,返回的 Promise 会立即失败。
179
+ */
180
+ fetchWithAuth(resource: RequestInfo | URL, options?: RequestInit): Promise<Response>;
181
+ /** 用户的个人信息,未经过 {@linkcode AuthCheck} 验证时为空。*/
182
+ userInfo?: UserInfo;
183
+ /** Access Token,未登录时为空。*/
184
+ accessToken?: string;
185
+ /** 解码后的 Access Token,未登录时为空。这里也会有一些用户的个人信息。*/
186
+ decodedAccessToken?: AccessTokenPayload;
187
+ }
188
+ export interface CasdoorAuth extends CommonAuth {
189
+ mode: "casdoor";
190
+ /** 全局的 Casdoor SDK 对象。*/
191
+ sdk: Sdk;
192
+ userInfo?: CasdoorUserInfo;
193
+ decodedAccessToken?: CasdoorAccessTokenPayload;
194
+ }
195
+ export interface LocalAuth extends CommonAuth {
196
+ mode: "local";
197
+ /**
198
+ * 在登录页面需要通过该函数调用登录 API。
199
+ *
200
+ * 账号或密码错误时会**抛出异常**。
201
+ */
202
+ localLogin(username: string, password: string): Promise<void>;
203
+ userInfo?: LocalUserInfo;
204
+ decodedAccessToken?: LocalAccessTokenPayload;
205
+ }
206
+ /**
207
+ * 获取登录鉴权相关的工具函数和全局状态。必须在 {@linkcode AuthProvider} 内部才能使用。
208
+ *
209
+ * 参数 `user` 仅供组件库内部使用,编写项目代码时可以忽略。
210
+ */
211
+ export declare function useAuth(user?: string): Auth;
212
+ export interface AuthProviderProps {
213
+ /**
214
+ * 要使用的登录机制。
215
+ *
216
+ * - `"casdoor"`:基于 Casdoor 的登录系统。
217
+ * - `"local"`:场站端使用的内网登录系统。
218
+ *
219
+ * @default "casdoor"
220
+ */
221
+ mode?: "casdoor" | "local";
222
+ /**
223
+ * 用于初始化 Casdoor SDK 的配置对象。
224
+ * {@linkcode SdkConfig.redirectPath | sdkConfig.redirectPath} 应该指向一个包含
225
+ * {@linkcode AuthCallback} 组件的路径。
226
+ */
227
+ sdkConfig?: SdkConfig;
228
+ /** {@linkcode mode | mode: "local"} 时,登录页面的路径。*/
229
+ loginPage?: string;
230
+ /**
231
+ * 登录 API 的源点,末尾不带 `/`。
232
+ * @default location.origin
233
+ */
234
+ signinOrigin?: string;
235
+ /**
236
+ * 登陆 API 的路径。
237
+ * @default "/api/signin"
238
+ */
239
+ signinPath?: string;
240
+ /** 需要管理登录状态的内容。*/
241
+ children: ReactNode;
242
+ }
243
+ /**
244
+ * 为包裹的内容提供登陆状态管理服务,通常应该包裹在整个 App 组件外部。
245
+ *
246
+ * 使用时至少需要提供 {@linkcode AuthProviderProps.sdkConfig | sdkConfig} 和
247
+ * {@linkcode AuthProviderProps.signinOrigin | signinOrigin} 两个属性。
248
+ * 其中,{@linkcode SdkConfig.redirectPath | sdkConfig.redirectPath} 应该指向一个包含
249
+ * {@linkcode AuthCallback} 组件的路径。
250
+ *
251
+ * 在 {@linkcode AuthProvider} 的内部,
252
+ * 可以使用 {@linkcode useAuth} 来获取登录鉴权相关的工具函数和全局状态,
253
+ * 也可以使用 {@linkcode AuthCheck} 来确保某个页面或内容只有已登录用户可以访问。
254
+ */
255
+ export declare function AuthProvider(props: AuthProviderProps): ReactNode;
256
+ export interface AuthCheckProps {
257
+ /**
258
+ * 用户必须拥有的角色。用户必须拥有这里列出的所有角色才能访问被保护的内容。
259
+ * 如果省略,则只需要登录即可访问。
260
+ */
261
+ roles?: string[];
262
+ /**
263
+ * 在检查过程中时向用户显示的加载界面。
264
+ * @default "正在登录……"
265
+ */
266
+ loading?: ReactNode;
267
+ /**
268
+ * 当用户未登录或登录状态验证失败时的回调。默认会跳转到登录。
269
+ * @default () => auth.login(location.href) // auth = useAuth()
270
+ */
271
+ onFail?: (err: unknown) => void;
272
+ /**
273
+ * 用户已登录,但不满足权限要求时显示的内容。
274
+ * @default "您无权访问此页面!"
275
+ */
276
+ forbidden?: ReactNode;
277
+ /** 用户登录后显示的内容。*/
278
+ children: ReactNode;
279
+ }
280
+ /**
281
+ * 在线检查用户是否登录、是否满足权限要求。
282
+ * 未登录的用户会被跳转到登录,不满足权限要求的用户会见到警告信息,只有检验通过的用户才能访问内部的内容。
283
+ *
284
+ * 该组件必须在 {@linkcode AuthProvider} 内部才能使用。
285
+ */
286
+ export declare function AuthCheck(props: AuthCheckProps): ReactNode;
287
+ export interface AuthCallbackProps {
288
+ /**
289
+ * 跳转时的回调。传入的 `redirect` 参数就是 {@linkcode Auth.login | login} 函数的参数。
290
+ * @default (redirect) => { location.href = redirect ?? "/"; }
291
+ */
292
+ onRedirect?: (redirect?: string) => void;
293
+ /**
294
+ * 发生错误时显示的内容。
295
+ * @default (err) => err.toString()
296
+ */
297
+ error?: (err: unknown) => ReactNode;
298
+ /**
299
+ * 跳转前显示的内容。
300
+ * @default "正在跳转……"
301
+ */
302
+ children?: ReactNode;
303
+ }
304
+ /**
305
+ * 从登录页面返回到的回调页面。
306
+ *
307
+ * 该组件必须在 {@linkcode AuthProvider} 内部才能使用。
308
+ */
309
+ export declare function AuthCallback(props: AuthCallbackProps): ReactNode;
310
+ export {};
@@ -0,0 +1,267 @@
1
+ import Sdk from "casdoor-js-sdk";
2
+ import { jwtDecode } from "jwt-decode";
3
+ import { createContext, useContext, useEffect, useMemo, useRef, useState } from "react";
4
+ import { jsx as _jsx } from "react/jsx-runtime";
5
+ const commonAuth = (user, setUser) => ({
6
+ logout(redirect) {
7
+ localStorage.removeItem("access_token");
8
+ setUser({});
9
+ if (redirect) {
10
+ location.href = redirect;
11
+ }
12
+ },
13
+ async fetchWithAuth(resource, options) {
14
+ const token = user.accessToken;
15
+ if (!token) {
16
+ throw new Error("未登录时无法使用 fetchWithAuth");
17
+ }
18
+ const headers = new Headers(options?.headers);
19
+ if (!headers.has("Authorization")) {
20
+ headers.set("Authorization", `Bearer ${token}`);
21
+ }
22
+ return await fetch(resource, {
23
+ ...options,
24
+ headers
25
+ });
26
+ }
27
+ });
28
+ function casdoorAuth(user, setUser, sdkConfig, signinOrigin, signinPath) {
29
+ const sdk = new Sdk(sdkConfig);
30
+ return {
31
+ mode: "casdoor",
32
+ sdk,
33
+ login(redirect) {
34
+ location.href = sdk.getSigninUrl();
35
+ if (redirect) {
36
+ sessionStorage.setItem("login_redirect", redirect);
37
+ }
38
+ },
39
+ async callback() {
40
+ // sdk 的类型声明有误
41
+ const resp = await sdk.signin(signinOrigin ?? location.origin, signinPath);
42
+ const token = resp.token;
43
+ if (!token) {
44
+ throw new Error("登录 API 返回了异常的结果:" + JSON.stringify(resp));
45
+ }
46
+ localStorage.setItem("access_token", token);
47
+ setUser({
48
+ accessToken: token,
49
+ get decodedAccessToken() {
50
+ return jwtDecode(token);
51
+ }
52
+ });
53
+ },
54
+ async validate() {
55
+ const token = user.accessToken;
56
+ if (!token) {
57
+ throw new Error("未登录!");
58
+ }
59
+ // sdk 的类型声明有误
60
+ const resp = await sdk.getUserInfo(token);
61
+ if ("status" in resp) {
62
+ localStorage.removeItem("access_token");
63
+ setUser({});
64
+ throw new Error("登陆状态无效!");
65
+ } else {
66
+ setUser({
67
+ ...user,
68
+ userInfo: resp
69
+ });
70
+ }
71
+ },
72
+ ...commonAuth(user, setUser),
73
+ ...user
74
+ };
75
+ }
76
+ const localAuth = (user, setUser, loginPage, signinOrigin, signinPath) => ({
77
+ mode: "local",
78
+ login(redirect) {
79
+ location.href = loginPage;
80
+ if (redirect) {
81
+ sessionStorage.setItem("login_redirect", redirect);
82
+ }
83
+ },
84
+ async localLogin(username, password) {
85
+ const resp = await fetch(new URL(signinPath ?? "/api/signin", signinOrigin ?? location.origin), {
86
+ method: "post",
87
+ headers: {
88
+ "Content-Type": "application/json"
89
+ },
90
+ body: JSON.stringify({
91
+ username,
92
+ password
93
+ })
94
+ });
95
+ if (resp.status !== 200) {
96
+ throw new Error("账号或密码错误!");
97
+ }
98
+ const json = await resp.json();
99
+ const token = json.token;
100
+ if (typeof token !== "string") {
101
+ throw new Error("登录 API 返回了异常的结果:" + JSON.stringify(json));
102
+ }
103
+ localStorage.setItem("access_token", token);
104
+ setUser({
105
+ accessToken: token,
106
+ get decodedAccessToken() {
107
+ return jwtDecode(token);
108
+ }
109
+ });
110
+ const redirect = sessionStorage.getItem("login_redirect");
111
+ sessionStorage.removeItem("login_redirect");
112
+ location.href = redirect ?? "/";
113
+ },
114
+ async callback() {},
115
+ async validate() {
116
+ const token = user.accessToken;
117
+ if (!token) {
118
+ throw new Error("未登录!");
119
+ }
120
+ const resp = await fetch(new URL("/api/userInfo", signinOrigin ?? location.origin), {
121
+ headers: {
122
+ Authorization: `Bearer ${token}`
123
+ }
124
+ });
125
+ try {
126
+ if (resp.status !== 200) {
127
+ throw new Error("登陆状态无效!");
128
+ }
129
+ const json = await resp.json();
130
+ if (!json.data) {
131
+ throw new Error("登陆状态无效!");
132
+ }
133
+ setUser({
134
+ ...user,
135
+ userInfo: json.data
136
+ });
137
+ } catch (err) {
138
+ localStorage.removeItem("access_token");
139
+ setUser({});
140
+ throw err;
141
+ }
142
+ },
143
+ ...commonAuth(user, setUser),
144
+ ...user
145
+ });
146
+ const AuthContext = /*#__PURE__*/createContext(undefined);
147
+
148
+ /**
149
+ * 获取登录鉴权相关的工具函数和全局状态。必须在 {@linkcode AuthProvider} 内部才能使用。
150
+ *
151
+ * 参数 `user` 仅供组件库内部使用,编写项目代码时可以忽略。
152
+ */
153
+ export function useAuth(t0) {
154
+ const user = t0 === undefined ? "useAuth" : t0;
155
+ const auth = useContext(AuthContext);
156
+ if (auth === undefined) {
157
+ throw new Error(user + " \u5FC5\u987B\u5728 AuthProvider \u5185\u90E8\u4F7F\u7528\uFF01");
158
+ }
159
+ return auth;
160
+ }
161
+ /**
162
+ * 为包裹的内容提供登陆状态管理服务,通常应该包裹在整个 App 组件外部。
163
+ *
164
+ * 使用时至少需要提供 {@linkcode AuthProviderProps.sdkConfig | sdkConfig} 和
165
+ * {@linkcode AuthProviderProps.signinOrigin | signinOrigin} 两个属性。
166
+ * 其中,{@linkcode SdkConfig.redirectPath | sdkConfig.redirectPath} 应该指向一个包含
167
+ * {@linkcode AuthCallback} 组件的路径。
168
+ *
169
+ * 在 {@linkcode AuthProvider} 的内部,
170
+ * 可以使用 {@linkcode useAuth} 来获取登录鉴权相关的工具函数和全局状态,
171
+ * 也可以使用 {@linkcode AuthCheck} 来确保某个页面或内容只有已登录用户可以访问。
172
+ */
173
+ export function AuthProvider(props) {
174
+ const [user, setUser] = useState(() => {
175
+ const token = localStorage.getItem("access_token");
176
+ if (!token) {
177
+ return {};
178
+ } else {
179
+ return {
180
+ accessToken: token,
181
+ get decodedAccessToken() {
182
+ return jwtDecode(token);
183
+ }
184
+ };
185
+ }
186
+ });
187
+ const auth = useMemo(() => {
188
+ if ((props.mode ?? "casdoor") === "casdoor") {
189
+ if (!props.sdkConfig) {
190
+ throw new Error(`设置了 mode === "casdoor",却没有设置 sdkConfig!`);
191
+ }
192
+ return casdoorAuth(user, setUser, props.sdkConfig, props.signinOrigin, props.signinPath);
193
+ } else {
194
+ if (!props.loginPage) {
195
+ throw new Error(`设置了 mode === "local",却没有设置 loginPage!`);
196
+ }
197
+ return localAuth(user, setUser, props.loginPage, props.signinOrigin, props.signinPath);
198
+ }
199
+ }, [props.mode, props.sdkConfig, props.loginPage, props.signinOrigin, props.signinPath, user]);
200
+ return /*#__PURE__*/_jsx(AuthContext.Provider, {
201
+ value: auth,
202
+ children: props.children
203
+ });
204
+ }
205
+ /**
206
+ * 在线检查用户是否登录、是否满足权限要求。
207
+ * 未登录的用户会被跳转到登录,不满足权限要求的用户会见到警告信息,只有检验通过的用户才能访问内部的内容。
208
+ *
209
+ * 该组件必须在 {@linkcode AuthProvider} 内部才能使用。
210
+ */
211
+ export function AuthCheck(props) {
212
+ const auth = useAuth("AuthCheck");
213
+ const userInfo = auth.userInfo;
214
+ // 用于确保在 Strict Mode 下副作用也只执行一次
215
+ const effectDone = useRef(false);
216
+ useEffect(() => {
217
+ if (effectDone.current) return;
218
+ effectDone.current = true;
219
+ if (!auth.userInfo) {
220
+ auth.validate().catch(props.onFail ?? (() => {
221
+ auth.login(location.href);
222
+ }));
223
+ }
224
+ // 这个副作用只在首次渲染时执行
225
+ // eslint-disable-next-line react-hooks/exhaustive-deps
226
+ }, []);
227
+ if (userInfo) {
228
+ if (!("roles" in userInfo) || (props.roles ?? []).every(role => userInfo.roles.includes(role))) {
229
+ return props.children;
230
+ } else {
231
+ return props.forbidden ?? "您无权访问此页面!";
232
+ }
233
+ } else {
234
+ return props.loading ?? "正在登录……";
235
+ }
236
+ }
237
+ /**
238
+ * 从登录页面返回到的回调页面。
239
+ *
240
+ * 该组件必须在 {@linkcode AuthProvider} 内部才能使用。
241
+ */
242
+ export function AuthCallback(props) {
243
+ const auth = useAuth("AuthCallback");
244
+ const [error, setError] = useState();
245
+ // 用于确保在 Strict Mode 下副作用也只执行一次
246
+ const effectDone = useRef(false);
247
+ useEffect(() => {
248
+ if (effectDone.current) return;
249
+ effectDone.current = true;
250
+ const redirect = sessionStorage.getItem("login_redirect");
251
+ sessionStorage.removeItem("login_redirect");
252
+ auth.callback().then(() => {
253
+ if (props.onRedirect) {
254
+ props.onRedirect(redirect ?? undefined);
255
+ } else {
256
+ location.href = redirect ?? "/";
257
+ }
258
+ }, setError);
259
+ // 这个副作用只在首次渲染时执行
260
+ // eslint-disable-next-line react-hooks/exhaustive-deps
261
+ }, []);
262
+ if (error) {
263
+ return props.error ? props.error(error) : error instanceof Error ? error.message : String(error);
264
+ } else {
265
+ return props.children ?? "正在跳转……";
266
+ }
267
+ }
@@ -0,0 +1,21 @@
1
+ import type { EChartsHOCType } from "./ECharts";
2
+ /**
3
+ * 为 ECharts 增加自适应容器尺寸的能力。
4
+ *
5
+ * 该组件通常需要添加类似 `width: 100%; height: 100%;` 的 CSS 属性,或者放在 Flex/Grid
6
+ * 容器中。
7
+ *
8
+ * 为了保证图像能正常缩小,如果使用 Flex,请务必设置 flex 方向上的
9
+ * `min-width`/`min-height`;在 Grid 中则需要确保 ECharts 所在的行/列的宽度设置为
10
+ * `minmax(..., ...fr)` 而不可直接使用 `fr`。不这么做会导致图像只能放大而无法缩小。
11
+ */
12
+ export declare const withAutoResize: EChartsHOCType;
13
+ /**
14
+ * 预置了自适应容器尺寸能力的 ECharts 组件。
15
+ *
16
+ * @see {@linkcode withAutoResize}
17
+ */
18
+ declare const AutoResizedECharts: import("./ECharts").EChartsComponentType<{}> & {
19
+ displayName: string;
20
+ };
21
+ export default AutoResizedECharts;
@@ -0,0 +1,98 @@
1
+ import { c as _c } from "react/compiler-runtime";
2
+ import { useEffect, useImperativeHandle, useRef } from "react";
3
+ import { debugAssert } from "../utils/debug";
4
+ import EChartsInstance from "./ECharts";
5
+
6
+ /**
7
+ * 为 ECharts 增加自适应容器尺寸的能力。
8
+ *
9
+ * 该组件通常需要添加类似 `width: 100%; height: 100%;` 的 CSS 属性,或者放在 Flex/Grid
10
+ * 容器中。
11
+ *
12
+ * 为了保证图像能正常缩小,如果使用 Flex,请务必设置 flex 方向上的
13
+ * `min-width`/`min-height`;在 Grid 中则需要确保 ECharts 所在的行/列的宽度设置为
14
+ * `minmax(..., ...fr)` 而不可直接使用 `fr`。不这么做会导致图像只能放大而无法缩小。
15
+ */
16
+ import { jsx as _jsx } from "react/jsx-runtime";
17
+ export const withAutoResize = ECharts => props => {
18
+ "use memo";
19
+
20
+ const $ = _c(9);
21
+ let outerRef;
22
+ let rest;
23
+ if ($[0] !== props) {
24
+ ({
25
+ ref: outerRef,
26
+ ...rest
27
+ } = props);
28
+ $[0] = props;
29
+ $[1] = outerRef;
30
+ $[2] = rest;
31
+ } else {
32
+ outerRef = $[1];
33
+ rest = $[2];
34
+ }
35
+ const ref = useRef(null);
36
+ let t0;
37
+ let t1;
38
+ if ($[3] === Symbol.for("react.memo_cache_sentinel")) {
39
+ t0 = () => {
40
+ debugAssert(ref.current, "ref should be connected");
41
+ return ref.current;
42
+ };
43
+ t1 = [];
44
+ $[3] = t0;
45
+ $[4] = t1;
46
+ } else {
47
+ t0 = $[3];
48
+ t1 = $[4];
49
+ }
50
+ useImperativeHandle(outerRef, t0, t1);
51
+ let t2;
52
+ let t3;
53
+ if ($[5] === Symbol.for("react.memo_cache_sentinel")) {
54
+ t2 = () => {
55
+ let firstFire = true;
56
+ const observer = new ResizeObserver(() => {
57
+ if (firstFire) {
58
+ firstFire = false;
59
+ } else {
60
+ debugAssert(ref.current, "observer event should only be triggered when ref is connected");
61
+ ref.current.instance?.resize();
62
+ }
63
+ });
64
+ debugAssert(ref.current, "effect should run after ref is connected");
65
+ observer.observe(ref.current.container);
66
+ return () => observer.disconnect();
67
+ };
68
+ t3 = [];
69
+ $[5] = t2;
70
+ $[6] = t3;
71
+ } else {
72
+ t2 = $[5];
73
+ t3 = $[6];
74
+ }
75
+ useEffect(t2, t3);
76
+ let t4;
77
+ if ($[7] !== rest) {
78
+ t4 = /*#__PURE__*/_jsx(ECharts, {
79
+ ref: ref,
80
+ ...rest
81
+ });
82
+ $[7] = rest;
83
+ $[8] = t4;
84
+ } else {
85
+ t4 = $[8];
86
+ }
87
+ return t4;
88
+ };
89
+
90
+ /**
91
+ * 预置了自适应容器尺寸能力的 ECharts 组件。
92
+ *
93
+ * @see {@linkcode withAutoResize}
94
+ */
95
+ const AutoResizedECharts = Object.assign(withAutoResize(EChartsInstance), {
96
+ displayName: "AutoResizedECharts"
97
+ });
98
+ export default AutoResizedECharts;