mix-public 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +34 -0
- package/mixRender.css +126 -0
- package/mixRender.js +41 -0
- package/package.json +14 -0
- package/pageRender/getInfo.js +97 -0
- package/pageRender/navigation.js +41 -0
- package/pageRender/tabbar.js +107 -0
- package/resource/axios.js +3 -0
- package/resource/cookie.js +224 -0
- package/resource/cryptoJS.js +57 -0
- package/resource/http.js +349 -0
- package/resource/libpag.js +7699 -0
- package/resource/libpag.wasm +0 -0
- package/resource/md5.js +2 -0
- package/resource/mixappfunc.js +2569 -0
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
class indexedDb {
|
|
2
|
+
constructor(dbName, version, storeName) {
|
|
3
|
+
this.dbName = dbName
|
|
4
|
+
this.version = version
|
|
5
|
+
this.storeName = storeName
|
|
6
|
+
this.indexdbrequest = window.indexedDB.open(dbName, version)
|
|
7
|
+
this.db
|
|
8
|
+
this.indexdbrequest.onsuccess = this.onsuccess
|
|
9
|
+
this.indexdbrequest.onerror = this.onerror
|
|
10
|
+
this.indexdbrequest.onupgradeneeded = this.onupgradeneeded
|
|
11
|
+
}
|
|
12
|
+
onsuccess = (event) => {
|
|
13
|
+
this.db = event.target.result // 数据库对象
|
|
14
|
+
}
|
|
15
|
+
onerror = (event) => {
|
|
16
|
+
console.log("indexdb error :", event)
|
|
17
|
+
}
|
|
18
|
+
onupgradeneeded = (event) => {
|
|
19
|
+
// 数据库创建或升级的时候会触发
|
|
20
|
+
this.db = event.target.result // 数据库对象
|
|
21
|
+
let objectStore
|
|
22
|
+
if (!this.db.objectStoreNames.contains(this.storeName)) {
|
|
23
|
+
objectStore = this.db.createObjectStore(this.storeName, { keyPath: "id" }) // 创建表
|
|
24
|
+
// objectStore.createIndex('name', 'name', { unique: true }) // 创建索引 可以让你搜索任意字段
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
// 添加数据
|
|
28
|
+
addData(data) {
|
|
29
|
+
return new Promise((resolve, reject) => {
|
|
30
|
+
const req = this.db
|
|
31
|
+
?.transaction([this.storeName], "readwrite") // 事务对象 指定表格名称和操作模式("只读"或"读写")
|
|
32
|
+
.objectStore(this.storeName) // 仓库对象
|
|
33
|
+
.add(data)
|
|
34
|
+
|
|
35
|
+
if (!req) return
|
|
36
|
+
// 操作成功
|
|
37
|
+
req["onsuccess"] = function (event) {
|
|
38
|
+
resolve({
|
|
39
|
+
code: 200,
|
|
40
|
+
success: true,
|
|
41
|
+
data: req.result,
|
|
42
|
+
msg: "数据添加成功!",
|
|
43
|
+
})
|
|
44
|
+
}
|
|
45
|
+
// 操作失败
|
|
46
|
+
req.onerror = function (event) {
|
|
47
|
+
let data = {
|
|
48
|
+
code: -1,
|
|
49
|
+
success: false,
|
|
50
|
+
data: req.result,
|
|
51
|
+
msg: "数据添加失败!",
|
|
52
|
+
}
|
|
53
|
+
resolve(data)
|
|
54
|
+
}
|
|
55
|
+
})
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// 根据id获取数据
|
|
59
|
+
getDataByKey(key) {
|
|
60
|
+
return new Promise((resolve, reject) => {
|
|
61
|
+
const req = this.db
|
|
62
|
+
?.transaction([this.storeName])
|
|
63
|
+
.objectStore(this.storeName)
|
|
64
|
+
.get(key)
|
|
65
|
+
if (!req) return
|
|
66
|
+
|
|
67
|
+
// 操作成功
|
|
68
|
+
req["onsuccess"] = function (event) {
|
|
69
|
+
resolve({
|
|
70
|
+
code: 200,
|
|
71
|
+
success: true,
|
|
72
|
+
data: req.result,
|
|
73
|
+
msg: "数据获取成功!",
|
|
74
|
+
})
|
|
75
|
+
}
|
|
76
|
+
// 操作失败
|
|
77
|
+
req.onerror = function (event) {
|
|
78
|
+
let data = {
|
|
79
|
+
code: -1,
|
|
80
|
+
success: false,
|
|
81
|
+
data: req.result,
|
|
82
|
+
msg: "数据获取失败!",
|
|
83
|
+
}
|
|
84
|
+
resolve(data)
|
|
85
|
+
}
|
|
86
|
+
})
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// 根据id修改数据
|
|
90
|
+
updateDBfunction(data) {
|
|
91
|
+
return new Promise((resolve, reject) => {
|
|
92
|
+
const req = this.db
|
|
93
|
+
?.transaction([this.storeName], "readwrite")
|
|
94
|
+
.objectStore(this.storeName)
|
|
95
|
+
.put(data)
|
|
96
|
+
if (!req) return
|
|
97
|
+
|
|
98
|
+
// 操作成功
|
|
99
|
+
req["onsuccess"] = function (event) {
|
|
100
|
+
resolve({ code: 200, success: true, data: null, msg: "数据更新成功!" })
|
|
101
|
+
}
|
|
102
|
+
// 操作失败
|
|
103
|
+
req.onerror = function (event) {
|
|
104
|
+
let data = {
|
|
105
|
+
code: -1,
|
|
106
|
+
success: false,
|
|
107
|
+
data: null,
|
|
108
|
+
msg: "数据更新失败!",
|
|
109
|
+
}
|
|
110
|
+
resolve(data)
|
|
111
|
+
}
|
|
112
|
+
})
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// 根据id删除数据
|
|
116
|
+
deleteDB(id) {
|
|
117
|
+
return new Promise((resolve, reject) => {
|
|
118
|
+
let req = this.db
|
|
119
|
+
?.transaction([this.storeName], "readwrite")
|
|
120
|
+
.objectStore(this.storeName)
|
|
121
|
+
.delete(id)
|
|
122
|
+
if (!req) return
|
|
123
|
+
|
|
124
|
+
// 操作成功
|
|
125
|
+
req["onsuccess"] = function (event) {
|
|
126
|
+
resolve({ code: 200, success: true, data: null, msg: "数据删除成功!" })
|
|
127
|
+
}
|
|
128
|
+
// 操作失败
|
|
129
|
+
req.onerror = function (event) {
|
|
130
|
+
let data = {
|
|
131
|
+
code: -1,
|
|
132
|
+
success: false,
|
|
133
|
+
data: null,
|
|
134
|
+
msg: "数据删除失败!",
|
|
135
|
+
}
|
|
136
|
+
resolve(data)
|
|
137
|
+
}
|
|
138
|
+
})
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
const local = {
|
|
142
|
+
get: function (e) {
|
|
143
|
+
return localStorage.getItem(e)
|
|
144
|
+
},
|
|
145
|
+
set: function (key, val) {
|
|
146
|
+
localStorage.setItem(key, val)
|
|
147
|
+
},
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const cookie = {
|
|
151
|
+
get: function (name) {
|
|
152
|
+
var arr,
|
|
153
|
+
reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)")
|
|
154
|
+
if ((arr = document.cookie.match(reg))) return unescape(arr[2])
|
|
155
|
+
else return null
|
|
156
|
+
},
|
|
157
|
+
set: function (name, value, date = 30) {
|
|
158
|
+
var Days = date // 设置有效期
|
|
159
|
+
var exp = new Date()
|
|
160
|
+
exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000)
|
|
161
|
+
document.cookie =
|
|
162
|
+
name + "=" + escape(value) + ";expires=" + exp.toGMTString()
|
|
163
|
+
},
|
|
164
|
+
del: function delCookie(name) {
|
|
165
|
+
var exp = new Date()
|
|
166
|
+
exp.setTime(exp.getTime() - 1)
|
|
167
|
+
var cval = getCookie(name)
|
|
168
|
+
if (cval != null)
|
|
169
|
+
document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString()
|
|
170
|
+
},
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export const Cookies = {
|
|
174
|
+
get: function (
|
|
175
|
+
name,
|
|
176
|
+
type = 1,
|
|
177
|
+
conf = { dbname: "dbname", version: 1, table: "cookies" },
|
|
178
|
+
) {
|
|
179
|
+
switch (type) {
|
|
180
|
+
case 1:
|
|
181
|
+
return cookie.get(name)
|
|
182
|
+
case 2:
|
|
183
|
+
return local.get(name)
|
|
184
|
+
case 3:
|
|
185
|
+
return new Promise((resolve, reject) => {
|
|
186
|
+
const db = new indexedDb(conf.dbname, conf.version, conf.table)
|
|
187
|
+
setTimeout(async () => {
|
|
188
|
+
const res = await db.getDataByKey(name)
|
|
189
|
+
resolve(res.data?.name)
|
|
190
|
+
}, 500)
|
|
191
|
+
})
|
|
192
|
+
}
|
|
193
|
+
},
|
|
194
|
+
set: function (
|
|
195
|
+
name,
|
|
196
|
+
value,
|
|
197
|
+
type = 1,
|
|
198
|
+
conf = { dbname: "dbname", version: 1, table: "cookies" },
|
|
199
|
+
) {
|
|
200
|
+
switch (type) {
|
|
201
|
+
case 1:
|
|
202
|
+
return cookie.set(name, value)
|
|
203
|
+
case 2:
|
|
204
|
+
return local.set(name, value)
|
|
205
|
+
case 3:
|
|
206
|
+
return new Promise(async (resolve, reject) => {
|
|
207
|
+
const db = await new indexedDb(conf.dbname, conf.version, conf.table)
|
|
208
|
+
setTimeout(async () => {
|
|
209
|
+
const res = await db.getDataByKey(name)
|
|
210
|
+
if (res.data) {
|
|
211
|
+
const updata = await db.updateDBfunction({
|
|
212
|
+
name: value,
|
|
213
|
+
id: name,
|
|
214
|
+
})
|
|
215
|
+
resolve(updata.data)
|
|
216
|
+
} else {
|
|
217
|
+
const adddata = await db.addData({ data: value, id: name })
|
|
218
|
+
resolve(adddata.data)
|
|
219
|
+
}
|
|
220
|
+
}, 500)
|
|
221
|
+
})
|
|
222
|
+
}
|
|
223
|
+
},
|
|
224
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @Author: Gui
|
|
3
|
+
* @Date: 2023-07-04 11:59:05
|
|
4
|
+
* @LastEditors: guicheng 1625811865@qq.com
|
|
5
|
+
* @LastEditTime: 2026-01-26 15:29:20
|
|
6
|
+
* @Description: kxs files
|
|
7
|
+
* @filePath:
|
|
8
|
+
*/
|
|
9
|
+
import CryptoJS from "crypto-js"
|
|
10
|
+
// 加密
|
|
11
|
+
export const mixLocalShakeDownEncrypt = function (message) {
|
|
12
|
+
const keyHex = CryptoJS.enc.Utf8.parse("2024SIG4W1SAVDTSA6OV0VN1G07PS3N5")
|
|
13
|
+
const ivHex = CryptoJS.enc.Utf8.parse("24072615")
|
|
14
|
+
const srcs = CryptoJS.enc.Utf8.parse(message)
|
|
15
|
+
const encrypted = CryptoJS.TripleDES.encrypt(srcs, keyHex, {
|
|
16
|
+
iv: ivHex,
|
|
17
|
+
mode: CryptoJS.mode.CBC,
|
|
18
|
+
padding: CryptoJS.pad.Pkcs7,
|
|
19
|
+
})
|
|
20
|
+
return encrypted.toString()
|
|
21
|
+
}
|
|
22
|
+
// 解密
|
|
23
|
+
export const mixLocalShakeDownDecrypt = function (message) {
|
|
24
|
+
try {
|
|
25
|
+
if (!message) {
|
|
26
|
+
return message
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const keyHex = CryptoJS.enc.Utf8.parse("2024SIG4W1SAVDTSA6OV0VN1G07PS3N5")
|
|
30
|
+
const ivHex = CryptoJS.enc.Utf8.parse("24072615")
|
|
31
|
+
const base64 = CryptoJS.enc.Base64.parse(message)
|
|
32
|
+
const src = CryptoJS.enc.Base64.stringify(base64)
|
|
33
|
+
const decrypt = CryptoJS.TripleDES.decrypt(src, keyHex, {
|
|
34
|
+
iv: ivHex,
|
|
35
|
+
mode: CryptoJS.mode.CBC,
|
|
36
|
+
padding: CryptoJS.pad.Pkcs7,
|
|
37
|
+
})
|
|
38
|
+
const decryptedStr = decrypt.toString(CryptoJS.enc.Utf8)
|
|
39
|
+
return decryptedStr.toString()
|
|
40
|
+
} catch (err) {
|
|
41
|
+
return ""
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
// 加密
|
|
45
|
+
export const mixRemoteShakeDownEncrypt = function (message) {
|
|
46
|
+
const keyHex = CryptoJS.enc.Utf8.parse("CBTU1dD4Kd5pyiGWTsI10jRQ3SvKusSV")
|
|
47
|
+
const ivHex = CryptoJS.enc.Utf8.parse("DYgjCEIMVrj2W9xN")
|
|
48
|
+
const srcs = CryptoJS.enc.Utf8.parse(message)
|
|
49
|
+
const encrypted = CryptoJS.AES.encrypt(srcs, keyHex, {
|
|
50
|
+
iv: ivHex,
|
|
51
|
+
mode: CryptoJS.mode.CBC,
|
|
52
|
+
padding: CryptoJS.pad.Pkcs7,
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
const str = CryptoJS.enc.Utf8.parse(encrypted.toString())
|
|
56
|
+
return CryptoJS.enc.Base64.stringify(str)
|
|
57
|
+
}
|
package/resource/http.js
ADDED
|
@@ -0,0 +1,349 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 原生JavaScript HTTP请求库
|
|
3
|
+
* 支持GET、POST、PUT、DELETE等方法
|
|
4
|
+
* 支持请求/响应拦截器、超时控制、取消请求等功能
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
class MixHttpRequest {
|
|
8
|
+
constructor(config = {}) {
|
|
9
|
+
this.defaultConfig = {
|
|
10
|
+
baseURL: 'https://app-manager.kexiaoshuang.com/',
|
|
11
|
+
timeout: 10000, // 10秒超时
|
|
12
|
+
headers: {
|
|
13
|
+
'Content-Type': 'application/json'
|
|
14
|
+
},
|
|
15
|
+
...config
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
// 拦截器
|
|
19
|
+
this.interceptors = {
|
|
20
|
+
request: {
|
|
21
|
+
use: (onFulfilled, onRejected) => {
|
|
22
|
+
this.requestInterceptor = { onFulfilled, onRejected };
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
response: {
|
|
26
|
+
use: (onFulfilled, onRejected) => {
|
|
27
|
+
this.responseInterceptor = { onFulfilled, onRejected };
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
this.requestInterceptor = null;
|
|
33
|
+
this.responseInterceptor = null;
|
|
34
|
+
this.abortControllers = new Map(); // 存储AbortController用于取消请求
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* 发送HTTP请求
|
|
39
|
+
* @param {string} url - 请求URL
|
|
40
|
+
* @param {Object} config - 请求配置
|
|
41
|
+
* @returns {Promise} - 返回Promise对象
|
|
42
|
+
*/
|
|
43
|
+
request(url, config = {}) {
|
|
44
|
+
// 合并配置
|
|
45
|
+
const mergedConfig = {
|
|
46
|
+
...this.defaultConfig,
|
|
47
|
+
...config,
|
|
48
|
+
headers: {
|
|
49
|
+
...this.defaultConfig.headers,
|
|
50
|
+
...(config.headers || {})
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
// 处理完整URL
|
|
55
|
+
const fullUrl = mergedConfig.baseURL ? `${mergedConfig.baseURL}${url}` : url;
|
|
56
|
+
|
|
57
|
+
// 创建AbortController用于取消请求
|
|
58
|
+
const abortController = new AbortController();
|
|
59
|
+
const requestId = Date.now() + Math.random().toString(36).substr(2, 9);
|
|
60
|
+
this.abortControllers.set(requestId, abortController);
|
|
61
|
+
|
|
62
|
+
// 请求拦截器
|
|
63
|
+
let requestConfig = { ...mergedConfig, url: fullUrl, signal: abortController.signal };
|
|
64
|
+
if (this.requestInterceptor) {
|
|
65
|
+
try {
|
|
66
|
+
if (this.requestInterceptor.onFulfilled) {
|
|
67
|
+
requestConfig = this.requestInterceptor.onFulfilled(requestConfig) || requestConfig;
|
|
68
|
+
}
|
|
69
|
+
} catch (error) {
|
|
70
|
+
if (this.requestInterceptor.onRejected) {
|
|
71
|
+
return Promise.reject(this.requestInterceptor.onRejected(error));
|
|
72
|
+
}
|
|
73
|
+
return Promise.reject(error);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// 创建请求Promise
|
|
78
|
+
const requestPromise = new Promise((resolve, reject) => {
|
|
79
|
+
const { timeout, signal } = requestConfig;
|
|
80
|
+
|
|
81
|
+
// 设置超时
|
|
82
|
+
let timeoutId;
|
|
83
|
+
if (timeout > 0) {
|
|
84
|
+
timeoutId = setTimeout(() => {
|
|
85
|
+
abortController.abort();
|
|
86
|
+
reject(new Error(`请求超时 (${timeout}ms)`));
|
|
87
|
+
this.abortControllers.delete(requestId);
|
|
88
|
+
}, timeout);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// 发送请求
|
|
92
|
+
this._sendRequest(requestConfig)
|
|
93
|
+
.then(response => {
|
|
94
|
+
clearTimeout(timeoutId);
|
|
95
|
+
this.abortControllers.delete(requestId);
|
|
96
|
+
|
|
97
|
+
// 响应拦截器
|
|
98
|
+
if (this.responseInterceptor) {
|
|
99
|
+
try {
|
|
100
|
+
if (this.responseInterceptor.onFulfilled) {
|
|
101
|
+
response = this.responseInterceptor.onFulfilled(response) || response;
|
|
102
|
+
}
|
|
103
|
+
resolve(response);
|
|
104
|
+
} catch (error) {
|
|
105
|
+
if (this.responseInterceptor.onRejected) {
|
|
106
|
+
reject(this.responseInterceptor.onRejected(error));
|
|
107
|
+
} else {
|
|
108
|
+
reject(error);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
} else {
|
|
112
|
+
resolve(response);
|
|
113
|
+
}
|
|
114
|
+
})
|
|
115
|
+
.catch(error => {
|
|
116
|
+
clearTimeout(timeoutId);
|
|
117
|
+
this.abortControllers.delete(requestId);
|
|
118
|
+
|
|
119
|
+
// 响应拦截器错误处理
|
|
120
|
+
if (this.responseInterceptor && this.responseInterceptor.onRejected) {
|
|
121
|
+
reject(this.responseInterceptor.onRejected(error));
|
|
122
|
+
} else {
|
|
123
|
+
reject(error);
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
// 添加取消方法
|
|
129
|
+
requestPromise.cancel = (reason = '请求已被取消') => {
|
|
130
|
+
abortController.abort();
|
|
131
|
+
this.abortControllers.delete(requestId);
|
|
132
|
+
return Promise.reject(new Error(reason));
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
return requestPromise;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* 实际发送请求的方法
|
|
140
|
+
* @param {Object} config - 请求配置
|
|
141
|
+
* @returns {Promise} - 返回Promise对象
|
|
142
|
+
*/
|
|
143
|
+
_sendRequest(config) {
|
|
144
|
+
return new Promise((resolve, reject) => {
|
|
145
|
+
const { url, method = 'GET', headers, body, signal } = config;
|
|
146
|
+
|
|
147
|
+
// 创建XMLHttpRequest对象
|
|
148
|
+
const xhr = new XMLHttpRequest();
|
|
149
|
+
|
|
150
|
+
// 初始化请求
|
|
151
|
+
xhr.open(method.toUpperCase(), url, true);
|
|
152
|
+
|
|
153
|
+
// 设置请求头
|
|
154
|
+
if (headers) {
|
|
155
|
+
Object.keys(headers).forEach(key => {
|
|
156
|
+
if (headers[key] !== null && headers[key] !== undefined) {
|
|
157
|
+
xhr.setRequestHeader(key, headers[key]);
|
|
158
|
+
}
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// 处理响应
|
|
163
|
+
xhr.onload = () => {
|
|
164
|
+
if (xhr.status >= 200 && xhr.status < 300) {
|
|
165
|
+
let responseData;
|
|
166
|
+
try {
|
|
167
|
+
// 尝试解析JSON
|
|
168
|
+
responseData = xhr.responseText ? JSON.parse(xhr.responseText) : xhr.responseText;
|
|
169
|
+
} catch {
|
|
170
|
+
// 如果不是JSON,返回原始文本
|
|
171
|
+
responseData = xhr.responseText;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const response = {
|
|
175
|
+
data: responseData,
|
|
176
|
+
status: xhr.status,
|
|
177
|
+
statusText: xhr.statusText,
|
|
178
|
+
headers: this._parseResponseHeaders(xhr.getAllResponseHeaders()),
|
|
179
|
+
config
|
|
180
|
+
};
|
|
181
|
+
resolve(response);
|
|
182
|
+
} else {
|
|
183
|
+
reject(this._createError(xhr, config));
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
// 处理错误
|
|
188
|
+
xhr.onerror = () => {
|
|
189
|
+
reject(this._createError(xhr, config));
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
xhr.onabort = () => {
|
|
193
|
+
reject(new Error('请求已被取消'));
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
// 设置中止信号
|
|
197
|
+
if (signal) {
|
|
198
|
+
signal.addEventListener('abort', () => {
|
|
199
|
+
xhr.abort();
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// 发送请求
|
|
204
|
+
xhr.send(this._prepareRequestBody(body, headers));
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* 准备请求体
|
|
210
|
+
* @param {*} body - 请求体数据
|
|
211
|
+
* @param {Object} headers - 请求头
|
|
212
|
+
* @returns {*} - 处理后的请求体
|
|
213
|
+
*/
|
|
214
|
+
_prepareRequestBody(body, headers) {
|
|
215
|
+
if (!body) return null;
|
|
216
|
+
|
|
217
|
+
const contentType = headers['Content-Type'] || '';
|
|
218
|
+
|
|
219
|
+
if (contentType.includes('application/json')) {
|
|
220
|
+
return JSON.stringify(body);
|
|
221
|
+
} else if (contentType.includes('application/x-www-form-urlencoded')) {
|
|
222
|
+
return new URLSearchParams(body).toString();
|
|
223
|
+
} else if (body instanceof FormData || body instanceof URLSearchParams) {
|
|
224
|
+
return body;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
return body;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* 解析响应头
|
|
232
|
+
* @param {string} headersString - 响应头字符串
|
|
233
|
+
* @returns {Object} - 解析后的响应头对象
|
|
234
|
+
*/
|
|
235
|
+
_parseResponseHeaders(headersString) {
|
|
236
|
+
const headers = {};
|
|
237
|
+
if (!headersString) return headers;
|
|
238
|
+
|
|
239
|
+
const lines = headersString.trim().split(/[\r\n]+/);
|
|
240
|
+
lines.forEach(line => {
|
|
241
|
+
const parts = line.split(': ');
|
|
242
|
+
const header = parts.shift();
|
|
243
|
+
const value = parts.join(': ');
|
|
244
|
+
if (header) {
|
|
245
|
+
headers[header] = value;
|
|
246
|
+
}
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
return headers;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* 创建错误对象
|
|
254
|
+
* @param {XMLHttpRequest} xhr - XMLHttpRequest对象
|
|
255
|
+
* @param {Object} config - 请求配置
|
|
256
|
+
* @returns {Error} - 错误对象
|
|
257
|
+
*/
|
|
258
|
+
_createError(xhr, config) {
|
|
259
|
+
const error = new Error(`请求失败: ${xhr.status} ${xhr.statusText}`);
|
|
260
|
+
error.status = xhr.status;
|
|
261
|
+
error.statusText = xhr.statusText;
|
|
262
|
+
error.config = config;
|
|
263
|
+
|
|
264
|
+
try {
|
|
265
|
+
error.data = xhr.responseText ? JSON.parse(xhr.responseText) : xhr.responseText;
|
|
266
|
+
} catch {
|
|
267
|
+
error.data = xhr.responseText;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
return error;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* GET请求
|
|
275
|
+
* @param {string} url - 请求URL
|
|
276
|
+
* @param {Object} config - 请求配置
|
|
277
|
+
* @returns {Promise} - 返回Promise对象
|
|
278
|
+
*/
|
|
279
|
+
get(url, config = {}) {
|
|
280
|
+
return this.request(url, { ...config, method: 'GET' });
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* POST请求
|
|
285
|
+
* @param {string} url - 请求URL
|
|
286
|
+
* @param {*} data - 请求数据
|
|
287
|
+
* @param {Object} config - 请求配置
|
|
288
|
+
* @returns {Promise} - 返回Promise对象
|
|
289
|
+
*/
|
|
290
|
+
post(url, data = null, config = {}) {
|
|
291
|
+
return this.request(url, { ...config, method: 'POST', body: data });
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* PUT请求
|
|
296
|
+
* @param {string} url - 请求URL
|
|
297
|
+
* @param {*} data - 请求数据
|
|
298
|
+
* @param {Object} config - 请求配置
|
|
299
|
+
* @returns {Promise} - 返回Promise对象
|
|
300
|
+
*/
|
|
301
|
+
put(url, data = null, config = {}) {
|
|
302
|
+
return this.request(url, { ...config, method: 'PUT', body: data });
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* DELETE请求
|
|
307
|
+
* @param {string} url - 请求URL
|
|
308
|
+
* @param {Object} config - 请求配置
|
|
309
|
+
* @returns {Promise} - 返回Promise对象
|
|
310
|
+
*/
|
|
311
|
+
delete(url, config = {}) {
|
|
312
|
+
return this.request(url, { ...config, method: 'DELETE' });
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* PATCH请求
|
|
317
|
+
* @param {string} url - 请求URL
|
|
318
|
+
* @param {*} data - 请求数据
|
|
319
|
+
* @param {Object} config - 请求配置
|
|
320
|
+
* @returns {Promise} - 返回Promise对象
|
|
321
|
+
*/
|
|
322
|
+
patch(url, data = null, config = {}) {
|
|
323
|
+
return this.request(url, { ...config, method: 'PATCH', body: data });
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* 取消请求
|
|
328
|
+
* @param {string} requestId - 请求ID
|
|
329
|
+
*/
|
|
330
|
+
cancelRequest(requestId) {
|
|
331
|
+
if (this.abortControllers.has(requestId)) {
|
|
332
|
+
this.abortControllers.get(requestId).abort();
|
|
333
|
+
this.abortControllers.delete(requestId);
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* 取消所有待处理请求
|
|
339
|
+
*/
|
|
340
|
+
cancelAllRequests() {
|
|
341
|
+
this.abortControllers.forEach(controller => {
|
|
342
|
+
controller.abort();
|
|
343
|
+
});
|
|
344
|
+
this.abortControllers.clear();
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
// 创建默认实例
|
|
349
|
+
export const mixHttp = new MixHttpRequest();
|