EasyWeChat在laravel框架中的使用技巧

EasyWeChat在laravel框架中的使用技巧

laravel框架實戰:
EasyWeChat

EasyWeChat公衆號配置

protected $wechat;

public function __construct ()
{
    $this->wechat = app('wechat.official_account');  //微信公衆號配置
}

報錯:Failed to cache access token.

報錯:Failed to cache access token.的原因大部分是:token緩存文件沒寫成功。

在使用wechat方法的上面加入以下代碼:

 $this->wechat->cache = new CacheBridge(app('cache.store'));

獲取公衆號臨時二維碼

$res = $this->wechat->qrcode->temporary('WXiangQian', 6 * 24 * 3600);

公衆號模板消息推送

$this->wechat->template_message->send([
    'touser' => $this->message['FromUserName'],
    'template_id' => 'template_id',// 模板消息id
    'url' => 'https://wxiangqian.github.io/', // 要跳轉的url
    'data' => [
        "first" => "歡迎來到本公衆號",
        "keyword1" => '你好,WXiangQian', // 必須爲keyword
        "keyword2" => "歡迎來到本公衆號",
        "keyword3" => "恭喜你成功入坑",
        "remark" => "小提示:期待您的持續關注", //備註
    ]
]);

公衆號服務端的使用

在微信開發中主要是負責接收用戶發送過來的消息,還有用戶觸發的一系列事件

$app->server->push(function ($message) {
    // $message['FromUserName'] // 用戶的 openid
    // $message['MsgType'] // 消息類型:event, text....
    return "您好!歡迎使用 EasyWeChat";
});

// 在 laravel 中:
$response = $app->server->serve();

// $response 爲 `Symfony\Component\HttpFoundation\Response` 實例
// 對於需要直接輸出響應的框架,或者原生 PHP 環境下
$response->send();

// 而 laravel 中直接返回即可:

return $response;

$message[‘MsgType’] 詳解

$app->server->push(function ($message) {
    switch ($message['MsgType']) {
        case 'event':
            return '收到事件消息';
            break;
        case 'text':
            return '收到文字消息';
            break;
        case 'image':
            return '收到圖片消息';
            break;
        case 'voice':
            return '收到語音消息';
            break;
        case 'video':
            return '收到視頻消息';
            break;
        case 'location':
            return '收到座標消息';
            break;
        case 'link':
            return '收到鏈接消息';
            break;
        case 'file':
            return '收到文件消息';
        // ... 其它消息
        default:
            return '收到其它消息';
            break;
    }

    // ...
});

MsgType Event 事件類型詳解

掃描帶參數的二維碼事件

EventKey 事件KEY值,比如:qrscene_123123,qrscene_爲前綴,後面爲二維碼的參數值
Ticket 二維碼的 ticket,可用來換取二維碼圖片
// (如:subscribe(訂閱)、unsubscribe(取消訂閱) ..., CLICK 等)
if($message['MsgType'] == 'event') {  //收到事件消息
    switch ($message['Event']) {
        case 'CLICK':          //點擊事件
            break;
        case 'subscribe':      //訂閱
            $event_info = substr($this->message['EventKey'], 8);
            $event_arr = explode('_', $event_info);
            if (!empty($event_arr) && $event_arr[0] == 'test') 
                // test邏輯
            }else{
                // 關注公衆號邏輯
                return $this->subscribe($event_arr);
            }
            break;
        case 'unsubscribe':    //取消訂閱
            return $this->unSubscribe();
            break;
        case 'SCAN':           //已關注的情況下掃描二維碼
            $this->scan();
            break;
    }
}

EasyWeChat小程序配置

    $this->config = [
        'app_id' => 'app_id',
        'secret' => 'secret',
        'response_type' => 'array',
        'log' => [
            'level' => 'debug',
         // 'file' => __DIR__.'/wechat.log',
        ],
    ];

    $this->app = Factory::miniProgram($this->config);

小程序報錯:Identifier “subscribe_message” is not defined.

報錯的原因是當前的laravel-wechat版本過低,需要更新,小程序訂閱消息是後續新增的功能,所以說低版本用不了訂閱消息。小程序的模板消息已停用,後續都會接入訂閱消息。

小程序訂閱消息推送

$send_data = [
    'template_id' => 'template_id', // 所需下發的訂閱模板id
    'touser' => 'openid',     // 接收者(用戶)的 openid
    'page' => '/pages/index,       // 點擊模板卡片後的跳轉頁面,僅限本小程序內的頁面。支持帶參數,(示例index?foo=bar)。該字段不填則模板無跳轉。
    'data' => [         
    // 模板內容,格式形如 { "key1": { "value": any }, "key2": { "value": any } }
        'thing2' => [
            'value' => '課程標題',
        ],
        'character_string6' => [
            'value' => '1/1',
        ],
        'thing7' => [
            'value' => '恭喜你已成功解鎖這個課程',
        ],
    ],
];
// 非線上環境
if (env('APP_ENV') != 'production') {
    $send_data['miniprogramState'] = 'developer';
}
$this->app->subscribe_message->send($send_data);

根據 jsCode 獲取用戶 session 信息

    $code = $this->request->input('code');
    // 返回openid
    $data = $this->app->auth->session($code);
    $openid = $data['openid'];

微信小程序消息解密

    $session = $this->request->input('session');
    $iv = $this->request->input('iv');
    $encryptedData = $this->request->input('encryptedData');
    $decrypted = AES::decrypt(
     base64_decode($encryptedData, false), base64_decode($session, false), base64_decode($iv, false)
    );
    $decrypted = json_decode($decrypted, true);

持續更新中~~~~~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章