lizhen_gitee 1 rok temu
rodzic
commit
eeea3a643f

+ 74 - 0
app/Common/Library/Easemob.php

@@ -0,0 +1,74 @@
+<?php
+namespace App\Common\Library;
+
+use Easemob\Auth;
+use Easemob\Push;
+
+class Easemob {
+
+    protected $auth;
+
+
+    public function __construct() {
+
+        //环信推送 正式
+        $easemob_config =  [
+            'appkey'        => '1137221110163975#tkenim',
+            'client_id'     => 'YXA6DEehGBpFTb-UOcgmWtveZQ',
+            'client_secret' => 'YXA6KhfJfqyJYBn2I20DdgTEzWpx5Cs',
+        ];
+
+        //初始化配置
+//        $easemob_config               = config('easemob');
+        $appKey                       = $easemob_config['appkey'];
+        $clientIdOrAppID              = $easemob_config['client_id'];
+        $clientSecretOrAppCertificate = $easemob_config['client_secret'];
+
+        $this->auth = new Auth($appKey,$clientIdOrAppID,$clientSecretOrAppCertificate);
+    }
+
+    //////////////////消息推送//////////////////////////
+    public function push_text($uid,$title,$msg){
+        $message = new Push($this->auth);
+
+        $pushMessage =
+        [
+            'title'=> $title,
+//            'subTitle'=> '',
+            'content'=> $msg,
+            /*'ext'=> [],
+            'config'=> [
+                'clickAction'=> [
+                    'url'=>'',
+                    'action'=>'',
+                    'activity'=>'',
+                ],
+                'badge'=> [
+                    'addNum'=> 0,
+                    'setNum'=> 0,
+                ],
+            ],
+
+            'easemob'=>[],
+            'apns'=> [],
+            'fcm'=> [],
+            'fcmV1'=>[],
+            'huawei'=> [],
+            'meizu'=> [],
+            'oppo'=> [],
+            'vivo'=> [],
+            'xiaomi'=> [],
+            'honor'=>[]*/
+        ];
+
+        $rs = $message->push_sync($uid,$pushMessage);
+        return $rs;
+        //dump($rs);
+    }
+
+
+
+
+
+
+}

+ 65 - 0
app/Console/Commands/Plantask.php

@@ -0,0 +1,65 @@
+<?php
+
+namespace App\Console\Commands;
+
+use Illuminate\Console\Command;
+
+use App\Common\Library\Easemob;
+use Illuminate\Support\Facades\DB;
+use App\Jobs\EasemobJob;
+
+class Plantask extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'command:Plantask';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = 'Command description';
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        parent::__construct();
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return int
+     */
+    public function handle()
+    {
+        $list = DB::table('mt_message')->where('dispatch_status',0)->orderBy('id','asc')->get()->toArray();
+        //dd($list);
+        //
+        if(!empty($list)){
+            foreach($list as $key => $val){
+                $msg_id = $val->id;
+                dispatch((new EasemobJob($msg_id))->delay(0));//加入队列
+            }
+        }
+
+        //更新
+        $ids = array_column($list,'id');
+
+        $update = [
+            'dispatch_status' => 1,
+        ];
+
+        DB::table('mt_message')->whereIn('id',$ids)->update($update);
+
+        return 0;
+    }
+}

+ 1 - 0
app/Console/Kernel.php

@@ -16,6 +16,7 @@ class Kernel extends ConsoleKernel
     protected function schedule(Schedule $schedule)
     {
         // $schedule->command('inspire')->hourly();
+        $schedule->command('command:Plantask')->everyMinute();// 每分钟定时处理
     }
 
     /**

+ 50 - 0
app/Jobs/EasemobJob.php

@@ -0,0 +1,50 @@
+<?php
+
+namespace App\Jobs;
+
+use Illuminate\Bus\Queueable;
+use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Foundation\Bus\Dispatchable;
+use Illuminate\Queue\InteractsWithQueue;
+use Illuminate\Queue\SerializesModels;
+
+use Illuminate\Support\Facades\DB;
+use App\Common\Library\Easemob;
+use Illuminate\Support\Facades\Log;
+
+class EasemobJob implements ShouldQueue
+{
+    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
+
+    protected $msgid;
+
+    /**
+     * Create a new job instance.
+     *
+     * @return void
+     */
+    public function __construct($msgid)
+    {
+        //
+        $this->msgid = $msgid;
+        Log::info(date('Y-m-d H:i:d').':'.$msgid);
+
+    }
+
+    /**
+     * Execute the job.
+     *
+     * @return void
+     */
+    public function handle()
+    {
+        //
+        $info = DB::table('mt_message')->where('id',$this->msgid)->first();
+
+        $easemob = new Easemob();
+        $rs = $easemob->push_text($info->user_id,$info->title,$info->content);
+        /*echo '<pre>';
+        var_dump($rs);*/
+
+    }
+}

+ 22 - 0
vendor/maniac/easemob-php/src/Push.php

@@ -25,6 +25,28 @@ final class Push
     }
     /// @endcond
 
+    //以同步方式发送推送通知
+    //https://docs-im-beta.easemob.com/push/push_send_notification.html
+    public function push_sync($uid, $pushMessage)
+    {
+
+        $uri = $this->auth->getBaseUri() . '/push/sync/' . $uid;
+
+        $strategy = 0;
+        $body = compact('strategy','pushMessage');
+
+        $header = $this->auth->headers();
+        $header['Content-Type'] =  'application/json';
+
+        $resp = Http::post($uri, $body, $header);
+        //dd($resp);
+        if (!$resp->ok()) {
+            return \Easemob\error($resp);
+        }
+        $data = $resp->data();
+        return $data['data'];
+        //return $data['data']['successKeys'];
+    }
     /**
      * \~chinese
      * \brief