{"id":441,"date":"2025-11-16T14:53:00","date_gmt":"2025-11-16T06:53:00","guid":{"rendered":"https:\/\/mitongxue.cn\/?p=441"},"modified":"2026-06-16T15:37:55","modified_gmt":"2026-06-16T07:37:55","slug":"http%e6%8e%a5%e5%8f%a3%e5%b0%81%e8%a3%85","status":"publish","type":"post","link":"https:\/\/mitongxue.cn\/index.php\/2025\/11\/16\/http%e6%8e%a5%e5%8f%a3%e5%b0%81%e8%a3%85\/","title":{"rendered":"http\u63a5\u53e3\u5c01\u88c5"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">http.js<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>import { qs } from 'qs';\nimport { message} from 'antd';\nimport { history } from 'umi';\nconst fetch =  require('dva').fetch;\n\nconst CheckStatus = res=>{\n    if(res.status === 200) {\n        return res;\n    }\n    message.error(`\u7f51\u7edc\u9519\u8bef ${res.status}`);\n    const error = res.statusText;\n    throw new Error(error);\n}\nconst judgeOkStatus = async(res)=>{\n    const cloneRes = await res.clone().json();\n    if(cloneRes.code !== 0) {\n        message.error(`${cloneRes.msg}&#91;${cloneRes.msg}]`);\n        \/\/\u8df3\u8f6c\u5230\u767b\u5f55\u9875\u9762\n        history.replace('\/users\/login');\n        \/\/ \u6e05\u9664token\n        sessionStorage.clear();\n    }\n    return res;\n}\nconst handlerError = err=>{\n   if(err instanceof TypeError) {\n        message.error(`\u7f51\u7edc\u8bf7\u6c42\u5931\u8d25&#91;${err}]`);\n   }\n   return {\n    code: -1,\n    data: false,\n    msg: `\u7f51\u7edc\u8bf7\u6c42\u5931\u8d25&#91;${err}]`\n   }\n}\n\nclass Http { \n    static async staticFetch(url=\"\", options={}) { \n        url = '\/react_oa_api' + url;\n        const defaultOptions = {\n            mode: 'cors', \/\/ \u652f\u6301\u8de8\u57df\uff0c\u4ee5CORS\u6a21\u5f0f\u53d1\u9001\u8bf7\u6c42\n            headers: {\n                Authorization: sessionStorage.getItem('token') || null,\n            },\n        }\n        if(options.method==='POST' || options.method==='PUT') {\n            defaultOptions.headers&#91;'Content-Type'] = 'application\/json';\n        }\n        const newOptions = {...defaultOptions, ...options};\n        return fetch(url, newOptions)\n        .then(CheckStatus)\n        .then(judgeOkStatus)\n        .then(res=>{\n            \/\/\u83b7\u53d6token\n            const token = res.headers.get('Authorization');\n            if(token) {\n                sessionStorage.setItem('token', token);\n            }\n            return res.json();\n        })\n        .cache(handlerError);\n    }\n\n    post(url,params = &#91;],option = {}){\n        const options = Object.assign({\n            method: 'POST',\n        },option);\n        options.body = JSON.stringify(params);\n        return Http.staticFetch(url, options);\n    }\n\n     put(url,params = &#91;],option = {}){\n        const options = Object.assign({\n            method: 'PUT',\n        },option);\n        options.body = JSON.stringify(params);\n        return Http.staticFetch(url, options);\n    }\n\n    get(url, option = {}){\n        const options = Object.assign({\n            method: 'GET',\n        },option);\n        Object.keys(option) &amp;&amp; (url += '?' + qs.stringify(option));\n        return Http.staticFetch(url, options);\n    }\n\n    delete(url, option = {}){\n        const options = Object.assign({\n            method: 'DELETE',\n        },option);\n        Object.keys(option) &amp;&amp; (url += '?' + qs.stringify(option));\n        return Http.staticFetch(url, options);\n    }\n\n\n}\n\nconst resFun = new Http();\nexport default resFun;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">user.js<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>import ajax from '..\/http';\n\nexport const userLogin = (params) => ajax.post('\/api\/user\/login', params);<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">index.js<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/webpack \u7684 require.context\u65b9\u6cd5\n\nconst requireApi = require.context('.', true, \/\\.js$\/);\nconst module = {};\nrequireApi.keys().forEach(key => {\n    if(key === '.\/index.js' || key === '.\/http.js') return;\n    Object.assign(module, requireApi(key));\n});\nexport default module;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>http.js user.js index.js<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,11],"tags":[],"class_list":["post-441","post","type-post","status-publish","format-standard","hentry","category-js","category-front"],"featured_image_urls":{"full":"","thumbnail":"","medium":"","medium_large":"","large":"","1536x1536":"","2048x2048":""},"author_info":{"info":["\u9648 \u67d0\u4eba"]},"category_info":"<a href=\"https:\/\/mitongxue.cn\/index.php\/category\/front\/js\/\" rel=\"category tag\">JS<\/a> <a href=\"https:\/\/mitongxue.cn\/index.php\/category\/front\/\" rel=\"category tag\">\u524d\u7aef<\/a>","tag_info":"\u524d\u7aef","comment_count":"0","_links":{"self":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/441","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/comments?post=441"}],"version-history":[{"count":1,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/441\/revisions"}],"predecessor-version":[{"id":442,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/441\/revisions\/442"}],"wp:attachment":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/media?parent=441"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/categories?post=441"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/tags?post=441"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}