From f4797a18cd834de515a7af67df0940a3475f0cf7 Mon Sep 17 00:00:00 2001 From: heduo Date: Fri, 29 Mar 2024 16:31:04 +0800 Subject: [PATCH] =?UTF-8?q?feat(hd):=20=E5=B0=81=E8=A3=85=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=AF=B9=E6=8E=A5=E8=AF=B7=E6=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.development | 3 ++ .env.production | 3 ++ .env.test | 3 ++ src/api/sys/user.ts | 10 ++++- src/enums/cacheEnum.ts | 2 + src/enums/httpEnum.ts | 1 + src/hooks/setting/index.ts | 2 + src/settings/encryptionSetting.ts | 2 +- src/store/modules/user.ts | 16 +++++++- src/utils/auth/index.ts | 6 ++- src/utils/cache/persistent.ts | 2 + src/utils/env.ts | 2 + src/utils/http/axios/index.ts | 65 +++++++++++++++++++++++++++++-- types/config.d.ts | 4 ++ 14 files changed, 111 insertions(+), 10 deletions(-) diff --git a/.env.development b/.env.development index 449b215..97a6b54 100644 --- a/.env.development +++ b/.env.development @@ -20,3 +20,6 @@ VITE_GLOB_UPLOAD_URL=/attach # Interface prefix VITE_GLOB_API_URL_PREFIX= + +#tmallab url +VITE_GLOB_TMALLAB_URL='http://8.130.74.127:8088/tmallab2022j' diff --git a/.env.production b/.env.production index 5301234..edcba1d 100644 --- a/.env.production +++ b/.env.production @@ -31,3 +31,6 @@ VITE_USE_PWA = false # Is it compatible with older browsers VITE_LEGACY = false + +#tmallab url +VITE_GLOB_TMALLAB_URL='https://www.tmallab.cn/tmallab2022j/' diff --git a/.env.test b/.env.test index 469b307..6abeaf4 100644 --- a/.env.test +++ b/.env.test @@ -34,3 +34,6 @@ VITE_USE_PWA = false # Is it compatible with older browsers VITE_LEGACY = false + +#tmallab url +VITE_GLOB_TMALLAB_URL='http://8.130.74.127:8088/tmallab2022j/' diff --git a/src/api/sys/user.ts b/src/api/sys/user.ts index 848a890..39b3121 100644 --- a/src/api/sys/user.ts +++ b/src/api/sys/user.ts @@ -1,4 +1,4 @@ -import { defHttp } from '/@/utils/http/axios'; +import { defHttp, tmallabHttp } from '/@/utils/http/axios'; import { LoginParams, LoginResultModel, GetUserInfoModel } from './model/userModel'; import { ErrorMessageMode } from '/#/axios'; @@ -8,6 +8,7 @@ enum Api { Logout = '/login/logout', GetUserInfo = '/login/detail', GetPermCode = '/getPermCode', + tmallabLoginApi = '/system/login', } /** @@ -39,3 +40,10 @@ export function getPermCode() { export function doLogout() { return defHttp.post({ url: Api.Logout }); } + +export function tmallabLoginApi(params: any) { + return tmallabHttp.post({ + url: Api.tmallabLoginApi, + params, + }); +} diff --git a/src/enums/cacheEnum.ts b/src/enums/cacheEnum.ts index 4a5cc11..efc440f 100644 --- a/src/enums/cacheEnum.ts +++ b/src/enums/cacheEnum.ts @@ -25,6 +25,8 @@ export const APP_LOCAL_CACHE_KEY = 'COMMON__LOCAL__KEY__'; // base global session key export const APP_SESSION_CACHE_KEY = 'COMMON__SESSION__KEY__'; +export const TMALLAB_TOKEN_KEY = 'TMALLAB__TOKEN__'; + export enum CacheTypeEnum { SESSION, LOCAL, diff --git a/src/enums/httpEnum.ts b/src/enums/httpEnum.ts index c797dc8..2209917 100644 --- a/src/enums/httpEnum.ts +++ b/src/enums/httpEnum.ts @@ -3,6 +3,7 @@ */ export enum ResultEnum { SUCCESS = 20000, + TMALLAB_SUCCESS = 200, ERROR = 10010, TIMEOUT = 30013, TYPE = 'success', diff --git a/src/hooks/setting/index.ts b/src/hooks/setting/index.ts index 56e393f..ad574b3 100644 --- a/src/hooks/setting/index.ts +++ b/src/hooks/setting/index.ts @@ -10,6 +10,7 @@ export const useGlobSetting = (): Readonly => { VITE_GLOB_APP_SHORT_NAME, VITE_GLOB_API_URL_PREFIX, VITE_GLOB_UPLOAD_URL, + VITE_GLOB_TMALLAB_URL, } = getAppEnvConfig(); if (!/[a-zA-Z\_]*/.test(VITE_GLOB_APP_SHORT_NAME)) { @@ -25,6 +26,7 @@ export const useGlobSetting = (): Readonly => { shortName: VITE_GLOB_APP_SHORT_NAME, urlPrefix: VITE_GLOB_API_URL_PREFIX, uploadUrl: VITE_GLOB_UPLOAD_URL, + tmallabUrl: VITE_GLOB_TMALLAB_URL, }; return glob as Readonly; }; diff --git a/src/settings/encryptionSetting.ts b/src/settings/encryptionSetting.ts index 3d61776..8218829 100644 --- a/src/settings/encryptionSetting.ts +++ b/src/settings/encryptionSetting.ts @@ -10,4 +10,4 @@ export const cacheCipher = { }; // Whether the system cache is encrypted using aes -export const enableStorageEncryption = !isDevMode(); +export const enableStorageEncryption = !isDevMode(); //该变量决定本地存储是否需要加密 diff --git a/src/store/modules/user.ts b/src/store/modules/user.ts index eee4ff7..26e6619 100644 --- a/src/store/modules/user.ts +++ b/src/store/modules/user.ts @@ -4,10 +4,10 @@ import { defineStore } from 'pinia'; import { store } from '/@/store'; import { RoleEnum } from '/@/enums/roleEnum'; import { PageEnum } from '/@/enums/pageEnum'; -import { ROLES_KEY, TOKEN_KEY, USER_INFO_KEY } from '/@/enums/cacheEnum'; +import { ROLES_KEY, TOKEN_KEY, USER_INFO_KEY, TMALLAB_TOKEN_KEY } from '/@/enums/cacheEnum'; import { getAuthCache, setAuthCache } from '/@/utils/auth'; import { GetUserInfoModel, LoginParams } from '/@/api/sys/model/userModel'; -import { doLogout, getUserInfo, loginApi } from '/@/api/sys/user'; +import { doLogout, getUserInfo, loginApi, tmallabLoginApi } from '/@/api/sys/user'; import { useI18n } from '/@/hooks/web/useI18n'; import { useMessage } from '/@/hooks/web/useMessage'; import { router } from '/@/router'; @@ -54,6 +54,9 @@ export const useUserStore = defineStore({ getToken(): string { return this.token || getAuthCache(TOKEN_KEY); }, + getTmallabToken(): string { + return getAuthCache(TMALLAB_TOKEN_KEY); + }, getRoleList(): RoleEnum[] { return this.roleList.length > 0 ? this.roleList : getAuthCache(ROLES_KEY); }, @@ -69,6 +72,9 @@ export const useUserStore = defineStore({ this.token = info ? info : ''; // for null or undefined value setAuthCache(TOKEN_KEY, info); }, + setTmallabToken(info: string | undefined) { + setAuthCache(TMALLAB_TOKEN_KEY, info); + }, setRoleList(roleList: RoleEnum[]) { this.roleList = roleList; setAuthCache(ROLES_KEY, roleList); @@ -111,6 +117,12 @@ export const useUserStore = defineStore({ return Promise.reject(error); } }, + //数据对接模拟登录 + async tmallabSimulationLogin(params: { loginName: string; password: string }): Promise { + const data = await tmallabLoginApi(params); + this.setTmallabToken(data.token); + return data; + }, async afterLoginAction(goHome?: boolean): Promise { if (!this.getToken) return null; // get user info diff --git a/src/utils/auth/index.ts b/src/utils/auth/index.ts index fdec836..2de173b 100644 --- a/src/utils/auth/index.ts +++ b/src/utils/auth/index.ts @@ -1,7 +1,7 @@ import { Persistent, BasicKeys } from '/@/utils/cache/persistent'; import { CacheTypeEnum } from '/@/enums/cacheEnum'; import projectSetting from '/@/settings/projectSetting'; -import { TOKEN_KEY } from '/@/enums/cacheEnum'; +import { TOKEN_KEY, TMALLAB_TOKEN_KEY } from '/@/enums/cacheEnum'; const { permissionCacheType } = projectSetting; const isLocal = permissionCacheType === CacheTypeEnum.LOCAL; @@ -9,7 +9,9 @@ const isLocal = permissionCacheType === CacheTypeEnum.LOCAL; export function getToken() { return getAuthCache(TOKEN_KEY); } - +export function getTmallabToken() { + return getAuthCache(TMALLAB_TOKEN_KEY) || ''; +} export function getAuthCache(key: BasicKeys) { const fn = isLocal ? Persistent.getLocal : Persistent.getSession; return fn(key) as T; diff --git a/src/utils/cache/persistent.ts b/src/utils/cache/persistent.ts index cd68a36..a665431 100644 --- a/src/utils/cache/persistent.ts +++ b/src/utils/cache/persistent.ts @@ -13,6 +13,7 @@ import { APP_LOCAL_CACHE_KEY, APP_SESSION_CACHE_KEY, MULTIPLE_TABS_KEY, + TMALLAB_TOKEN_KEY, } from '/@/enums/cacheEnum'; import { DEFAULT_CACHE_TIME } from '/@/settings/encryptionSetting'; import { toRaw } from 'vue'; @@ -20,6 +21,7 @@ import { pick, omit } from 'lodash-es'; interface BasicStore { [TOKEN_KEY]: string | number | null | undefined; + [TMALLAB_TOKEN_KEY]: string | number | null | undefined; [USER_INFO_KEY]: UserInfo; [ROLES_KEY]: string[]; [LOCK_INFO_KEY]: LockInfo; diff --git a/src/utils/env.ts b/src/utils/env.ts index 408398a..8595fb6 100644 --- a/src/utils/env.ts +++ b/src/utils/env.ts @@ -28,6 +28,7 @@ export function getAppEnvConfig() { VITE_GLOB_APP_SHORT_NAME, VITE_GLOB_API_URL_PREFIX, VITE_GLOB_UPLOAD_URL, + VITE_GLOB_TMALLAB_URL, } = ENV; if (!/^[a-zA-Z\_]*$/.test(VITE_GLOB_APP_SHORT_NAME)) { @@ -42,6 +43,7 @@ export function getAppEnvConfig() { VITE_GLOB_APP_SHORT_NAME, VITE_GLOB_API_URL_PREFIX, VITE_GLOB_UPLOAD_URL, + VITE_GLOB_TMALLAB_URL, }; } diff --git a/src/utils/http/axios/index.ts b/src/utils/http/axios/index.ts index 60328e7..9f7d396 100644 --- a/src/utils/http/axios/index.ts +++ b/src/utils/http/axios/index.ts @@ -10,7 +10,7 @@ import { useGlobSetting } from '/@/hooks/setting'; import { useMessage } from '/@/hooks/web/useMessage'; import { RequestEnum, ResultEnum, ContentTypeEnum } from '/@/enums/httpEnum'; import { isString } from '/@/utils/is'; -import { getToken } from '/@/utils/auth'; +import { getToken, getTmallabToken } from '/@/utils/auth'; import { setObjToUrlParams, deepMerge } from '/@/utils'; import { useErrorLogStoreWithOut } from '/@/store/modules/errorLog'; import { useI18n } from '/@/hooks/web/useI18n'; @@ -43,7 +43,7 @@ const transform: AxiosTransform = { // 错误的时候返回 const { data } = res; - console.log('=============', res); + // console.log('=============', res); if (!data) { // return '[HTTP] Request has no return value'; throw new Error(t('sys.api.apiRequestFailed')); @@ -52,7 +52,9 @@ const transform: AxiosTransform = { const { code, data: result, msg: message } = data; // 这里逻辑可以根据项目进行修改 - const hasSuccess = data && Reflect.has(data, 'code') && code === ResultEnum.SUCCESS; + const hasSuccess = + (data && Reflect.has(data, 'code') && code === ResultEnum.SUCCESS) || + ResultEnum.TMALLAB_SUCCESS; if (hasSuccess) { return result; } @@ -80,7 +82,7 @@ const transform: AxiosTransform = { } else if (options.errorMessageMode === 'message') { createMessage.error(timeoutMsg); } - console.log('=============22222', timeoutMsg, options); + // console.log('=============22222', timeoutMsg, options); throw new Error(timeoutMsg || t('sys.api.apiRequestFailed')); }, @@ -139,12 +141,17 @@ const transform: AxiosTransform = { requestInterceptors: (config, options) => { // 请求之前处理config const token = getToken(); + const tmallabToken = getTmallabToken(); if (token && (config as Recordable)?.requestOptions?.withToken !== false) { // jwt token (config as Recordable).headers.Authorization = options.authenticationScheme ? `${options.authenticationScheme} ${token}` : token; } + if (tmallabToken && (config as Recordable)?.requestOptions?.withTmallabToken !== false) { + // jwt token + (config as Recordable).headers.Token = tmallabToken; + } return config; }, @@ -240,7 +247,57 @@ function createAxios(opt?: Partial) { ), ); } +function createTmallabAxios(opt?: Partial) { + return new VAxios( + deepMerge( + { + // See https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication#authentication_schemes + // authentication schemes,e.g: Bearer + // authenticationScheme: 'Bearer', + authenticationScheme: '', + timeout: 0, + // 基础接口地址 + // baseURL: globSetting.apiUrl, + + headers: { 'Content-Type': ContentTypeEnum.JSON }, + // 如果是form-data格式 + // headers: { 'Content-Type': ContentTypeEnum.FORM_URLENCODED }, + // 数据处理方式 + transform, + // 配置项,下面的选项都可以在独立的接口请求中覆盖 + requestOptions: { + // 默认将prefix 添加到url + joinPrefix: true, + // 是否返回原生响应头 比如:需要获取响应头时使用该属性 + isReturnNativeResponse: false, + // 需要对返回数据进行处理 + isTransformResponse: true, + // post请求的时候添加参数到url + joinParamsToUrl: false, + // 格式化提交参数时间 + formatDate: true, + // 消息提示类型 + // errorMessageMode: 'message', + // 接口地址 + apiUrl: globSetting.tmallabUrl, + // 接口拼接地址 + urlPrefix: urlPrefix, + // 是否加入时间戳 + joinTime: false, + // 忽略重复请求 + ignoreCancelToken: true, + // 是否携带token + withToken: false, + //是否携带数据对接token + withTmallabToken: true, + }, + }, + opt || {}, + ), + ); +} export const defHttp = createAxios(); +export const tmallabHttp = createTmallabAxios(); // other api url // export const otherHttp = createAxios({ diff --git a/types/config.d.ts b/types/config.d.ts index cb5a5a9..6465a6c 100644 --- a/types/config.d.ts +++ b/types/config.d.ts @@ -146,6 +146,8 @@ export interface GlobConfig { urlPrefix?: string; // Project abbreviation shortName: string; + //tmallab url + tmallabUrl: string; } export interface GlobEnvConfig { // Site title @@ -158,4 +160,6 @@ export interface GlobEnvConfig { VITE_GLOB_APP_SHORT_NAME: string; // Upload url VITE_GLOB_UPLOAD_URL?: string; + //tmallab url + VITE_GLOB_TMALLAB_URL: string; } -- 2.24.1