feat: login rest
This commit is contained in:
+47
-15
@@ -38,13 +38,20 @@ class WxRequest {
|
||||
method: options.method || 'GET',
|
||||
data: options.data || {},
|
||||
header: header,
|
||||
timeout: options.timeout || 60000
|
||||
timeout: options.timeout || 60000,
|
||||
skipToken: options.skipToken || false
|
||||
};
|
||||
|
||||
// Apply request interceptor
|
||||
config = this.interceptors.request(config);
|
||||
return new Promise(async (resolve, reject) => {
|
||||
// Apply request interceptor (Async)
|
||||
try {
|
||||
config = await Promise.resolve(this.interceptors.request(config));
|
||||
} catch (ignore) {
|
||||
// If interceptor fails/rejects, likely means we shouldn't proceed
|
||||
// But for now let's just log or reject
|
||||
console.warn('Interceptor warning', ignore);
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
wx.request({
|
||||
...config,
|
||||
success: (res) => {
|
||||
@@ -114,11 +121,14 @@ class WxRequest {
|
||||
formData: formData
|
||||
};
|
||||
|
||||
// Apply request interceptor (reuse logic for token injection)
|
||||
// Note: wx.uploadFile doesn't support method/data in the same way, but we use interceptor for header mainly
|
||||
config = this.interceptors.request(config);
|
||||
// Apply request interceptor (Async)
|
||||
return new Promise(async (resolve, reject) => {
|
||||
try {
|
||||
config = await Promise.resolve(this.interceptors.request(config));
|
||||
} catch (ignore) {
|
||||
console.warn('Interceptor warning', ignore);
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
wx.uploadFile({
|
||||
url: config.url,
|
||||
filePath: config.filePath,
|
||||
@@ -182,9 +192,13 @@ class WxRequest {
|
||||
formData: formData
|
||||
};
|
||||
|
||||
config = this.interceptors.request(config);
|
||||
return new Promise(async (resolve, reject) => {
|
||||
try {
|
||||
config = await Promise.resolve(this.interceptors.request(config));
|
||||
} catch (ignore) {
|
||||
console.warn('Interceptor warning', ignore);
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
wx.uploadFile({
|
||||
url: config.url,
|
||||
filePath: config.filePath,
|
||||
@@ -238,19 +252,37 @@ class WxRequest {
|
||||
|
||||
// Initialize with default instance
|
||||
const request = new WxRequest({
|
||||
baseUrl: 'http://192.168.0.184:8889',
|
||||
//baseUrl: 'https://go.sundynix.cn/api',
|
||||
//baseUrl: 'http://192.168.0.184:8889',
|
||||
baseUrl: 'https://go.sundynix.cn/api',
|
||||
header: {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
});
|
||||
|
||||
// Example: Setup default interceptors
|
||||
request.setRequestInterceptor((config) => {
|
||||
request.setRequestInterceptor(async (config) => {
|
||||
// Skip checking token for login API itself
|
||||
if (config.url.includes('/auth/miniLogin') || config.skipToken) {
|
||||
return config;
|
||||
}
|
||||
|
||||
let token = wx.getStorageSync('token');
|
||||
|
||||
// If no token, attempt to wait for login
|
||||
if (!token) {
|
||||
const app = getApp();
|
||||
if (app && app.ensureLogin) {
|
||||
try {
|
||||
token = await app.ensureLogin();
|
||||
} catch (e) {
|
||||
// Login failed
|
||||
console.warn('Auto-login failed in interceptor', e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Inject token if available
|
||||
const token = wx.getStorageSync('token');
|
||||
if (token) {
|
||||
// User requested: Bearer + token
|
||||
config.header['Authorization'] = `Bearer ${token}`;
|
||||
}
|
||||
return config;
|
||||
|
||||
Reference in New Issue
Block a user