laravel-wechat:微信SDK for Laravel, 基于overtrue/wechat
EasyWeChat文档

消息类型的判断与消息回复
WechatController.php

<?php
namespace App\Http\Controllers;
use EasyWeChat\Material\Material;
use EasyWeChat\Message\Image;
use EasyWeChat\Message\News;
use EasyWeChat\Message\ShortVideo;
use EasyWeChat\Message\Video;
use EasyWeChat\Message\Voice;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Controllers\MaterialController;
class WechatController extends Controller
{
    public function serve()
    {
        $wechat = app('wechat');
        $userApi = $wechat->user;
        $wechat->server->setMessageHandler(function($message) use ($userApi,$wechat){
            switch ($message->MsgType) {
                case 'event':
                    switch($message->Event){
                        case 'subscribe':
                            return "你好啊,".$userApi->get($message->FromUserName)->nickname."。\n感谢您的关注,我的微博是http://laji.blog,联系方式在网页左侧哦!";
                            break;
                        case 'unsubscribe':
                            return '5555555~';
                            break;
                        case 'CLICK':
                            switch($message->EventKey){
                                case 'YOU_ARE_GOOD':
                                    return '谢谢你,么么哒(づ ̄ 3 ̄)づ';
                                    break;
                                case 'TEST_NORMAL_MENU':
                                    return '['.$message->EventKey.']你点击了测试普通菜单';
                                    break;
                                case 'TEST_CUSTOM_MENU':
                                    return '['.$message->EventKey.']你点击了测试个性化菜单';
                                    break;
                                case 'GET_INSTRUCTIONS':
                                    $str =  '回复以下指令获取相关信息:'."\n".
                                        'me:获取个人信息'."\n".
                                        'openid:获取个人openid'."\n".
                                        'unionid:获取个人unionid'."\n".
                                        'groupid:获取个人groupid';
                                    return $str;
                                    break;

                            }
                        default:
                            return null;
                            break;
                    }
                    break;
                case 'text':
                    $user = $userApi->get($message->FromUserName);
                    switch($message->Content){
                        case 'me':
                        case 'ME':
                            $nickname = $user->nickname;
                            $remark = ($user->remark)?$user->remark:'无';
                            $sex = ($user->sex)?(($user->sex==1)?'男':'女'):'未知';
                            $area = ($user->country)?$user->country:''.($user->province)?$user->province:''.($user->city)?$user->city:'';
                            $language = $user->language;
                            $headimg = $user->headimgurl;
                            $str = '昵称:'.$nickname."\n".
                                   '备注名:'.$remark."\n".
                                   '性别:'.$sex."\n".
                                   '地区:'.$area."\n".
                                   '语言:'.$language."\n".
                                   '头像:'.$headimg;
                        if($str){
                                return $str;
                            }else{}
                                return null;
                            break;
                        case 'openid':
                        case 'OPENID':
                            return 'openid:'.$user->openid;
                            break;
                        case 'unionid':
                        case 'UNIONID':
                            return 'unionid:'.$user->unionid;
                            break;
                        case 'groupid':
                        case 'GROUPID':
                            return 'groupid:'.$user->groupid;
                            break;
                        default:
                            $str =  '回复以下指令获取相关信息:'."\n".
                                    'me:获取个人信息'."\n".
                                    'openid:获取个人openid'."\n".
                                    'unionid:获取个人unionid'."\n".
                                    'groupid:获取个人groupid';
                            return $str;
                            break;
                    }
                    break;
                case 'image':
                    return '图片链接:'.$message->PicUrl;
                    //$image = new Image(['media_id'=>'L0dH8QtQESoxmvHUnu07rdpJZW0Ii-RZ_YZ4jOTu4qE']);
                    return $image;
                    break;
                case 'voice':
                    $voice = new Voice(['media_id'=>$message->MediaId]);
                    return $voice;
                    //以客服发送消息不用return
                    //$result = $wechat->staff->message($voice)->to($message->FromUserName)->send(); //作为客服消息发送
                    break;
                case 'video':
                    $voice = new Voice(['media_id'=>'L0dH8QtQESoxmvHUnu07rd7bC_DuCY6fVJWbk7Wy0Go']);
                    return $voice;
                    break;
                case 'shortvideo':
                    $voice = new Voice(['media_id'=>'L0dH8QtQESoxmvHUnu07rd7bC_DuCY6fVJWbk7Wy0Go']);

                    return $voice;
                    break;
                case 'location':
                    $str = "纬度:".$message->Location_X."\n经度:".$message->Location_Y."\n位置信息:".$message->Label;
                    return $str;
                    break;
                case 'link':
                    $str = "消息标题:".$message->Title."\n消息描述:".$message->Description."\n".$message->Url;
                    return $str;
                    break;
                default:
                    return null;
                    break;
            }

        });
        return $wechat->server->serve();
    }
}

对用户的一些简单操作
WechatController.php

<?php    
namespace App\Http\Controllers;    
use EasyWeChat\Foundation\Application;
use Illuminate\Http\Request;    
use App\Http\Requests;
use App\Http\Controllers\Controller;
class UsersController extends Controller
{
    public $wechat;

    public function __construct(Application $wechat)
    {
        $this->wechat = $wechat;
    }

    //获取所有用户
    public function users()
    {
        $users = $this->wechat->user->lists();
        return $users;
    }

    //获取该用户的信息
    public function user($openId)
    {
        $user = $this->wechat->user->get($openId);
        return $user;
    }

    //修改用户备注
    public function remark($openId,$remark)
    {
        if($this->wechat->user->remark($openId,$remark)){
            return '修改用户:【'.$this->user($openId)->get('nickname').'】的备注为:【'.$remark.'】成功。';
        }
    }

    //获取用户所属用户组ID
    public function group($openId)
    {
        $userGroupId = $this->wechat->user->group($openId);
        return $userGroupId;
    }
}

素材管理
MaterialController.php

<?php    
namespace App\Http\Controllers;    
use EasyWeChat\Foundation\Application;
use Illuminate\Http\Request;    
use App\Http\Requests;
use App\Http\Controllers\Controller
class MaterialController extends Controller
{
    // 永久素材
    public $material;
    // 临时素材
    public $temporary;

    public function __construct(Application $app)
    {
        $this->material = $app->material;
        $this->temporary = $app->material_temporary;

    }

    //上传素材(要注意路径的文件夹和路由不要冲突了,不然就变成访问文件夹寻找index.html然后报404错误了)
    //永久素材
    public function material_image_upload($path=null){
        if($path==null){
            $path = public_path().'/images/mdzz.jpg';
        }else{
            $path = $path;
        }
        $image = $this->material->uploadImage($path);
        return $image;
    }
    public function material_voice_upload($path=null){
        if($path==null){
            $path = public_path().'/voice/mdzz.mp3';
        }else{
            $path = $path;
        }
        $voice = $this->material->uploadVoice($path);
        return $voice;
    }
    public function material_video_upload($path=null){
        if($path==null){
            $path = public_path().'/video/mdzz.mp4';
        }else{
            $path = $path;
        }
        $video = $this->material->uploadVideo($path,'妈的智障','23333');
        return $video;
    }
    public function material_thumb_upload($path=null){
        if($path==null){
            $path = public_path().'/thumb/mdzz.jpg';
        }else{
            $path = $path;
        }
        $thumb = $this->material->uploadThumb($path);
        return $thumb;
    }

    //临时素材
    public function temporary_image_upload($path=null){
        if($path==null){
            $path = public_path().'/images/mdzz.jpg';
        }else{
            $path = $path;
        }
        $image = $this->temporary->uploadImage($path,'妈的智障','23333');
        return $image;
    }
    public function temporary_voice_upload($path=null){
        if($path==null){
            $path = public_path().'/voice/mdzz.mp3';
        }else{
            $path = $path;
        }
        $voice = $this->temporary->uploadVoice($path);
        return $voice;
    }
    public function temporary_video_upload($path=null){
        if($path==null){
            $path = public_path().'/video/mdzz.mp4';
        }else{
            $path = $path;
        }
        $video = $this->temporary->uploadVideo($path);
        return $video;
    }
    public function temporary_thumb_upload($path=null){
        if($path==null){
            $path = public_path().'/thumb/mdzz.jpg';
        }else{
            $path = $path;
        }
        $thumb = $this->temporary->uploadThumb($path);
        return $thumb;
    }

    //获取素材列表
    public function news(){
        $news = $this->material->lists('news');
        return $news;
    }
    public function images(){
        $images = $this->material->lists('image');
        return $images;
    }
    public function voices(){
        $voices = $this->material->lists('voice');
        return $voices;
    }
    public function videos(){
        $videos = $this->material->lists('video');
        return $videos;
    }
    public function thumbs(){
        $thumbs = $this->material->lists('thumb');
        return $thumbs;
    }

    //查找/显示素材
    public function media($mediaId){
        $media = $this->material->get($mediaId);
        return $media;
    }

    //具体更多请查看文档:
    ////easywechat.org/docs//material.html
}

群发消息和模板消息
MessageController.php

<?php    
namespace App\Http\Controllers;    
use EasyWeChat\Foundation\Application;
use Illuminate\Http\Request;    
use App\Http\Requests;
use App\Http\Controllers\Controller;
class MessageController extends Controller
{

    public $broadcast;
    public $notice;

    public function __construct(Application $app)
    {
        $this->broadcast = $app->broadcast;
        $this->notice = $app->notice;
    }

    //群发消息
    public function message(){
        $this->broadcast->send('image','c8PiEzRTKgBBNKVEL0A36WpHHhGLCPBSZBBvdrIdLt0');
        return 'Done';
    }

    public function message_text(){
        $this->broadcast->sendText('这是一条群发消息~');
        return 'Done';
    }
    public function message_image(){
        $this->broadcast->sendImage('c8PiEzRTKgBBNKVEL0A36WpHHhGLCPBSZBBvdrIdLt0');
        return 'Done';
    }

    //模板消息
    public function notice(){
        $userId = 'o88PVvtqmYa5UmQZdhd7Iq772vIc';
        $templateId = 'zR_e8qrO-QX3HnhSOtONISSc_SrzUjq502ehj9qns_o';
        $url = 'http://laji.blog/mall';
        $data = array(
            'first' => array('恭喜你购买成功!','#555555'),
            'name'  => array('节操',"#336699"),
            'price' => array('450.0元', "#FF0000"),
            'remark'=> array('欢迎您再次购买!',"#5599FF"),
        );
        $messageId = $this->notice->uses($templateId)->withUrl($url)->andData($data)->andReceiver($userId)->send();
        return 'Done:'.$messageId;
    }
}

自定义菜单
MenuController.php

<?php    
namespace App\Http\Controllers;    
use EasyWeChat\Foundation\Application;
use Illuminate\Http\Request;    
use App\Http\Requests;
use App\Http\Controllers\Controller;

class MenuController extends Controller
{

    public $menu;

    public function __construct(Application $app)
    {
        $this->menu = $app->menu;
    }

    public function menu_all(){
        //获取查询菜单
        $menus_all = $this->menu->all();
        return $menus_all;
    }

    public function menu_current(){
        //获取自定义菜单
        $menus_current = $this->menu->current();
        return $menus_current;
    }

    public function menu_add_normal(){
        //添加普通菜单
        //自定义菜单一级允许3个,二级允许5个
        $buttons = array(
            array(
                "name"       => "菜单",
                "sub_button" => array(
                    array("type" => "view",
                        "name" => "登录",
                        "url"  => "http://laji.blog/laravel/wechat/public/user"
                    ),
                    array("type" => "view",
                        "name" => "博客",
                        "url"  => "http://laji.blog/"
                    ),
                    array(
                        "type" => "view",
                        "name" => "商城",
                        "url"  => "http://laji.blog/mall/"
                    ),
                    array(
                        "type" => "click",
                        "name" => "点赞",
                        "key" => "YOU_ARE_GOOD"
                    ),
                    array(
                        "type" => "click",
                        "name" => "查看指令",
                        "key" => "GET_INSTRUCTIONS"
                    )
                )
            ),
            array(
                "type" => "click",
                "name" => "测试普通菜单",
                "key"  => "TEST_NORMAL_MENU"
            )
        );

        $this->menu->add($buttons);
        return 'Done';

    }

    public function menu_add_custom(){
        //添加个性化菜单
        //自定义菜单一级允许3个,二级允许5个
        $buttons = array(
            array(
                "name"       => "菜单",
                "sub_button" => array(
                    array("type" => "view",
                        "name" => "登录",
                        "url"  => "http://laji.blog/laravel/wechat/public/user"
                    ),
                    array("type" => "view",
                        "name" => "博客",
                        "url"  => "http://laji.blog/"
                    ),
                    array(
                        "type" => "view",
                        "name" => "商城",
                        "url"  => "http://laji.blog/mall/"
                    ),
                    array(
                        "type" => "click",
                        "name" => "点赞",
                        "key" => "YOU_ARE_GOOD"
                    ),
                    array(
                        "type" => "click",
                        "name" => "查看指令",
                        "key" => "GET_INSTRUCTIONS"
                    )
                )
            ),
            array(
                "type" => "click",
                "name" => "测试个性化菜单",
                "key"  => "TEST_CUSTOM_MENU"
            )
        );

        $matchRule = array(
            "group_id"             => "0",
            "sex"                  => "1",
            "country"              => "中国",
            "province"             => "广东",
            "city"                 => "广州",
            "client_platform_type" => "2"  //系统型号:IOS(1), Android(2),Others(3)
        );

        $this->menu->add($buttons,$matchRule);
        return 'Done';

    }

    public function menu_delete(){
        $this->menu->destroy(); //删除全部
        return 'Done';
    }

    public function menu_delete_menuid($menuId){
        $this->menu->destroy($menuId);
        return 'Done';
    }
}

客服
StaffController.php

<?php    
namespace App\Http\Controllers;    
use EasyWeChat\Foundation\Application;
use Illuminate\Http\Request;    
use App\Http\Requests;
use App\Http\Controllers\Controller;

class StaffController extends Controller
{

    public $staff;

    public function __construct(Application $app)
    {
        $this->staff = $app->staff;
    }

    public function staff_lists(){
        return $this->staff->lists();
    }
    public function staff_onlines(){
        return $this->staff->onlines();
    }

    //kf_account 完整客服账号,格式为:账号前缀@公众号微信号,账号前缀最多10个字符,必须是英文或者数字字符。如果没有公众号微信号,请前往微信公众平台设置。
    //nickname 客服昵称,最长6个汉字或12个英文字符
    //password 客服账号登录密码,格式为密码明文的32位加密MD5值

    //大概测试号不行~
    public function staff_create(){
        $email = '[email protected]';
        $nickname = 'admin';
        $password = md5('123456');
        $this->staff->create($email,$nickname,$password);
        return 'Done';
    }
    public function staff_update(){
        $email = '[email protected]';
        $nickname = 'admin';
        $password = md5('123456');
        $this->staff->update($email,$nickname,$password);
        return 'Done';
    }
    public function staff_delete(){
        $email = '[email protected]';
        $nickname = 'admin';
        $password = md5('123456');
        $this->staff->delete($email,$nickname,$password);
        return 'Done';
    }
    public function staff_avatar(){
        $email = '[email protected]';
        $avatarPath = public_path().'/images/mdzz.jpg';
        $this->staff->avatar($email,$avatarPath);
        return 'Done';
    }
    public function staff_message_send(){
        $openId = 'o88PVvtqmYa5UmQZdhd7Iq772vIc';
        $message = '你好啊,这是来自客服的一条指定用户的消息';
        $this->staff->message($message)->to($openId)->send();
        return 'Done';
    }
    public function staff_message_sendby(){
        $openId = 'o88PVvtqmYa5UmQZdhd7Iq772vIc';
        $message = '你好啊,这是来自指定客服的一条指定用户的消息';
        $email = '[email protected]';
        $this->staff->message($message)->by($email)->to($openId)->send();
        return 'Done';
    }
}