meteorlxy/laravel-wechat

This package is abandoned and no longer maintained. No replacement package was suggested.

Wechat package for Laravel

0.1.4 2017-09-26 14:05 UTC

This package is auto-updated.

Last update: 2022-04-11 23:36:34 UTC


README

个人边学习边开发的Wechat SDK for Laravel

还远远没有完成

部分内容参考自overtrue/wechatovertrue/laravel-wechat

使用说明

  1. 使用Composer引入
composer require meteorlxy/laravel-wechat
  1. 注册ServiceProvider

config/app.php中加入

'providers' => [
    ...
    Meteorlxy\LaravelWechat\WechatServiceProvider::class,
]
  1. 配置文件

运行以下命令,将在config/文件夹下生成wechat.php文件

php artisan vendor:publish

可以直接修改wechat.php,也可以在.env中添加如下变量覆盖默认配置

WECHAT_APPID
WECHAT_APPSECRET
WECHAT_TOKEN
WECHAT_AES_KEY
WECHAT_SERVER
WECHAT_TIMEOUTE
  1. 使用
<?php

namespace App\Http\Controllers\Wechat;

use Illuminate\Http\Request;
use Meteorlxy\LaravelWechat\Controllers\WechatController as BaseController;

class WechatController extends BaseController
{
    public function listen() {
        $this->wechat->server->setHandler(function($message) {
            switch($message->MsgType) {
                case 'event' :
                    $text = '处理文字消息';
                    break;
                case 'event' :
                    $text = '处理事件消息';
                    break;
                default:
                    $text = '默认处理器';
                    break;
            }
            return [
                'Content' => $text,
                
                // 若不设置MsgType,默认回复为Text消息,则必须设置Content
                'MsgType' => 'text',
                
                // 若不设置FromUserName和ToUserName,默认回复给消息来源用户
                'FromUserName' => $message->ToUserName,
                'ToUserName' => $message->FromUserName,
            ];
        });

        return $this->wechat->server->handle($request);
    }
}