{"id":31437,"date":"2024-11-25T10:52:21","date_gmt":"2024-11-25T02:52:21","guid":{"rendered":"https:\/\/fwq.ai\/blog\/31437\/"},"modified":"2024-11-25T10:52:21","modified_gmt":"2024-11-25T02:52:21","slug":"%e5%b0%8f%e7%a8%8b%e5%ba%8f%e8%8e%b7%e5%8f%96openid%e5%8f%8a%e7%94%a8%e6%88%b7%e4%bf%a1%e6%81%af%e7%9a%84%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/31437\/","title":{"rendered":"\u5c0f\u7a0b\u5e8f\u83b7\u53d6openid\u53ca\u7528\u6237\u4fe1\u606f\u7684\u65b9\u6cd5"},"content":{"rendered":"<p style=\"text-align: left\">\u8fd9\u6b21\u7ed9\u5927\u5bb6\u5e26\u6765\u5c0f\u7a0b\u5e8f\u83b7\u53d6openid\u53ca\u7528\u6237\u4fe1\u606f\u7684\u65b9\u6cd5\uff0c\u5c0f\u7a0b\u5e8f\u83b7\u53d6openid\u53ca\u7528\u6237\u4fe1\u606f\u7684\u6709\u54ea\u4e9b\uff0c\u4e0b\u9762\u5c31\u662f\u5b9e\u6218\u6848\u4f8b\uff0c\u4e00\u8d77\u6765\u770b\u4e00\u4e0b\u3002<\/p>\n<p style=\"text-align: left\">\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u83b7\u53d6openid\u53ca\u7528\u6237\u4fe1\u606f\u7684\u65b9\u6cd5<\/p>\n<p style=\"text-align: left\"><strong>1. \u83b7\u53d6openid<\/strong><\/p>\n<p style=\"text-align: left\"><span style=\"color: #800000\"><strong>1.1 \u83b7\u53d6code<\/strong><\/span><\/p>\n<p style=\"text-align: left\">\u8c03\u7528\u63a5\u53e3\u83b7\u53d6\u767b\u5f55\u51ed\u8bc1\uff08code\uff09\u8fdb\u800c\u6362\u53d6\u6001\u4fe1\u606f\uff0c\u5305\u62ec\u7528\u6237\u7684\u552f\u4e00\u6807\u8bc6\uff08openid\uff09 \u53ca\u672c\u6b21\u767b\u5f55\u7684\u4f1a\u8bdd\u5bc6\u94a5\uff08_key\uff09\u3002\u7528\u6237\u6570\u636e\u7684\u52a0\u89e3\u5bc6\u901a\u8baf\u9700\u8981\u4f9d\u8d56\u4f1a\u8bdd\u5bc6\u94a5\u5b8c\u6210\u3002<\/p>\n<pre>wx.login({\r\n \/\/\u83b7\u53d6code\r\n success: function(res) {\r\n   code = res.code \/\/\u8fd4\u56decode\r\n }\r\n})<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\"><span style=\"color: #800000\"><strong>1.2 \u83b7\u53d6openid<\/strong><\/span><\/p>\n<p style=\"text-align: left\">\u62ff\u5230\u4e0a\u4e00\u6b65\u83b7\u53d6\u7684code\uff0c\u7ed3\u5408\u5c0f\u7a0b\u5e8f appid \u548c secret \u8bf7\u6c42\u63a5\u53e3api.weixin.qq.com\/sns\/jscode2session?appid=APPID&amp;secret=SECRET&amp;js_code=JSCODE&amp;grant_type=authorization_code \u6362\u53d6openid\uff0c\u4e0e openid \u4e00\u540c\u88ab\u8fd4\u56de\u7684\uff0c\u8fd8\u5305\u62ec session_key\uff0c\u5176\u4e2d session_key \u662f\u5bf9\u7528\u6237\u6570\u636e\u8fdb\u884c\u52a0\u5bc6\u7b7e\u540d\u7684\u5bc6\u94a5\u3002\u4e3a\u4e86\u81ea\u8eab\u5e94\u7528\u5b89\u5168\uff0csession_key \u4e0d\u5e94\u8be5\u5728\u7f51\u7edc\u4e0a\u4f20\u8f93\u3002<\/p>\n<pre>wx.request({\r\n url: 'https:\/\/api.weixin.qq.com\/sns\/jscode2session?appid=APPID&amp;secret=SECRET&amp;js_code='+ code +'&amp;grant_type=authorization_code',\r\n data: {},\r\n header: {\r\n   'content-type': 'application\/json'\r\n },\r\n success: function(res) {\r\n  openid = res.data.openid \/\/\u8fd4\u56deopenid\r\n }\r\n})<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\"><strong>2. \u83b7\u53d6\u7528\u6237\u4fe1\u606f<\/strong><\/p>\n<p style=\"text-align: left\"><span style=\"color: #800000\"><strong>2.1 \u5728app.js\u4e2d\u521b\u5efa\u8be5\u5168\u5c40\u65b9\u6cd5<\/strong><\/span><\/p>\n<pre>\/\/app.js\r\ngetUserInfo:function(cb){\r\n var that = this\r\n if(this.globalData.personInfo){\r\n  typeof cb == \"function\" &amp;&amp; cb(this.globalData.personInfo)\r\n }else{\r\n  \/\/\u8c03\u7528\u767b\u5f55\u63a5\u53e3\r\n  wx.login({\r\n   success: function () {\r\n    wx.getUserInfo({\r\n     success: function (res) {\r\n      that.globalData.personInfo = res.userInfo\r\n      typeof cb == \"function\" &amp;&amp; cb(that.globalData.personInfo)\r\n     }\r\n    })\r\n   }\r\n  })\r\n }\r\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\"><span style=\"color: #800000\"><strong>2.2 \u5b9e\u4f8b\u5316\u5168\u5c40\u65b9\u6cd5\u83b7\u53d6\u7528\u6237\u4fe1\u606f<\/strong><\/span><\/p>\n<pre>var that = this;\r\n\/\/\u8c03\u7528\u5e94\u7528\u5b9e\u4f8b\u7684\u65b9\u6cd5\u83b7\u53d6\u5168\u5c40\u6570\u636e\r\napp.getUserInfo(function (personInfo) {\r\n \/\/\u66f4\u65b0\u6570\u636e\r\n that.setData({\r\n  personInfo: personInfo\r\n })\r\n})<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u76f8\u4fe1\u770b\u4e86\u672c\u6587\u6848\u4f8b\u4f60\u5df2\u7ecf\u638c\u63e1\u4e86\u65b9\u6cd5\uff0c\u66f4\u591a\u7cbe\u5f69\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n<p>\u63a8\u8350\u9605\u8bfb\uff1a<\/p>\n<p style=\"text-align: left\"><\/p>\n<p style=\"text-align: left\"><\/p>\n<p style=\"text-align: left\"><\/p>\n<p style=\"text-align: left\"><\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5c0f\u7a0b\u5e8f\u83b7\u53d6openid\u53ca\u7528\u6237\u4fe1\u606f\u7684\u65b9\u6cd5\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>\u8fd9\u6b21\u7ed9\u5927\u5bb6\u5e26\u6765\u5c0f\u7a0b\u5e8f\u83b7\u53d6openid\u53ca\u7528\u6237\u4fe1\u606f\u7684\u65b9\u6cd5\uff0c\u5c0f\u7a0b\u5e8f\u83b7\u53d6openid\u53ca\u7528\u6237\u4fe1\u606f\u7684\u6709\u54ea\u4e9b\uff0c\u4e0b\u9762\u5c31\u662f\u5b9e\u6218\u6848\u4f8b\uff0c\u4e00\u8d77\u6765\u770b\u4e00\u4e0b\u3002 \u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u83b7\u53d6openid\u53ca\u7528\u6237\u4fe1\u606f\u7684\u65b9\u6cd5 1. \u83b7\u53d6openid 1.1 \u83b7\u53d6code \u8c03\u7528\u63a5\u53e3\u83b7\u53d6\u767b\u5f55\u51ed\u8bc1\uff08code\uff09\u8fdb\u800c\u6362\u53d6\u6001\u4fe1\u606f\uff0c\u5305\u62ec\u7528\u6237\u7684\u552f\u4e00\u6807\u8bc6\uff08openid\uff09 \u53ca\u672c\u6b21\u767b\u5f55\u7684\u4f1a\u8bdd\u5bc6\u94a5\uff08_key\uff09\u3002\u7528\u6237\u6570\u636e\u7684\u52a0\u89e3\u5bc6\u901a\u8baf\u9700\u8981\u4f9d\u8d56\u4f1a\u8bdd\u5bc6\u94a5\u5b8c\u6210\u3002 wx.login({ \/\/\u83b7\u53d6code success: function(res) { code = res.code \/\/\u8fd4\u56decode } }) \u767b\u5f55\u540e\u590d\u5236 1.2 \u83b7\u53d6openid \u62ff\u5230\u4e0a\u4e00\u6b65\u83b7\u53d6\u7684code\uff0c\u7ed3\u5408\u5c0f\u7a0b\u5e8f appid \u548c secret \u8bf7\u6c42\u63a5\u53e3api.weixin.qq.com\/sns\/jscode2session?appid=APPID&amp;secret=SECRET&amp;js_code=JSCODE&amp;grant_type=authorization_code \u6362\u53d6openid\uff0c\u4e0e openid \u4e00\u540c\u88ab\u8fd4\u56de\u7684\uff0c\u8fd8\u5305\u62ec session_key\uff0c\u5176\u4e2d session_key \u662f\u5bf9\u7528\u6237\u6570\u636e\u8fdb\u884c\u52a0\u5bc6\u7b7e\u540d\u7684\u5bc6\u94a5\u3002\u4e3a\u4e86\u81ea\u8eab\u5e94\u7528\u5b89\u5168\uff0csession_key \u4e0d\u5e94\u8be5\u5728\u7f51\u7edc\u4e0a\u4f20\u8f93\u3002 wx.request({ url: &#8216;https:\/\/api.weixin.qq.com\/sns\/jscode2session?appid=APPID&amp;secret=SECRET&amp;js_code=&#8217;+ code +&#8217;&amp;grant_type=authorization_code&#8217;, data: {}, header: { &#8216;content-type&#8217;: &#8216;application\/json&#8217; }, success: function(res) { openid = res.data.openid \/\/\u8fd4\u56deopenid } }) [&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-31437","post","type-post","status-publish","format-standard","hentry","category-19"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/31437","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=31437"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/31437\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=31437"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=31437"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=31437"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}