feat(api): 从URL参数中获取token并自动添加到请求头

- 新增getToken方法,从URL参数中获取token,返回空字符串或token值
- 新增buildHeaders方法,构建请求头并自动添加token字段
- 所有HTTP请求方法(get, post, patch, del)中,统一调用buildHeaders设置请求头
- 在ApiService中暴露getToken方法方便外部调用
- 改进请求流程,确保带token的请求能够正确发送验证信息
This commit is contained in:
zhouyonggao 2025-12-23 11:36:43 +08:00
parent d3250d864d
commit 4742b3db05
1 changed files with 33 additions and 7 deletions

View File

@ -49,6 +49,16 @@ const getMobile = () => {
return value && String(value).trim() ? String(value).trim() : ''; return value && String(value).trim() ? String(value).trim() : '';
}; };
/**
* URL 参数中获取 token
* @returns {string} token不存在返回空字符串
*/
const getToken = () => {
const params = new URLSearchParams(window.location.search || '');
const value = params.get('token');
return value && String(value).trim() ? String(value).trim() : '';
};
/** /**
* URL 参数中获取商户 ID * URL 参数中获取商户 ID
* @returns {string} 商户 ID不存在返回空字符串 * @returns {string} 商户 ID不存在返回空字符串
@ -97,6 +107,19 @@ const parsePaginatedResponse = (data) => {
}; };
}; };
/**
* 预处理请求 headers添加 token
* @returns {Object} headers 对象
*/
const buildHeaders = () => {
const headers = { 'Content-Type': 'application/json' };
const token = getToken();
if (token) {
headers['token'] = token;
}
return headers;
};
/** /**
* 通用 GET 请求 * 通用 GET 请求
* @param {string} endpoint - API 端点 * @param {string} endpoint - API 端点
@ -124,7 +147,9 @@ const get = async (endpoint, options = {}) => {
url += (endpoint.includes('?') ? '&' : '?') + queryString; url += (endpoint.includes('?') ? '&' : '?') + queryString;
} }
const response = await fetch(url); const response = await fetch(url, {
headers: buildHeaders()
});
if (!response.ok) { if (!response.ok) {
throw new Error(`请求失败: ${response.status} ${response.statusText}`); throw new Error(`请求失败: ${response.status} ${response.statusText}`);
} }
@ -149,7 +174,7 @@ const post = async (endpoint, body, options = {}) => {
const response = await fetch(url, { const response = await fetch(url, {
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/json' }, headers: buildHeaders(),
body: JSON.stringify(body) body: JSON.stringify(body)
}); });
@ -181,10 +206,7 @@ const patch = async (endpoint, body) => {
const url = API_BASE + endpoint; const url = API_BASE + endpoint;
const response = await fetch(url, { const response = await fetch(url, {
method: 'PATCH', method: 'PATCH',
headers: { headers: buildHeaders(),
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: JSON.stringify(body) body: JSON.stringify(body)
}); });
@ -209,7 +231,10 @@ const del = async (endpoint, options = {}) => {
url += (endpoint.includes('?') ? '&' : '?') + 'soft=1'; url += (endpoint.includes('?') ? '&' : '?') + 'soft=1';
} }
const response = await fetch(url, { method: 'DELETE' }); const response = await fetch(url, {
method: 'DELETE',
headers: buildHeaders()
});
if (!response.ok) { if (!response.ok) {
const errorText = await response.text(); const errorText = await response.text();
throw new Error(errorText || `请求失败: ${response.status}`); throw new Error(errorText || `请求失败: ${response.status}`);
@ -435,6 +460,7 @@ window.ApiService = {
API_BASE, API_BASE,
getUserId, getUserId,
getMobile, getMobile,
getToken,
hasOnlyUserId, hasOnlyUserId,
getMerchantId, getMerchantId,
buildUserQueryString, buildUserQueryString,