|
|
@@ -63,6 +63,15 @@ export function request({
|
|
|
timeout = 15000,
|
|
|
}) {
|
|
|
return new Promise((resolve, reject) => {
|
|
|
+ if (!url) {
|
|
|
+ reject(new Error("请求路径不能为空"));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 相对路径由 request() 自动拼接 baseUrl;绝对 URL(http(s):// 或 //host)直通
|
|
|
+ const finalUrl =
|
|
|
+ /^https?:\/\//i.test(url) || url.startsWith("//")
|
|
|
+ ? url
|
|
|
+ : `${getApiBaseUrl()}${url}`;
|
|
|
// 未显式传 token 时自动从登录态读取,登录后所有请求自动带上凭证
|
|
|
let effectiveToken = token;
|
|
|
let effectiveSessionId = sessionId;
|
|
|
@@ -81,7 +90,7 @@ export function request({
|
|
|
: { "content-type": "application/json", platform: "wxapp" };
|
|
|
|
|
|
uni.request({
|
|
|
- url,
|
|
|
+ url: finalUrl,
|
|
|
method,
|
|
|
data,
|
|
|
header,
|
|
|
@@ -140,6 +149,15 @@ export function uploadFile({
|
|
|
reject(new Error("缺少文件路径"));
|
|
|
return;
|
|
|
}
|
|
|
+ if (!url) {
|
|
|
+ reject(new Error("请求路径不能为空"));
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 相对路径由 uploadFile() 自动拼接 baseUrl;绝对 URL(http(s):// 或 //host)直通
|
|
|
+ const finalUrl =
|
|
|
+ /^https?:\/\//i.test(url) || url.startsWith("//")
|
|
|
+ ? url
|
|
|
+ : `${getApiBaseUrl()}${url}`;
|
|
|
const { token, sessionId } = getAuthCredentials();
|
|
|
const header = token
|
|
|
? {
|
|
|
@@ -150,7 +168,7 @@ export function uploadFile({
|
|
|
: { platform: "wxapp" };
|
|
|
|
|
|
uni.uploadFile({
|
|
|
- url,
|
|
|
+ url: finalUrl,
|
|
|
filePath,
|
|
|
name,
|
|
|
formData,
|
|
|
@@ -210,10 +228,8 @@ export async function testServerConnection(config) {
|
|
|
.replace(/\/$/, ""),
|
|
|
port: String(config.port || "").trim(),
|
|
|
};
|
|
|
- const baseUrl = getApiBaseUrl(normalized);
|
|
|
- if (!baseUrl) throw new Error("请填写服务器地址");
|
|
|
await request({
|
|
|
- url: `${baseUrl}/main/connection/getConnectionTest`,
|
|
|
+ url: `/main/connection/getConnectionTest`,
|
|
|
timeout: 5000,
|
|
|
});
|
|
|
return true;
|
|
|
@@ -231,9 +247,9 @@ function collectAuthorities(tree) {
|
|
|
return authorities;
|
|
|
}
|
|
|
|
|
|
-async function loadPermissionTree(baseUrl) {
|
|
|
+async function loadPermissionTree() {
|
|
|
const response = await request({
|
|
|
- url: `${baseUrl}/system/resources/getResourcesTreePDA`,
|
|
|
+ url: "/system/resources/getResourcesTreePDA",
|
|
|
});
|
|
|
const tree = Array.isArray(response.data) ? response.data : [];
|
|
|
savePermissionData(tree, collectAuthorities(tree));
|
|
|
@@ -241,10 +257,9 @@ async function loadPermissionTree(baseUrl) {
|
|
|
}
|
|
|
|
|
|
// 拉取当前登录人的扩展资料(部门、岗位、角色等),登录后调用一次写入本地
|
|
|
-export async function fetchCurrentUser(baseUrl) {
|
|
|
- if (!baseUrl) throw new Error("缺少 baseUrl");
|
|
|
+export async function fetchCurrentUser() {
|
|
|
const response = await request({
|
|
|
- url: `${baseUrl}/system/account/getLoginUser`,
|
|
|
+ url: "/system/account/getLoginUser",
|
|
|
});
|
|
|
return response.data || null;
|
|
|
}
|
|
|
@@ -254,11 +269,10 @@ export async function fetchCurrentUser(baseUrl) {
|
|
|
// profile 响应把用户数据放在 user 子对象里,内部摊平到顶层
|
|
|
// 岗位名称不在 profile 直接返回,而是 postId(逗号分隔的 id 串),
|
|
|
// 内部再调 /hr/position/page 用 id 匹配出 positionName 后回填到返回对象的 position 字段
|
|
|
-export async function fetchUserDetail(baseUrl, userId) {
|
|
|
- if (!baseUrl) throw new Error("缺少 baseUrl");
|
|
|
+export async function fetchUserDetail(userId) {
|
|
|
if (!userId && userId !== 0) throw new Error("缺少用户ID");
|
|
|
const response = await request({
|
|
|
- url: `${baseUrl}/main/users/${userId}/profile`,
|
|
|
+ url: `/main/users/${userId}/profile`,
|
|
|
});
|
|
|
const data = response.data || null;
|
|
|
if (!data) return null;
|
|
|
@@ -268,7 +282,7 @@ export async function fetchUserDetail(baseUrl, userId) {
|
|
|
? { ...data, ...data.user }
|
|
|
: data;
|
|
|
try {
|
|
|
- const names = await resolvePostIds(baseUrl, profile.postId);
|
|
|
+ const names = await resolvePostIds(profile.postId);
|
|
|
if (names) profile.position = names;
|
|
|
} catch (error) {
|
|
|
// 岗位解析失败不影响其他字段
|
|
|
@@ -277,7 +291,7 @@ export async function fetchUserDetail(baseUrl, userId) {
|
|
|
}
|
|
|
|
|
|
// 把 profile.postId("1111,2222")解析成岗位名称字符串,用 / 拼接多个岗位
|
|
|
-async function resolvePostIds(baseUrl, postIdString) {
|
|
|
+async function resolvePostIds(postIdString) {
|
|
|
if (!postIdString) return "";
|
|
|
const ids = String(postIdString)
|
|
|
.split(",")
|
|
|
@@ -285,7 +299,7 @@ async function resolvePostIds(baseUrl, postIdString) {
|
|
|
.filter(Boolean);
|
|
|
if (!ids.length) return "";
|
|
|
const response = await request({
|
|
|
- url: `${baseUrl}/hr/position/page`,
|
|
|
+ url: "/hr/position/page",
|
|
|
method: "POST",
|
|
|
data: { pageNum: 1, size: 1000, status: 1 },
|
|
|
});
|
|
|
@@ -334,10 +348,8 @@ export async function performLogin({ account, password, rememberPassword }) {
|
|
|
return data;
|
|
|
}
|
|
|
|
|
|
- const baseUrl = getApiBaseUrl(config);
|
|
|
- if (!baseUrl) throw new Error("请先配置服务器地址");
|
|
|
const response = await request({
|
|
|
- url: `${baseUrl}/main/user/login`,
|
|
|
+ url: `/main/user/login`,
|
|
|
method: "POST",
|
|
|
data: { loginName, loginPwd },
|
|
|
});
|
|
|
@@ -351,7 +363,7 @@ export async function performLogin({ account, password, rememberPassword }) {
|
|
|
rememberPassword,
|
|
|
});
|
|
|
try {
|
|
|
- await loadPermissionTree(baseUrl);
|
|
|
+ await loadPermissionTree();
|
|
|
} catch (error) {
|
|
|
savePermissionData([], []);
|
|
|
uni.showToast({ title: error.message || "权限加载失败", icon: "none" });
|
|
|
@@ -359,8 +371,8 @@ export async function performLogin({ account, password, rememberPassword }) {
|
|
|
try {
|
|
|
// 部门/岗位在 getLoginUser 里没有,单独调 getById 拿,再合并
|
|
|
const [basic, detail] = await Promise.allSettled([
|
|
|
- fetchCurrentUser(baseUrl),
|
|
|
- fetchUserDetail(baseUrl, data.userId),
|
|
|
+ fetchCurrentUser(),
|
|
|
+ fetchUserDetail(data.userId),
|
|
|
]);
|
|
|
const basicData = basic.status === "fulfilled" ? basic.value : null;
|
|
|
const detailData = detail.status === "fulfilled" ? detail.value : null;
|
|
|
@@ -383,10 +395,8 @@ export async function performLogin({ account, password, rememberPassword }) {
|
|
|
|
|
|
export async function getCompanyBranding(config = getServerConfig()) {
|
|
|
if (config.mode !== "server") return null;
|
|
|
- const baseUrl = getApiBaseUrl(config);
|
|
|
- if (!baseUrl) return null;
|
|
|
const response = await request({
|
|
|
- url: `${baseUrl}/pda/mes/us/indexName`,
|
|
|
+ url: `/pda/mes/us/indexName`,
|
|
|
timeout: 5000,
|
|
|
});
|
|
|
return response.data || null;
|
|
|
@@ -396,11 +406,9 @@ export async function getCompanyBranding(config = getServerConfig()) {
|
|
|
// 演示模式或未配置服务器时直接返回(不抛错),由调用方继续清理本地
|
|
|
export async function serverLogout() {
|
|
|
if (getServerConfig().mode !== "server") return;
|
|
|
- const baseUrl = getApiBaseUrl();
|
|
|
- if (!baseUrl) return;
|
|
|
try {
|
|
|
await request({
|
|
|
- url: `${baseUrl}/main/user/logout`,
|
|
|
+ url: `/main/user/logout`,
|
|
|
method: "POST",
|
|
|
timeout: 5000,
|
|
|
});
|