1// API åºç¡å°å 2const API_BASE = 'api'; 3 4// ========== å ¨å±ç¼åï¼sessionStorageï¼5åéè¿æï¼========== 5const Cache = { 6 _prefix: 'xxgk_cache_', 7 _ttl: 5 * 60 * 1000, // 5åé 8 9 get(key) { 10 try { 11 const raw = sessionStorage.getItem(this._prefix + key); 12 if (!raw) return null; 13 const obj = JSON.parse(raw); 14 if (Date.now() - obj.time > this._ttl) { 15 sessionStorage.removeItem(this._prefix + key); 16 return null; 17 } 18 return obj.data; 19 } catch (e) { return null; } 20 }, 21 22 set(key, data) { 23 try { 24 sessionStorage.setItem(this._prefix + key, JSON.stringify({ 25 time: Date.now(), 26 data: data 27 })); 28 } catch (e) {} 29 }, 30 31 clear(key) { 32 if (key) sessionStorage.removeItem(this._prefix + key); 33 else { 34 Object.keys(sessionStorage).forEach(k => { 35 if (k.startsWith(this._prefix)) sessionStorage.removeItem(k); 36 }); 37 } 38 } 39}; 40 41// èªå¨æ£æµtoken keyï¼åå°admin页é¢ç¨admin_tokenï¼åå°ç¨tokenï¼ 42function _getTokenKey() { 43 return window.location.pathname.indexOf('/admin/') > -1 ? 'admin_token' : 'token'; 44} 45 46// ç»ä¸è¯·æ±å°è£ 47function request(url, options = {}) { 48 const token = localStorage.getItem(_getTokenKey()); 49 const headers = options.headers || {}; 50 if (token) { 51 headers['Authorization'] = 'Bearer ' + token; 52 } 53 // ç¡®ä¿è·¯å¾æ¼æ¥æ£ç¡®ï¼èªå¨è¡¥ææ ï¼ 54 const fullUrl = API_BASE + (url.startsWith('/') ? url : '/' + url); 55 return fetch(fullUrl, { ...options, headers }) 56 .then(res => res.json()) 57 .catch(err => { 58 console.error('请æ±å¤±è´¥:', err); 59 throw err; 60 }); 61} 62 63// GET è¯·æ± 64function get(url, params = {}) { 65 const q = new URLSearchParams(params).toString(); 66 return request(url + (q ? '?' + q : '')); 67} 68 69// POST 表å 70function postForm(url, data = {}) { 71 const form = new URLSearchParams(); 72 Object.keys(data).forEach(k => form.append(k, data[k])); 73 return request(url, { 74 method: 'POST', 75 headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, 76 body: form 77 }); 78} 79 80// POST JSON 81function postJson(url, data = {}) { 82 return request(url, { 83 method: 'POST', 84 headers: { 'Content-Type': 'application/json' }, 85 body: JSON.stringify(data) 86 }); 87} 88 89// PUT 表å 90function putForm(url, data = {}) { 91 const form = new URLSearchParams(); 92 Object.keys(data).forEach(k => form.append(k, data[k])); 93 return request(url, { 94 method: 'PUT', 95 headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, 96 body: form 97 }); 98} 99 100// DELETE 101function del(url) { 102 return request(url, { method: 'DELETE' }); 103} 104 105// ===== API æ¥å£ ===== 106const API = { 107 // --- éç¨è¯·æ±æ¹æ³ --- 108 get: get, 109 postForm: postForm, 110 postJson: postJson, 111 putForm: putForm, 112 delete: del, 113 request: request, 114 115 // --- è®¤è¯ --- 116 login: data => postForm('/auth/login', data), 117 logout: () => postForm('/auth/logout'), 118 getUserInfo: () => get('/auth/userinfo'), 119 120 // --- æ ç®åç±» --- 121 categoryTree: () => get('/category/tree'), 122 categoryTreeByDim: dim => get('/category/tree', { dimension: dim }), 123 categoryRoot: () => get('/category/root'), 124 categoryChildren: pid => get('/category/children/' + (pid || '0')), 125 categoryDetail: id => get('/category/' + id), 126 categoryNav: dim => get('/category/nav', { dimension: dim }), 127 128 // --- ä¿¡æ¯å ¬å¼ --- 129 infoList: params => get('/information/list', params), 130 infoDetail: id => get('/information/' + id), 131 infoSearch: params => get('/information/search', params), 132 infoPublished: params => get('/information/published', params), 133 134 // --- æåï¼æå/å¶åº¦/å¹´æ¥ï¼--- 135 manualList: params => get('/manual/list', params), 136 manualDetail: code => get('/manual/detail/' + code), 137 138 // --- ä¾ç³è¯·å ¬å¼ --- 139 applySubmit: data => postJson('/apply', data), 140 applyQuery: code => get('/apply/query/' + code), 141 142 // --- éä»¶ --- 143 attachmentList: infoCode => get('/attachment/list?infoCode=' + encodeURIComponent(infoCode)), 144 attachmentDownload: id => API_BASE + '/attachment/download/' + id, 145};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.