IGt.Batch.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 15-4-9
  6. * Time: 下午3:45
  7. */
  8. header("Content-Type: text/html; charset=utf-8");
  9. require_once(dirname(__FILE__) . '/' . 'IGt.Push.php');
  10. class IGtBatch
  11. {
  12. var $batchId;
  13. var $innerMsgList = array();
  14. var $seqId = 0;
  15. var $APPKEY;
  16. var $push;
  17. var $lastPostData;
  18. public function __construct($appkey, $push)
  19. {
  20. $this->APPKEY = $appkey;
  21. $this->push = $push;
  22. $this->batchId = uniqid();
  23. }
  24. public function getBatchId()
  25. {
  26. return $this->batchId;
  27. }
  28. public function add($message, $target)
  29. {
  30. if ($this->seqId >= 5000) {
  31. throw new Exception("Can not add over 5000 message once! Please call submit() first.");
  32. } else {
  33. $this->seqId += 1;
  34. $innerMsg = new SingleBatchItem();
  35. $innerMsg->set_seqId($this->seqId);
  36. $innerMsg->set_data($this->createSingleJson($message, $target));
  37. array_push($this->innerMsgList, $innerMsg);
  38. }
  39. return $this->seqId . "";
  40. }
  41. public function createSingleJson($message, $target)
  42. {
  43. $params = $this->push->getSingleMessagePostData($message,$target);
  44. return json_encode($params);
  45. }
  46. public function submit()
  47. {
  48. $requestId = LangUtils::randomUUID();
  49. $data = array();
  50. $data["appkey"]=$this->APPKEY;
  51. $data["serialize"] = "pb";
  52. $data["async"] = GTConfig::isPushSingleBatchAsync();
  53. $data["action"] = "pushMessageToSingleBatchAction";
  54. $data['requestId'] = $requestId;
  55. $singleBatchRequest = new SingleBatchRequest();
  56. $singleBatchRequest->set_batchId($this->batchId);
  57. foreach ($this->innerMsgList as $index => $innerMsg) {
  58. $singleBatchRequest->add_batchItem();
  59. $singleBatchRequest->set_batchItem($index, $innerMsg);
  60. }
  61. $data["singleDatas"] = base64_encode($singleBatchRequest->SerializeToString());
  62. $this->seqId = 0;
  63. $this->innerMsgList = array();
  64. $this->lastPostData = $data;
  65. $result = $this->push->httpPostJSON(null, $data, true);
  66. return $result;
  67. }
  68. public function retry()
  69. {
  70. $result = $this->push->httpPostJSON(null, $this->lastPostData, true);
  71. return $result;
  72. }
  73. public function setApiUrl($apiUrl) {
  74. }
  75. }