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