Commit f4797a18 authored by heduo's avatar heduo

feat(hd): 封装数据对接请求

parent d8fc6a14
......@@ -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'
......@@ -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/'
......@@ -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/'
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,
});
}
......@@ -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,
......
......@@ -3,6 +3,7 @@
*/
export enum ResultEnum {
SUCCESS = 20000,
TMALLAB_SUCCESS = 200,
ERROR = 10010,
TIMEOUT = 30013,
TYPE = 'success',
......
......@@ -10,6 +10,7 @@ export const useGlobSetting = (): Readonly<GlobConfig> => {
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<GlobConfig> => {
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<GlobConfig>;
};
......@@ -10,4 +10,4 @@ export const cacheCipher = {
};
// Whether the system cache is encrypted using aes
export const enableStorageEncryption = !isDevMode();
export const enableStorageEncryption = !isDevMode(); //该变量决定本地存储是否需要加密
......@@ -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<string>(TOKEN_KEY);
},
getTmallabToken(): string {
return getAuthCache<string>(TMALLAB_TOKEN_KEY);
},
getRoleList(): RoleEnum[] {
return this.roleList.length > 0 ? this.roleList : getAuthCache<RoleEnum[]>(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<any> {
const data = await tmallabLoginApi(params);
this.setTmallabToken(data.token);
return data;
},
async afterLoginAction(goHome?: boolean): Promise<GetUserInfoModel | null> {
if (!this.getToken) return null;
// get user info
......
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<T>(key: BasicKeys) {
const fn = isLocal ? Persistent.getLocal : Persistent.getSession;
return fn(key) as T;
......
......@@ -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;
......
......@@ -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,
};
}
......
......@@ -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<CreateAxiosOptions>) {
),
);
}
function createTmallabAxios(opt?: Partial<CreateAxiosOptions>) {
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({
......
......@@ -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;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment