Showing
11 changed files
with
83 additions
and
25 deletions
... | ... | @@ -16,6 +16,7 @@ import { |
16 | 16 | UpdateDataComponentParams, |
17 | 17 | } from './model'; |
18 | 18 | import { defHttp } from '/@/utils/http/axios'; |
19 | +import { isShareMode } from '/@/views/sys/share/hook'; | |
19 | 20 | |
20 | 21 | enum DataBoardUrl { |
21 | 22 | GET_DATA_BOARD = '/data_board', |
... | ... | @@ -224,7 +225,7 @@ export const sendCommandOneway = (params: SendCommandParams) => { |
224 | 225 | url: `${SendCommand.ONEWAY}/${params.deviceId}`, |
225 | 226 | params: params.value, |
226 | 227 | }, |
227 | - { joinPrefix: false } | |
228 | + { joinPrefix: false, withShareToken: isShareMode() } | |
228 | 229 | ); |
229 | 230 | }; |
230 | 231 | ... | ... |
1 | 1 | import { defHttp } from '/@/utils/http/axios'; |
2 | 2 | import { ViewTypeEnum } from '/@/views/sys/share/config/config'; |
3 | +import { isShareMode } from '/@/views/sys/share/hook'; | |
3 | 4 | |
4 | 5 | enum Api { |
5 | 6 | CHECK = '/share/check', |
... | ... | @@ -8,9 +9,14 @@ enum Api { |
8 | 9 | } |
9 | 10 | |
10 | 11 | export const checkShareAccessToken = (type: ViewTypeEnum, id: string) => { |
11 | - return defHttp.get<Record<'data', boolean>>({ | |
12 | - url: `${Api.CHECK}/${type}/${id}`, | |
13 | - }); | |
12 | + return defHttp.get<Record<'data', boolean>>( | |
13 | + { | |
14 | + url: `${Api.CHECK}/${type}/${id}`, | |
15 | + }, | |
16 | + { | |
17 | + withShareToken: isShareMode(), | |
18 | + } | |
19 | + ); | |
14 | 20 | }; |
15 | 21 | |
16 | 22 | export const sharePageLogin = (publicId: string) => { |
... | ... | @@ -21,14 +27,20 @@ export const sharePageLogin = (publicId: string) => { |
21 | 27 | }, |
22 | 28 | { |
23 | 29 | joinPrefix: false, |
30 | + withShareToken: isShareMode(), | |
24 | 31 | } |
25 | 32 | ); |
26 | 33 | }; |
27 | 34 | |
28 | 35 | export const getShareContent = (record: Record<'accessCredentials' | 'id', string>) => { |
29 | 36 | const { id, accessCredentials } = record; |
30 | - return defHttp.get({ | |
31 | - url: `${Api.SHARE_CONTENT}/${ViewTypeEnum.DATA_BOARD}/share_data/${id}`, | |
32 | - params: { accessCredentials }, | |
33 | - }); | |
37 | + return defHttp.get( | |
38 | + { | |
39 | + url: `${Api.SHARE_CONTENT}/${ViewTypeEnum.DATA_BOARD}/share_data/${id}`, | |
40 | + params: { accessCredentials }, | |
41 | + }, | |
42 | + { | |
43 | + withShareToken: isShareMode(), | |
44 | + } | |
45 | + ); | |
34 | 46 | }; | ... | ... |
... | ... | @@ -5,6 +5,10 @@ export const JWT_TOKEN_KEY = 'JWT_TOKEN'; |
5 | 5 | |
6 | 6 | export const REFRESH_TOKEN_KEY = 'REFRESH_TOKEN'; |
7 | 7 | |
8 | +export const SHARE_JWT_TOKEN_KEY = 'SHARE_JWT_TOKEN'; | |
9 | + | |
10 | +export const SHARE_REFRESH_TOKEN_KEY = 'SHARE_REFRESH_TOKEN'; | |
11 | + | |
8 | 12 | export const LOCALE_KEY = 'LOCALE__'; |
9 | 13 | |
10 | 14 | // user info key | ... | ... |
... | ... | @@ -4,7 +4,14 @@ import { defineStore } from 'pinia'; |
4 | 4 | import { store } from '/@/store'; |
5 | 5 | import { RoleEnum } from '/@/enums/roleEnum'; |
6 | 6 | import { PageEnum } from '/@/enums/pageEnum'; |
7 | -import { JWT_TOKEN_KEY, REFRESH_TOKEN_KEY, ROLES_KEY, USER_INFO_KEY } from '/@/enums/cacheEnum'; | |
7 | +import { | |
8 | + JWT_TOKEN_KEY, | |
9 | + REFRESH_TOKEN_KEY, | |
10 | + ROLES_KEY, | |
11 | + SHARE_JWT_TOKEN_KEY, | |
12 | + SHARE_REFRESH_TOKEN_KEY, | |
13 | + USER_INFO_KEY, | |
14 | +} from '/@/enums/cacheEnum'; | |
8 | 15 | import { getAuthCache, setAuthCache } from '/@/utils/auth'; |
9 | 16 | import { |
10 | 17 | LoginParams, |
... | ... | @@ -33,6 +40,8 @@ interface UserState { |
33 | 40 | lastUpdateTime: number; |
34 | 41 | jwtToken?: string; |
35 | 42 | refreshToken?: string; |
43 | + shareJwtToken?: string; | |
44 | + shareRefreshToken?: string; | |
36 | 45 | outTarget?: string; |
37 | 46 | } |
38 | 47 | |
... | ... | @@ -100,6 +109,13 @@ export const useUserStore = defineStore({ |
100 | 109 | setAuthCache(JWT_TOKEN_KEY, jwtToken); |
101 | 110 | setAuthCache(REFRESH_TOKEN_KEY, refreshToken); |
102 | 111 | }, |
112 | + | |
113 | + storeShareToken(jwtToken: string, refreshToken: string) { | |
114 | + this.shareJwtToken = jwtToken; | |
115 | + this.shareRefreshToken = refreshToken; | |
116 | + setAuthCache(SHARE_JWT_TOKEN_KEY, jwtToken); | |
117 | + setAuthCache(SHARE_REFRESH_TOKEN_KEY, refreshToken); | |
118 | + }, | |
103 | 119 | setToken(info: string | undefined) { |
104 | 120 | this.jwtToken = info; |
105 | 121 | setAuthCache(JWT_TOKEN_KEY, info); | ... | ... |
1 | 1 | import { Persistent, BasicKeys } from '/@/utils/cache/persistent'; |
2 | -import { CacheTypeEnum } from '/@/enums/cacheEnum'; | |
2 | +import { CacheTypeEnum, SHARE_JWT_TOKEN_KEY, SHARE_REFRESH_TOKEN_KEY } from '/@/enums/cacheEnum'; | |
3 | 3 | import projectSetting from '/@/settings/projectSetting'; |
4 | 4 | import { JWT_TOKEN_KEY, REFRESH_TOKEN_KEY } from '/@/enums/cacheEnum'; |
5 | 5 | |
... | ... | @@ -26,3 +26,11 @@ export function getJwtToken() { |
26 | 26 | export function getRefreshToken() { |
27 | 27 | return getAuthCache(REFRESH_TOKEN_KEY); |
28 | 28 | } |
29 | + | |
30 | +export function getShareJwtToken() { | |
31 | + return getAuthCache(SHARE_JWT_TOKEN_KEY); | |
32 | +} | |
33 | + | |
34 | +export function getShareRefreshToken() { | |
35 | + return getAuthCache(SHARE_REFRESH_TOKEN_KEY); | |
36 | +} | ... | ... |
... | ... | @@ -80,7 +80,6 @@ export class VAxios { |
80 | 80 | private refreshTokenBeforeReq(doRefreshTokenApi: () => Promise<unknown>): Promise<unknown> { |
81 | 81 | // 创建一个未完成的promise,把改变状态的resolve方法交给请求token结束后执行 |
82 | 82 | const promise = new Promise((resolve) => { |
83 | - console.log('等待新token'); | |
84 | 83 | // 等待队列放的是一个回调函数,来延迟resolve的执行,以此控制promise状态的改变 |
85 | 84 | this.waitingQueue.push(() => resolve(null)); |
86 | 85 | }); |
... | ... | @@ -88,7 +87,6 @@ export class VAxios { |
88 | 87 | this.refreshing = true; |
89 | 88 | // 模拟请求刷新Token接口,当接口返回数据时执行then方法 TODO 添加catch捕获异常 |
90 | 89 | doRefreshTokenApi().then(() => { |
91 | - console.log('刷新token成功,放行队列中的请求', this.waitingQueue.length); | |
92 | 90 | this.refreshing = false; |
93 | 91 | this.waitingQueue.forEach((cb) => cb()); |
94 | 92 | this.waitingQueue.length = 0; |
... | ... | @@ -244,7 +242,6 @@ export class VAxios { |
244 | 242 | const { requestOptions } = this.options; |
245 | 243 | |
246 | 244 | const opt: RequestOptions = Object.assign({}, requestOptions, options); |
247 | - | |
248 | 245 | const { beforeRequestHook, requestCatchHook, transformRequestHook } = transform || {}; |
249 | 246 | if (beforeRequestHook && isFunction(beforeRequestHook)) { |
250 | 247 | conf = beforeRequestHook(conf, opt); | ... | ... |
... | ... | @@ -10,7 +10,7 @@ import { useGlobSetting } from '/@/hooks/setting'; |
10 | 10 | import { useMessage } from '/@/hooks/web/useMessage'; |
11 | 11 | import { RequestEnum, ContentTypeEnum } from '/@/enums/httpEnum'; |
12 | 12 | import { isString } from '/@/utils/is'; |
13 | -import { getJwtToken } from '/@/utils/auth'; | |
13 | +import { getJwtToken, getShareJwtToken } from '/@/utils/auth'; | |
14 | 14 | import { setObjToUrlParams, deepMerge } from '/@/utils'; |
15 | 15 | import { useErrorLogStoreWithOut } from '/@/store/modules/errorLog'; |
16 | 16 | import { useI18n } from '/@/hooks/web/useI18n'; |
... | ... | @@ -92,12 +92,25 @@ const transform: AxiosTransform = { |
92 | 92 | */ |
93 | 93 | requestInterceptors: (config, options) => { |
94 | 94 | // 请求之前处理config |
95 | - const token = getJwtToken(); | |
96 | - if (token && (config as Recordable)?.requestOptions?.withToken !== false) { | |
97 | - // jwt token | |
98 | - config.headers['X-Authorization'] = options.authenticationScheme | |
99 | - ? `${options.authenticationScheme} ${token}` | |
100 | - : token; | |
95 | + const { requestOptions } = config; | |
96 | + const { withShareToken } = requestOptions || {}; | |
97 | + const { requestOptions: { withToken } = {} } = options; | |
98 | + if (withToken !== false) { | |
99 | + const shareToken = getShareJwtToken(); | |
100 | + if (withShareToken && shareToken) { | |
101 | + console.log({ withShareToken, shareToken }); | |
102 | + config.headers['X-Authorization'] = options.authenticationScheme | |
103 | + ? `${options.authenticationScheme} ${shareToken}` | |
104 | + : shareToken; | |
105 | + } else { | |
106 | + const token = getJwtToken(); | |
107 | + console.log({ token }); | |
108 | + if (token) { | |
109 | + config.headers['X-Authorization'] = options.authenticationScheme | |
110 | + ? `${options.authenticationScheme} ${token}` | |
111 | + : token; | |
112 | + } | |
113 | + } | |
101 | 114 | } |
102 | 115 | return config; |
103 | 116 | }, |
... | ... | @@ -194,7 +207,7 @@ function createAxios(opt?: Partial<CreateAxiosOptions>) { |
194 | 207 | ignoreCancelToken: true, |
195 | 208 | // 是否携带token |
196 | 209 | withToken: true, |
197 | - }, | |
210 | + } as RequestOptions, | |
198 | 211 | }, |
199 | 212 | opt || {} |
200 | 213 | ) | ... | ... |
src/views/sys/share/hook/index.ts
0 → 100644
... | ... | @@ -37,7 +37,7 @@ |
37 | 37 | const { params } = ROUTE; |
38 | 38 | const { publicId } = params as Partial<ShareRouteParams>; |
39 | 39 | const { token, refreshToken } = await sharePageLogin(publicId!); |
40 | - userStore.storeToken(token, refreshToken); | |
40 | + userStore.storeShareToken(token, refreshToken); | |
41 | 41 | }; |
42 | 42 | |
43 | 43 | const getCheckNeedAccessToken = async () => { | ... | ... |
1 | 1 | import { useWebSocket } from '@vueuse/core'; |
2 | 2 | import { Ref, unref } from 'vue'; |
3 | 3 | import { DataBoardLayoutInfo } from '../types/type'; |
4 | -import { JWT_TOKEN_KEY } from '/@/enums/cacheEnum'; | |
5 | 4 | import { useGlobSetting } from '/@/hooks/setting'; |
6 | -import { getAuthCache } from '/@/utils/auth'; | |
7 | 5 | import { isNullAndUnDef } from '/@/utils/is'; |
6 | +import { getJwtToken, getShareJwtToken } from '/@/utils/auth'; | |
7 | +import { isShareMode } from '/@/views/sys/share/hook'; | |
8 | 8 | |
9 | 9 | interface SocketMessage { |
10 | 10 | tsSubCmds: SocketMessageItem[]; |
... | ... | @@ -50,7 +50,7 @@ const generateMessage = (deviceId: string, cmdId: number, attr: string): SocketM |
50 | 50 | }; |
51 | 51 | |
52 | 52 | export function useSocketConnect(dataSourceRef: Ref<DataBoardLayoutInfo[]>) { |
53 | - const token = getAuthCache(JWT_TOKEN_KEY); | |
53 | + const token = isShareMode() ? getShareJwtToken() : getJwtToken(); | |
54 | 54 | |
55 | 55 | const cmdIdMapping = new Map<number, GroupMappingRecord[]>(); |
56 | 56 | ... | ... |