PHP code example of azhida / laravel-work_wechat_message

1. Go to this page and download the library: Download azhida/laravel-work_wechat_message library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

azhida / laravel-work_wechat_message example snippets




return [

    // ...

    // 企业微信消息
    'work_wechat_message' => [
        // 会话内容存档配置信息
        'msg_save' => [
            'corp_id' => env('WECHAT_WORK_CORP_ID', ''), // 企业ID
            'secret'   => env('WECHAT_WORK_MSG_SAVE_SECRET', ''), // 

            // server config
            'token' => env('WECHAT_WORK_MSG_SAVE_TOKEN', ''), //
            'aes_key' => env('WECHAT_WORK_MSG_SAVE_AES_KEY', ''), //

            'private_key_file_path' => env('WECHAT_WORK_PRIVATE_KEY_FILE_PATH', ''), // work_msg_save_private_key 私匙文件地址,写 绝对路径
            'media_to_disk' => env('WECHAT_WORK_MSG_SAVE_MEDIA_TO_DISK', 'public'), // 媒体文件是否上传存储的磁盘
        ]
        // ...
    ]

];



return [
    // ...
  
    /*
     * 企业微信
     */
     'work' => [
         // ...

         // 会话内容存档配置信息
         'msg_save' => [
             'corp_id' => env('WECHAT_WORK_CORP_ID', ''), // 企业ID
             'secret'   => env('WECHAT_WORK_MSG_SAVE_SECRET', ''), // 会话内容存档 secret

             // server config
             'token' => env('WECHAT_WORK_MSG_SAVE_TOKEN', ''), // 
             'aes_key' => env('WECHAT_WORK_MSG_SAVE_AES_KEY', ''), //

             'private_key_file_path' => env('WECHAT_WORK_PRIVATE_KEY_FILE_PATH', ''), // work_msg_save_private_key 私匙文件地址,写 绝对路径
             'media_to_disk' => env('WECHAT_WORK_MSG_SAVE_MEDIA_TO_DISK', 'public'), // 媒体文件是否上传存储的磁盘
         ]
     ],  
];

vim app/Services/WorkWeChatMessageSaveService.php



namespace App\Services;

use App\Models\TestWxWorkChatData;
use App\Models\TestWxWorkChatDataPullLog;
use Azhida\LaravelWorkWechatMessage\WorkWechatMessage;

class WorkWeChatMessageSaveService extends WorkWechatMessage
{
    /**
     * 企业微信会话内容存档服务 [说明:目前仅支持 linux]
     */

    public function __construct()
    {
        parent::__construct();
    }

    protected function handleOnePullLog(array $chats, int $min_seq, int $max_seq, int $count)
    {
        // 一次拉取,写一条拉取日志
        TestWxWorkChatDataPullLog::query()->create([
            'min_seq' => $min_seq,
            'max_seq' => $max_seq,
            'count' => $count,
            'res' => $chats,
        ]);
    }

    protected function handleOneMessage(array $item = [])
    {
        // 逐条解密消息并入库
        TestWxWorkChatData::query()->create([
            'seq' => $item['seq'], // 消息的seq值,标识消息的序号
            'msgid' => $item['msgid'], // 消息id,消息的唯一标识,企业可以使用此字段进行消息去重。
            'msg' => $item['msg'], // 明文消息,json格式
            'data' => $item, // 加密数据,json格式,即chatdata里的每一项数据
        ]);
    }

}