getuiChajian.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * 个推2023-07-06
  4. */
  5. class getuiChajian extends Chajian{
  6. private $appid = '';
  7. private $appkey = '';
  8. private $mastersecret = '';
  9. private $apptype = '0'; //0所有平台,1仅安卓,2仅苹果
  10. private $pushurl = 'https://restapi.getui.com/v2/{appid}';
  11. protected function initChajian()
  12. {
  13. $this->appid = getconfig('getui_appid');
  14. $this->appkey = getconfig('getui_appkey');
  15. $this->mastersecret = getconfig('getui_mastersecret');
  16. $this->apptype = getconfig('getui_apptype','0');
  17. }
  18. /**
  19. * 获取token
  20. */
  21. public function gettoken(){
  22. $url = str_replace('{appid}',$this->appid, $this->pushurl).'/auth';
  23. $token = c('cache')->get('getui'.$this->appid.'');
  24. if(isempt($token)){
  25. $timestamp = ''.time().'000';
  26. $sign = hash("sha256", $this->appkey.$timestamp.$this->mastersecret);
  27. $result = c('curl')->postcurl($url, json_encode(array(
  28. "sign" => $sign,
  29. "timestamp" => $timestamp,
  30. "appkey" => $this->appkey,
  31. )),0, array(
  32. 'content-type' => 'application/json;charset=utf-8'
  33. ));
  34. if($result){
  35. $barr = json_decode($result, true);
  36. if($barr['code']==0){
  37. $token = $barr['data']['token'];
  38. $expire_time = $barr['data']['expire_time'];
  39. c('cache')->set('getui'.$this->appid.'',$token, $expire_time * 0.001 - time());
  40. }else{
  41. echo $result;
  42. }
  43. }
  44. }
  45. return $token;
  46. }
  47. /**
  48. * 判断是否可以发送
  49. */
  50. public function sendbool()
  51. {
  52. if(!$this->appid || !$this->appkey || !$this->mastersecret)return false;
  53. return true;
  54. }
  55. /**
  56. * 是否安卓的
  57. */
  58. public function isandroid()
  59. {
  60. if(!$this->sendbool())return false;
  61. if($this->apptype=='2')return false;
  62. return true;
  63. }
  64. /**
  65. * 是否安卓的
  66. */
  67. public function isios()
  68. {
  69. if(!$this->sendbool())return false;
  70. if($this->apptype=='1')return false;
  71. return true;
  72. }
  73. /**
  74. * 推送
  75. */
  76. public function push($cid, $title, $cont)
  77. {
  78. if(!$this->sendbool())return 'params empty';
  79. $url = str_replace('{appid}',$this->appid, $this->pushurl).'/push/single/batch/cid';
  80. $token = $this->gettoken();
  81. if(is_string($cid))$cid = explode(',', $cid);
  82. $msg_list = array();
  83. foreach($cid as $_cid){
  84. $parr = array();
  85. $parr['request_id'] = 'a'.time().rand(1000,9999).'';
  86. //$parr['settings'] = array('ttl' => '-1');
  87. $parr['audience']['cid'] = array($_cid);
  88. $parr['push_message']['notification'] = array(
  89. 'title' => $title,
  90. 'body' => $cont,
  91. 'click_type' => 'startapp',
  92. );
  93. //离线厂商推送的
  94. $parr['push_channel']['ios'] = array(
  95. 'type' => 'notify',
  96. 'payload' => 'notify',
  97. 'aps' => array(
  98. 'alert' => array(
  99. 'title' => $title,
  100. 'body' => $cont,
  101. ),
  102. 'sound'=>'default',
  103. ),
  104. 'auto_badge' => '1'
  105. );
  106. $parr['push_channel']['android'] = array(
  107. 'ups' => array(
  108. 'notification' => array(
  109. 'title' => $title,
  110. 'body' => $cont,
  111. 'click_type' => 'startapp',
  112. 'notify_id' => rand(100,99999),
  113. )
  114. )
  115. );
  116. $msg_list[] = $parr;
  117. }
  118. $toboay = array(
  119. 'is_async' => false,
  120. 'msg_list' => $msg_list
  121. );
  122. $result = c('curl')->postcurl($url, json_encode($toboay),0, array(
  123. 'content-type' => 'application/json;charset=utf-8',
  124. 'token' => $token,
  125. ));
  126. return $result;
  127. }
  128. }
粤ICP备19079148号