{"id":33345,"date":"2024-11-25T08:28:10","date_gmt":"2024-11-25T00:28:10","guid":{"rendered":"https:\/\/fwq.ai\/blog\/33345\/"},"modified":"2024-11-25T08:28:10","modified_gmt":"2024-11-25T00:28:10","slug":"%e5%be%ae%e4%bf%a1%e5%b0%8f%e7%a8%8b%e5%ba%8f%e4%b9%8b%e8%b0%83%e7%94%a8%e5%be%ae%e4%bf%a1%e6%8e%88%e6%9d%83%e7%aa%97%e5%8f%a3","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/33345\/","title":{"rendered":"\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u4e4b\u8c03\u7528\u5fae\u4fe1\u6388\u6743\u7a97\u53e3"},"content":{"rendered":"<h2><strong><span style=\"font-size: 14px\">0. \u5f15\u8a00<\/span><\/strong><\/h2>\n<p>\u3000\u3000\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u4e3a\u4e86\u4f18\u5316\u7528\u6237\u4f53\u9a8c\uff0c\u53d6\u6d88\u4e86\u5728\u8fdb\u5165\u5c0f\u7a0b\u5e8f\u65f6\u7acb\u9a6c\u51fa\u73b0\u6388\u6743\u7a97\u53e3\u3002\u9700\u8981\u7528\u6237\u4e3b\u52a8\u70b9\u51fb\u6309\u94ae\uff0c\u89e6\u53d1\u6388\u6743\u7a97\u53e3\u3002<\/p>\n<p>\u3000\u3000\u90a3\u4e48\uff0c\u5728\u6211\u5b9e\u8df5\u8fc7\u7a0b\u4e2d\uff0c\u51fa\u73b0\u4e86\u4ee5\u4e0b\u95ee\u9898\u3002<\/p>\n<pre>1. \u65e0\u6cd5\u5f39\u51fa\u6388\u6743\u7a97\u53e32. \u5e0c\u671b\u5728\u7528\u6237\u5df2\u7ecf\u6388\u6743\u7684\u60c5\u51b5\u4e0b\uff0c\u4e0d\u663e\u793a\u6309\u94ae<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h2><strong><span style=\"font-size: 14px\">1. \u5177\u4f53\u5b9e\u73b0<\/span><\/strong><\/h2>\n<p>\u3000\u3000<strong>app.js\u7684onLaunch()<\/strong>\u51fd\u6570\u4e2d\uff0c\u6dfb\u52a0\u83b7\u53d6\u7528\u6237\u4e2a\u4eba\u4fe1\u606f\u7684\u4ee3\u7801\u6bb5\u3002\u5b9e\u73b0\u5728\u7528\u6237\u5df2\u7ecf\u6388\u6743\u7684\u60c5\u51b5\uff08\u4f8b\u5982\u7b2c\u4e8c\u6b21\u6253\u5f00\u5c0f\u7a0b\u5e8f\u65f6\uff09\u4e0b\uff0c\u81ea\u52a8\u83b7\u53d6\u7528\u6237\u4e2a\u4eba\u4fe1\u606f\uff0c\u800c\u4e0d\u9700\u8981\u7528\u6237\u7684\u6388\u6743\u3002<\/p>\n<pre>wx.getSetting({\n    success: res =&gt; {\n        if (res.authSetting['scope.userInfo']) {\n            console.log(\"app: \" + \"\u7528\u6237\u5df2\u7ecf\u6388\u6743\")\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                success: res =&gt; {\n                    \/\/ \u53ef\u4ee5\u5c06 res \u53d1\u9001\u7ed9\u540e\u53f0\u89e3\u7801\u51fa unionId\n                    this.globalData.userInfo = res.userInfo\n                    console.log(this.globalData.userInfo)\n                    this.globalData.hasUserInfo = true\n                    \/\/ \u7531\u4e8e getUserInfo \u662f\u7f51\u7edc\u8bf7\u6c42\uff0c\u53ef\u80fd\u4f1a\u5728 Page.onLoad \u4e4b\u540e\u624d\u8fd4\u56de\n                    \/\/ \u6240\u4ee5\u6b64\u5904\u52a0\u5165 callback \u4ee5\u9632\u6b62\u8fd9\u79cd\u60c5\u51b5\n                    if (this.userInfoReadyCallback) {\n                        this.userInfoReadyCallback(res)\n                    }\n                },\n                fail: (res) =&gt; {\n                    console.log(\"app: \" + \"\u83b7\u53d6\u7528\u6237\u4fe1\u606f\u5931\u8d25\")\n                }\n            })\n        }else {\n            console.log(\"app: \" + \"\u7528\u6237\u6682\u65f6\u672a\u6388\u6743\")\n        }\n    }\n})<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u3000\u3000<strong>me.wxml<\/strong>\u4e2d\u6dfb\u52a0\u6388\u6743\u6309\u94ae\uff08\u5177\u4f53\u7684\u9875\u9762\u6839\u636e\u5927\u5bb6\u7684\u5b9e\u9645\u60c5\u51b5\uff09\u3002\u8fd9\u91cc\u7684button\u7ec4\u4ef6\u5fc5\u987b\u6309\u7167\u5982\u4e0b\u7684\u5f62\u5f0f\u3002<\/p>\n<pre>&lt;button open-type=\"getUserInfo\" bindgetuserinfo=\"\u4f60\u81ea\u5df1\u5b9a\u4e49\u51fd\u6570\"&gt;&lt;\/button&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<pre>&lt;block wx:if=\"{{!hasUserInfo}}\"&gt;\n    &lt;image src='..\/..\/images\/icon\/wechat.png'&gt;&lt;\/image&gt;\n    &lt;button open-type=\"getUserInfo\" bindgetuserinfo=\"getUserInfo\"&gt;\u5fae\u4fe1\u6388\u6743\u767b\u5f55&lt;\/button&gt;    \n&lt;\/block&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u3000\u3000\u6548\u679c\u8fd9\u6837\uff0c\u5177\u4f53\u7684\u6837\u5f0f\u6839\u636e\u5927\u5bb6\u7684\u559c\u597d\u66f4\u6539<img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/000\/051\/3e34401369213aaa2a537b61168f2355-0.png\" style=\"max-width:90%\" class=\"aligncenter\" title=\"\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u4e4b\u8c03\u7528\u5fae\u4fe1\u6388\u6743\u7a97\u53e3\u63d2\u56fe\" alt=\"\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u4e4b\u8c03\u7528\u5fae\u4fe1\u6388\u6743\u7a97\u53e3\u63d2\u56fe\" \/><\/p>\n<p>\u3000\u3000<strong>me.js<\/strong>\u4e2d\u6dfb\u52a0\u5982\u4e0b\u53d8\u91cf\u548c\u65b9\u6cd5\uff0c\u5728\u7528\u6237\u4e4b\u524d\u6ca1\u6709\u6388\u6743\u7684\u60c5\u51b5\u4e0b\uff0c\u9700\u8981\u7528\u6237\u4e3b\u52a8\u70b9\u51fb\u6309\u94ae\u3002<\/p>\n<pre>data: {\n    userInfo: null,\n    hasUserInfo: false\n},\ngetUserInfo: function(e) {\n    console.log(\"me: \" + \"\u7528\u6237\u70b9\u51fb\u6388\u6743\")\n    if(e.detail.userInfo){\n        this.setData({\n            userInfo: e.detail.userInfo,\n            hasUserInfo: true\n        })\n        app.data.userInfo = this.userInfo\n        app.data.hasUserInfo = true\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h2><strong><span style=\"font-size: 14px\">2. \u65e0\u6cd5\u5f39\u51fa\u6388\u6743\u7a97\u53e3<\/span><\/strong><\/h2>\n<p>\u3000\u3000<img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/000\/051\/ff9f6b1802cbcc44081fb64562547f24-1.png\" style=\"max-width:90%\" class=\"aligncenter\" title=\"\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u4e4b\u8c03\u7528\u5fae\u4fe1\u6388\u6743\u7a97\u53e3\u63d2\u56fe1\" alt=\"\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u4e4b\u8c03\u7528\u5fae\u4fe1\u6388\u6743\u7a97\u53e3\u63d2\u56fe1\" \/><\/p>\n<p>\u3000\u3000\u8fd9\u91cc\u4e00\u5b9a\u8981\u6ce8\u610f<\/p>\n<p>\u3000\u3000<span style=\"color: #ff0000\"><em><strong>\u6388\u6743\u7a97\u53e3\u53ea\u4f1a\u5728\u7528\u6237\u7b2c\u4e00\u6b21\u6388\u6743\u65f6\u51fa\u73b0<\/strong><\/em><\/span>\uff0c\u4e5f\u5c31\u662f\uff0c\u53ea\u4f1a\u51fa\u73b0\u4e00\u6b21\uff01\uff01<\/p>\n<p>\u3000\u3000\u5728\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u5f00\u53d1\u5de5\u5177\u91cc\uff0c\u9700\u8981\u6211\u4eec<span style=\"color: #ff0000\"><em><strong>\u6e05\u9664\u6240\u6709\u7f13\u5b58<\/strong><\/em><\/span><\/p>\n<p>\u3000\u3000<img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/000\/051\/b63e3d76647e055165cc8cf5540f45cd-2.png\" style=\"max-width:90%\" class=\"aligncenter\" title=\"\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u4e4b\u8c03\u7528\u5fae\u4fe1\u6388\u6743\u7a97\u53e3\u63d2\u56fe2\" alt=\"\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u4e4b\u8c03\u7528\u5fae\u4fe1\u6388\u6743\u7a97\u53e3\u63d2\u56fe2\" \/><\/p>\n<h2><strong><span style=\"font-size: 14px\">3. \u5df2\u7ecf\u6388\u6743\u7684\u60c5\u51b5\u4e0b\uff0c\u4e0d\u663e\u793a\u6309\u94ae<\/span><\/strong><\/h2>\n<p>\u3000\u3000\u7531\u4e8e\u7528\u6237\u5df2\u7ecf\u6388\u6743\u7684\u65f6\u5019\uff0capp.js\u4f1a\u83b7\u53d6\u7528\u6237\u4e2a\u4eba\u4fe1\u606f\uff08\u800c\u4e0d\u662f\u5728\u7528\u6237\u70b9\u51fb\u6388\u6743\u6309\u94ae\u65f6\u83b7\u53d6\uff09\uff0c\u4f46\u662f\u8fd9\u4e2a\u8fc7\u7a0b\u662f\u5f02\u6b65\u7684\u3002<\/p>\n<p>\u3000\u3000\u53ef\u4ee5\u770b\u5230\u6211\u4eec\u7684\u6388\u6743\u6309\u94ae\u7684\u51fa\u73b0\u65f6\u6839\u636e<strong><span style=\"color: #ff0000\">{{!hasUserInfo}}<\/span><\/strong>\u7684\u771f\u503c\u6765\u5224\u65ad\uff0c\u8fd9\u4e2a\u503c\u53ef\u4ee5\u901a\u8fc7app.js\u662f\u5426\u83b7\u53d6\u4e86\u4fe1\u606f\u6765\u8d4b\u503c\u3002<\/p>\n<pre>&lt;block wx:if=\"{{!hasUserInfo}}\"&gt;\n    &lt;image class=\"userAvatar\" src='..\/..\/images\/icon\/wechat.png'&gt;&lt;\/image&gt;\n    &lt;button open-type=\"getUserInfo\" bindgetuserinfo=\"getUserInfo\"&gt;\u5fae\u4fe1\u6388\u6743\u767b\u5f55&lt;\/button&gt;\n&lt;\/block&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u3000\u3000\u4f46\u662f\uff0c\u53ef\u80fd\u51fa\u73b0\u7528\u6237\u5df2\u7ecf\u6388\u6743\u4e86\uff0c\u4f46\u662fapp.js\u83b7\u53d6\u4e2a\u4eba\u4fe1\u606f\u65f6\u8fc7\u6162\uff0c\u7136\u800c\u6211\u4eec\u7684\u6388\u6743\u6309\u94ae\u5374\u9519\u8bef\u7684\u4ee5\u4e3aapp.js\u6ca1\u6709\u83b7\u53d6\u5230\u4fe1\u606f\uff0c\u6240\u4ee5\u5c06\u6388\u6743\u6309\u94ae\u6e32\u67d3\u4e86\u51fa\u6765\u3002<\/p>\n<p>\u3000\u3000\u8fd9\u65f6\uff0c\u6211\u4eec\u5e0c\u671b\uff0c\u5728app.js\u5224\u65ad<span style=\"color: #ff0000\"><strong>\u7528\u6237\u5df2\u7ecf\u6388\u6743<\/strong><\/span>\u4e14<strong><span style=\"color: #ff0000\">\u83b7\u53d6\u5230\u4fe1\u606f<\/span><\/strong>\u540e\uff0c\u544a\u8bc9\u6211\u4eec\u7684\u6388\u6743\u6309\u94ae\u3002<\/p>\n<p>\u3000\u3000\u6211\u4eec\u5728<strong>me.js<\/strong>\u4e2d\u6dfb\u52a0\u5982\u4e0b\u4ee3\u7801\u6bb5\u3002<\/p>\n<pre>onLoad: function() {    \/\/ \u83b7\u53d6\u4e2a\u4eba\u4fe1\u606f\n    if(app.globalData.userInfo){        \n            this.setData({\n            userInfo: app.globalData.userInfo,\n            hasUserInfo: true\n        })\n    }else{        \n        \/\/ \u5728app.js\u6ca1\u6709\u83b7\u53d6\u5230\u4fe1\u606f\u65f6\uff0c\u5224\u65adapp.js\u7684\u5f02\u6b65\u64cd\u4f5c\u662f\u5426\u8fd4\u56de\u4fe1\u606f\n        app.userInfoReadyCallback = res =&gt; {            \n                this.setData({\n                userInfo: app.globalData.userInfo,\n                hasUserInfo: true\n            })\n        }\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u3000\u3000\u4e3a\u4ec0\u4e48\u8fd9\u91cc\u4f1a\u6709\u4e2a<em><strong>app.userInfoReadyCallback<\/strong><\/em>\u51fd\u6570\u5462\uff0c\u6211\u4eec\u6ce8\u610f\u5230\u5728app.js\u7684wx.getSetting\u91cc\u6709\u4e00\u6bb5\u56de\u8c03\u51fd\u6570\uff0c\u8fd9\u4e2a\u51fd\u6570\u5c31\u662f\u7528\u6765\u89e3\u51b3\u5f02\u6b65\u7684\u95ee\u9898\u3002<\/p>\n<p>\u3000\u3000<img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/000\/051\/c3c75fb389c3ae146a1cdf23956895f1-3.png\" style=\"max-width:90%\" class=\"aligncenter\" title=\"\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u4e4b\u8c03\u7528\u5fae\u4fe1\u6388\u6743\u7a97\u53e3\u63d2\u56fe3\" alt=\"\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u4e4b\u8c03\u7528\u5fae\u4fe1\u6388\u6743\u7a97\u53e3\u63d2\u56fe3\" \/><\/p>\n<p>\u63a8\u8350\u6559\u7a0b\uff1a\u300a\u300b<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u4e4b\u8c03\u7528\u5fae\u4fe1\u6388\u6743\u7a97\u53e3\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>0. \u5f15\u8a00 \u3000\u3000\u5fae\u4fe1\u5c0f\u7a0b\u5e8f\u4e3a\u4e86\u4f18\u5316\u7528\u6237\u4f53\u9a8c\uff0c\u53d6\u6d88\u4e86\u5728\u8fdb\u5165\u5c0f\u7a0b\u5e8f\u65f6\u7acb\u9a6c\u51fa\u73b0\u6388\u6743\u7a97\u53e3\u3002\u9700\u8981\u7528\u6237\u4e3b\u52a8\u70b9\u51fb\u6309\u94ae\uff0c\u89e6\u53d1\u6388\u6743\u7a97\u53e3\u3002 \u3000\u3000\u90a3\u4e48\uff0c\u5728\u6211\u5b9e\u8df5\u8fc7\u7a0b\u4e2d\uff0c\u51fa\u73b0\u4e86\u4ee5\u4e0b\u95ee\u9898\u3002 1. \u65e0\u6cd5\u5f39\u51fa\u6388\u6743\u7a97\u53e32. \u5e0c\u671b\u5728\u7528\u6237\u5df2\u7ecf\u6388\u6743\u7684\u60c5\u51b5\u4e0b\uff0c\u4e0d\u663e\u793a\u6309\u94ae \u767b\u5f55\u540e\u590d\u5236 1. \u5177\u4f53\u5b9e\u73b0 \u3000\u3000app.js\u7684onLaunch()\u51fd\u6570\u4e2d\uff0c\u6dfb\u52a0\u83b7\u53d6\u7528\u6237\u4e2a\u4eba\u4fe1\u606f\u7684\u4ee3\u7801\u6bb5\u3002\u5b9e\u73b0\u5728\u7528\u6237\u5df2\u7ecf\u6388\u6743\u7684\u60c5\u51b5\uff08\u4f8b\u5982\u7b2c\u4e8c\u6b21\u6253\u5f00\u5c0f\u7a0b\u5e8f\u65f6\uff09\u4e0b\uff0c\u81ea\u52a8\u83b7\u53d6\u7528\u6237\u4e2a\u4eba\u4fe1\u606f\uff0c\u800c\u4e0d\u9700\u8981\u7528\u6237\u7684\u6388\u6743\u3002 wx.getSetting({ success: res =&gt; { if (res.authSetting[&#8216;scope.userInfo&#8217;]) { console.log(&#8220;app: &#8221; + &#8220;\u7528\u6237\u5df2\u7ecf\u6388\u6743&#8221;) \/\/ \u5df2\u7ecf\u6388\u6743\uff0c\u53ef\u4ee5\u76f4\u63a5\u8c03\u7528 getUserInfo \u83b7\u53d6\u5934\u50cf\u6635\u79f0\uff0c\u4e0d\u4f1a\u5f39\u6846 wx.getUserInfo({ success: res =&gt; { \/\/ \u53ef\u4ee5\u5c06 res \u53d1\u9001\u7ed9\u540e\u53f0\u89e3\u7801\u51fa unionId this.globalData.userInfo = res.userInfo console.log(this.globalData.userInfo) this.globalData.hasUserInfo = true \/\/ \u7531\u4e8e getUserInfo \u662f\u7f51\u7edc\u8bf7\u6c42\uff0c\u53ef\u80fd\u4f1a\u5728 Page.onLoad \u4e4b\u540e\u624d\u8fd4\u56de \/\/ \u6240\u4ee5\u6b64\u5904\u52a0\u5165 callback \u4ee5\u9632\u6b62\u8fd9\u79cd\u60c5\u51b5 if [&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-33345","post","type-post","status-publish","format-standard","hentry","category-19"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/33345","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=33345"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/33345\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=33345"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=33345"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=33345"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}