怎么用队列方式执行公众号主动发消息

jian

尝试队列构造时传入$app对象,或者在队列文件内Factory::officialAccount($config);都会报 Serialization of 'Closure' is not allowed错误,因为有闭包,请问怎么处理?

麻烦贴一下代码

一般的做法是把业务逻辑封装成 Job, 然后将 Job 推入队列

job:

<?php

namespace App\Jobs;

use EasyWeChat\Factory;
use EasyWeChat\Kernel\Messages\Message;
use EasyWeChat\Work\Application;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

class WxFaceMerge implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected
        $wxapp;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($wxapp)
    {
        $this->wxapp = $wxapp;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        \Log::debug($this->wxapp->user->get('on0eVjnYStxkCSaaCamYCpMZDmwA'));
//        \Log::debug($this->message);
    }
}

执行的controller 方法

public function job()
    {
        $config = config('wechat');
        /**
         * 公众号构建
         */
        $app = Factory::officialAccount($config);
        dispatch((new WxFaceMerge($app)));
    }

然后运行controler job方法就报序列化报错

我撤币了,应该放在job的handle里实例化,不应该放在构造里,谢大牛了

不过安老大,在运行队列里怎么指定openid发送消息呢?

呵呵,懂了,我以为这个客服,是那个客服人员的客服!