{"id":13635300,"url":"https://github.com/overtrue/laravel-wechat","last_synced_at":"2025-05-14T22:05:39.236Z","repository":{"id":29029364,"uuid":"32556722","full_name":"overtrue/laravel-wechat","owner":"overtrue","description":"微信 SDK for Laravel, 基于 overtrue/wechat","archived":false,"fork":false,"pushed_at":"2025-02-25T14:27:31.000Z","size":262,"stargazers_count":2892,"open_issues_count":2,"forks_count":504,"subscribers_count":104,"default_branch":"7.x","last_synced_at":"2025-05-14T22:04:55.079Z","etag":null,"topics":["laravel-wechat","php","wechat","wechat-sdk"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":"Unmaintained","scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/overtrue.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["overtrue"]}},"created_at":"2015-03-20T01:55:13.000Z","updated_at":"2025-05-11T12:39:42.000Z","dependencies_parsed_at":"2024-06-18T10:45:20.025Z","dependency_job_id":"0043b1be-d8b1-421c-9d6e-d2b6ea8087f4","html_url":"https://github.com/overtrue/laravel-wechat","commit_stats":{"total_commits":287,"total_committers":40,"mean_commits":7.175,"dds":0.3344947735191638,"last_synced_commit":"37f942c0cb0bbe7fed5120f4582437cd9780f3a2"},"previous_names":[],"tags_count":74,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/overtrue%2Flaravel-wechat","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/overtrue%2Flaravel-wechat/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/overtrue%2Flaravel-wechat/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/overtrue%2Flaravel-wechat/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/overtrue","download_url":"https://codeload.github.com/overtrue/laravel-wechat/tar.gz/refs/heads/7.x","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254235687,"owners_count":22036962,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["laravel-wechat","php","wechat","wechat-sdk"],"created_at":"2024-08-02T00:00:43.751Z","updated_at":"2025-05-14T22:05:39.191Z","avatar_url":"https://github.com/overtrue.png","language":"PHP","funding_links":["https://github.com/sponsors/overtrue"],"categories":["后端","PHP"],"sub_categories":[],"readme":"# EasyWeChat for Laravel\n\n微信 SDK EasyWeChat for Laravel， 基于 [w7corp/easywechat](https://github.com/w7corp/easywechat)\n\n[![Sponsor me](https://github.com/overtrue/overtrue/blob/master/sponsor-me-button-s.svg?raw=true)](https://github.com/sponsors/overtrue)\n\n\u003e 7.x 起不再默认支持 Lumen。\n\n## 框架要求\n\n- overtrue/laravel-wechat:^7.0 -\u003e Laravel \u003e= 8.0\n- overtrue/laravel-wechat:^6.0 -\u003e Laravel/Lumen \u003e= 7.0\n- overtrue/laravel-wechat:^5.1 -\u003e Laravel/Lumen \u003e= 5.1\n\n## 安装\n\n```bash\ncomposer require overtrue/laravel-wechat:^7.2\n```\n\n## 配置\n\n1. 创建配置文件：\n\n```shell\nphp artisan vendor:publish --provider=\"Overtrue\\\\LaravelWeChat\\\\ServiceProvider\"\n```\n\n2. 可选，添加别名\n\n```php\n'aliases' =\u003e [\n    // ...\n    'EasyWeChat' =\u003e Overtrue\\LaravelWeChat\\EasyWeChat::class,\n],\n```\n\n3. 每个模块基本都支持多账号，默认为 `default`。\n\n## 使用\n\n:rotating_light: 在中间件 `App\\Http\\Middleware\\VerifyCsrfToken` 排除微信相关的路由，如：\n\n```php\nprotected $except = [\n    // ...\n    'wechat',\n];\n```\n对于 Laravel 11.x 可以使用`bootstrap/app.php` 中的`$middleware-\u003evalidateCsrfTokens`方法:\n```php\n-\u003ewithMiddleware(function (Middleware $middleware) {\n    $middleware-\u003evalidateCsrfTokens(except: [\n        // ...\n        'wechat',\n    ]);\n})\n```\n\n下面以接收普通消息为例写一个例子。\n\n路由：\n\n```php\nRoute::any('/wechat', 'WeChatController@serve');\n```\n\n\u003e 注意：一定是 `Route::any`, 因为微信服务端认证的时候是 `GET`, 接收用户消息时是 `POST` ！\n\n然后创建控制器 `WeChatController`：\n\n```php\n\u003c?php\n\nnamespace App\\Http\\Controllers;\n\nuse Log;\n\nclass WeChatController extends Controller\n{\n    public function serve()\n    {\n        Log::info('request arrived.'); \n\n        $server = app('easywechat.official_account')-\u003egetServer();\n\n        $server-\u003ewith(function($message){\n            return \"欢迎关注 overtrue！\";\n        });\n\n        return $server-\u003eserve();\n    }\n}\n```\n\n## OAuth 中间件\n\n使用中间件的情况下 `app/config/easywechat.php` 中的 `oauth.callback` 就随便填写吧(因为用不着了 :smile:)。\n\n1. 在 `app/Http/Kernel.php` 中添加路由中间件：\n\n```php\nprotected $routeMiddleware = [\n    // ...\n    'easywechat.oauth' =\u003e \\Overtrue\\LaravelWeChat\\Middleware\\OAuthAuthenticate::class,\n];\n```\n\n2. 在路由中添加中间件：\n\n```php\n//...\nRoute::group(['middleware' =\u003e ['web', 'easywechat.oauth']], function () {\n    Route::get('/user', function () {\n        $user = session('easywechat.oauth_user.default'); // 拿到授权用户资料\n\n        dd($user);\n    });\n});\n```\n\n中间件支持指定配置名称：`'easywechat.oauth:default'`，当然，你也可以在中间件参数指定当前的 `scopes`:\n\n```php\nRoute::group(['middleware' =\u003e ['easywechat.oauth:snsapi_userinfo']], function () {\n  // ...\n});\n\n// 或者指定账户的同时指定 scopes:\nRoute::group(['middleware' =\u003e ['easywechat.oauth:default,snsapi_userinfo']], function () {\n  // ...\n});\n```\n\n上面的路由定义了 `/user` 是需要微信授权的，那么在这条路由的**回调 或 控制器对应的方法里**， 你就可以从 `session('easywechat.oauth_user.default')` 拿到已经授权的用户信息了。\n\n## 模拟授权\n\n有时候我们希望在本地开发完成后线上才真实的走微信授权流程，这将减少我们的开发成本，那么你需要做以下两步：\n\n1. 准备模拟授权资料：\n2. \n```php\nuse Illuminate\\Support\\Arr;\nuse Overtrue\\Socialite\\User as SocialiteUser;\n\n$user = new SocialiteUser([\n            'id' =\u003e 'mock-openid',\n            'name' =\u003e 'overtrue',\n            'nickname' =\u003e 'overtrue',\n            'avatar' =\u003e 'http://example.com/avatars/overtrue.png',\n            'email' =\u003e null,\n            'original' =\u003e [],\n            'provider' =\u003e 'WeChat',\n        ]);\n```\n\n\u003e 以上字段在 scope 为 `snsapi_userinfo` 时尽可能配置齐全哦，当然，如果你的模式只是 `snsapi_base` 的话只需要 `openid` 就好了。\n\n2. 将资料写入 session：\n\n\u003e 注意：一定要在调用 OAuth 中间件之前写入，比如你可以创建一个全局中间件来完成这件事儿，只在开发环境启用即可。\n\n```php\nsession(['easywechat.oauth_user.default' =\u003e $user]); // 同理，`default` 可以更换为您对应的其它配置名\n```\n\n## 事件\n\n\u003e 你可以监听相应的事件，并对事件发生后执行相应的操作。\n\n- OAuth 网页授权：`Overtrue\\LaravelWeChat\\Events\\WeChatUserAuthorized`\n\n```php\n// 该事件有以下属性\n$event-\u003euser; // 同 session('easywechat.oauth_user.default') 一样\n$event-\u003eisNewSession; // 是不是新的会话（第一次创建 session 时为 true）\n$event-\u003eaccount; // 当前中间件所使用的账号，对应在配置文件中的配置项名称\n```\n\n\n## 开放平台支持\n\n您可以适用内置的 `Overtrue\\LaravelWeChat\\Traits\\HandleOpenPlatformServerEvents` 来快速完成开放平台的服务端验证工作：\n\n*routes/web.php:*\n```php\nRoute::any('/open-platform/server', OpenPlatformController::class);\n```\n\n*app/Http/Controllers/OpenPlatformController.php:*\n\n```php\n\u003c?php\n\nnamespace App\\Http\\Controllers;\n\nuse Overtrue\\LaravelWeChat\\Traits\\HandleOpenPlatformServerEvents;\n\nclass OpenPlatformController extends Controller\n{\n    use HandleOpenPlatformServerEvents;\n    \n    public function __invoke(Application $application): \\Psr\\Http\\Message\\ResponseInterface\n    {\n        $app = app('easywechat.open_platform');\n        \n        return $this-\u003ehandleServerEvents($app);\n    }\n}\n```\n\nTips: 默认会根据微信开放平台的推送内容触发如下事件，你可以监听相应的事件并进行处理：\n\n- 授权方成功授权：`Overtrue\\LaravelWeChat\\Events\\OpenPlatform\\Authorized`\n- 授权方更新授权：`Overtrue\\LaravelWeChat\\Events\\OpenPlatform\\AuthorizeUpdated`\n- 授权方取消授权：`Overtrue\\LaravelWeChat\\Events\\OpenPlatform\\Unauthorized`\n- 开放平台推送 VerifyTicket：`Overtrue\\LaravelWeChat\\Events\\OpenPlatform\\VerifyTicketRefreshed`\n\n```php\n// 事件有如下属性\n$message = $event-\u003epayload; // 开放平台事件通知内容\n```\n\n配置后 `http://example.com/open-platform/server` 则为开放平台第三方应用设置的授权事件接收 URL。\n\n\n更多 SDK 的具体使用请参考：\u003chttps://www.easywechat.com\u003e\n\n## :heart: Sponsor me \n\n[![Sponsor me](https://github.com/overtrue/overtrue/blob/master/sponsor-me.svg?raw=true)](https://github.com/sponsors/overtrue)\n\n如果你喜欢我的项目并想支持它，[点击这里 :heart:](https://github.com/sponsors/overtrue)\n\n## Project supported by JetBrains\n\nMany thanks to Jetbrains for kindly providing a license for me to work on this and other open-source projects.\n\n[![](https://resources.jetbrains.com/storage/products/company/brand/logos/jb_beam.svg)](https://www.jetbrains.com/?from=https://github.com/overtrue)\n\n\n## PHP 扩展包开发\n\n\u003e 想知道如何从零开始构建 PHP 扩展包？\n\u003e\n\u003e 请关注我的实战课程，我会在此课程中分享一些扩展开发经验 —— [《PHP 扩展包实战教程 - 从入门到发布》](https://learnku.com/courses/creating-package)\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fovertrue%2Flaravel-wechat","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fovertrue%2Flaravel-wechat","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fovertrue%2Flaravel-wechat/lists"}