{"id":32531,"date":"2024-11-25T15:38:40","date_gmt":"2024-11-25T07:38:40","guid":{"rendered":"https:\/\/fwq.ai\/blog\/32531\/"},"modified":"2024-11-25T15:38:40","modified_gmt":"2024-11-25T07:38:40","slug":"%e5%b0%8f%e7%a8%8b%e5%ba%8f%e5%bc%80%e5%8f%91%e4%b9%8b%e8%8e%b7%e5%8f%96%e7%94%a8%e6%88%b7unionid%e3%80%81%e6%98%b5%e7%a7%b0%e3%80%81%e5%a4%b4%e5%83%8f%e4%bf%a1%e6%81%af%e7%9a%84%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/32531\/","title":{"rendered":"\u5c0f\u7a0b\u5e8f\u5f00\u53d1\u4e4b\u83b7\u53d6\u7528\u6237UnionID\u3001\u6635\u79f0\u3001\u5934\u50cf\u4fe1\u606f\u7684\u65b9\u6cd5"},"content":{"rendered":"<p>\u672c\u7bc7\u6587\u7ae0\u7ed9\u5927\u5bb6\u5e26\u6765\u7684\u5185\u5bb9\u662f\u5173\u4e8e\u5c0f\u7a0b\u5e8f\u5f00\u53d1\u4e4b\u83b7\u53d6\u7528\u6237unionid\u3001\u6635\u79f0\u3001\u5934\u50cf\u4fe1\u606f\u7684\u65b9\u6cd5\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>\u6700\u8fd1\u5728\u5f00\u53d1\u5c0f\u7a0b\u5e8f\uff0c\u8bb0\u5f55\u4e0b\u6765\u4e00\u4e9b\u3002<\/p>\n<p>\u4ee5\u524d\u83b7\u53d6\u7528\u6237\u4fe1\u606f\u53ef\u4ee5\u4f7f\u7528&nbsp;wx.getUserInfo&nbsp;\uff0c\u4f46\u662f\u540e\u6765\u5b98\u65b9\u8fdb\u884c\u4e86\u8c03\u6574\uff0c\u6240\u4ee5\u8981\u6362\u4e00\u4e2a\u601d\u8def\u4e86\u3002<\/p>\n<p>\u83b7\u53d6\u7528\u6237\u6635\u79f0\u3001\u5934\u50cf<\/p>\n<p>\u8fd9\u4e00\u6b65\u8fd8\u662f\u5f88\u65b9\u4fbf\u7684\uff0c\u4f7f\u7528\u5c0f\u7a0b\u5e8f\u5185\u7f6e\u7684\u7ec4\u4ef6\u5c31\u53ef\u4ee5\u5b9e\u73b0\uff0c\u53ef\u4ee5\u83b7\u53d6\u4ee5\u4e0b\u7684\u6570\u636e<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/image\/902\/940\/280\/1548212827419327.png\" class=\"aligncenter\" title=\"\u5c0f\u7a0b\u5e8f\u5f00\u53d1\u4e4b\u83b7\u53d6\u7528\u6237UnionID\u3001\u6635\u79f0\u3001\u5934\u50cf\u4fe1\u606f\u7684\u65b9\u6cd5\u63d2\u56fe\" alt=\"\u5c0f\u7a0b\u5e8f\u5f00\u53d1\u4e4b\u83b7\u53d6\u7528\u6237UnionID\u3001\u6635\u79f0\u3001\u5934\u50cf\u4fe1\u606f\u7684\u65b9\u6cd5\u63d2\u56fe\" \/>\u53ef\u4ee5\u770b\u5230\uff0c\u80fd\u83b7\u53d6\u5230\u7684\u76f8\u5173\u4fe1\u606f\u8fd8\u662f\u4e0d\u5c11\u7684\uff0c\u4e0b\u9762\u662f\u4ee5\u5934\u50cf\u548c\u6635\u79f0\u4e3a\u4f8b<\/p>\n<pre>&lt;!-- \u5934\u50cf --&gt;\n&lt;open-data type=\"userAvatarUrl\"&gt;&lt;\/open-data&gt;\n&lt;!-- \u6635\u79f0 --&gt;\n&lt;open-data type=\"userNickName\" lang=\"zh_CN\"&gt;&lt;\/open-data&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u83b7\u53d6\u7528\u6237UnionID<\/strong><\/p>\n<p>\u83b7\u53d6\u7528\u6237\u7684\u6635\u79f0\u3001\u5934\u50cf\u5f88\u7b80\u5355\uff0c\u4f46\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u7528\u6237\u7684UnionID\uff0c\u53ef\u4ee5\u4f7f\u7528wx.login\u3001wx.request\u6765\u5b9e\u73b0\u3002\u5148\u770b\u4e0b\u5b98\u65b9\u7ed9\u51fa\u7684\u6d41\u7a0b<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/image\/669\/976\/346\/1548212850590446.png\" class=\"aligncenter\" title=\"\u5c0f\u7a0b\u5e8f\u5f00\u53d1\u4e4b\u83b7\u53d6\u7528\u6237UnionID\u3001\u6635\u79f0\u3001\u5934\u50cf\u4fe1\u606f\u7684\u65b9\u6cd5\u63d2\u56fe1\" alt=\"\u5c0f\u7a0b\u5e8f\u5f00\u53d1\u4e4b\u83b7\u53d6\u7528\u6237UnionID\u3001\u6635\u79f0\u3001\u5934\u50cf\u4fe1\u606f\u7684\u65b9\u6cd5\u63d2\u56fe1\" \/>\u53ef\u4ee5\u770b\u5230\u6d41\u7a0b\u5e76\u4e0d\u590d\u6742\uff0c\u4e0b\u9762\u662f\u5c0f\u7a0b\u5e8f\u7684JS\u5b9e\u4f8b<\/p>\n<pre>onLoad: function (options) {\n    var that = this;\n    wx.login({\n        success: function (res) {\n            if (res.code) {\n                \/\/ \u53d1\u8d77\u7f51\u7edc\u8bf7\u6c42,\u83b7\u53d6\u7528\u6237UnionID\n                wx.request({\n                    url: 'https:\/\/xxxx',\n                    data: {\n                        code: res.code\n                    },\n                    success: function (res) {\n                        if (res.data.message == 'success') {\n                            \/\/ \u83b7\u53d6\u6570\u636e\u6210\u529f\n                            console.log(res.data.data)\n                        }\n                    }\n                })\n            } else {\n                \/\/ \u83b7\u53d6code\u5931\u8d25\n                console.log('\u767b\u5f55\u5931\u8d25\uff01' + res.errMsg)\n            }\n        }\n    });\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4e0b\u9762\u662f\u540e\u7aefPHP\u7684\u4ee3\u7801<\/p>\n<pre>&lt;?php\n$code = $_GET['code'];\n$url = 'https:\/\/api.weixin.qq.com\/sns\/jscode2session?appid=' . $this-&gt;appid . '&amp;secret=' . $this-&gt;secret . '&amp;js_code=' . $code . '&amp;grant_type=authorization_code';\n$userInfo = file_get_contents($url);\n$userInfo = json_decode($userInfo, true);\nif (!$userInfo['unionid']) {\n  echo json_encode(array('data'=&gt;'','message'=&gt;'error'));\n} else {\n  echo json_encode(array('data'=&gt; $userInfo['unionid'],'message'=&gt;'success'));\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u83b7\u53d6\u5230UnionID\u540e\uff0c\u53ef\u4ee5\u7ee7\u7eed\u4e1a\u52a1\u6d41\u7a0b\u4e86\u3002<\/p>\n<p><span style=\"font-family:\"><br \/><\/span> <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5c0f\u7a0b\u5e8f\u5f00\u53d1\u4e4b\u83b7\u53d6\u7528\u6237UnionID\u3001\u6635\u79f0\u3001\u5934\u50cf\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>\u672c\u7bc7\u6587\u7ae0\u7ed9\u5927\u5bb6\u5e26\u6765\u7684\u5185\u5bb9\u662f\u5173\u4e8e\u5c0f\u7a0b\u5e8f\u5f00\u53d1\u4e4b\u83b7\u53d6\u7528\u6237unionid\u3001\u6635\u79f0\u3001\u5934\u50cf\u4fe1\u606f\u7684\u65b9\u6cd5\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 \u6700\u8fd1\u5728\u5f00\u53d1\u5c0f\u7a0b\u5e8f\uff0c\u8bb0\u5f55\u4e0b\u6765\u4e00\u4e9b\u3002 \u4ee5\u524d\u83b7\u53d6\u7528\u6237\u4fe1\u606f\u53ef\u4ee5\u4f7f\u7528&nbsp;wx.getUserInfo&nbsp;\uff0c\u4f46\u662f\u540e\u6765\u5b98\u65b9\u8fdb\u884c\u4e86\u8c03\u6574\uff0c\u6240\u4ee5\u8981\u6362\u4e00\u4e2a\u601d\u8def\u4e86\u3002 \u83b7\u53d6\u7528\u6237\u6635\u79f0\u3001\u5934\u50cf \u8fd9\u4e00\u6b65\u8fd8\u662f\u5f88\u65b9\u4fbf\u7684\uff0c\u4f7f\u7528\u5c0f\u7a0b\u5e8f\u5185\u7f6e\u7684\u7ec4\u4ef6\u5c31\u53ef\u4ee5\u5b9e\u73b0\uff0c\u53ef\u4ee5\u83b7\u53d6\u4ee5\u4e0b\u7684\u6570\u636e \u53ef\u4ee5\u770b\u5230\uff0c\u80fd\u83b7\u53d6\u5230\u7684\u76f8\u5173\u4fe1\u606f\u8fd8\u662f\u4e0d\u5c11\u7684\uff0c\u4e0b\u9762\u662f\u4ee5\u5934\u50cf\u548c\u6635\u79f0\u4e3a\u4f8b &lt;!&#8211; \u5934\u50cf &#8211;&gt; &lt;open-data type=&#8221;userAvatarUrl&#8221;&gt;&lt;\/open-data&gt; &lt;!&#8211; \u6635\u79f0 &#8211;&gt; &lt;open-data type=&#8221;userNickName&#8221; lang=&#8221;zh_CN&#8221;&gt;&lt;\/open-data&gt; \u767b\u5f55\u540e\u590d\u5236 \u83b7\u53d6\u7528\u6237UnionID \u83b7\u53d6\u7528\u6237\u7684\u6635\u79f0\u3001\u5934\u50cf\u5f88\u7b80\u5355\uff0c\u4f46\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u9700\u8981\u7528\u6237\u7684UnionID\uff0c\u53ef\u4ee5\u4f7f\u7528wx.login\u3001wx.request\u6765\u5b9e\u73b0\u3002\u5148\u770b\u4e0b\u5b98\u65b9\u7ed9\u51fa\u7684\u6d41\u7a0b \u53ef\u4ee5\u770b\u5230\u6d41\u7a0b\u5e76\u4e0d\u590d\u6742\uff0c\u4e0b\u9762\u662f\u5c0f\u7a0b\u5e8f\u7684JS\u5b9e\u4f8b onLoad: function (options) { var that = this; wx.login({ success: function (res) { if (res.code) { \/\/ \u53d1\u8d77\u7f51\u7edc\u8bf7\u6c42,\u83b7\u53d6\u7528\u6237UnionID wx.request({ url: &#8216;https:\/\/xxxx&#8217;, data: { code: res.code }, success: function (res) { if (res.data.message == &#8216;success&#8217;) [&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-32531","post","type-post","status-publish","format-standard","hentry","category-19"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/32531","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=32531"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/32531\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=32531"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=32531"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=32531"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}