添加登录页面

This commit is contained in:
zk
2026-07-10 14:18:48 +08:00
parent 4585c824b7
commit 45b2c3073f
10 changed files with 470 additions and 3 deletions
@@ -0,0 +1,54 @@
/**
* 统一请求封装
*/
const request = (() => {
const BASE_URL = '/admin';
/** 从 cookie 中获取 Token */
function getToken() {
const match = document.cookie.match(/(?:^|;\s*)Token=([^;]*)/);
return match ? match[1] : '';
}
/** 统一请求方法 */
async function http(url, options = {}) {
const config = {
headers: { 'Content-Type': 'application/json', 'Token': getToken(), ...options.headers },
...options
};
const response = await fetch(BASE_URL + url, config);
// 401 未登录,跳转登录页
if (response.status === 401) {
window.location.href = BASE_URL + '/page/login';
return Promise.reject('未登录');
}
const result = await response.json();
// 业务异常
if (result.status !== 200) {
const msg = result.message || '请求失败';
alert(msg);
return Promise.reject(msg);
}
return result.data;
}
return {
get(url, params) {
const query = params ? '?' + new URLSearchParams(params).toString() : '';
return http(url + query, { method: 'GET' });
},
post(url, data) {
return http(url, { method: 'POST', body: JSON.stringify(data) });
},
put(url, data) {
return http(url, { method: 'PUT', body: JSON.stringify(data) });
},
del(url, data) {
return http(url, { method: 'DELETE', body: data ? JSON.stringify(data) : undefined });
}
};
})();