PageSourceSearch

https://anguo.gov.cn/anguoxxgk/js/api.js

js anguo.gov.cn collected 2026-10-02 22:03:11 UTC 4,525 bytes, 145 lines download raw bytes

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.