dever 5 years ago
parent
commit
68c297a0b8
6 changed files with 239 additions and 91 deletions
  1. 26 6
      database/push.php
  2. 20 81
      lib/Data.php
  3. 3 1
      lib/Manage.php
  4. 187 0
      lib/Push.php
  5. 0 3
      sdk/sg.php
  6. 3 0
      sdk/xg.php

+ 26 - 6
database/push.php

@@ -51,22 +51,42 @@ return array
 			'list'		=> true,
 		),
 
-		'appid'		=> array
+		'android_appid'		=> array
 		(
 			'type' 		=> 'varchar-100',
-			'name' 		=> '推送appid',
+			'name' 		=> '安卓推送appid',
 			'default' 	=> '',
-			'desc' 		=> '推送appid',
+			'desc' 		=> '安卓推送appid',
 			'match' 	=> 'is_string',
 			'update'	=> 'textarea',
 		),
 
-		'appsecret'		=> array
+		'android_appsecret'		=> array
 		(
 			'type' 		=> 'varchar-200',
-			'name' 		=> '推送appsecret',
+			'name' 		=> '安卓推送appsecret',
 			'default' 	=> '',
-			'desc' 		=> '推送appsecret',
+			'desc' 		=> '安卓推送appsecret',
+			'match' 	=> 'is_string',
+			'update'	=> 'textarea',
+		),
+
+		'ios_appid'		=> array
+		(
+			'type' 		=> 'varchar-100',
+			'name' 		=> '苹果推送appid',
+			'default' 	=> '',
+			'desc' 		=> '苹果推送appid',
+			'match' 	=> 'is_string',
+			'update'	=> 'textarea',
+		),
+
+		'ios_appsecret'		=> array
+		(
+			'type' 		=> 'varchar-200',
+			'name' 		=> '苹果推送appsecret',
+			'default' 	=> '',
+			'desc' 		=> '苹果推送appsecret',
 			'match' 	=> 'is_string',
 			'update'	=> 'textarea',
 		),

+ 20 - 81
lib/Data.php

@@ -31,9 +31,9 @@ class Data
 
 		$where = ' and a.state = 1 and a.project_id = ' . $project;
         # 去掉a.type判断
-        $where .= ' and a.scope == 2';
+        $where .= ' and a.scope = 2';
         # 这个是兼容历史版本
-        //$where .= ' (and a.type <= 10 || a.scope == 2)';
+        //$where .= ' (and a.type <= 10 || a.scope = 2)';
 		# 读取outbox里的数据
 		$sql = 'select a.name,a.content,a.id,a.type,a.uid,a.project_id,a.scope from '.$outbox.' as a where not exists(select oid from '.$inbox.' where a.id = oid and uid = '.$uid.')' . $where . ' ';
 
@@ -108,6 +108,22 @@ class Data
 		Dever::alert('错误的消息信息');
 	}
 
+    public function test_api()
+    {
+        $type = Dever::input('type', 1);
+        $id = Dever::input('id', 1);
+        $push = array('type' => $type, 'id' => $id);
+
+        $push = json_encode($push);
+        $uid = -1;
+        $to_uid = 2;
+        $name = Dever::input('name', 'test');
+        $content = Dever::input('content', 'test');
+        $result = $this->push($uid, $to_uid, $name, $content, 11, 1, 1, $push);
+
+        return 'ok';
+    }
+
 	/**
      * 推送消息
      * 
@@ -183,87 +199,10 @@ class Data
                         # 发送全部
                         $to_uid = array();
                     }
-                    $this->push($config['push'], $to_uid, $name, $content, $push);
+                    Dever::load('message/lib/push')->send($config['push'], $to_uid, $name, $content, $push);
+                    //Dever::daemon('lib/push.send?', 'message');
                 }
             }
         }
 	}
-
-    private function push($id, $uid, $name, $content, $push)
-    {
-        $config = Dever::db('message/push')->one($id);
-        if ($config) {
-            # 现在就是腾讯信鸽
-            if ($config['type'] == 1) {
-                Dever::apply('sdk/sg', 'message');
-                $push = new \XingeApp($config['appid'], $config['appsecret']);
-                $mess = new \Message();
-                $mess->setExpireTime(86400);
-                $mess->setTitle($name);
-                $mess->setContent($content);
-                //$mess->setType(\Message::TYPE_MESSAGE);
-                $mess->setType(\Message::TYPE_NOTIFICATION);
-                $action = new \ClickAction();
-                $action->setActionType(\ClickAction::TYPE_ACTIVITY);
-                $action->setActivity($push);
-                $mess->setAction($action);
-
-                $total = count($uid);
-                $prefix = 'dever_';
-                $num = 10;
-                if ($total <= 0) {
-                    # 发全部
-                    $ret = $push->PushAllDevices(0, $mess);
-                } elseif ($total == 1) {
-                    # 发单条
-                    $ret = $push->PushSingleAccount(0, $prefix . $uid, $mess);
-                } elseif ($total <= $num) {
-                    # 发列表
-                    $accountList = array_walk(
-                        $uid, 
-                        function(&$value, $key, $prefix){$value = $prefix.$value;}, 
-                        $prefix
-                    );
-                    $ret = $push->PushAccountList(0, $accountList, $mess);
-                } else {
-                    # 大批量发送
-                    $ret = $push->CreateMultipush($mess, \XingeApp::IOSENV_DEV);
-                    if (!($ret['ret_code'] === 0)) {
-                        
-                    } else {
-                        $page = intval($total/$num)+1;
-                        for ($i = 0; $i <= $page; $i++) {
-                            $array = array();
-                            for($j = 0; $j <= $i+$num; $j++) {
-                                $k = $i + $j;
-                                if (isset($uid[$k])) {
-                                    $array[$k] = $prefix . $uid[$k];
-                                }
-                            }
-                            array_push($ret, $push->PushAccountListMultiple($ret['result']['push_id'], $array));
-                        }
-                    }
-                }
-                
-                return ($ret);
-            }
-        }
-    }
-
-    function DemoPushSingleAccountIOS()
-    {
-        $push = new XingeApp(000, 'secret_key');
-        $mess = new MessageIOS();
-        $mess->setExpireTime(86400);
-        $mess->setAlert("ios test");
-        //$mess->setAlert(array('key1'=>'value1'));
-        $mess->setBadge(1);
-        $mess->setSound("beep.wav");
-        $custom = array('key1'=>'value1', 'key2'=>'value2');
-        $mess->setCustom($custom);
-        $acceptTime1 = new TimeInterval(0, 0, 23, 59);
-        $mess->addAcceptTime($acceptTime1);
-        $ret = $push->PushSingleAccount(0, 'joelliu', $mess, XingeApp::IOSENV_DEV);
-        return $ret;
-    }
 }

+ 3 - 1
lib/Manage.php

@@ -17,6 +17,8 @@ class Manage
     	$type = Dever::param('type', $param);
     	$scope = Dever::param('scope', $param);
 
-    	Dever::load('message/lib/data')->push($uid, $to_uid, $name, $content, $type, $project_id, $scope, $id);
+    	if ($to_uid && $name && $content) {
+    		Dever::load('message/lib/data')->push($uid, $to_uid, $name, $content, $type, $project_id, $scope, $id);
+    	}
     }
 }

+ 187 - 0
lib/Push.php

@@ -0,0 +1,187 @@
+<?php
+
+namespace Message\Lib;
+
+use Dever;
+Dever::apply('sdk/xg', 'message');
+
+class Push
+{
+	private $prefix = 'dever_';
+	public function send($id, $uid, $name, $content, $param)
+    {
+        $this->config = Dever::db('message/push')->one($id);
+        if ($this->config) {
+            # 现在就是腾讯信鸽
+            if ($this->config['type'] == 1) {
+                $total = count($uid);
+                $num = 10;
+
+                if ($total <= 0) {
+                    # 发全部
+                    $result = $this->sendAll($name, $content, $param);
+                } elseif ($total == 1) {
+                    # 发单条
+                    $result = $this->sendOne($uid[0], $name, $content, $param);
+                } elseif ($total <= $num) {
+                	# 发多条
+                    $result = $this->sendList($uid, $name, $content, $param);
+                } else {
+                	# 批量发送
+                	$result = $this->sendMul($uid, $name, $content, $param);
+                }
+                return $result;
+            }
+        }
+
+        return false;
+    }
+
+    private function sendAction($source_type, $type, $account, $name, $content, $param)
+    {
+    	$result = array();
+    	$method = $source_type . '_message';
+		$message = $this->$method($name, $content, $param);
+
+		$push = $this->get($source_type);
+		if ($type == 1) {
+			$method = 'PushAllDevices';
+			$send = array(0, $message);
+		} elseif ($type == 2) {
+			$method = 'PushSingleAccount';
+			$send = array(0, $account, $message);
+		} elseif ($type == 3) {
+			$method = 'PushAccountList';
+			$send = array(0, $account, $message);
+		}
+
+		if ($source_type == 'ios') {
+			$send[] = \XingeApp::IOSENV_DEV;
+		}
+
+		$result[$source_type] = call_user_func_array(array($push, $method), $send);
+
+		$log['result'] = $result;
+		$log['send'] = $send;
+		$log['message'] = array($name, $content, $param);
+		$log = Dever::json_encode($log);
+		Dever::log($log, 'message_app_push');
+
+		return $result;
+    }
+
+    private function sendMul($account, $name, $content, $param)
+    {
+    	return $this->sendList($account, $name, $content, $param);
+    	# 大批量发送
+        $result = $push->CreateMultipush($mess, \XingeApp::IOSENV_DEV);
+        if (!($result['ret_code'] === 0)) {
+            
+        } else {
+            $page = intval($total/$num)+1;
+            for ($i = 0; $i <= $page; $i++) {
+                $array = array();
+                for($j = 0; $j <= $i+$num; $j++) {
+                    $k = $i + $j;
+                    if (isset($uid[$k])) {
+                        $array[$k] = $prefix . $uid[$k];
+                    }
+                }
+                array_push($result, $push->PushAccountListMultiple($ret['result']['push_id'], $array));
+            }
+        }
+    }
+
+    private function sendList($account, $name, $content, $param)
+   	{
+   		$result = array();
+   		list($android, $ios) = $this->getUser($account);
+   		
+   		if ($android) {
+   			$result += $this->sendAction('android', 1, $android, $name, $content, $param);
+   		}
+
+   		if ($ios) {
+   			$result += $this->sendAction('ios', 1, $ios, $name, $content, $param);
+   		}
+
+   		return $result;
+   	}
+
+    private function sendOne($account, $name, $content, $param)
+   	{
+   		$result = array();
+   		$info = Dever::db('passport/app')->one(array('uid' => $account));
+   		$info['source_type'] = 'ios';
+   		if ($info && $info['source_type']) {
+   			$result = $this->sendAction($info['source_type'], 2, $this->prefix . $account, $name, $content, $param);
+   		}
+
+   		return $result;
+   	}
+
+    private function sendAll($name, $content, $param)
+   	{
+   		$result = $this->sendAction('android', 1, false, $name, $content, $param);
+   		$result += $this->sendAction('ios', 1, false, $name, $content, $param);
+
+   		return $result;
+   	}
+
+   	private function getUser($account)
+   	{
+   		$android = array();
+   		$ios = array();
+   		$info = Dever::db('passport/app')->getAllByUids(array('uid' => $account));
+   		if ($info) {
+   			foreach ($info as $k => $v) {
+   				if ($v['source_type'] == 'android') {
+   					$android[] = $this->prefix . $v['uid'];
+   				} elseif ($v['source_type'] == 'ios') {
+   					$ios[] = $this->prefix . $v['uid'];
+   				}
+   			}
+   		}
+
+   		return array($android, $ios);
+   	}
+
+    private function android_message($name, $content, $param, $type = 1)
+    {
+    	$message = new \Message();
+        $message->setExpireTime(86400);
+        $message->setTitle($name);
+        $message->setContent($content);
+        $message->setType($type);
+        $action = new \ClickAction();
+        $action->setActionType(\ClickAction::TYPE_ACTIVITY);
+        $action->setActivity($param);
+        $message->setAction($action);
+
+        return $message;
+    }
+
+    private function ios_message($name, $content, $param, $type = 1)
+    {
+    	$message = new \MessageIOS();
+		$message->setExpireTime(86400);
+		$message->setAlert(array('title' => $name, 'content' => $content));
+		$message->setBadge(-2);
+		//$mess->setSound("beep.wav");
+		$custom = json_decode($param);
+		$message->setCustom($custom);
+		$message->setRaw($param);
+		$time = new \TimeInterval(0, 0, 23, 59);
+		$message->addAcceptTime($time);
+
+        return $message;
+    }
+
+   	private function get($type = 'android')
+   	{
+   		if (!isset($this->push[$type])) {
+   			$this->push[$type] = new \XingeApp($this->config[$type . '_appid'], $this->config[$type . '_appsecret']);
+   		}
+   		return $this->push[$type];
+   	}
+}

+ 0 - 3
sdk/sg.php

@@ -1,3 +0,0 @@
-<?php
-
-Dever::apply('sdk/sg/XingeApp', 'message');

+ 3 - 0
sdk/xg.php

@@ -0,0 +1,3 @@
+<?php
+
+Dever::apply('sdk/xg/XingeApp', 'message');