laravel easywechat 开发微信公众服务号
https://packagist.org/packages/overtrue/laravel-wechat
一、安装easywechat
composer require overtrue/laravel-wechat
二、创建配置文件
php artisan vendor:publish --provider="Overtrue\LaravelWeChat\ServiceProvider"
三、修改应用根目录下的 config/wechat.php
/* * 公众号 */ 'official_account' => [ 'default' => [ 'app_id' => env('WECHAT_OFFICIAL_ACCOUNT_APPID', 'wx113156b4239e0fc9'), // AppID 'secret' => env('WECHAT_OFFICIAL_ACCOUNT_SECRET', '012ce0fc6667a8931bf9b34f24er0c17'), // AppSecret 'token' => env('WECHAT_OFFICIAL_ACCOUNT_TOKEN', 'Qj3vVq47ZY5q4w9VD2E074eOz69vw994'), // Token 'aes_key' => env('WECHAT_OFFICIAL_ACCOUNT_AES_KEY', 'XK83W636GL6L3I6X3XfyfQ658WgUI6y0YwMPM6G383p'), // EncodingAESKey /* * OAuth 配置 * * scopes:公众平台(snsapi_userinfo / snsapi_base),开放平台:snsapi_login * callback:OAuth授权完成后的回调页地址(如果使用中间件,则随便填写。。。) */ // 'oauth' => [ // 'scopes' => array_map('trim', explode(',', env('WECHAT_OFFICIAL_ACCOUNT_OAUTH_SCOPES', 'snsapi_userinfo'))), // 'callback' => env('WECHAT_OFFICIAL_ACCOUNT_OAUTH_CALLBACK', '/examples/oauth_callback.php'), // ], ], ],
四、定义路由,一定是 Route::any, 因为微信服务端认证的时候是 GET, 接收用户消息时是 POST !
Route::group(['domain' => config('app.m_domain'),'namespace' => 'M'], function () { Route::any('/wechat', 'WeChatController@serve'); });
五、创建控制器 WechatController
php artisan make:controller M/WeChatController
<?php namespace App\Http\Controllers\M; use Illuminate\Http\Request; use App\Http\Controllers\Controller; use Illuminate\Support\Facades\Log; class WeChatController extends Controller { // /** * 处理微信的请求消息 * * @return string */ public function serve() { Log::info('request arrived.'); # 注意:Log 为 Laravel 组件,所以它记的日志去 Laravel 日志看,而不是 EasyWeChat 日志 $app = app('wechat.official_account'); $app->server->push(function($message){ return "欢迎关注 overtrue!"; }); return $app->server->serve(); } }
六、在中间件 App\Http\Middleware\VerifyCsrfToken 排除微信相关的路由
<?php namespace App\Http\Middleware; use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as Middleware; class VerifyCsrfToken extends Middleware { /** * Indicates whether the XSRF-TOKEN cookie should be set on the response. * * @var bool */ protected $addHttpCookie = true; /** * The URIs that should be excluded from CSRF verification. * * @var array */ protected $except = [ 'wechat_notify', 'ali_notify', 'wechat', 'wechat/*' ]; }
然后将这个代码上传到云服务器 (注:服务器一定要在公网中 (或通过某种工具使自己的电脑映射到公网上),否则微信无法验证)
默认配置好虚拟主机,(必须用 80 端口 (http://)/443 端口 (https://)),指向:项目名 /public
七、 在公众号后台修改配置信息
Token 必须和我们配置文件中写的 token 一致,EncodingAESKey 是可选参数(兼容模式下必填),配置我们点击启用就基本配置完成了
八、新建公众号菜单
<?php namespace App\Http\Controllers\M; use App\Services\Wechat\MenuService; use Illuminate\Http\Request; use App\Http\Controllers\Controller; use Illuminate\Support\Facades\Log; class WeChatController extends Controller { // /** * 处理微信的请求消息 * * @return string */ public function serve() { Log::info('request arrived.'); # 注意:Log 为 Laravel 组件,所以它记的日志去 Laravel 日志看,而不是 EasyWeChat 日志 $app = app('wechat.official_account'); $app->server->push(function($message){ return "欢迎关注 一览商城!"; }); return $app->server->serve(); } public function wxMenu(Request $request,MenuService $menuService){ $buttons = [ [ "name" => "我要采购", "sub_button" => [ [ "type" => "view", "name" => "发布采购", "url" => "http://m.bchem.com/inquiry/add" ], [ "type" => "view", "name" => "采购管理", "url" => "http://m.bchem.com/inquiry/lists" ], [ "type" => "view", "name" => "采购订单", "url" => "http://m.bchem.com/order/lists" ], ], ], [ "name" => "我要销售", "sub_button" => [ [ "type" => "view", "name" => "发布产品", "url" => "http://m.bchem.com/sale/goods_add" ], [ "type" => "view", "name" => "产品管理", "url" => "http://m.bchem.com/sale/goods_list" ], [ "type" => "view", "name" => "询盘管理", "url" => "http://m.bchem.com/sale/lists" ], [ "type" => "view", "name" => "销售订单", "url" => "http://m.bchem.com/sale/order" ], ], ], [ "name" => "会员中心", "sub_button" => [ [ "type" => "view", "name" => "注册/登录", "url" => "http://m.bchem.com/users/sign_in" ], /*[ "type" => "click", "name" => "联系客服", "key" => "CONTACT_KEFU" ],*/ [ "type" => "view", "name" => "会员中心", "url" => "http://m.bchem.com/member/index" ], ], ], ]; return $menuService->index($request,$buttons); } }
<?php namespace App\Services\Wechat; class MenuService { protected $app; public function __construct() { $this->app = app('wechat.official_account'); } public function index($request,$buttons){ if($request->get('type')=='create'){ $this->create($buttons); return '创建菜单成功!'; }elseif($request->get('type')=='list'){ return $this->lists(); }elseif($request->get('type')=='current'){ return $this->current(); }elseif($request->get('type')=='delete'){ $this->delete(); return '删除菜单成功!'; } } //添加普通菜单 public function create($buttons){ $this->app->menu->create($buttons); } //读取(查询)已设置菜单 public function lists(){ return $this->app->menu->list(); } //获取当前菜单 public function current(){ return $this->app->menu->current(); } //删除菜单 public function delete(){ $this->app->menu->delete(); } }
浏览器打开:
创建菜单:http://m.bchem.com/wechat/menu?type=create
删除菜单:http://m.bchem.com/wechat/menu?type=delete