基础路径配置
This commit is contained in:
parent
ba830dff60
commit
3696a8cf84
87
utils/api-base-config.js
Normal file
87
utils/api-base-config.js
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
const env = __VITE_ENV__;
|
||||||
|
|
||||||
|
export const API_CONTEXT_CACHE_KEY = "ykt_team_api_context";
|
||||||
|
|
||||||
|
export const CORP_API_BASE_URL_MAP = {
|
||||||
|
wwa54dfba0b5441ef1: "https://crm.gykqyy.com/ykt/",
|
||||||
|
};
|
||||||
|
|
||||||
|
function normalizeBaseUrl(url) {
|
||||||
|
return String(url || "").replace(/\/+$/, "");
|
||||||
|
}
|
||||||
|
|
||||||
|
function getDefaultBaseUrl() {
|
||||||
|
return normalizeBaseUrl(env.MP_API_BASE_URL);
|
||||||
|
}
|
||||||
|
|
||||||
|
function readCachedContext() {
|
||||||
|
if (typeof uni === "undefined" || typeof uni.getStorageSync !== "function") {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const cached = uni.getStorageSync(API_CONTEXT_CACHE_KEY);
|
||||||
|
if (!cached || typeof cached !== "object" || !cached.baseUrl) return null;
|
||||||
|
return {
|
||||||
|
corpId: cached.corpId || "",
|
||||||
|
baseUrl: normalizeBaseUrl(cached.baseUrl),
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function writeCachedContext(context) {
|
||||||
|
if (typeof uni === "undefined" || typeof uni.setStorageSync !== "function") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
uni.setStorageSync(API_CONTEXT_CACHE_KEY, {
|
||||||
|
corpId: context.corpId || "",
|
||||||
|
baseUrl: normalizeBaseUrl(context.baseUrl),
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
// 缓存失败不能阻断接口请求。
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function createContext(corpId, baseUrl) {
|
||||||
|
return {
|
||||||
|
corpId: corpId || "",
|
||||||
|
baseUrl: normalizeBaseUrl(baseUrl),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function resolveApiContext(corpId) {
|
||||||
|
const normalizedCorpId = String(corpId || "").trim();
|
||||||
|
|
||||||
|
if (normalizedCorpId) {
|
||||||
|
const configuredBaseUrl = CORP_API_BASE_URL_MAP[normalizedCorpId];
|
||||||
|
const context = createContext(
|
||||||
|
normalizedCorpId,
|
||||||
|
configuredBaseUrl || getDefaultBaseUrl()
|
||||||
|
);
|
||||||
|
writeCachedContext(context);
|
||||||
|
return context;
|
||||||
|
}
|
||||||
|
|
||||||
|
const cachedContext = readCachedContext();
|
||||||
|
if (cachedContext) return cachedContext;
|
||||||
|
|
||||||
|
const context = createContext("", getDefaultBaseUrl());
|
||||||
|
writeCachedContext(context);
|
||||||
|
return context;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getApiBaseUrlByCorpId(corpId) {
|
||||||
|
return resolveApiContext(corpId).baseUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getDefaultUploadUrl() {
|
||||||
|
return `${getDefaultBaseUrl()}/upload`;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function buildApiUrl(path, corpId) {
|
||||||
|
const { baseUrl } = resolveApiContext(corpId);
|
||||||
|
const normalizedPath = String(path || "").replace(/^\/+/, "");
|
||||||
|
return normalizedPath ? `${baseUrl}/${normalizedPath}` : baseUrl;
|
||||||
|
}
|
||||||
@ -1,6 +1,5 @@
|
|||||||
import { loading, hideLoading } from "./widget";
|
import { loading, hideLoading } from "./widget";
|
||||||
const env = __VITE_ENV__;
|
import { buildApiUrl, getDefaultUploadUrl } from "./api-base-config";
|
||||||
const baseUrl = env.MP_API_BASE_URL;
|
|
||||||
|
|
||||||
const defaultOptions = {
|
const defaultOptions = {
|
||||||
header: {
|
header: {
|
||||||
@ -58,7 +57,7 @@ async function refreshAccessToken() {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const res = await uni.request({
|
const res = await uni.request({
|
||||||
url: `${baseUrl}/auth/refresh`,
|
url: buildApiUrl("/auth/refresh"),
|
||||||
method: "POST",
|
method: "POST",
|
||||||
header: {
|
header: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
@ -101,7 +100,7 @@ const request = async (options = {}, showLoading = true) => {
|
|||||||
const config = {
|
const config = {
|
||||||
...defaultOptions,
|
...defaultOptions,
|
||||||
...options,
|
...options,
|
||||||
url: baseUrl + options.url,
|
url: buildApiUrl(options.url, options.data.corpId),
|
||||||
};
|
};
|
||||||
|
|
||||||
// 添加 token 到请求头
|
// 添加 token 到请求头
|
||||||
@ -199,24 +198,25 @@ function removeTask(task) {
|
|||||||
|
|
||||||
export default request;
|
export default request;
|
||||||
|
|
||||||
export const uploadUrl = `${baseUrl}/upload`;
|
export const uploadUrl = getDefaultUploadUrl();
|
||||||
|
|
||||||
export function getFullPath(path) {
|
export function getFullPath(path, corpId) {
|
||||||
return `${baseUrl}/${path}`;
|
return buildApiUrl(path, corpId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export function upload(path) {
|
export function upload(path, corpId) {
|
||||||
|
const currentUploadUrl = buildApiUrl("/upload", corpId);
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
uni.uploadFile({
|
uni.uploadFile({
|
||||||
url: uploadUrl, // 替换为你的上传接口地址
|
url: currentUploadUrl,
|
||||||
filePath: path,
|
filePath: path,
|
||||||
name: 'file',
|
name: 'file',
|
||||||
fileType: 'image',
|
fileType: 'image',
|
||||||
success: (res) => {
|
success: (res) => {
|
||||||
try {
|
try {
|
||||||
const url = JSON.parse(res.data).filePath;
|
const url = JSON.parse(res.data).filePath;
|
||||||
resolve(url ? getFullPath(url) : '')
|
resolve(url ? getFullPath(url, corpId) : '')
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
resolve()
|
resolve()
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user