{"id":24734,"date":"2024-11-21T16:42:45","date_gmt":"2024-11-21T08:42:45","guid":{"rendered":"https:\/\/fwq.ai\/blog\/24734\/"},"modified":"2024-11-21T16:42:45","modified_gmt":"2024-11-21T08:42:45","slug":"%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8hyperf%e6%a1%86%e6%9e%b6%e8%bf%9b%e8%a1%8c%e5%af%86%e7%a0%81%e9%87%8d%e7%bd%ae","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/24734\/","title":{"rendered":"\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c\u5bc6\u7801\u91cd\u7f6e"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/000\/164\/169779838197384.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c\u5bc6\u7801\u91cd\u7f6e\u63d2\u56fe\" alt=\"\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c\u5bc6\u7801\u91cd\u7f6e\u63d2\u56fe\" \/><\/p>\n<p>\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c<\/p>\n<p>\u5bfc\u8bed\uff1a\u5bc6\u7801\u91cd\u7f6e\u662f\u7f51\u7ad9\u6216\u5e94\u7528\u4e2d\u5e38\u89c1\u7684\u529f\u80fd\u4e4b\u4e00\uff0c\u5f53\u7528\u6237\u5fd8\u8bb0\u81ea\u5df1\u7684\u5bc6\u7801\u6216\u8005\u9700\u8981\u66f4\u6539\u5bc6\u7801\u65f6\uff0c\u901a\u8fc7\u91cd\u7f6e\u5bc6\u7801\u529f\u80fd\u53ef\u4ee5\u65b9\u4fbf\u7528\u6237\u91cd\u65b0\u8bbe\u7f6e\u65b0\u5bc6\u7801\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u5b9e\u73b0\u5bc6\u7801\u91cd\u7f6e\u529f\u80fd\uff0c\u5e76\u63d0\u4f9b\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<p>\u4e00\u3001\u8bbe\u8ba1\u601d\u8def<\/p>\n<p>\u5728\u8bbe\u8ba1\u5bc6\u7801\u91cd\u7f6e\u529f\u80fd\u65f6\uff0c\u4e00\u822c\u9700\u8981\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li>\u7528\u6237\u70b9\u51fb&#8221;\u5fd8\u8bb0\u5bc6\u7801&#8221;\u6309\u94ae\uff0c\u8fdb\u5165\u5bc6\u7801\u91cd\u7f6e\u9875\u9762\u3002<\/li>\n<li>\u7528\u6237\u8f93\u5165\u6ce8\u518c\u65f6\u4f7f\u7528\u7684\u90ae\u7bb1\u6216\u624b\u673a\u53f7\u3002<\/li>\n<li>\u7cfb\u7edf\u68c0\u67e5\u7528\u6237\u63d0\u4ea4\u7684\u90ae\u7bb1\u6216\u624b\u673a\u53f7\u662f\u5426\u5b58\u5728\uff0c\u5982\u679c\u5b58\u5728\uff0c\u5219\u5411\u8be5\u90ae\u7bb1\u6216\u624b\u673a\u53f7\u53d1\u9001\u91cd\u7f6e\u5bc6\u7801\u7684\u94fe\u63a5\u3002<\/li>\n<li>\u7528\u6237\u6253\u5f00\u6536\u5230\u7684\u91cd\u7f6e\u5bc6\u7801\u94fe\u63a5\uff0c\u8df3\u8f6c\u81f3\u5bc6\u7801\u91cd\u7f6e\u9875\u9762\u3002<\/li>\n<li>\u7528\u6237\u8f93\u5165\u65b0\u5bc6\u7801\u548c\u786e\u8ba4\u5bc6\u7801\uff0c\u7cfb\u7edf\u8fdb\u884c\u5bc6\u7801\u7684\u91cd\u7f6e\u3002<\/li>\n<li>\u5bc6\u7801\u91cd\u7f6e\u6210\u529f\u540e\uff0c\u7528\u6237\u53ef\u4ee5\u4f7f\u7528\u65b0\u5bc6\u7801\u8fdb\u884c\u767b\u5f55\u3002<\/li>\n<\/ol>\n<p>\u4e8c\u3001\u4ee3\u7801\u5b9e\u73b0<\/p>\n<ol>\n<li>\u521b\u5efa\u5bc6\u7801\u91cd\u7f6e\u63a7\u5236\u5668\u6587\u4ef6\uff08ResetPasswordController.php\uff09<\/li>\n<\/ol>\n<pre>&lt;?php namespace AppController;\n\nuse AppServiceEmailService;\nuse AppServiceUserService;\nuse HyperfHttpServerAnnotationAutoController;\n\n\/**\n * @AutoController()\n *\/\nclass ResetPasswordController\n{\n    \/**\n     * \u53d1\u9001\u91cd\u7f6e\u5bc6\u7801\u94fe\u63a5\n     *\/\n    public function sendResetLink(UserService $userService, EmailService $emailService)\n    {\n        $email = request()-&gt;input('email');\n        \n        \/\/ \u68c0\u67e5\u90ae\u7bb1\u662f\u5426\u5b58\u5728\n        if (!$userService-&amp;gt;checkEmailExists($email)) {\n            return ['code' =&amp;gt; 400, 'message' =&amp;gt; '\u8be5\u90ae\u7bb1\u4e0d\u5b58\u5728'];\n        }\n        \n        \/\/ \u53d1\u9001\u91cd\u7f6e\u5bc6\u7801\u94fe\u63a5\n        $emailService-&amp;gt;sendResetLinkEmail($email);\n        \n        return ['code' =&amp;gt; 200, 'message' =&amp;gt; '\u5df2\u53d1\u9001\u91cd\u7f6e\u5bc6\u7801\u94fe\u63a5\uff0c\u8bf7\u67e5\u6536\u90ae\u7bb1'];\n    }\n\n    \/**\n     * \u91cd\u7f6e\u5bc6\u7801\n     *\/\n    public function resetPassword(UserService $userService)\n    {\n        $email = request()-&amp;gt;input('email');\n        $token = request()-&amp;gt;input('token');\n        $password = request()-&amp;gt;input('password');\n        \n        \/\/ \u9a8c\u8bc1\u91cd\u7f6e\u5bc6\u7801\u94fe\u63a5\u7684\u5408\u6cd5\u6027\n        if (!$userService-&amp;gt;validateResetToken($email, $token)) {\n            return ['code' =&amp;gt; 400, 'message' =&amp;gt; '\u91cd\u7f6e\u5bc6\u7801\u94fe\u63a5\u5df2\u5931\u6548'];\n        }\n        \n        \/\/ \u66f4\u65b0\u7528\u6237\u5bc6\u7801\n        $userService-&amp;gt;updatePassword($email, $password);\n        \n        return ['code' =&amp;gt; 200, 'message' =&amp;gt; '\u5bc6\u7801\u91cd\u7f6e\u6210\u529f'];\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li>\u521b\u5efa\u90ae\u4ef6\u670d\u52a1\u6587\u4ef6\uff08EmailService.php\uff09<\/li>\n<\/ol>\n<pre>&lt;?php namespace AppService;\n\nclass EmailService\n{\n    \/**\n     * \u53d1\u9001\u91cd\u7f6e\u5bc6\u7801\u94fe\u63a5\u5230\u7528\u6237\u90ae\u7bb1\n     *\/\n    public function sendResetLinkEmail($email)\n    {\n        \/\/ \u53d1\u9001\u90ae\u4ef6\u7684\u903b\u8f91\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li>\u521b\u5efa\u7528\u6237\u670d\u52a1\u6587\u4ef6\uff08UserService.php\uff09<\/li>\n<\/ol>\n<pre>&lt;?php namespace AppService;\n\nclass UserService\n{\n    \/**\n     * \u68c0\u67e5\u90ae\u7bb1\u662f\u5426\u5b58\u5728\n     *\/\n    public function checkEmailExists($email)\n    {\n        \/\/ \u5224\u65ad\u90ae\u7bb1\u662f\u5426\u5b58\u5728\u7684\u903b\u8f91\n    }\n    \n    \/**\n     * \u9a8c\u8bc1\u91cd\u7f6e\u5bc6\u7801\u94fe\u63a5\u7684\u5408\u6cd5\u6027\n     *\/\n    public function validateResetToken($email, $token)\n    {\n        \/\/ \u9a8c\u8bc1\u91cd\u7f6e\u5bc6\u7801\u94fe\u63a5\u7684\u5408\u6cd5\u6027\u903b\u8f91\n    }\n    \n    \/**\n     * \u66f4\u65b0\u7528\u6237\u5bc6\u7801\n     *\/\n    public function updatePassword($email, $password)\n    {\n        \/\/ \u66f4\u65b0\u7528\u6237\u5bc6\u7801\u7684\u903b\u8f91\n    }\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4e09\u3001\u4f7f\u7528\u793a\u4f8b<\/p>\n<ol>\n<li>\u8def\u7531\u8bbe\u7f6e\uff08routes.php\uff09<\/li>\n<\/ol>\n<pre>&lt;?php Router::post('\/reset\/send', 'AppControllerResetPasswordController@sendResetLink');\nRouter::post('\/reset\/reset', 'AppControllerResetPasswordController@resetPassword');<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ol>\n<li>\u524d\u7aef\u9875\u9762\u4ee3\u7801<\/li>\n<\/ol>\n<p>\u53d1\u9001\u91cd\u7f6e\u5bc6\u7801\u94fe\u63a5\u9875\u9762\uff08send_reset_link.blade.php\uff09<\/p>\n<pre><\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 \u767b\u5f55\u540e\u590d\u5236 \u53d1\u9001\u91cd\u7f6e\u5bc6\u7801\u94fe\u63a5 <\/p>\n<p>\u91cd\u7f6e\u5bc6\u7801\u9875\u9762\uff08reset_password.blade.php\uff09<\/p>\n<pre><\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 \u767b\u5f55\u540e\u590d\u5236 \u91cd\u7f6e\u5bc6\u7801 <\/p>\n<p>\u56db\u3001\u603b\u7ed3<\/p>\n<p>\u901a\u8fc7\u4f7f\u7528Hyperf\u6846\u67b6\uff0c\u6211\u4eec\u53ef\u4ee5\u7b80\u5355\u9ad8\u6548\u5730\u5b9e\u73b0\u5bc6\u7801\u91cd\u7f6e\u529f\u80fd\u3002\u4ee5\u4e0a\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5b9e\u9645\u4f7f\u7528\u4e2d\u53ef\u80fd\u9700\u8981\u6839\u636e\u4e1a\u52a1\u9700\u6c42\u8fdb\u884c\u9002\u5f53\u7684\u4fee\u6539\u548c\u6269\u5c55\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u60a8\u7406\u89e3\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c\u5bc6\u7801\u91cd\u7f6e\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c\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>\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u8fdb\u884c \u5bfc\u8bed\uff1a\u5bc6\u7801\u91cd\u7f6e\u662f\u7f51\u7ad9\u6216\u5e94\u7528\u4e2d\u5e38\u89c1\u7684\u529f\u80fd\u4e4b\u4e00\uff0c\u5f53\u7528\u6237\u5fd8\u8bb0\u81ea\u5df1\u7684\u5bc6\u7801\u6216\u8005\u9700\u8981\u66f4\u6539\u5bc6\u7801\u65f6\uff0c\u901a\u8fc7\u91cd\u7f6e\u5bc6\u7801\u529f\u80fd\u53ef\u4ee5\u65b9\u4fbf\u7528\u6237\u91cd\u65b0\u8bbe\u7f6e\u65b0\u5bc6\u7801\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Hyperf\u6846\u67b6\u5b9e\u73b0\u5bc6\u7801\u91cd\u7f6e\u529f\u80fd\uff0c\u5e76\u63d0\u4f9b\u4ee3\u7801\u793a\u4f8b\u3002 \u4e00\u3001\u8bbe\u8ba1\u601d\u8def \u5728\u8bbe\u8ba1\u5bc6\u7801\u91cd\u7f6e\u529f\u80fd\u65f6\uff0c\u4e00\u822c\u9700\u8981\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a \u7528\u6237\u70b9\u51fb&#8221;\u5fd8\u8bb0\u5bc6\u7801&#8221;\u6309\u94ae\uff0c\u8fdb\u5165\u5bc6\u7801\u91cd\u7f6e\u9875\u9762\u3002 \u7528\u6237\u8f93\u5165\u6ce8\u518c\u65f6\u4f7f\u7528\u7684\u90ae\u7bb1\u6216\u624b\u673a\u53f7\u3002 \u7cfb\u7edf\u68c0\u67e5\u7528\u6237\u63d0\u4ea4\u7684\u90ae\u7bb1\u6216\u624b\u673a\u53f7\u662f\u5426\u5b58\u5728\uff0c\u5982\u679c\u5b58\u5728\uff0c\u5219\u5411\u8be5\u90ae\u7bb1\u6216\u624b\u673a\u53f7\u53d1\u9001\u91cd\u7f6e\u5bc6\u7801\u7684\u94fe\u63a5\u3002 \u7528\u6237\u6253\u5f00\u6536\u5230\u7684\u91cd\u7f6e\u5bc6\u7801\u94fe\u63a5\uff0c\u8df3\u8f6c\u81f3\u5bc6\u7801\u91cd\u7f6e\u9875\u9762\u3002 \u7528\u6237\u8f93\u5165\u65b0\u5bc6\u7801\u548c\u786e\u8ba4\u5bc6\u7801\uff0c\u7cfb\u7edf\u8fdb\u884c\u5bc6\u7801\u7684\u91cd\u7f6e\u3002 \u5bc6\u7801\u91cd\u7f6e\u6210\u529f\u540e\uff0c\u7528\u6237\u53ef\u4ee5\u4f7f\u7528\u65b0\u5bc6\u7801\u8fdb\u884c\u767b\u5f55\u3002 \u4e8c\u3001\u4ee3\u7801\u5b9e\u73b0 \u521b\u5efa\u5bc6\u7801\u91cd\u7f6e\u63a7\u5236\u5668\u6587\u4ef6\uff08ResetPasswordController.php\uff09 &lt;?php namespace AppController; use AppServiceEmailService; use AppServiceUserService; use HyperfHttpServerAnnotationAutoController; \/** * @AutoController() *\/ class ResetPasswordController { \/** * \u53d1\u9001\u91cd\u7f6e\u5bc6\u7801\u94fe\u63a5 *\/ public function sendResetLink(UserService $userService, EmailService $emailService) { $email = request()-&gt;input(&#8217;email&#8217;); \/\/ \u68c0\u67e5\u90ae\u7bb1\u662f\u5426\u5b58\u5728 if (!$userService-&amp;gt;checkEmailExists($email)) { return [&#8216;code&#8217; =&amp;gt; 400, &#8216;message&#8217; =&amp;gt; &#8216;\u8be5\u90ae\u7bb1\u4e0d\u5b58\u5728&#8217;]; } [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-24734","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24734","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=24734"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/24734\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=24734"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=24734"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=24734"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}