{"id":32482,"date":"2024-11-25T09:14:13","date_gmt":"2024-11-25T01:14:13","guid":{"rendered":"https:\/\/fwq.ai\/blog\/32482\/"},"modified":"2024-11-25T09:14:13","modified_gmt":"2024-11-25T01:14:13","slug":"%e5%be%ae%e4%bf%a1%e5%b0%8f%e7%a8%8b%e5%ba%8f%e6%94%af%e6%8c%81-cookie%e7%9a%84%e4%bb%a3%e7%a0%81%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/32482\/","title":{"rendered":"\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u652f\u6301 cookie\u7684\u4ee3\u7801\u5b9e\u73b0"},"content":{"rendered":"<p>\u672c\u7bc7\u6587\u7ae0\u7ed9\u5927\u5bb6\u5e26\u6765\u7684\u5185\u5bb9\u662f\u5173\u4e8e\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u652f\u6301 cookie\u7684\u4ee3\u7801\u5b9e\u73b0\uff0c\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u6709\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<p><strong>weapp-cookie<\/strong><\/p>\n<p>\u4e00\u884c\u4ee3\u7801\u8ba9\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u652f\u6301 cookie\uff0c\u4f20\u9001\u95e8\uff1a<\/p>\n<p><strong>Intro<\/strong><\/p>\n<p>\u5fae\u4fe1\u539f\u751f\u7684 wx.request \u7f51\u7edc\u8bf7\u6c42\u63a5\u53e3\u5e76\u4e0d\u652f\u6301\u4f20\u7edf\u7684 Cookie\uff0c\u4f46\u6709\u65f6\u5019\u6211\u4eec\u73b0\u6709\u7684\u540e\u7aef\u63a5\u53e3\u786e\u4e8e\u4f9d\u8d56 Cookie\uff08\u6bd4\u5982\u670d\u52a1\u5668\u7528\u6237\u767b\u5f55\u6001\uff09\uff0c\u8fd9\u4e2a\u5e93\u53ef\u7528\u4e00\u884c\u4ee3\u7801\u4e3a\u4f60\u7684\u5c0f\u7a0b\u5e8f\u5b9e\u73b0 Cookie \u673a\u5236\uff0c\u4ee5\u4fdd\u8bc1\u57fa\u4e8e cookie \u7684\u670d\u52a1\u4f1a\u8bdd\u4e0d\u4f1a\u5931\u6548\uff0c\u4e0e web \u7aef\u5171\u7528\u4f1a\u8bdd\u673a\u5236<\/p>\n<p><strong>Featrues<\/strong><\/p>\n<ul>\n<li>\n<p>\u4e00\u884c\u4ee3\u7801\u8ba9\u5c0f\u7a0b\u5e8f\u652f\u6301 cookie<\/p>\n<\/li>\n<li>\n<p>\u53ef\u4f7f\u7528 api \u83b7\u53d6\u3001\u8bbe\u7f6e cookie<\/p>\n<\/li>\n<li>\n<p>\u652f\u6301 domain\/path \u4f5c\u7528\u57df<\/p>\n<\/li>\n<\/ul>\n<p><strong>Install<\/strong><\/p>\n<pre>npm install weapp-cookie --save\n\n# \u5c06 npm \u5305\u590d\u5236\u5230 vendor \u6587\u4ef6\u5939\uff0c\u907f\u514d\u5c0f\u7a0b\u5e8f\u53ef\u80fd\u4e0d\u80fd\u627e\u5230\u6587\u4ef6\uff08tips\uff1a\u4f7f\u7528 wepy\/mpvue \u7b49\u6846\u67b6\u65e0\u9700\u6b64\u6b65\uff09\ncp -rf .\/node_modules\/ .\/vendor\/<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>Usage<\/strong><\/p>\n<p>\u5728\u5c0f\u7a0b\u5e8f\u6839\u76ee\u5f55\u7684 app.js \u4e00\u884c\u4ee3\u7801\u5f15\u5165\u5373\u53ef<\/p>\n<pre>\/\/ app.js\nimport '.\/vendor\/weapp-cookie\/index'\n\n\/\/ tips: \u4f7f\u7528 wepy\/mpvue \u53ef\u4ee5\u76f4\u63a5\u5728\u5165\u53e3 js \u5f15\u5165 weapp-cookie \u6a21\u5757\n\/\/ import 'weapp-cookie'\n\nApp({\n    onLaunch: function () { }\n    \/\/ ...\n})<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u539f\u6765\u7684 wx.request \u8c03\u7528\u65b9\u5f0f\u4fdd\u6301\u4e0d\u53d8\uff0c\u5f15\u5165\u540e weapp-cookie \u4f1a\u5728\u5e95\u5c42\u81ea\u52a8\u4ee3\u7406 wx.request \u7684\u63a5\u53e3\u8bbf\u95ee\uff0c\u4ee5\u652f\u6301 cookie \u5b58\u50a8\u548c\u53d1\u9001<\/p>\n<pre>\/\/ pages\/home\/index.js\n\nPage({\n    onLoad: function () {\n        wx.request({\n            url: 'https:\/\/example.com\/login',\n            data: {\n                username: 'admin',\n                password: '123456'\n            },\n            success: function (res) {\n                \/*\n                 * \u63a5\u53e3\u8c03\u7528\u6210\u529f\u540e weapp-cookie \u4f1a\u81ea\u52a8\u4fdd\u5b58\u540e\u7aef\u53d1\u9001\u7684\u6240\u6709Cookie\uff08\u6bd4\u5982\uff1aSessionID\uff09\n                 * \u5e76\u5728\u540e\u7eed\u7684\u6240\u6709\u8bf7\u6c42\u4e2d\u5e26\u4e0a\uff0c\u4ee5\u4fdd\u8bc1\u57fa\u4e8e cookie \u7684\u670d\u52a1\u5668\u4f1a\u8bdd\u673a\u5236\u4e0d\u4f1a\u5931\u6548\uff0c\n                 * \u5b9e\u73b0\u4e0e web \u7aef\u5171\u7528\u4f1a\u8bdd\u673a\u5236\uff08\u65e0\u9700\u518d\u624b\u52a8\u7ef4\u62a4 3rd_session_key\uff09 \n                 *\/\n            }\n        })\n    }\n})<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>cookie \u64cd\u4f5c\u53ef\u901a\u8fc7 api \u8c03\u7528<\/p>\n<pre>import cookies from 'weapp-cookie'\n\n\/\/ \u83b7\u53d6 cookie\nlet token = cookies.get('csrf_token', 'example.com')\n\n\/\/ \u8bbe\u7f6e cookie\nlet cookie = cookies.set('uid', 100, { domain: 'example.com' })\n\n\/\/ \u5220\u9664 cookie\nlet isRemoved = cookies.remove('uid', 'example.com')\n\n\/\/ \u5224\u65ad\u662f\u5426\u5b58\u5728 cookie\nlet hasToken = cookies.has('uid', 'example.com')\n\n\/\/ ... \u8be6\u60c5\u8bf7\u53c2\u8003 Api<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>Api<\/strong><\/p>\n<p><strong>CookieStore<\/strong><\/p>\n<pre>import cookies from 'weapp-cookie'\n\n\/**\n* \u83b7\u53d6 cookie \u503c\n* @param {String} name       cookie \u540d\u79f0\n* @param {String} [domain]   \u6307\u5b9a\u57df\u540d\uff08\u53ef\u9009\uff09\n* @return {String}           cookie \u503c\n*\/\ncookies.get(String name, String domain)\n\n\/**\n* \u8bbe\u7f6e cookie\n* @param {String}  name              cookie \u540d\u79f0\n* @param {String}  value             cookie \u503c\n* @param {Object}  options           cookie \u9009\u9879\n* @param {String}  options.domain    \u8bbe\u7f6e\u57df\u540d\n* @param {String}  [options.path]      \n* @param {Date}    [options.expires]\n* @param {Number}  [options.maxAge]\n* @param {Boolean} [options.httpOnly]\n* @return {Cookie}           cookie \u5bf9\u8c61\n*\/\ncookies.set(String name, String value, Object options)\n\n\/**\n* \u662f\u5426\u5b58\u5728\u67d0\u4e2a cookie\n* @param  {String}  name       cookie \u540d\u79f0\n* @param  {String}  [domain]   \u6307\u5b9a\u57df\u540d\uff08\u53ef\u9009\uff0c\u4e0d\u6307\u5b9a\u5219\u4efb\u610f\u57df\u540d\u5305\u542b\u540d\u79f0\u4e3a name \u7684 cokkie \u5373\u4e3a\u5b58\u5728\uff09\n* @return {Boolean}            \u662f\u5426\u5b58\u5728\n*\/\ncookies.has(String name, String domain)\n\n\/**\n* \u5220\u9664 cookie\n* @param  {Array}  name      cookie \u952e\n* @param  {String} [domain]  \u6307\u5b9a\u57df\u540d\uff08\u53ef\u9009\uff0c\u4e0d\u6307\u5b9a\u5219\u5220\u9664\u6240\u6709\u57df\u540d\u4e2d\u540d\u79f0\u4e3a name \u7684 cookie\uff09\n* @return {Boolean}          \u662f\u5426\u5220\u9664\u6210\u529f\n*\/\ncookies.remove(String name, String domain)\n\n\/**\n* \u83b7\u53d6 cookie \u5bf9\u8c61\n* @param {String} name       cookie \u540d\u79f0\n* @param {String} [domain]   \u6307\u5b9a\u57df\u540d\uff08\u53ef\u9009\uff09\n* @return {Cookie}           cookie \u5bf9\u8c61\n*\/\ncookies.getCookie(String name, String domain)\n\n\/**\n* \u83b7\u53d6 cookies JSON\u5bf9\u8c61\n* @param  {String} [domain]  \u6307\u5b9a\u57df\u540d\uff08\u53ef\u9009\uff0c\u4e0d\u6307\u5b9a\u5219\u83b7\u53d6\u5305\u542b\u6240\u6709\u57df\u540d\u7684 cookie \u503c\u5bf9\u8c61\uff09\n* @return {Object}           cookie JSON\u5bf9\u8c61\n*\/\ncookies.getCookies(String domain)\n\n\/**\n* \u6e05\u9664 cookie\n* @param  {String} [domain]  \u6307\u5b9a\u57df\u540d\uff08\u53ef\u9009\uff0c\u4e0d\u6307\u5b9a\u5219\u6e05\u9664\u6240\u6709\u57df\u540d cookie\uff09\n* @return {Boolean}          \u662f\u5426\u6e05\u9664\u6210\u529f\n*\/\ncookies.clearCookies (domain)\n\n\/**\n* \u83b7\u53d6\u6240\u6709\u5b58\u50a8\u7684\u57df\u540d\u548c cookies \u7ed3\u6784\n* @return {Object}   obj   \u7ed3\u6784JSON\u5bf9\u8c61\n*\/\ncookies.dir(domain)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>Cookie<\/strong><\/p>\n<pre>import cookies from 'weapp-cookie'\n\n\/\/ \u83b7\u53d6 cookie \u5bf9\u8c61\nlet cookie = cookies.getCookie('uuid', 'example.com')\n\n\/\/ ===== cookie \u5c5e\u6027 =====\ncookie.name:        String\ncookie.value:       String\ncookie.domain:      String\ncookie.path:        String\ncookie.expires:     Date\ncookie.maxAge:      Number\ncookie.httpOnly:    Boolean\n\n\/\/ ===== cookie \u65b9\u6cd5 =====\n\n\/**\n * \u9a8c\u8bc1 cookie \u662f\u5426\u8fc7\u671f\n * @return {Boolean} \u662f\u5426\u8fc7\u671f\n *\/\ncookie.isExpired()\n\n\/**\n * \u9a8c\u8bc1 cookie \u662f\u5426\u53ef\u6301\u4e45\u5316\n * @return {Boolean} \u662f\u5426\u53ef\u6301\u4e45\u5316\n *\/\ncookie.isPersistence()<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u652f\u6301 cookie\u7684\u4ee3\u7801\u5b9e\u73b0\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>\u672c\u7bc7\u6587\u7ae0\u7ed9\u5927\u5bb6\u5e26\u6765\u7684\u5185\u5bb9\u662f\u5173\u4e8e\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u652f\u6301 cookie\u7684\u4ee3\u7801\u5b9e\u73b0\uff0c\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u6709\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\u3002 weapp-cookie \u4e00\u884c\u4ee3\u7801\u8ba9\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u652f\u6301 cookie\uff0c\u4f20\u9001\u95e8\uff1a Intro \u5fae\u4fe1\u539f\u751f\u7684 wx.request \u7f51\u7edc\u8bf7\u6c42\u63a5\u53e3\u5e76\u4e0d\u652f\u6301\u4f20\u7edf\u7684 Cookie\uff0c\u4f46\u6709\u65f6\u5019\u6211\u4eec\u73b0\u6709\u7684\u540e\u7aef\u63a5\u53e3\u786e\u4e8e\u4f9d\u8d56 Cookie\uff08\u6bd4\u5982\u670d\u52a1\u5668\u7528\u6237\u767b\u5f55\u6001\uff09\uff0c\u8fd9\u4e2a\u5e93\u53ef\u7528\u4e00\u884c\u4ee3\u7801\u4e3a\u4f60\u7684\u5c0f\u7a0b\u5e8f\u5b9e\u73b0 Cookie \u673a\u5236\uff0c\u4ee5\u4fdd\u8bc1\u57fa\u4e8e cookie \u7684\u670d\u52a1\u4f1a\u8bdd\u4e0d\u4f1a\u5931\u6548\uff0c\u4e0e web \u7aef\u5171\u7528\u4f1a\u8bdd\u673a\u5236 Featrues \u4e00\u884c\u4ee3\u7801\u8ba9\u5c0f\u7a0b\u5e8f\u652f\u6301 cookie \u53ef\u4f7f\u7528 api \u83b7\u53d6\u3001\u8bbe\u7f6e cookie \u652f\u6301 domain\/path \u4f5c\u7528\u57df Install npm install weapp-cookie &#8211;save # \u5c06 npm \u5305\u590d\u5236\u5230 vendor \u6587\u4ef6\u5939\uff0c\u907f\u514d\u5c0f\u7a0b\u5e8f\u53ef\u80fd\u4e0d\u80fd\u627e\u5230\u6587\u4ef6\uff08tips\uff1a\u4f7f\u7528 wepy\/mpvue \u7b49\u6846\u67b6\u65e0\u9700\u6b64\u6b65\uff09 cp -rf .\/node_modules\/ .\/vendor\/ \u767b\u5f55\u540e\u590d\u5236 Usage \u5728\u5c0f\u7a0b\u5e8f\u6839\u76ee\u5f55\u7684 app.js \u4e00\u884c\u4ee3\u7801\u5f15\u5165\u5373\u53ef \/\/ app.js import &#8216;.\/vendor\/weapp-cookie\/index&#8217; \/\/ tips: [&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-32482","post","type-post","status-publish","format-standard","hentry","category-19"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/32482","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=32482"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/32482\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=32482"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=32482"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=32482"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}