1
This commit is contained in:
119
composables/configState.ts
Normal file
119
composables/configState.ts
Normal file
@@ -0,0 +1,119 @@
|
||||
import {useState} from '#imports';
|
||||
import type {CmsWebsite} from "~/api/cms/cmsWebsite/model";
|
||||
import type {CmsNavigation} from "~/api/cms/cmsNavigation/model";
|
||||
import type {User} from "~/api/system/user/model";
|
||||
import type {Company} from "~/api/system/company/model";
|
||||
import type {Layout} from "~/api/layout/model";
|
||||
import type {Config} from "~/types/global";
|
||||
import type {CmsWebsiteField} from "~/api/cms/cmsWebsiteField/model";
|
||||
import type {CmsWebsiteSetting} from "~/api/cms/cmsWebsiteSetting/model";
|
||||
|
||||
// 网站信息
|
||||
export const useWebsite = () => useState<CmsWebsite>('website', undefined);
|
||||
|
||||
// 网站设置
|
||||
export const useSetting = () =>
|
||||
useState<CmsWebsiteSetting>('setting', () => {
|
||||
return {};
|
||||
});
|
||||
|
||||
// 扩展字段
|
||||
export const useConfigInfo = () =>
|
||||
useState<Config>('config', () => {
|
||||
return {
|
||||
productParentId: undefined,
|
||||
searchCategoryId: undefined
|
||||
};
|
||||
});
|
||||
|
||||
// 主导航
|
||||
export const useMenu = () =>
|
||||
useState<CmsNavigation[]>('menu', () => {
|
||||
return [];
|
||||
});
|
||||
|
||||
// 副导航
|
||||
export const useSubMenu = () =>
|
||||
useState<CmsNavigation[]>('subMenu', () => {
|
||||
return [];
|
||||
});
|
||||
|
||||
// 页面元素
|
||||
export const usePage = () => useState<CmsNavigation>('page', () => {
|
||||
return {};
|
||||
});
|
||||
|
||||
|
||||
// 页面布局
|
||||
export const useLayout = () => useState<Layout>('layout', () => {
|
||||
return {
|
||||
showBanner: true
|
||||
}
|
||||
});
|
||||
|
||||
// 固钉
|
||||
export const useProductAffix = () =>
|
||||
useState<boolean>('affixTop', () => {
|
||||
return false;
|
||||
});
|
||||
// 后台管理域名
|
||||
export const useSysDomain = () => useState('SysDomain', () => '');
|
||||
|
||||
// 登录凭证
|
||||
export const useToken = () => useState('token', () => {
|
||||
if(localStorage.getItem('token')){
|
||||
return localStorage.getItem('token')
|
||||
}
|
||||
return ''
|
||||
});
|
||||
|
||||
// 用户信息
|
||||
export const useUser = () =>
|
||||
useState<User>('user', () => {
|
||||
return {
|
||||
userId: 0,
|
||||
avatar: '',
|
||||
phone: '',
|
||||
balance: 0,
|
||||
nickname: '',
|
||||
gradeId: 0,
|
||||
gradeName: '',
|
||||
certification: false,
|
||||
tenantId: 0,
|
||||
tenantName: '',
|
||||
};
|
||||
});
|
||||
|
||||
// 企业信息
|
||||
export const useCompany = () =>
|
||||
useState<Company>('company', () => {
|
||||
return {};
|
||||
});
|
||||
|
||||
// 是否显示登录弹窗
|
||||
export const useShowLogin = () => useState('showLogin', () => false);
|
||||
|
||||
// 登录凭证
|
||||
export const useIsMobile = () => useState('isMobile', () => false);
|
||||
|
||||
// 购物车状态
|
||||
export const useCart = () => useState<any>('cart', () => {
|
||||
return {
|
||||
num: 1,
|
||||
type: 1,
|
||||
payType: 102,
|
||||
payPrice: 0,
|
||||
month: 1,
|
||||
comments: '',
|
||||
list: [],
|
||||
totalPrice: 0
|
||||
};
|
||||
});
|
||||
|
||||
export const useLogo = () => useState<CmsWebsiteField>('logo', () => {
|
||||
return {
|
||||
siteName: '',
|
||||
siteLogo: undefined,
|
||||
value: '',
|
||||
};
|
||||
})
|
||||
45
composables/useClientRequest.ts
Normal file
45
composables/useClientRequest.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import {getBaseUrl, isArray} from '~/utils/tool';
|
||||
import {MODULES_API_URL, TENANT_ID} from "~/config";
|
||||
|
||||
type FetchType = typeof $fetch;
|
||||
export type FetchOptions = Parameters<FetchType>[1];
|
||||
|
||||
export const useClientRequest = <T = unknown>(url: string, opts?: FetchOptions) => {
|
||||
// 配置信息
|
||||
const runtimeConfig = useRuntimeConfig();
|
||||
// 请求接口
|
||||
const baseUrl = ref('');
|
||||
baseUrl.value = runtimeConfig.public.apiServer;
|
||||
// 开发环境
|
||||
if(process.env.NODE_ENV === 'development'){
|
||||
baseUrl.value = `http://127.0.0.1:9002/api`
|
||||
}
|
||||
// 登录凭证token
|
||||
const token = useToken();
|
||||
const _token = localStorage.getItem('token');
|
||||
if(_token){
|
||||
token.value = _token
|
||||
}
|
||||
|
||||
const defaultOptions: FetchOptions = {
|
||||
baseURL: baseUrl.value,
|
||||
onRequest({ options }) {
|
||||
// @ts-ignore
|
||||
options.headers = (options.headers || {}) as { [key: string]: string };
|
||||
if (token.value) {
|
||||
// @ts-ignore
|
||||
options.headers.Authorization = token.value;
|
||||
}
|
||||
},
|
||||
onResponse({ response }) {
|
||||
if (+response.status === 0 && +response._data.code !== 0) {
|
||||
ElMessage.error(response._data.message);
|
||||
}
|
||||
},
|
||||
onResponseError({ response }) {
|
||||
ElMessage.error(isArray(response._data.data.message) ? response._data.data.message[0] : response._data.data.message);
|
||||
}
|
||||
};
|
||||
|
||||
return $fetch<T>(runtimeConfig.public.apiServer + url, { ...defaultOptions, ...opts });
|
||||
};
|
||||
71
composables/useServerRequest.ts
Normal file
71
composables/useServerRequest.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
/**
|
||||
* 参考文档
|
||||
* https://blog.csdn.net/m0_63281537/article/details/126699761
|
||||
*/
|
||||
|
||||
import { useFetch } from '#app';
|
||||
import type { UseFetchOptions } from '#app';
|
||||
import {isArray} from '~/utils/tool';
|
||||
import {isInteger} from "~/utils/common";
|
||||
|
||||
export const useServerRequest = <T>(url: string, opts?: UseFetchOptions<T, unknown>) => {
|
||||
// 配置信息
|
||||
const runtimeConfig = useRuntimeConfig();
|
||||
// 请求接口
|
||||
const baseUrl = ref('');
|
||||
baseUrl.value = runtimeConfig.public.apiServer;
|
||||
// 开发环境
|
||||
if(process.env.NODE_ENV === 'development'){
|
||||
// baseUrl.value = `http://127.0.0.1:9002/api`
|
||||
}
|
||||
// 登录凭证token
|
||||
const token = useToken();
|
||||
const _token = localStorage.getItem('token');
|
||||
if(_token){
|
||||
token.value = _token
|
||||
}
|
||||
const defaultOptions: UseFetchOptions<unknown> = {
|
||||
baseURL: baseUrl.value,
|
||||
onRequest({ options }) {
|
||||
// @ts-ignore
|
||||
options.headers = (options.headers || {}) as { [key: string]: string };
|
||||
// TODO 0 默认租户ID
|
||||
if(process.env.NODE_ENV === 'development') {
|
||||
// @ts-ignore
|
||||
options.headers.tenantid = `${runtimeConfig.public.tenantId}`;
|
||||
}
|
||||
if (token.value) {
|
||||
// @ts-ignore
|
||||
options.headers.Authorization = token.value;
|
||||
}
|
||||
// TODO 1 从二级域名解构租户ID
|
||||
const domain = window.location.hostname;
|
||||
const parts = domain.split('.');
|
||||
const subDomain = parts[0];
|
||||
if (isInteger(subDomain)) {
|
||||
console.log(`${subDomain}`)
|
||||
// options.headers.tenantid = `${subDomain}`;
|
||||
}
|
||||
// TODO 2 从绑定域名解构的租户ID
|
||||
if(localStorage.getItem('TenantId')){
|
||||
// @ts-ignore
|
||||
options.headers.tenantid = `${localStorage.getItem('TenantId')}`;
|
||||
}
|
||||
},
|
||||
onResponse({ response }) {
|
||||
if (+response.status === 0 && +response._data.code !== 0) {
|
||||
process.client && ElMessage.error(response._data.message);
|
||||
}
|
||||
if(+response.status === 500){
|
||||
ElMessage.error('网络请求错误')
|
||||
}
|
||||
return response._data.data;
|
||||
},
|
||||
onResponseError({ response }) {
|
||||
process.client &&
|
||||
ElMessage.error(isArray(response._data.data.message) ? response._data.data.message[0] : response._data.data.message);
|
||||
}
|
||||
};
|
||||
// console.log('请求接口:', baseUrl.value+url)
|
||||
return useFetch<T>(url, { ...defaultOptions, ...opts } as any);
|
||||
};
|
||||
Reference in New Issue
Block a user