Push.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. namespace Message\Lib;
  3. use Dever;
  4. Dever::apply('sdk/xg', 'message');
  5. class Push
  6. {
  7. private $prefix = 'dever_';
  8. public function send($id, $uid, $name, $content, $param)
  9. {
  10. $this->config = Dever::db('message/push')->one($id);
  11. if ($this->config) {
  12. # 现在就是腾讯信鸽
  13. if ($this->config['type'] == 1) {
  14. $total = count($uid);
  15. $num = 10;
  16. if ($total <= 0) {
  17. # 发全部
  18. $result = $this->sendAll($name, $content, $param);
  19. } elseif ($total == 1) {
  20. # 发单条
  21. $result = $this->sendOne($uid[0], $name, $content, $param);
  22. } elseif ($total <= $num) {
  23. # 发多条
  24. $result = $this->sendList($uid, $name, $content, $param);
  25. } else {
  26. # 批量发送
  27. $result = $this->sendMul($uid, $name, $content, $param);
  28. }
  29. return $result;
  30. }
  31. }
  32. return false;
  33. }
  34. private function sendAction($source_type, $type, $account, $name, $content, $param)
  35. {
  36. $result = array();
  37. $method = $source_type . '_message';
  38. $message = $this->$method($name, $content, $param);
  39. $push = $this->get($source_type);
  40. if ($type == 1) {
  41. $method = 'PushAllDevices';
  42. $send = array(0, $message);
  43. } elseif ($type == 2) {
  44. $method = 'PushSingleAccount';
  45. $send = array(0, $account, $message);
  46. } elseif ($type == 3) {
  47. $method = 'PushAccountList';
  48. $send = array(0, $account, $message);
  49. }
  50. if ($source_type == 'ios') {
  51. $send[] = \XingeApp::IOSENV_DEV;
  52. }
  53. $result[$source_type]['send'] = $send;
  54. $result[$source_type]['message'] = array($name, $content, $param);
  55. $result[$source_type]['result'] = call_user_func_array(array($push, $method), $send);
  56. $log = Dever::json_encode($result[$source_type]);
  57. Dever::log($log, 'message_app_push');
  58. return $result;
  59. }
  60. private function sendMul($account, $name, $content, $param)
  61. {
  62. return $this->sendList($account, $name, $content, $param);
  63. # 大批量发送
  64. $result = $push->CreateMultipush($mess, \XingeApp::IOSENV_DEV);
  65. if (!($result['ret_code'] === 0)) {
  66. } else {
  67. $page = intval($total/$num)+1;
  68. for ($i = 0; $i <= $page; $i++) {
  69. $array = array();
  70. for($j = 0; $j <= $i+$num; $j++) {
  71. $k = $i + $j;
  72. if (isset($uid[$k])) {
  73. $array[$k] = $prefix . $uid[$k];
  74. }
  75. }
  76. array_push($result, $push->PushAccountListMultiple($ret['result']['push_id'], $array));
  77. }
  78. }
  79. }
  80. private function sendList($account, $name, $content, $param)
  81. {
  82. $result = array();
  83. list($android, $ios) = $this->getUser($account);
  84. if ($android) {
  85. $result += $this->sendAction('android', 1, $android, $name, $content, $param);
  86. }
  87. if ($ios) {
  88. $result += $this->sendAction('ios', 1, $ios, $name, $content, $param);
  89. }
  90. return $result;
  91. }
  92. private function sendOne($account, $name, $content, $param)
  93. {
  94. $result = array();
  95. $info = Dever::db('passport/app')->one(array('uid' => $account));
  96. if (!$info) {
  97. $info = Dever::db('passport/user')->one(array('id' => $account));
  98. }
  99. //$info['source_type'] = 'ios';
  100. if ($info && $info['source_type'] && ($info['source_type'] == 'ios' || $info['source_type'] == 'android')) {
  101. $result = $this->sendAction($info['source_type'], 2, $this->prefix . $account, $name, $content, $param);
  102. } else {
  103. $result = '没有账户信息';
  104. }
  105. return $result;
  106. }
  107. private function sendAll($name, $content, $param)
  108. {
  109. $result = array();
  110. $result += $this->sendAction('android', 1, false, $name, $content, $param);
  111. $result += $this->sendAction('ios', 1, false, $name, $content, $param);
  112. return $result;
  113. }
  114. private function getUser($account)
  115. {
  116. $android = array();
  117. $ios = array();
  118. $info = Dever::db('passport/app')->getAllByUids(array('uid' => $account));
  119. if ($info) {
  120. foreach ($info as $k => $v) {
  121. if ($v['source_type'] == 'android') {
  122. $android[] = $this->prefix . $v['uid'];
  123. } elseif ($v['source_type'] == 'ios') {
  124. $ios[] = $this->prefix . $v['uid'];
  125. }
  126. }
  127. }
  128. return array($android, $ios);
  129. }
  130. private function android_message($name, $content, $param, $type = 1)
  131. {
  132. $message = new \Message();
  133. $message->setExpireTime(86400);
  134. $message->setTitle($name);
  135. $message->setContent($content);
  136. $message->setType($type);
  137. $action = new \ClickAction();
  138. $action->setActionType(\ClickAction::TYPE_ACTIVITY);
  139. $action->setActivity($param);
  140. $message->setAction($action);
  141. return $message;
  142. }
  143. private function ios_message($name, $content, $param, $type = 1)
  144. {
  145. $message = new \MessageIOS();
  146. $message->setExpireTime(86400);
  147. $message->setAlert(array('title' => $name, 'content' => $content));
  148. $message->setBadge(-2);
  149. //$mess->setSound("beep.wav");
  150. $custom = Dever::json_decode($param);
  151. $message->setCustom($custom);
  152. //$message->setRaw($param);
  153. $time = new \TimeInterval(0, 0, 23, 59);
  154. $message->addAcceptTime($time);
  155. return $message;
  156. }
  157. private function get($type = 'android')
  158. {
  159. if (!isset($this->push[$type])) {
  160. $this->push[$type] = new \XingeApp($this->config[$type . '_appid'], $this->config[$type . '_appsecret']);
  161. }
  162. return $this->push[$type];
  163. }
  164. }