From 089bf5d2378b3c4a61d795b2a92bede2c193b771 Mon Sep 17 00:00:00 2001
From: admin <344137771@qq.com>
Date: Tue, 06 Jan 2026 11:22:58 +0800
Subject: [PATCH] 1
---
src/utils/request.js | 118 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 118 insertions(+), 0 deletions(-)
diff --git a/src/utils/request.js b/src/utils/request.js
new file mode 100644
index 0000000..10c7610
--- /dev/null
+++ b/src/utils/request.js
@@ -0,0 +1,118 @@
+import axios from 'axios'
+import config from '@/config/config'
+import { getToken, removeAll } from '@/utils/auth'
+import { signatureGenerate } from '@/utils/crypto'
+import { Notification } from 'element-ui'
+
+
+
+// 创建 axios 实例
+const request = axios.create({
+ // API 请求的默认前缀
+ baseURL: config.BASE_API_URL,
+
+ // 请求超时时间
+ timeout: 20000,
+})
+
+/**
+ * 异常拦截处理器
+ *
+ * @param {*} error
+ */
+const errorHandler = error => {
+ // 判断是否是响应错误信息
+ if (error.response) {
+ if (error.response.status == 401) {
+ removeAll()
+ location.reload()
+ } else if (error.response.status == 403) {
+ Notification({
+ title: '警告',
+ type: 'warning',
+ message: '无权限操作 ...',
+ position: 'top-right',
+ })
+ } else {
+ Notification({
+ message: '网络异常,请稍后再试...',
+ position: 'top-right',
+ })
+ }
+ }
+
+ return Promise.reject(error)
+}
+
+// 请求拦截器
+request.interceptors.request.use(config => {
+
+ const token = getToken()
+ if (token) {
+ config.headers['Authorization'] = `Bearer ${token}`
+ }
+ const signature = signatureGenerate();
+ config.headers["Sign"] = signature.signature;
+ config.headers["Systemrandom"] = signature.systemRandom;
+ config.headers["Tissuepaper"] = signature.timestamp;
+
+ // console.log("config => " + JSON.stringify(config));
+
+ return config
+}, errorHandler)
+
+// 响应拦截器
+request.interceptors.response.use(response => {
+ return response.data
+}, errorHandler)
+
+/**
+ * GET 请求
+ *
+ * @param {String} url
+ * @param {Object} data
+ * @param {Object} options
+ * @returns {Promise<any>}
+ */
+export const get = (url, data = {}, options = {}) => {
+ return request({
+ url,
+ params: data,
+ method: 'get',
+ ...options,
+ })
+}
+
+/**
+ * POST 请求
+ *
+ * @param {String} url
+ * @param {Object} data
+ * @param {Object} options
+ * @returns {Promise<any>}
+ */
+export const post = (url, data = {}, options = {}) => {
+ return request({
+ url,
+ method: 'post',
+ data: data,
+ ...options,
+ })
+}
+
+/**
+ * 上传文件 POST 请求
+ *
+ * @param {String} url
+ * @param {Object} data
+ * @param {Object} options
+ * @returns {Promise<any>}
+ */
+export const upload = (url, data = {}, options = {}) => {
+ return request({
+ url,
+ method: 'post',
+ data: data,
+ ...options,
+ })
+}
--
Gitblit v1.9.3