{"id":34055,"date":"2024-11-25T16:14:40","date_gmt":"2024-11-25T08:14:40","guid":{"rendered":"https:\/\/fwq.ai\/blog\/34055\/"},"modified":"2024-11-25T16:14:40","modified_gmt":"2024-11-25T08:14:40","slug":"%e5%85%b3%e4%ba%8e%e5%b0%8f%e7%a8%8b%e5%ba%8f%e7%9a%84request%e5%b0%81%e8%a3%85%ef%bc%88%e9%99%84%e8%af%a6%e7%bb%86%e6%b5%81%e7%a8%8b%ef%bc%89","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/34055\/","title":{"rendered":"\u5173\u4e8e\u5c0f\u7a0b\u5e8f\u7684request\u5c01\u88c5\uff08\u9644\u8be6\u7ec6\u6d41\u7a0b\uff09"},"content":{"rendered":"<p>\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u7684 request \u5c01\u88c5   <\/p>\n<h1>\u80cc\u666f<\/h1>\n<p>\u4e4b\u524d\u5c0f\u7a0b\u5e8f\u4ee3\u7801\u6df7\u4e71\uff0c\u6240\u4ee5\u65b0\u9879\u76ee\u4e00\u5f00\u59cb\u5c31\u51c6\u5907\u5f04\u4e2a\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u7684 request \u7684\u5c01\u88c5<\/p>\n<h1>\u6d41\u7a0b<\/h1>\n<p>\u5148\u6765\u8bf4\u8bf4\u6574\u4e2a\u6d41\u7a0b\uff1a<\/p>\n<p>1.appjs \u91cc\u9762\u5df2\u8fdb\u5165\u5c31\u53bb\u83b7\u53d6\u7528\u6237\u4fe1\u606f\uff0c\u5982\u679c\u6ca1\u6709\u767b\u5f55\u5219\u9ed8\u8ba4\u767b\u5f55\uff0c\u8fd9\u91cc\u4e0d\u505a\u9519\u8bef\u5904\u7406<\/p>\n<p>2.\u7528\u6237\u5fc5\u987b\u540c\u610f\u6388\u6743\u624d\u80fd\u8fdb\u884c\u64cd\u4f5c\uff0c\u5982\u679c\u4e0d\u540c\u610f\u6388\u6743\u5219\u4f1a\u4e00\u76f4\u8df3\u8f6c\u5230\u6388\u6743\u9875\u9762<\/p>\n<p>3.\u5728\u6388\u6743\u9875\u9762\u70b9\u51fb\u6388\u6743\u767b\u5f55\u540e\uff0c\u8c03\u7528\u767b\u5f55\u63a5\u53e3\uff0c\u6210\u529f\u540e\u8fd4\u56de\u8c03\u8d77\u6388\u6743\u7684\u9875\u9762\uff0c <\/p>\n<h1>app.js<\/h1>\n<p>onLaunch \u91cc\u9762\u83b7\u53d6\u7528\u6237\u4fe1\u606f<\/p>\n<pre>appSelf = this;\n        \/\/ \u5e94\u7528\u7a0b\u5e8f\u7b2c\u4e00\u6b21\u8fdb\u5165\uff0c\u83b7\u53d6\u7528\u6237\u4fe1\u606f\uff0c\u4e0d\u505a\u4efb\u4f55\u9519\u8bef\u5904\u7406\n        userInfo().then( (res)=&gt;{\n            console.log(res);\/\/ \u6253\u5370\u7ed3\u679c\n            if (!res.code) {\n                appSelf.globalData.userInfo = res\n            }\n        }).catch( (errMsg)=&gt;{\n            console.log(errMsg);\/\/ \u9519\u8bef\u63d0\u793a\u4fe1\u606f\n        });<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<h1>httpUtils.js<\/h1>\n<h2>request \u7684\u5c01\u88c5<\/h2>\n<pre>const request = function (path, method, data, header) {\n    let user_id = \"\";\n    let token = \"\";\n    try {\n        user_id = wx.getStorageSync(USER_ID_KEY);\n        token = wx.getStorageSync(TOKEN_KEY);\n    } catch (e) {}\n    header = header || {};\n    let cookie = [];\n    cookie.push(\"USERID=\" + user_id);\n    cookie.push(\"TOKEN=\" + token);\n    cookie.push(\"device=\" + 1);\n    cookie.push(\"app_name=\" + 1);\n    cookie.push(\"app_version=\" + ENV_VERSION);\n    cookie.push(\"channel=\" + 1);\n    header.cookie = cookie.join(\"; \");\n    return new Promise((resolve, reject) =&gt; {\n        wx.request({\/\/\u540e\u53f0\u8bf7\u6c42\n            url: API_BASE_URL + path,\n            header: header,\n            method: method,\n            data: data,\n            success: function (res) {\n                if (res.statusCode !== 200) {\n                    reject(res.data)\n                } else {\n                    if (res.data.code === 20006) {\n                        login().then( (res)=&gt;{\n                            resolve(res)\n                        }).catch( (errMsg)=&gt;{\n                            reject(errMsg);\n                        })\n                    }\n                    resolve(res.data)\n                }\n            },\n            fail: function (res) {\n                reject(\"not data\");\n            }\n        });\n    });\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<h2>login<\/h2>\n<pre>const login = function () {\n    try {\n        wx.removeStorageSync(USER_ID_KEY)\n        wx.removeStorageSync(TOKEN_KEY)\n    } catch (e) {}\n    return new Promise((resolve, reject) =&gt; {\n        wx.login({\n            success: res =&gt; {\n                let code = res.code;\n                \/\/ \u5df2\u7ecf\u6388\u6743\uff0c\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528 getUserInfo \u83b7\u53d6\u5934\u50cf\u6635\u79f0\uff0c\u4e0d\u4f1a\u5f39\u6846\n                wx.getUserInfo({\n                    withCredentials: true,\n                    success: res =&gt; {\n                        let userInfo = res.userInfo;\n                        let name = userInfo.nickName;\n                        let avatar = userInfo.avatarUrl;\n                        let sex = userInfo.gender;\n                        let data = {\n                            code: code,\n                            encryptedData: res.encryptedData,\n                            iv: res.iv,\n                            name: name,\n                            avatar: avatar,\n                            sex: sex,\n                            from: FROM,\n                        };\n                        request(\"\/api\/user\/login\/byWeChatApplet\", \"POST\", data).then( (res)=&gt;{\n                            if (!res.code) {\n                                try {\n                                    wx.setStorageSync(USER_ID_KEY, res.user_id);\n                                    wx.setStorageSync(TOKEN_KEY, res.token)\n                                } catch (e) {\n                                    reject(JSON.stringify(e));\n                                }\n                            }\n                            resolve(res)\n                        }).catch( (errMsg)=&gt;{\n                            reject(errMsg)\n                        });\n                    },\n                    fail: function (res) {\n                        console.log(res);\n\n                        if (res.errMsg &amp;&amp; res.errMsg.startsWith(\"getUserInfo:fail\") &amp;&amp; res.errMsg.search(\"unauthorized\") != -1) {\n                            \/\/ \u8df3\u8f6c\u6388\u6743\u9875\u9762\n                            wx.navigateTo({\n                                url: '\/pages\/auth\/auth'\n                            })\n                            return;\n                        }\n                        wx.getSetting({\n                            success: (res) =&gt; {\n                                if (!res.authSetting[\"scope.userInfo\"]) {\n                                    \/\/ \u8df3\u8f6c\u6388\u6743\u9875\u9762\n                                    wx.navigateTo({\n                                        url: '\/pages\/auth\/auth'\n                                    })\n                                }\n                            }\n                        });\n                    }\n                })\n            }\n        })\n    });\n};<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<h1>auth.js<\/h1>\n<p>\u6388\u6743\u9875\u9762 js<\/p>\n<pre>Page({\n    data: {\n    },\n    onLoad: function () {\n        self = this;\n    },\n\n    auth: function (e) {\n        console.log(app.globalData.userInfo);\n        if (e.detail.userInfo) {\n            login().then( (res)=&gt;{\n                console.log(res);\/\/ \u6253\u5370\u7ed3\u679c\n                if (res.code) {\n                    \/\/ \u63a5\u53e3\u9519\u8bef\n                    return\n                }\n                \/\/ \u8df3\u8f6c\u56de\u4e0a\u4e00\u4e2a\u9875\u9762\n                wx.navigateBack()\n            }).catch( (errMsg)=&gt;{\n                console.log(errMsg);\/\/ \u9519\u8bef\u63d0\u793a\u4fe1\u606f\n            });\n        }\n    },\n\n});<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<h1>\u9879\u76ee\u5730\u5740<\/h1>\n<p>https:\/\/github.com\/lmxdawn\/wx&#8230;<\/p>\n<p>\u4e00\u4e2a vue + thinkphp5.1 \u642d\u5efa\u7684\u540e\u53f0\u7ba1\u7406\uff1ahttps:\/\/github.com\/lmxdawn\/vu&#8230;<\/p>\n<p>\u6f14\u793a\uff1a<br \/><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/image\/364\/380\/270\/1631351141734401.png\" class=\"aligncenter\" title=\"\u5173\u4e8e\u5c0f\u7a0b\u5e8f\u7684request\u5c01\u88c5\uff08\u9644\u8be6\u7ec6\u6d41\u7a0b\uff09\u63d2\u56fe\" alt=\"\u5173\u4e8e\u5c0f\u7a0b\u5e8f\u7684request\u5c01\u88c5\uff08\u9644\u8be6\u7ec6\u6d41\u7a0b\uff09\u63d2\u56fe\" \/><\/p>\n<pre>&lt;br\/&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5173\u4e8e\u5c0f\u7a0b\u5e8f\u7684request\u5c01\u88c5\uff08\u9644\u8be6\u7ec6\u6d41\u7a0b\uff09\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u7684 request \u5c01\u88c5 \u80cc\u666f \u4e4b\u524d\u5c0f\u7a0b\u5e8f\u4ee3\u7801\u6df7\u4e71\uff0c\u6240\u4ee5\u65b0\u9879\u76ee\u4e00\u5f00\u59cb\u5c31\u51c6\u5907\u5f04\u4e2a\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u7684 request \u7684\u5c01\u88c5 \u6d41\u7a0b \u5148\u6765\u8bf4\u8bf4\u6574\u4e2a\u6d41\u7a0b\uff1a 1.appjs \u91cc\u9762\u5df2\u8fdb\u5165\u5c31\u53bb\u83b7\u53d6\u7528\u6237\u4fe1\u606f\uff0c\u5982\u679c\u6ca1\u6709\u767b\u5f55\u5219\u9ed8\u8ba4\u767b\u5f55\uff0c\u8fd9\u91cc\u4e0d\u505a\u9519\u8bef\u5904\u7406 2.\u7528\u6237\u5fc5\u987b\u540c\u610f\u6388\u6743\u624d\u80fd\u8fdb\u884c\u64cd\u4f5c\uff0c\u5982\u679c\u4e0d\u540c\u610f\u6388\u6743\u5219\u4f1a\u4e00\u76f4\u8df3\u8f6c\u5230\u6388\u6743\u9875\u9762 3.\u5728\u6388\u6743\u9875\u9762\u70b9\u51fb\u6388\u6743\u767b\u5f55\u540e\uff0c\u8c03\u7528\u767b\u5f55\u63a5\u53e3\uff0c\u6210\u529f\u540e\u8fd4\u56de\u8c03\u8d77\u6388\u6743\u7684\u9875\u9762\uff0c app.js onLaunch \u91cc\u9762\u83b7\u53d6\u7528\u6237\u4fe1\u606f appSelf = this; \/\/ \u5e94\u7528\u7a0b\u5e8f\u7b2c\u4e00\u6b21\u8fdb\u5165\uff0c\u83b7\u53d6\u7528\u6237\u4fe1\u606f\uff0c\u4e0d\u505a\u4efb\u4f55\u9519\u8bef\u5904\u7406 userInfo().then( (res)=&gt;{ console.log(res);\/\/ \u6253\u5370\u7ed3\u679c if (!res.code) { appSelf.globalData.userInfo = res } }).catch( (errMsg)=&gt;{ console.log(errMsg);\/\/ \u9519\u8bef\u63d0\u793a\u4fe1\u606f }); \u767b\u5f55\u540e\u590d\u5236 httpUtils.js request \u7684\u5c01\u88c5 const request = function (path, method, data, header) { let user_id = &#8220;&#8221;; let [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-34055","post","type-post","status-publish","format-standard","hentry","category-19"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/34055","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=34055"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/34055\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=34055"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=34055"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=34055"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}