1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
| class NetworkOptimizer { constructor() { this.requestCache = new Map(); this.requestQueue = []; this.maxConcurrent = 3; this.currentConcurrent = 0; } async smartRequest(options) { const { url, method = 'GET', data = {}, cache = true, timeout = 10000, retry = 3 } = options; if (cache && method === 'GET') { const cached = this.getCachedRequest(url, data); if (cached) { return cached; } } return new Promise((resolve, reject) => { this.requestQueue.push({ options: { url, method, data, timeout, retry }, resolve, reject }); this.processQueue(); }); } async processQueue() { if (this.currentConcurrent >= this.maxConcurrent || this.requestQueue.length === 0) { return; } this.currentConcurrent++; const request = this.requestQueue.shift(); try { const result = await this.executeRequest(request.options); if (request.options.method === 'GET') { this.setCachedRequest(request.options.url, request.options.data, result); } request.resolve(result); } catch (error) { request.reject(error); } finally { this.currentConcurrent--; this.processQueue(); } } async executeRequest(options) { const { url, method, data, timeout, retry } = options; let lastError; for (let i = 0; i < retry; i++) { try { const result = await this.makeRequest(url, method, data, timeout); return result; } catch (error) { lastError = error; if (i < retry - 1) { await this.delay(Math.pow(2, i) * 1000); } } } throw lastError; } makeRequest(url, method, data, timeout) { return new Promise((resolve, reject) => { const requestOptions = { url, method, data, timeout, success: resolve, fail: reject }; wx.request(requestOptions); }); } getCachedRequest(url, data) { const key = this.generateCacheKey(url, data); const cached = this.requestCache.get(key); if (cached && Date.now() - cached.timestamp < 300000) { return cached.data; } return null; } setCachedRequest(url, data, result) { const key = this.generateCacheKey(url, data); this.requestCache.set(key, { data: result, timestamp: Date.now() }); } generateCacheKey(url, data) { return `${url}_${JSON.stringify(data)}`; } delay(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } async batchRequest(requests) { const promises = requests.map(request => this.smartRequest(request) ); return Promise.allSettled(promises); } }
const networkOptimizer = new NetworkOptimizer();
Page({ async loadData() { try { const [userInfo, productList, config] = await Promise.all([ networkOptimizer.smartRequest({ url: '/api/user/info', cache: true }), networkOptimizer.smartRequest({ url: '/api/products', cache: true }), networkOptimizer.smartRequest({ url: '/api/config', cache: true }) ]); this.setData({ userInfo, productList, config }); } catch (error) { console.error('数据加载失败:', error); } } });
|