Laravel集成EasyWeChat接收普通消息和事件推送

Laravel快速集成EasyWeChat:http://www.appblog.cn/2018/02/11/Laravel快速集成EasyWeChat/

<?php
/**
 * Created by PhpStorm.
 * User: yezhou
 * Date: 2018/2/7
 * Time: 13:20
 */

namespace App\Http\Controllers;

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) {
            switch ($message['MsgType']) {
                case 'event':
                    Log::info('收到事件消息');
                    //接收事件推送: https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140454
                    switch ($message['Event']) {
                        case 'subscribe':  //关注事件, 扫描带参数二维码事件(用户未关注时,进行关注后的事件推送)
                            return "欢迎关注 HTTP://WWW.AppBlog.CN!";
                            break;
                        case 'unsubscribe':  //取消关注事件

                            break;
                        case 'SCAN':  //扫描带参数二维码事件(用户已关注时的事件推送)
                            return "欢迎关注 HTTP://WWW.AppBlog.CN!";
                            break;
                        case 'LOCATION':  //上报地理位置事件
                            return "经度: " . $message['Longitude'] . "\n纬度: " . $message['Latitude'] . "\n精度: " . $message['Precision'];
                            break;
                        case 'CLICK':  //自定义菜单事件(点击菜单拉取消息时的事件推送)
                            return "事件KEY值: " . $message['EventKey'];
                            break;
                        case 'VIEW':  //自定义菜单事件(点击菜单拉取消息时的事件推送)
                            return "跳转URL: " . $message['EventKey'];
                            break;
                        case 'ShakearoundUserShake':
                            Log::info('摇一摇周边事件');
                            //摇一摇事件通知: https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1443448066
                            return 'ChosenBeacon\n' . 'Uuid: ' . $message['ChosenBeacon']['Uuid'] . 'Major: ' . $message['ChosenBeacon']['Major'] . 'Minor: ' . $message['ChosenBeacon']['Minor'] . 'Distance: ' . $message['ChosenBeacon']['Distance'];
                            break;
                        default:
                            return $message['Event'];
                            break;
                    }
                    break;
                //接收普通消息: https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140453
                case 'text':
                    Log::info('收到文字消息');
                    return "Content: " . $message['Content'];
                    break;
                case 'image':
                    Log::info('收到图片消息');
                    return "MediaId: " . $message['MediaId'] . "\nPicUrl: " . $message['PicUrl'];
                    break;
                case 'voice':
                    Log::info('收到语音消息');
                    return "MediaId: " . $message['MediaId'] . "\nFormat: " . $message['Format'] . "\nRecognition: " . $message['Recognition'];
                    break;
                case 'video':
                    Log::info('收到视频消息');
                    return "MediaId: " . $message['MediaId'] . "\nThumbMediaId: " . $message['ThumbMediaId'];
                    break;
                case 'shortvideo':
                    Log::info('收到小视频消息');
                    return "MediaId: " . $message['MediaId'] . "\nThumbMediaId: " . $message['ThumbMediaId'];
                    break;
                case 'location':
                    return "Location_X: " . $message['Location_X'] . "\nLocation_Y: " . $message['Location_Y'] . "\nScale: " . $message['Scale'] . "\nLabel: " . $message['Label'];
                    Log::info('收到坐标消息');
                    break;
                case 'link':
                    Log::info('收到链接消息');
                    return "Title: " . $message['Title'] . "\nDescription: " . $message['Description'] . "\nUrl: " . $message['Url'];
                    break;
                default:
                    Log::info('收到其它消息');
                    return $message['MsgType'];
                    break;
            }
        });

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

}

版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/02/26/laravel-integrate-easywechat-to-receive-regular-messages-and-event-push-notifications/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
Laravel集成EasyWeChat接收普通消息和事件推送
Laravel快速集成EasyWeChat:http://www.appblog.cn/2018/02/11/Laravel快速集成EasyWeChat/ <?php /** * Created by PhpStorm. * User: yezhou * Date……
<<上一篇
下一篇>>
文章目录
关闭
目 录