refactor(mobile): 移动端上传/展示去 IP 化,统一公网域名并支持 NODE_ENV 环境感知
This commit is contained in:
@ -81,6 +81,8 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
|
import { getBaseUrl } from "../../../utils/request";
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: "TaskTreeNode",
|
name: "TaskTreeNode",
|
||||||
props: {
|
props: {
|
||||||
@ -118,8 +120,8 @@ export default {
|
|||||||
if (!url) return "";
|
if (!url) return "";
|
||||||
if (url.startsWith("http")) return url;
|
if (url.startsWith("http")) return url;
|
||||||
// 后端返回 /api/v1/upload/files/xxx.jpg,需要补全域名
|
// 后端返回 /api/v1/upload/files/xxx.jpg,需要补全域名
|
||||||
const DOMAIN = "http://192.168.9.80:8011";
|
const domain = getBaseUrl().replace(/\/api.*$/, '');
|
||||||
return DOMAIN + (url.startsWith("/") ? url : "/" + url);
|
return domain + (url.startsWith("/") ? url : "/" + url);
|
||||||
},
|
},
|
||||||
previewImage(urls, index) {
|
previewImage(urls, index) {
|
||||||
const fullUrls = (urls || []).map(img => this.imageUrl(img));
|
const fullUrls = (urls || []).map(img => this.imageUrl(img));
|
||||||
|
|||||||
@ -200,7 +200,7 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
import request, { get, post, patch, put } from "../../utils/request";
|
import request, { get, post, patch, put, getBaseUrl } from "../../utils/request";
|
||||||
import { setUserNameMap, formatUserName, formatUserAvatar } from "../../utils/format";
|
import { setUserNameMap, formatUserName, formatUserAvatar } from "../../utils/format";
|
||||||
import WorkspaceArea from "./components/WorkspaceArea.vue";
|
import WorkspaceArea from "./components/WorkspaceArea.vue";
|
||||||
import TreeCanvas from "./components/TreeCanvas.vue";
|
import TreeCanvas from "./components/TreeCanvas.vue";
|
||||||
@ -316,7 +316,7 @@ export default {
|
|||||||
openEditRecord({ task, record }) { this.recordPopup = { visible: true, task }; this.recordForm = { recordId: record.id, remark: record.remark || "", images: record.images || [], pendingCount: 0 }; this.isUploading = false; },
|
openEditRecord({ task, record }) { this.recordPopup = { visible: true, task }; this.recordForm = { recordId: record.id, remark: record.remark || "", images: record.images || [], pendingCount: 0 }; this.isUploading = false; },
|
||||||
closeRecordPopup() { this.recordPopup = { visible: false, task: null }; },
|
closeRecordPopup() { this.recordPopup = { visible: false, task: null }; },
|
||||||
async handleChooseImage() { const maxSlots = 9 - (this.recordForm.images.length + this.recordForm.pendingCount); if (maxSlots <= 0) return; const chooseRes = await new Promise((resolve, reject) => { uni.chooseImage({ count: maxSlots, sizeType: ["compressed"], sourceType: ["camera", "album"], success: resolve, fail: reject }); }).catch(() => null); if (!chooseRes || !chooseRes.tempFilePaths || !chooseRes.tempFilePaths.length) return; let compressSkipCount = 0; const compressedPaths = []; for (const p of chooseRes.tempFilePaths) { try { const compressed = await new Promise((resolve, reject) => { uni.compressImage({ src: p, quality: 60, success: resolve, fail: reject }); }); compressedPaths.push(compressed.tempFilePath); } catch { compressSkipCount++; } } if (!compressedPaths.length) return; this.isUploading = true; this.recordForm.pendingCount += compressedPaths.length; for (const path of compressedPaths) { const url = await this.uploadFile(path); if (url) this.recordForm.images.push(url); this.recordForm.pendingCount--; } this.isUploading = false; },
|
async handleChooseImage() { const maxSlots = 9 - (this.recordForm.images.length + this.recordForm.pendingCount); if (maxSlots <= 0) return; const chooseRes = await new Promise((resolve, reject) => { uni.chooseImage({ count: maxSlots, sizeType: ["compressed"], sourceType: ["camera", "album"], success: resolve, fail: reject }); }).catch(() => null); if (!chooseRes || !chooseRes.tempFilePaths || !chooseRes.tempFilePaths.length) return; let compressSkipCount = 0; const compressedPaths = []; for (const p of chooseRes.tempFilePaths) { try { const compressed = await new Promise((resolve, reject) => { uni.compressImage({ src: p, quality: 60, success: resolve, fail: reject }); }); compressedPaths.push(compressed.tempFilePath); } catch { compressSkipCount++; } } if (!compressedPaths.length) return; this.isUploading = true; this.recordForm.pendingCount += compressedPaths.length; for (const path of compressedPaths) { const url = await this.uploadFile(path); if (url) this.recordForm.images.push(url); this.recordForm.pendingCount--; } this.isUploading = false; },
|
||||||
uploadFile(filePath) { return new Promise((resolve) => { uni.uploadFile({ url: "http://192.168.9.80:8011/api/v1/upload/", filePath, name: "file", success(res) { try { const data = JSON.parse(res.data); resolve(data.url || null); } catch { resolve(null); } }, fail: () => resolve(null) }); }); },
|
uploadFile(filePath) { return new Promise((resolve) => { uni.uploadFile({ url: getBaseUrl() + "/upload/", filePath, name: "file", success(res) { try { const data = JSON.parse(res.data); resolve(data.url || null); } catch { resolve(null); } }, fail: () => resolve(null) }); }); },
|
||||||
removeRecordImage(i) { this.recordForm.images.splice(i, 1); },
|
removeRecordImage(i) { this.recordForm.images.splice(i, 1); },
|
||||||
previewRecordImage(i) { uni.previewImage({ urls: this.recordForm.images, current: i }); },
|
previewRecordImage(i) { uni.previewImage({ urls: this.recordForm.images, current: i }); },
|
||||||
async doSaveRecord() { if (this.isUploading) return; this.recordSaving = true; try { const payload = { remark: this.recordForm.remark.trim(), images: this.recordForm.images }; if (this.recordForm.recordId) await put(`/records/${this.recordForm.recordId}`, payload); else await patch(`/tasks/${this.recordPopup.task.id}/records`, payload); uni.showToast({ title: "已保存", icon: "success" }); this.closeRecordPopup(); this.doQuery(this.product.serial_number); } catch {} finally { this.recordSaving = false; } },
|
async doSaveRecord() { if (this.isUploading) return; this.recordSaving = true; try { const payload = { remark: this.recordForm.remark.trim(), images: this.recordForm.images }; if (this.recordForm.recordId) await put(`/records/${this.recordForm.recordId}`, payload); else await patch(`/tasks/${this.recordPopup.task.id}/records`, payload); uni.showToast({ title: "已保存", icon: "success" }); this.closeRecordPopup(); this.doQuery(this.product.serial_number); } catch {} finally { this.recordSaving = false; } },
|
||||||
|
|||||||
@ -52,7 +52,7 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
import request, { get, put } from "../../utils/request";
|
import request, { get, put, getBaseUrl } from "../../utils/request";
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
data() {
|
data() {
|
||||||
@ -116,7 +116,8 @@ export default {
|
|||||||
imageUrl(url) {
|
imageUrl(url) {
|
||||||
if (!url) return "";
|
if (!url) return "";
|
||||||
if (url.startsWith("http")) return url;
|
if (url.startsWith("http")) return url;
|
||||||
return "http://192.168.9.80:8011" + (url.startsWith("/") ? url : "/" + url);
|
const domain = getBaseUrl().replace(/\/api.*$/, '');
|
||||||
|
return domain + (url.startsWith("/") ? url : "/" + url);
|
||||||
},
|
},
|
||||||
|
|
||||||
previewImage(urls, index) {
|
previewImage(urls, index) {
|
||||||
|
|||||||
@ -1,18 +1,34 @@
|
|||||||
/**
|
/**
|
||||||
* uni.request 封装 — 双 Token 无感刷新 + 并发请求队列
|
* uni.request 封装 — 双 Token 无感刷新 + 并发请求队列
|
||||||
*
|
*
|
||||||
* 环境选择:优先读取 storage 中的 env_base_url,
|
* 网络环境选择(三级优先级):
|
||||||
* 未设置时默认走生产服务器。开发时如需切换,在登录前手动设置。
|
* 1. env_base_url 缓存(App 内运行时动态切换,最高优先级)
|
||||||
|
* 2. process.env.NODE_ENV 自动判断(development → 本地 IP / production → 公网域名)
|
||||||
|
* 3. 生产默认 PROD_URL
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const PROD_URL = "http://track_back.iris-rs.cn/api/v1";
|
const PROD_URL = "http://track_back.iris-rs.cn/api/v1";
|
||||||
|
const LOCAL_URL = "http://192.168.9.80:8011/api/v1";
|
||||||
|
|
||||||
// ============================================================
|
// ============================================================
|
||||||
// 环境 URL
|
// 环境 URL(自动感知:缓存配置 > NODE_ENV > 生产默认)
|
||||||
// ============================================================
|
// ============================================================
|
||||||
|
|
||||||
function getBaseUrl() {
|
export function getBaseUrl() {
|
||||||
return uni.getStorageSync("env_base_url") || PROD_URL;
|
// 1. 最高优先级:动态切换的缓存配置(如通过 App 内隐藏菜单切换)
|
||||||
|
const envUrl = uni.getStorageSync("env_base_url");
|
||||||
|
if (envUrl) {
|
||||||
|
return envUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. 自动环境判断
|
||||||
|
if (process.env.NODE_ENV === 'development') {
|
||||||
|
// 本地运行/真机调试时,自动使用本地开发机 IP
|
||||||
|
return LOCAL_URL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. 生产打包发行时,自动使用公网域名
|
||||||
|
return PROD_URL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ============================================================
|
// ============================================================
|
||||||
|
|||||||
@ -1,119 +0,0 @@
|
|||||||
/**
|
|
||||||
* uni.request 封装 — 本地开发环境 (192.168.9.80)
|
|
||||||
* 双 Token 无感刷新 + 并发请求队列
|
|
||||||
*/
|
|
||||||
|
|
||||||
const BASE_URL = "http://192.168.9.80:8011/api/v1";
|
|
||||||
|
|
||||||
function getAccessToken() { return uni.getStorageSync("access_token") || ""; }
|
|
||||||
function getRefreshToken() { return uni.getStorageSync("refresh_token") || ""; }
|
|
||||||
function setTokens(accessToken, refreshToken) {
|
|
||||||
uni.setStorageSync("access_token", accessToken);
|
|
||||||
uni.setStorageSync("refresh_token", refreshToken);
|
|
||||||
}
|
|
||||||
function clearTokens() {
|
|
||||||
uni.removeStorageSync("access_token");
|
|
||||||
uni.removeStorageSync("refresh_token");
|
|
||||||
uni.removeStorageSync("token");
|
|
||||||
uni.removeStorageSync("user");
|
|
||||||
}
|
|
||||||
|
|
||||||
let isRefreshing = false;
|
|
||||||
let retryQueue = [];
|
|
||||||
|
|
||||||
function processQueue(error, newToken) {
|
|
||||||
retryQueue.forEach((p) => {
|
|
||||||
if (newToken) p.resolve(newToken);
|
|
||||||
else p.reject(error);
|
|
||||||
});
|
|
||||||
retryQueue = [];
|
|
||||||
}
|
|
||||||
|
|
||||||
async function refreshAccessToken() {
|
|
||||||
const refreshToken = getRefreshToken();
|
|
||||||
if (!refreshToken) throw new Error("无 Refresh Token");
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
uni.request({
|
|
||||||
url: BASE_URL + "/auth/refresh",
|
|
||||||
method: "POST",
|
|
||||||
data: { refresh_token: refreshToken },
|
|
||||||
header: { "Content-Type": "application/json" },
|
|
||||||
timeout: 10000,
|
|
||||||
success(res) {
|
|
||||||
if (res.statusCode >= 200 && res.statusCode < 300) resolve(res.data);
|
|
||||||
else reject(res);
|
|
||||||
},
|
|
||||||
fail(err) { reject(err); },
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function request(options) {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
const url = options.url.startsWith("http") ? options.url : BASE_URL + options.url;
|
|
||||||
const accessToken = getAccessToken();
|
|
||||||
|
|
||||||
uni.request({
|
|
||||||
url,
|
|
||||||
method: options.method || "GET",
|
|
||||||
data: options.data || {},
|
|
||||||
header: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
...(accessToken ? { Authorization: `Bearer ${accessToken}` } : {}),
|
|
||||||
...(options.header || {}),
|
|
||||||
},
|
|
||||||
timeout: 15000,
|
|
||||||
success(res) {
|
|
||||||
const code = res.statusCode;
|
|
||||||
if (code >= 200 && code < 300) { resolve(res.data); return; }
|
|
||||||
if (code === 401) {
|
|
||||||
if (url.includes("/auth/refresh") || url.includes("/auth/login")) { reject(res); return; }
|
|
||||||
if (isRefreshing) {
|
|
||||||
retryQueue.push({
|
|
||||||
resolve: (newToken) => {
|
|
||||||
options.header = options.header || {};
|
|
||||||
options.header.Authorization = `Bearer ${newToken}`;
|
|
||||||
request(options).then(resolve).catch(reject);
|
|
||||||
},
|
|
||||||
reject: (err) => reject(err),
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
isRefreshing = true;
|
|
||||||
refreshAccessToken()
|
|
||||||
.then((data) => {
|
|
||||||
setTokens(data.access_token, getRefreshToken());
|
|
||||||
processQueue(null, data.access_token);
|
|
||||||
options.header = options.header || {};
|
|
||||||
options.header.Authorization = `Bearer ${data.access_token}`;
|
|
||||||
request(options).then(resolve).catch(reject);
|
|
||||||
})
|
|
||||||
.catch(() => {
|
|
||||||
console.error("[Request] Refresh Token 也过期");
|
|
||||||
processQueue(new Error("refresh_failed"), null);
|
|
||||||
clearTokens();
|
|
||||||
uni.showToast({ title: "登录已过期,请重新登录", icon: "none" });
|
|
||||||
setTimeout(() => uni.reLaunch({ url: "/pages/login/login" }), 1000);
|
|
||||||
reject(res);
|
|
||||||
})
|
|
||||||
.finally(() => { isRefreshing = false; });
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (code === 403) uni.showToast({ title: res.data?.detail || "无权操作", icon: "none", duration: 3000 });
|
|
||||||
else if (code === 400) uni.showToast({ title: res.data?.detail || "请求参数有误", icon: "none", duration: 2500 });
|
|
||||||
else if (code === 409) uni.showToast({ title: res.data?.detail || "操作冲突", icon: "none", duration: 3000 });
|
|
||||||
else { const d = res.data?.detail || ""; uni.showToast({ title: d || `请求失败 (${code})`, icon: "none", duration: 3000 }); }
|
|
||||||
reject(res);
|
|
||||||
},
|
|
||||||
fail() { uni.showToast({ title: "网络连接失败", icon: "none" }); reject(new Error("network")); },
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
export function get(url, params = {}) {
|
|
||||||
const query = Object.entries(params).filter(([, v]) => v != null && v !== "").map(([k, v]) => `${k}=${encodeURIComponent(v)}`).join("&");
|
|
||||||
return request({ url: query ? `${url}?${query}` : url, method: "GET" });
|
|
||||||
}
|
|
||||||
export function post(url, data = {}) { return request({ url, method: "POST", data }); }
|
|
||||||
export function patch(url, data = {}) { return request({ url, method: "PATCH", data }); }
|
|
||||||
export function put(url, data = {}) { return request({ url, method: "PUT", data }); }
|
|
||||||
@ -1,120 +0,0 @@
|
|||||||
/**
|
|
||||||
* uni.request 封装 — 生产环境(172.16.0.198)
|
|
||||||
* 打包 APK 前:将 request.prod.js 重命名为 request.js 替换原文件
|
|
||||||
* 双 Token 无感刷新 + 并发请求队列
|
|
||||||
*/
|
|
||||||
|
|
||||||
const BASE_URL = "http://172.16.0.198:8011/api/v1";
|
|
||||||
|
|
||||||
function getAccessToken() { return uni.getStorageSync("access_token") || ""; }
|
|
||||||
function getRefreshToken() { return uni.getStorageSync("refresh_token") || ""; }
|
|
||||||
function setTokens(accessToken, refreshToken) {
|
|
||||||
uni.setStorageSync("access_token", accessToken);
|
|
||||||
uni.setStorageSync("refresh_token", refreshToken);
|
|
||||||
}
|
|
||||||
function clearTokens() {
|
|
||||||
uni.removeStorageSync("access_token");
|
|
||||||
uni.removeStorageSync("refresh_token");
|
|
||||||
uni.removeStorageSync("token");
|
|
||||||
uni.removeStorageSync("user");
|
|
||||||
}
|
|
||||||
|
|
||||||
let isRefreshing = false;
|
|
||||||
let retryQueue = [];
|
|
||||||
|
|
||||||
function processQueue(error, newToken) {
|
|
||||||
retryQueue.forEach((p) => {
|
|
||||||
if (newToken) p.resolve(newToken);
|
|
||||||
else p.reject(error);
|
|
||||||
});
|
|
||||||
retryQueue = [];
|
|
||||||
}
|
|
||||||
|
|
||||||
async function refreshAccessToken() {
|
|
||||||
const refreshToken = getRefreshToken();
|
|
||||||
if (!refreshToken) throw new Error("无 Refresh Token");
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
uni.request({
|
|
||||||
url: BASE_URL + "/auth/refresh",
|
|
||||||
method: "POST",
|
|
||||||
data: { refresh_token: refreshToken },
|
|
||||||
header: { "Content-Type": "application/json" },
|
|
||||||
timeout: 10000,
|
|
||||||
success(res) {
|
|
||||||
if (res.statusCode >= 200 && res.statusCode < 300) resolve(res.data);
|
|
||||||
else reject(res);
|
|
||||||
},
|
|
||||||
fail(err) { reject(err); },
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function request(options) {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
const url = options.url.startsWith("http") ? options.url : BASE_URL + options.url;
|
|
||||||
const accessToken = getAccessToken();
|
|
||||||
|
|
||||||
uni.request({
|
|
||||||
url,
|
|
||||||
method: options.method || "GET",
|
|
||||||
data: options.data || {},
|
|
||||||
header: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
...(accessToken ? { Authorization: `Bearer ${accessToken}` } : {}),
|
|
||||||
...(options.header || {}),
|
|
||||||
},
|
|
||||||
timeout: 15000,
|
|
||||||
success(res) {
|
|
||||||
const code = res.statusCode;
|
|
||||||
if (code >= 200 && code < 300) { resolve(res.data); return; }
|
|
||||||
if (code === 401) {
|
|
||||||
if (url.includes("/auth/refresh") || url.includes("/auth/login")) { reject(res); return; }
|
|
||||||
if (isRefreshing) {
|
|
||||||
retryQueue.push({
|
|
||||||
resolve: (newToken) => {
|
|
||||||
options.header = options.header || {};
|
|
||||||
options.header.Authorization = `Bearer ${newToken}`;
|
|
||||||
request(options).then(resolve).catch(reject);
|
|
||||||
},
|
|
||||||
reject: (err) => reject(err),
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
isRefreshing = true;
|
|
||||||
refreshAccessToken()
|
|
||||||
.then((data) => {
|
|
||||||
setTokens(data.access_token, getRefreshToken());
|
|
||||||
processQueue(null, data.access_token);
|
|
||||||
options.header = options.header || {};
|
|
||||||
options.header.Authorization = `Bearer ${data.access_token}`;
|
|
||||||
request(options).then(resolve).catch(reject);
|
|
||||||
})
|
|
||||||
.catch(() => {
|
|
||||||
console.error("[Request] Refresh Token 也过期");
|
|
||||||
processQueue(new Error("refresh_failed"), null);
|
|
||||||
clearTokens();
|
|
||||||
uni.showToast({ title: "登录已过期,请重新登录", icon: "none" });
|
|
||||||
setTimeout(() => uni.reLaunch({ url: "/pages/login/login" }), 1000);
|
|
||||||
reject(res);
|
|
||||||
})
|
|
||||||
.finally(() => { isRefreshing = false; });
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (code === 403) uni.showToast({ title: res.data?.detail || "无权操作", icon: "none", duration: 3000 });
|
|
||||||
else if (code === 400) uni.showToast({ title: res.data?.detail || "请求参数有误", icon: "none", duration: 2500 });
|
|
||||||
else if (code === 409) uni.showToast({ title: res.data?.detail || "操作冲突", icon: "none", duration: 3000 });
|
|
||||||
else { const d = res.data?.detail || ""; uni.showToast({ title: d || `请求失败 (${code})`, icon: "none", duration: 3000 }); }
|
|
||||||
reject(res);
|
|
||||||
},
|
|
||||||
fail() { uni.showToast({ title: "网络连接失败", icon: "none" }); reject(new Error("network")); },
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
export function get(url, params = {}) {
|
|
||||||
const query = Object.entries(params).filter(([, v]) => v != null && v !== "").map(([k, v]) => `${k}=${encodeURIComponent(v)}`).join("&");
|
|
||||||
return request({ url: query ? `${url}?${query}` : url, method: "GET" });
|
|
||||||
}
|
|
||||||
export function post(url, data = {}) { return request({ url, method: "POST", data }); }
|
|
||||||
export function patch(url, data = {}) { return request({ url, method: "PATCH", data }); }
|
|
||||||
export function put(url, data = {}) { return request({ url, method: "PUT", data }); }
|
|
||||||
@ -1,74 +0,0 @@
|
|||||||
/**
|
|
||||||
* uni.request 封装 — 生产环境(服务器 172.16.0.198)
|
|
||||||
* 打包 APK 前:将 request.prod.js 重命名为 request.js 替换原文件
|
|
||||||
*/
|
|
||||||
|
|
||||||
const BASE_URL = "http://172.16.0.198:8011/api/v1";
|
|
||||||
|
|
||||||
export default function request(options) {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
const url = options.url.startsWith("http") ? options.url : BASE_URL + options.url;
|
|
||||||
const token = uni.getStorageSync("token") || "";
|
|
||||||
|
|
||||||
uni.request({
|
|
||||||
url,
|
|
||||||
method: options.method || "GET",
|
|
||||||
data: options.data || {},
|
|
||||||
header: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
...(token ? { Authorization: `Bearer ${token}` } : {}),
|
|
||||||
...(options.header || {}),
|
|
||||||
},
|
|
||||||
timeout: 15000,
|
|
||||||
success(res) {
|
|
||||||
const code = res.statusCode;
|
|
||||||
if (code >= 200 && code < 300) {
|
|
||||||
resolve(res.data);
|
|
||||||
} else if (code === 401) {
|
|
||||||
uni.removeStorageSync("token");
|
|
||||||
uni.removeStorageSync("user");
|
|
||||||
uni.showToast({ title: "登录已过期,请重新登录", icon: "none" });
|
|
||||||
setTimeout(() => uni.reLaunch({ url: "/pages/login/login" }), 1000);
|
|
||||||
reject(res);
|
|
||||||
} else if (code === 403) {
|
|
||||||
uni.showToast({ title: res.data?.detail || "无权操作", icon: "none", duration: 3000 });
|
|
||||||
reject(res);
|
|
||||||
} else if (code === 400) {
|
|
||||||
uni.showToast({ title: res.data?.detail || "请求参数有误", icon: "none", duration: 2500 });
|
|
||||||
reject(res);
|
|
||||||
} else if (code === 409) {
|
|
||||||
uni.showToast({ title: res.data?.detail || "操作冲突", icon: "none", duration: 3000 });
|
|
||||||
reject(res);
|
|
||||||
} else {
|
|
||||||
const detail = res.data?.detail || "";
|
|
||||||
uni.showToast({ title: detail ? `${detail}` : `请求失败 (${code})`, icon: "none", duration: 3000 });
|
|
||||||
reject(res);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
fail() {
|
|
||||||
uni.showToast({ title: "网络连接失败", icon: "none" });
|
|
||||||
reject(new Error("network"));
|
|
||||||
},
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
export function get(url, params = {}) {
|
|
||||||
const query = Object.entries(params)
|
|
||||||
.filter(([, v]) => v != null && v !== "")
|
|
||||||
.map(([k, v]) => `${k}=${encodeURIComponent(v)}`)
|
|
||||||
.join("&");
|
|
||||||
return request({ url: query ? `${url}?${query}` : url, method: "GET" });
|
|
||||||
}
|
|
||||||
|
|
||||||
export function post(url, data = {}) {
|
|
||||||
return request({ url, method: "POST", data });
|
|
||||||
}
|
|
||||||
|
|
||||||
export function patch(url, data = {}) {
|
|
||||||
return request({ url, method: "PATCH", data });
|
|
||||||
}
|
|
||||||
|
|
||||||
export function put(url, data = {}) {
|
|
||||||
return request({ url, method: "PUT", data });
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user