(function() { window.uni = window.uni || {}; // 1. Network Requests uni.request = function(options) { let headers = options.header || {}; let body = undefined; let url = options.url; // Handle Content-Type and Body if (options.method === 'POST') { if (!headers['Content-Type']) { headers['Content-Type'] = 'application/json'; } if (headers['Content-Type'].includes('application/x-www-form-urlencoded')) { if (options.data) { body = Object.keys(options.data) .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(options.data[key])}`) .join('&'); } } else { // Default to JSON if (options.data) { body = JSON.stringify(options.data); } } } else if (options.method === 'GET' && options.data) { // Append query params for GET const queryString = Object.keys(options.data) .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(options.data[key])}`) .join('&'); url += (url.includes('?') ? '&' : '?') + queryString; } fetch(url, { method: options.method || 'GET', headers: headers, body: body }) .then(response => { // You might want to handle non-200 status codes here if needed, // but uni.request success callback usually fires even for 4xx/5xx // with the statusCode in the result. return response.json().then(data => ({ data: data, statusCode: response.status, header: response.headers })).catch(() => ({ data: null, // or text statusCode: response.status, header: response.headers })); }) .then(res => { if (options.success) { options.success(res); } }) .catch(err => { console.error('uni.request error:', err); if (options.fail) { options.fail(err); } }); }; // 2. Storage uni.setStorageSync = function(key, data) { try { // uniApp stores data as is, but localStorage only supports strings. // We wrap it to handle types like numbers or booleans correctly upon retrieval if we wanted to match uni strictly, // but JSON.stringify is the standard web way. window.localStorage.setItem(key, JSON.stringify(data)); } catch (e) { console.error('setStorageSync error', e); } }; uni.getStorageSync = function(key) { try { const value = window.localStorage.getItem(key); if (value === null) return ''; // uniApp returns empty string if not found return JSON.parse(value); } catch (e) { return window.localStorage.getItem(key); // Fallback for non-JSON } }; uni.setStorage = function(obj) { try { uni.setStorageSync(obj.key, obj.data); if(obj.success) obj.success(); } catch(e) { if(obj.fail) obj.fail(e); } } uni.getStorage = function(obj) { try { const res = uni.getStorageSync(obj.key); if(obj.success) obj.success({ data: res }); } catch(e) { if(obj.fail) obj.fail(e); } } uni.removeStorageSync = function(key) { window.localStorage.removeItem(key); } // 3. Interaction uni.showToast = function(options) { // Simple alert or console log for now. // In a real implementation, creating a DOM element for toast is better. // const title = options.title || ''; // const icon = options.icon || 'none'; // console.log(`[Toast] ${title} (${icon})`); // // alert(title); // Alert is too intrusive // Create a simple custom toast const toast = document.createElement('div'); toast.style.position = 'fixed'; toast.style.top = '50%'; toast.style.left = '50%'; toast.style.transform = 'translate(-50%, -50%)'; toast.style.backgroundColor = 'rgba(0,0,0,0.7)'; toast.style.color = '#fff'; toast.style.padding = '10px 20px'; toast.style.borderRadius = '5px'; toast.style.zIndex = '9999'; toast.innerText = options.title; document.body.appendChild(toast); setTimeout(() => { document.body.removeChild(toast); }, options.duration || 1500); }; uni.showLoading = function(options) { // console.log('[Loading]', options); } uni.hideLoading = function() { // console.log('[HideLoading]'); } // 4. Navigation uni.navigateTo = function(options) { window.location.href = options.url; }; // 5. System Info uni.getSystemInfoSync = function() { return { appVersion: '1.0.0', // Mock platform: 'devtools' // Mock }; }; // 6. Global App Object Mock window.getApp = function() { return { globalData: { defaultMatchLogo: 'https://orienteering.beswell.com/card/nanning/logo.png' // Placeholder }, $cardconfigType: 'remote' }; }; // 7. App Action Helper (Mocking native bridge) window.appAction = function(url, actType) { console.log('[appAction]', url); if (url.startsWith('action://')) { if (url.includes('to_login')) { alert('请先登录 (模拟跳转)'); } else if (url.includes('to_home')) { window.location.href = 'index.html'; } else if (url.includes('to_detail')) { // Extract ID if possible or just alert alert('正在进入比赛... (模拟跳转)'); } } else { window.location.href = url; } }; })();