bianjunhui há 1 ano atrás
pai
commit
1c7a50ab80
1 ficheiros alterados com 108 adições e 0 exclusões
  1. 108 0
      application/api/controller/Send.php

+ 108 - 0
application/api/controller/Send.php

@@ -0,0 +1,108 @@
+<?php
+namespace app\api\controller;
+use app\common\controller\Api;
+use think\Db;
+use DateTime;
+use think\Request;
+
+class Send extends Api
+{
+
+    protected $noNeedLogin = ['*'];
+    protected $noNeedRight = ['*'];
+
+    public function _initialize()
+    {
+        parent::_initialize();
+        $url = $_SERVER['HTTP_HOST'];
+        if (strpos($url, 'westkiss') !== false) {
+            $this->oneSignalAppId='6bbd561f-4d8e-4d04-a6c4-dbef1bf99694';
+            $this->oneSignalRestApiKey='MDMwZThjYWMtOWQ4ZC00YzU3LWIwNjktOGYzNTA2NjA3NTBh';
+        }else{
+            $this->oneSignalAppId='b3124d44-7bc7-4965-95dc-0ecb502fdaea';
+            $this->oneSignalRestApiKey='NzRhMTYyODUtYzczYi00Yjg5LWI3NzktODFmMmY0MGUyODIx';
+        }
+    }
+    public function test()
+    {
+        $data =input();
+        $id=$data['ids'];
+        $wheres['id']=$id;
+        $one_push = Db::name('message')->where($wheres)->find();
+        if (empty($one_push)) {
+            echo '没有查到推送队列';
+            exit;
+        }
+        $result =$this->sendTest($one_push);
+    }
+    public function sendTest($one_push){
+        $template_dsc =$one_push['template_dsc'];
+        $title =$one_push['template_name'];
+        $type =$one_push['type'];
+        $url =$one_push['para'];
+        $data['push_para']=$url;
+        $data['push_type']=$type;
+        $data['title']=$title;
+        $data['message_id']=$one_push['id'];
+        $apns_alert['title']=$title;
+        $images['id'] = 'https://message.romandhair.com/uploads/20240307/9d98d28158a102ec6d5ba9caaab05f5b.jpg';
+        $re=array();
+        for($i=0;$i<10;$i++){
+            $dt = new DateTime();
+            $minute =10*($i+1);
+            $dt->modify("+$minute minute");
+            $fields = [
+                'app_id' =>  $this->oneSignalAppId,
+                //'included_segments' => ['All'], // 使用 'included_segments' 或 'include_player_ids'
+                //'include_player_ids' => ['player_id1', 'player_id2'], // 如果你要推送给指定的用户
+                'filters' => [ // 可选,基于特定条件过滤用户
+                    ['field' => 'tag', 'key' => 'SufixUid', 'relation' => '=', 'value' => $i]
+                ],
+                'contents' => ['en' => $template_dsc], // 消息内容
+                'data' =>$data, // 可选,额外的自定义数据字段
+                'content_available'=>true,
+                'is_ios'=>true,
+                'ios_badge_type'=>'Increase',
+                'apns_alert'=>$apns_alert,
+                'ios_attachments'=> $images,
+                'send_after'=>$dt,
+            ];
+            if($one_push['template_image']){
+                $request = Request::instance();
+                $domain=$request->domain();
+                $images['id'] = $domain.$one_push['template_image'];
+                //$images['id'] = 'https://message.romandhair.com/uploads/20240307/9d98d28158a102ec6d5ba9caaab05f5b.jpg';
+                $fields['ios_attachments']=$images;
+            }
+            $re[] = $this ->sendMessage($fields);
+        }
+        return $re;
+    }
+
+    public function sendMessage($fields){
+        $headers = [
+            'Content-Type: application/json; charset=utf-8',
+            'Authorization: Basic ' . $this->oneSignalRestApiKey
+        ];
+        $ch = curl_init();
+        curl_setopt($ch, CURLOPT_URL, 'https://onesignal.com/api/v1/notifications');
+        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
+        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
+        curl_setopt($ch, CURLOPT_HEADER, FALSE);
+        curl_setopt($ch, CURLOPT_POST, TRUE);
+        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
+        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
+        
+        $response = curl_exec($ch);
+        if ($response === FALSE) {
+            die('Curl failed: ' . curl_error($ch));
+        }
+        curl_close($ch);
+        $responseData = json_decode($response, TRUE);
+        print_r($responseData);
+        return $responseData;
+    }
+
+
+
+}