XingeApp.php 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677
  1. <?php
  2. /*
  3. * Copyright ? 1998 - 2014 Tencent. All Rights Reserved. 腾讯公司 版权所有
  4. */
  5. class XingeApp
  6. {
  7. const DEVICE_ALL = 0;
  8. const DEVICE_BROWSER = 1;
  9. const DEVICE_PC = 2;
  10. const DEVICE_ANDROID = 3;
  11. const DEVICE_IOS = 4;
  12. const DEVICE_WINPHONE = 5;
  13. const IOSENV_PROD = 1;
  14. const IOSENV_DEV = 2;
  15. const IOS_MIN_ID = 2200000000;
  16. public function __construct($accessId, $secretKey)
  17. {
  18. assert(isset($accessId) && isset($secretKey));
  19. $this->accessId = $accessId;
  20. $this->secretKey = $secretKey;
  21. }
  22. public function __destruct()
  23. {
  24. }
  25. /**
  26. * 使用默认设置推送消息给单个android设备
  27. */
  28. public static function PushTokenAndroid($accessId, $secretKey, $title, $content, $token)
  29. {
  30. $push = new XingeApp($accessId, $secretKey);
  31. $mess = new Message();
  32. $mess->setTitle($title);
  33. $mess->setContent($content);
  34. $mess->setType(Message::TYPE_NOTIFICATION);
  35. $mess->setStyle(new Style(0, 1, 1, 1, 0));
  36. $action = new ClickAction();
  37. $action->setActionType(ClickAction::TYPE_ACTIVITY);
  38. $mess->setAction($action);
  39. $ret = $push->PushSingleDevice($token, $mess);
  40. return $ret;
  41. }
  42. /**
  43. * 使用默认设置推送消息给单个ios设备
  44. */
  45. public static function PushTokenIos($accessId, $secretKey, $content, $token, $environment)
  46. {
  47. $push = new XingeApp($accessId, $secretKey);
  48. $mess = new MessageIOS();
  49. $mess->setAlert($content);
  50. $ret = $push->PushSingleDevice($token, $mess, $environment);
  51. return $ret;
  52. }
  53. /**
  54. * 使用默认设置推送消息给单个android版账户
  55. */
  56. public static function PushAccountAndroid($accessId, $secretKey, $title, $content, $account)
  57. {
  58. $push = new XingeApp($accessId, $secretKey);
  59. $mess = new Message();
  60. $mess->setTitle($title);
  61. $mess->setContent($content);
  62. $mess->setType(Message::TYPE_NOTIFICATION);
  63. $mess->setStyle(new Style(0, 1, 1, 1, 0));
  64. $action = new ClickAction();
  65. $action->setActionType(ClickAction::TYPE_ACTIVITY);
  66. $mess->setAction($action);
  67. $ret = $push->PushSingleAccount(0, $account, $mess);
  68. return $ret;
  69. }
  70. /**
  71. * 使用默认设置推送消息给单个ios版账户
  72. */
  73. public static function PushAccountIos($accessId, $secretKey, $content, $account, $environment)
  74. {
  75. $push = new XingeApp($accessId, $secretKey);
  76. $mess = new MessageIOS();
  77. $mess->setAlert($content);
  78. $ret = $push->PushSingleAccount(0, $account, $mess, $environment);
  79. return $ret;
  80. }
  81. /**
  82. * 使用默认设置推送消息给所有设备android版
  83. */
  84. public static function PushAllAndroid($accessId, $secretKey, $title, $content)
  85. {
  86. $push = new XingeApp($accessId, $secretKey);
  87. $mess = new Message();
  88. $mess->setTitle($title);
  89. $mess->setContent($content);
  90. $mess->setType(Message::TYPE_NOTIFICATION);
  91. $mess->setStyle(new Style(0, 1, 1, 1, 0));
  92. $action = new ClickAction();
  93. $action->setActionType(ClickAction::TYPE_ACTIVITY);
  94. $mess->setAction($action);
  95. $ret = $push->PushAllDevices(0, $mess);
  96. return $ret;
  97. }
  98. /**
  99. * 使用默认设置推送消息给所有设备ios版
  100. */
  101. public static function PushAllIos($accessId, $secretKey, $content, $environment)
  102. {
  103. $push = new XingeApp($accessId, $secretKey);
  104. $mess = new MessageIOS();
  105. $mess->setAlert($content);
  106. $ret = $push->PushAllDevices(0, $mess, $environment);
  107. return $ret;
  108. }
  109. /**
  110. * 使用默认设置推送消息给标签选中设备android版
  111. */
  112. public static function PushTagAndroid($accessId, $secretKey, $title, $content, $tag)
  113. {
  114. $push = new XingeApp($accessId, $secretKey);
  115. $mess = new Message();
  116. $mess->setTitle($title);
  117. $mess->setContent($content);
  118. $mess->setType(Message::TYPE_NOTIFICATION);
  119. $mess->setStyle(new Style(0, 1, 1, 1, 0));
  120. $action = new ClickAction();
  121. $action->setActionType(ClickAction::TYPE_ACTIVITY);
  122. $mess->setAction($action);
  123. $ret = $push->PushTags(0, array(0 => $tag), 'OR', $mess);
  124. return $ret;
  125. }
  126. /**
  127. * 使用默认设置推送消息给标签选中设备ios版
  128. */
  129. public static function PushTagIos($accessId, $secretKey, $content, $tag, $environment)
  130. {
  131. $push = new XingeApp($accessId, $secretKey);
  132. $mess = new MessageIOS();
  133. $mess->setAlert($content);
  134. $ret = $push->PushTags(0, array(0 => $tag), 'OR', $mess, $environment);
  135. return $ret;
  136. }
  137. /**
  138. * 推送消息给单个设备
  139. */
  140. public function PushSingleDevice($deviceToken, $message, $environment = 0)
  141. {
  142. $ret = array('ret_code' => -1, 'err_msg' => 'message not valid');
  143. if (!($message instanceof Message) && !($message instanceof MessageIOS)) return $ret;
  144. if (!$this->ValidateMessageType($message)) {
  145. $ret['err_msg'] = 'message type not fit accessId';
  146. return $ret;
  147. }
  148. if ($message instanceof MessageIOS) {
  149. if ($environment != XingeApp::IOSENV_DEV && $environment != XingeApp::IOSENV_PROD) {
  150. $ret['err_msg'] = "ios message environment invalid";
  151. return $ret;
  152. }
  153. }
  154. if (!$message->isValid()) return $ret;
  155. $params = array();
  156. $params['access_id'] = $this->accessId;
  157. $params['expire_time'] = $message->getExpireTime();
  158. $params['send_time'] = $message->getSendTime();
  159. if ($message instanceof Message) $params['multi_pkg'] = $message->getMultiPkg();
  160. $params['device_token'] = $deviceToken;
  161. $params['message_type'] = $message->getType();
  162. $params['message'] = $message->toJson();
  163. $params['timestamp'] = time();
  164. $params['environment'] = $environment;
  165. return $this->callRestful(self::RESTAPI_PUSHSINGLEDEVICE, $params);
  166. }
  167. /**
  168. * 推送消息给单个账户
  169. */
  170. public function PushSingleAccount($deviceType, $account, $message, $environment = 0)
  171. {
  172. $ret = array('ret_code' => -1);
  173. if (!is_int($deviceType) || $deviceType < 0 || $deviceType > 5) {
  174. $ret['err_msg'] = 'deviceType not valid';
  175. return $ret;
  176. }
  177. if (!is_string($account) || empty($account)) {
  178. $ret['err_msg'] = 'account not valid';
  179. return $ret;
  180. }
  181. if (!($message instanceof Message) && !($message instanceof MessageIOS)) {
  182. $ret['err_msg'] = 'message is not android or ios';
  183. return $ret;
  184. }
  185. if (!$this->ValidateMessageType($message)) {
  186. $ret['err_msg'] = 'message type not fit accessId';
  187. return $ret;
  188. }
  189. if ($message instanceof MessageIOS) {
  190. if ($environment != XingeApp::IOSENV_DEV && $environment != XingeApp::IOSENV_PROD) {
  191. $ret['err_msg'] = "ios message environment invalid";
  192. return $ret;
  193. }
  194. }
  195. if (!$message->isValid()) {
  196. $ret['err_msg'] = 'message not valid';
  197. return $ret;
  198. }
  199. $params = array();
  200. $params['access_id'] = $this->accessId;
  201. $params['expire_time'] = $message->getExpireTime();
  202. $params['send_time'] = $message->getSendTime();
  203. if ($message instanceof Message)
  204. $params['multi_pkg'] = $message->getMultiPkg();
  205. $params['device_type'] = $deviceType;
  206. $params['account'] = $account;
  207. $params['message_type'] = $message->getType();
  208. $params['message'] = $message->toJson();
  209. $params['timestamp'] = time();
  210. $params['environment'] = $environment;
  211. return $this->callRestful(self::RESTAPI_PUSHSINGLEACCOUNT, $params);
  212. }
  213. /**
  214. * 推送消息给多个账户
  215. */
  216. public function PushAccountList($deviceType, $accountList, $message, $environment = 0)
  217. {
  218. $ret = array('ret_code' => -1);
  219. if (!is_int($deviceType) || $deviceType < 0 || $deviceType > 5) {
  220. $ret['err_msg'] = 'deviceType not valid';
  221. return $ret;
  222. }
  223. if (!is_array($accountList) || empty($accountList)) {
  224. $ret['err_msg'] = 'accountList not valid';
  225. return $ret;
  226. }
  227. if (!($message instanceof Message) && !($message instanceof MessageIOS)) {
  228. $ret['err_msg'] = 'message is not android or ios';
  229. return $ret;
  230. }
  231. if (!$this->ValidateMessageType($message)) {
  232. $ret['err_msg'] = 'message type not fit accessId';
  233. return $ret;
  234. }
  235. if ($message instanceof MessageIOS) {
  236. if ($environment != XingeApp::IOSENV_DEV && $environment != XingeApp::IOSENV_PROD) {
  237. $ret['err_msg'] = "ios message environment invalid";
  238. return $ret;
  239. }
  240. }
  241. if (!$message->isValid()) {
  242. $ret['err_msg'] = 'message not valid';
  243. return $ret;
  244. }
  245. $params = array();
  246. $params['access_id'] = $this->accessId;
  247. $params['expire_time'] = $message->getExpireTime();
  248. if ($message instanceof Message)
  249. $params['multi_pkg'] = $message->getMultiPkg();
  250. $params['device_type'] = $deviceType;
  251. $params['account_list'] = json_encode($accountList);
  252. $params['message_type'] = $message->getType();
  253. $params['message'] = $message->toJson();
  254. $params['timestamp'] = time();
  255. $params['environment'] = $environment;
  256. return $this->callRestful(self::RESTAPI_PUSHACCOUNTLIST, $params);
  257. }
  258. /**
  259. * 推送消息给APP所有设备
  260. */
  261. public function PushAllDevices($deviceType, $message, $environment = 0)
  262. {
  263. $ret = array('ret_code' => -1, 'err_msg' => 'message not valid');
  264. if (!is_int($deviceType) || $deviceType < 0 || $deviceType > 5) {
  265. $ret['err_msg'] = 'deviceType not valid';
  266. return $ret;
  267. }
  268. if (!($message instanceof Message) && !($message instanceof MessageIOS)) return $ret;
  269. if (!$this->ValidateMessageType($message)) {
  270. $ret['err_msg'] = 'message type not fit accessId';
  271. return $ret;
  272. }
  273. if ($message instanceof MessageIOS) {
  274. if ($environment != XingeApp::IOSENV_DEV && $environment != XingeApp::IOSENV_PROD) {
  275. $ret['err_msg'] = "ios message environment invalid";
  276. return $ret;
  277. }
  278. }
  279. if (!$message->isValid()) return $ret;
  280. $params = array();
  281. $params['access_id'] = $this->accessId;
  282. $params['expire_time'] = $message->getExpireTime();
  283. $params['send_time'] = $message->getSendTime();
  284. if ($message instanceof Message) $params['multi_pkg'] = $message->getMultiPkg();
  285. $params['device_type'] = $deviceType;
  286. $params['message_type'] = $message->getType();
  287. $params['message'] = $message->toJson();
  288. $params['timestamp'] = time();
  289. $params['environment'] = $environment;
  290. if (!is_null($message->getLoopInterval()) && $message->getLoopInterval() > 0
  291. && !is_null($message->getLoopTimes()) && $message->getLoopTimes() > 0
  292. ) {
  293. $params['loop_interval'] = $message->getLoopInterval();
  294. $params['loop_times'] = $message->getLoopTimes();
  295. }
  296. //var_dump($params);
  297. return $this->callRestful(self::RESTAPI_PUSHALLDEVICE, $params);
  298. }
  299. /**
  300. * 推送消息给指定tags的设备
  301. * 若要推送的tagList只有一项,则tagsOp应为OR
  302. */
  303. public function PushTags($deviceType, $tagList, $tagsOp, $message, $environment = 0)
  304. {
  305. $ret = array('ret_code' => -1, 'err_msg' => 'message not valid');
  306. if (!is_int($deviceType) || $deviceType < 0 || $deviceType > 5) {
  307. $ret['err_msg'] = 'deviceType not valid';
  308. return $ret;
  309. }
  310. if (!is_array($tagList) || empty($tagList)) {
  311. $ret['err_msg'] = 'tagList not valid';
  312. return $ret;
  313. }
  314. if (!is_string($tagsOp) || ($tagsOp != 'AND' && $tagsOp != 'OR')) {
  315. $ret['err_msg'] = 'tagsOp not valid';
  316. return $ret;
  317. }
  318. if (!($message instanceof Message) && !($message instanceof MessageIOS)) return $ret;
  319. if (!$this->ValidateMessageType($message)) {
  320. $ret['err_msg'] = 'message type not fit accessId';
  321. return $ret;
  322. }
  323. if ($message instanceof MessageIOS) {
  324. if ($environment != XingeApp::IOSENV_DEV && $environment != XingeApp::IOSENV_PROD) {
  325. $ret['err_msg'] = "ios message environment invalid";
  326. return $ret;
  327. }
  328. }
  329. if (!$message->isValid()) return $ret;
  330. $params = array();
  331. $params['access_id'] = $this->accessId;
  332. $params['expire_time'] = $message->getExpireTime();
  333. $params['send_time'] = $message->getSendTime();
  334. if ($message instanceof Message) $params['multi_pkg'] = $message->getMultiPkg();
  335. $params['device_type'] = $deviceType;
  336. $params['message_type'] = $message->getType();
  337. $params['tags_list'] = json_encode($tagList);
  338. $params['tags_op'] = $tagsOp;
  339. $params['message'] = $message->toJson();
  340. $params['timestamp'] = time();
  341. $params['environment'] = $environment;
  342. if (!is_null($message->getLoopInterval()) && $message->getLoopInterval() > 0
  343. && !is_null($message->getLoopTimes()) && $message->getLoopTimes() > 0
  344. ) {
  345. $params['loop_interval'] = $message->getLoopInterval();
  346. $params['loop_times'] = $message->getLoopTimes();
  347. }
  348. return $this->callRestful(self::RESTAPI_PUSHTAGS, $params);
  349. }
  350. /**
  351. * 创建批量推送任务
  352. */
  353. public function CreateMultipush($message, $environment = 0)
  354. {
  355. $ret = array('ret_code' => -1);
  356. if (!($message instanceof Message) && !($message instanceof MessageIOS)) {
  357. $ret['err_msg'] = 'message is not android or ios';
  358. return $ret;
  359. }
  360. if (!$this->ValidateMessageType($message)) {
  361. $ret['err_msg'] = 'message type not fit accessId';
  362. return $ret;
  363. }
  364. if ($message instanceof MessageIOS) {
  365. if ($environment != XingeApp::IOSENV_DEV && $environment != XingeApp::IOSENV_PROD) {
  366. $ret['err_msg'] = "ios message environment invalid";
  367. return $ret;
  368. }
  369. }
  370. if (!$message->isValid()) {
  371. $ret['err_msg'] = 'message not valid';
  372. return $ret;
  373. }
  374. $params = array();
  375. $params['access_id'] = $this->accessId;
  376. $params['expire_time'] = $message->getExpireTime();
  377. if ($message instanceof Message)
  378. $params['multi_pkg'] = $message->getMultiPkg();
  379. $params['message_type'] = $message->getType();
  380. $params['message'] = $message->toJson();
  381. $params['timestamp'] = time();
  382. $params['environment'] = $environment;
  383. return $this->callRestful(self::RESTAPI_CREATEMULTIPUSH, $params);
  384. }
  385. /**
  386. * 按帐号大批量推送
  387. */
  388. public function PushAccountListMultiple($pushId, $accountList)
  389. {
  390. // $pushId = intval($pushId);
  391. $ret = array('ret_code' => -1);
  392. if ($pushId <= 0) {
  393. $ret['err_msg'] = 'pushId not valid';
  394. return $ret;
  395. }
  396. if (!is_array($accountList) || empty($accountList)) {
  397. $ret['err_msg'] = 'accountList not valid';
  398. return $ret;
  399. }
  400. $params = array();
  401. $params['access_id'] = $this->accessId;
  402. $params['push_id'] = $pushId;
  403. $params['account_list'] = json_encode($accountList);
  404. $params['timestamp'] = time();
  405. return $this->callRestful(self::RESTAPI_PUSHACCOUNTLISTMULTIPLE, $params);
  406. }
  407. /**
  408. * 按Token大批量推送
  409. */
  410. public function PushDeviceListMultiple($pushId, $deviceList)
  411. {
  412. // $pushId = intval($pushId);
  413. $ret = array('ret_code' => -1);
  414. if ($pushId <= 0) {
  415. $ret['err_msg'] = 'pushId not valid';
  416. return $ret;
  417. }
  418. if (!is_array($deviceList) || empty($deviceList)) {
  419. $ret['err_msg'] = 'deviceList not valid';
  420. return $ret;
  421. }
  422. $params = array();
  423. $params['access_id'] = $this->accessId;
  424. $params['push_id'] = $pushId;
  425. $params['device_list'] = json_encode($deviceList);
  426. $params['timestamp'] = time();
  427. return $this->callRestful(self::RESTAPI_PUSHDEVICELISTMULTIPLE, $params);
  428. }
  429. /**
  430. * 查询消息推送状态
  431. * @param array $pushIdList pushId(string)数组
  432. */
  433. public function QueryPushStatus($pushIdList)
  434. {
  435. $ret = array('ret_code' => -1);
  436. $idList = array();
  437. if (!is_array($pushIdList) || empty($pushIdList)) {
  438. $ret['err_msg'] = 'pushIdList not valid';
  439. return $ret;
  440. }
  441. foreach ($pushIdList as $pushId) {
  442. $idList[] = array('push_id' => $pushId);
  443. }
  444. $params = array();
  445. $params['access_id'] = $this->accessId;
  446. $params['push_ids'] = json_encode($idList);
  447. $params['timestamp'] = time();
  448. return $this->callRestful(self::RESTAPI_QUERYPUSHSTATUS, $params);
  449. }
  450. /**
  451. * 查询应用覆盖的设备数
  452. */
  453. public function QueryDeviceCount()
  454. {
  455. $params = array();
  456. $params['access_id'] = $this->accessId;
  457. $params['timestamp'] = time();
  458. return $this->callRestful(self::RESTAPI_QUERYDEVICECOUNT, $params);
  459. }
  460. /**
  461. * 查询应用标签
  462. */
  463. public function QueryTags($start = 0, $limit = 100)
  464. {
  465. $ret = array('ret_code' => -1);
  466. if (!is_int($start) || !is_int($limit)) {
  467. $ret['err_msg'] = 'start or limit not valid';
  468. return $ret;
  469. }
  470. $params = array();
  471. $params['access_id'] = $this->accessId;
  472. $params['start'] = $start;
  473. $params['limit'] = $limit;
  474. $params['timestamp'] = time();
  475. return $this->callRestful(self::RESTAPI_QUERYTAGS, $params);
  476. }
  477. /**
  478. * 查询标签下token数量
  479. */
  480. public function QueryTagTokenNum($tag)
  481. {
  482. $ret = array('ret_code' => -1);
  483. if (!is_string($tag)) {
  484. $ret['err_msg'] = 'tag is not valid';
  485. return $ret;
  486. }
  487. $params = array();
  488. $params['access_id'] = $this->accessId;
  489. $params['tag'] = $tag;
  490. $params['timestamp'] = time();
  491. return $this->callRestful(self::RESTAPI_QUERYTAGTOKENNUM, $params);
  492. }
  493. /**
  494. * 查询token的标签
  495. */
  496. public function QueryTokenTags($deviceToken)
  497. {
  498. $ret = array('ret_code' => -1);
  499. if (!is_string($deviceToken)) {
  500. $ret['err_msg'] = 'deviceToken is not valid';
  501. return $ret;
  502. }
  503. $params = array();
  504. $params['access_id'] = $this->accessId;
  505. $params['device_token'] = $deviceToken;
  506. $params['timestamp'] = time();
  507. return $this->callRestful(self::RESTAPI_QUERYTOKENTAGS, $params);
  508. }
  509. /**
  510. * 取消定时发送
  511. */
  512. public function CancelTimingPush($pushId)
  513. {
  514. $ret = array('ret_code' => -1);
  515. if (!is_string($pushId) || empty($pushId)) {
  516. $ret['err_msg'] = 'pushId not valid';
  517. return $ret;
  518. }
  519. $params = array();
  520. $params['access_id'] = $this->accessId;
  521. $params['push_id'] = $pushId;
  522. $params['timestamp'] = time();
  523. return $this->callRestful(self::RESTAPI_CANCELTIMINGPUSH, $params);
  524. }
  525. //json转换为数组
  526. protected function json2Array($json)
  527. {
  528. $json = stripslashes($json);
  529. return json_decode($json, true);
  530. }
  531. protected function callRestful($url, $params)
  532. {
  533. $paramsBase = new ParamsBase($params);
  534. $sign = $paramsBase->generateSign(RequestBase::METHOD_POST, $url, $this->secretKey);
  535. $params['sign'] = $sign;
  536. $requestBase = new RequestBase();
  537. $ret = $this->json2Array($requestBase->exec($url, $params, RequestBase::METHOD_POST));
  538. return $ret;
  539. }
  540. private function ValidateToken($token)
  541. {
  542. if ($this->accessId >= 2200000000) {
  543. return strlen($token) == 64;
  544. } else {
  545. return (strlen($token) == 40 || strlen($token) == 64);
  546. }
  547. }
  548. public function InitParams()
  549. {
  550. $params = array();
  551. $params['access_id'] = $this->accessId;
  552. $params['timestamp'] = time();
  553. return $params;
  554. }
  555. public function BatchSetTag($tagTokenPairs)
  556. {
  557. $ret = array('ret_code' => -1);
  558. foreach ($tagTokenPairs as $pair) {
  559. if (!($pair instanceof TagTokenPair)) {
  560. $ret['err_msg'] = 'tag-token pair type error!';
  561. return $ret;
  562. }
  563. if (!$this->ValidateToken($pair->token)) {
  564. $ret['err_msg'] = sprintf("invalid token %s", $pair->token);
  565. return $ret;
  566. }
  567. }
  568. $params = $this->InitParams();
  569. $tag_token_list = array();
  570. foreach ($tagTokenPairs as $pair) {
  571. array_push($tag_token_list, array($pair->tag, $pair->token));
  572. }
  573. $params['tag_token_list'] = json_encode($tag_token_list);
  574. return $this->callRestful(self::RESTAPI_BATCHSETTAG, $params);
  575. }
  576. public function BatchDelTag($tagTokenPairs)
  577. {
  578. $ret = array('ret_code' => -1);
  579. foreach ($tagTokenPairs as $pair) {
  580. if (!($pair instanceof TagTokenPair)) {
  581. $ret['err_msg'] = 'tag-token pair type error!';
  582. return $ret;
  583. }
  584. if (!$this->ValidateToken($pair->token)) {
  585. $ret['err_msg'] = sprintf("invalid token %s", $pair->token);
  586. return $ret;
  587. }
  588. }
  589. $params = $this->InitParams();
  590. $tag_token_list = array();
  591. foreach ($tagTokenPairs as $pair) {
  592. array_push($tag_token_list, array($pair->tag, $pair->token));
  593. }
  594. $params['tag_token_list'] = json_encode($tag_token_list);
  595. return $this->callRestful(self::RESTAPI_BATCHDELTAG, $params);
  596. }
  597. public function QueryInfoOfToken($deviceToken)
  598. {
  599. $ret = array('ret_code' => -1);
  600. if (!is_string($deviceToken)) {
  601. $ret['err_msg'] = 'deviceToken is not valid';
  602. return $ret;
  603. }
  604. $params = array();
  605. $params['access_id'] = $this->accessId;
  606. $params['device_token'] = $deviceToken;
  607. $params['timestamp'] = time();
  608. return $this->callRestful(self::RESTAPI_QUERYINFOOFTOKEN, $params);
  609. }
  610. public function QueryTokensOfAccount($account)
  611. {
  612. $ret = array('ret_code' => -1);
  613. if (!is_string($account)) {
  614. $ret['err_msg'] = 'account is not valid';
  615. return $ret;
  616. }
  617. $params = array();
  618. $params['access_id'] = $this->accessId;
  619. $params['account'] = $account;
  620. $params['timestamp'] = time();
  621. return $this->callRestful(self::RESTAPI_QUERYTOKENSOFACCOUNT, $params);
  622. }
  623. public function DeleteTokenOfAccount($account, $deviceToken)
  624. {
  625. $ret = array('ret_code' => -1);
  626. if (!is_string($account) || !is_string($deviceToken)) {
  627. $ret['err_msg'] = 'account or deviceToken is not valid';
  628. return $ret;
  629. }
  630. $params = array();
  631. $params['access_id'] = $this->accessId;
  632. $params['account'] = $account;
  633. $params['device_token'] = $deviceToken;
  634. $params['timestamp'] = time();
  635. return $this->callRestful(self::RESTAPI_DELETETOKENOFACCOUNT, $params);
  636. }
  637. public function DeleteAllTokensOfAccount($account)
  638. {
  639. $ret = array('ret_code' => -1);
  640. if (!is_string($account)) {
  641. $ret['err_msg'] = 'account is not valid';
  642. return $ret;
  643. }
  644. $params = array();
  645. $params['access_id'] = $this->accessId;
  646. $params['account'] = $account;
  647. $params['timestamp'] = time();
  648. return $this->callRestful(self::RESTAPI_DELETEALLTOKENSOFACCOUNT, $params);
  649. }
  650. private function ValidateMessageType($message)
  651. {
  652. if ($this->accessId >= XingeApp::IOS_MIN_ID and $message instanceof MessageIOS)
  653. return true;
  654. else if ($this->accessId < XingeApp::IOS_MIN_ID and $message instanceof Message)
  655. return true;
  656. else
  657. return false;
  658. }
  659. public $accessId = ''; //应用的接入Id
  660. public $secretKey = ''; //应用的skey
  661. const RESTAPI_PUSHSINGLEDEVICE = 'http://openapi.xg.qq.com/v2/push/single_device';
  662. const RESTAPI_PUSHSINGLEACCOUNT = 'http://openapi.xg.qq.com/v2/push/single_account';
  663. const RESTAPI_PUSHACCOUNTLIST = 'http://openapi.xg.qq.com/v2/push/account_list';
  664. const RESTAPI_PUSHALLDEVICE = 'http://openapi.xg.qq.com/v2/push/all_device';
  665. const RESTAPI_PUSHTAGS = 'http://openapi.xg.qq.com/v2/push/tags_device';
  666. const RESTAPI_QUERYPUSHSTATUS = 'http://openapi.xg.qq.com/v2/push/get_msg_status';
  667. const RESTAPI_QUERYDEVICECOUNT = 'http://openapi.xg.qq.com/v2/application/get_app_device_num';
  668. const RESTAPI_QUERYTAGS = 'http://openapi.xg.qq.com/v2/tags/query_app_tags';
  669. const RESTAPI_CANCELTIMINGPUSH = 'http://openapi.xg.qq.com/v2/push/cancel_timing_task';
  670. const RESTAPI_BATCHSETTAG = 'http://openapi.xg.qq.com/v2/tags/batch_set';
  671. const RESTAPI_BATCHDELTAG = 'http://openapi.xg.qq.com/v2/tags/batch_del';
  672. const RESTAPI_QUERYTOKENTAGS = 'http://openapi.xg.qq.com/v2/tags/query_token_tags';
  673. const RESTAPI_QUERYTAGTOKENNUM = 'http://openapi.xg.qq.com/v2/tags/query_tag_token_num';
  674. const RESTAPI_CREATEMULTIPUSH = 'http://openapi.xg.qq.com/v2/push/create_multipush';
  675. const RESTAPI_PUSHACCOUNTLISTMULTIPLE = 'http://openapi.xg.qq.com/v2/push/account_list_multiple';
  676. const RESTAPI_PUSHDEVICELISTMULTIPLE = 'http://openapi.xg.qq.com/v2/push/device_list_multiple';
  677. const RESTAPI_QUERYINFOOFTOKEN = 'http://openapi.xg.qq.com/v2/application/get_app_token_info';
  678. const RESTAPI_QUERYTOKENSOFACCOUNT = 'http://openapi.xg.qq.com/v2/application/get_app_account_tokens';
  679. const RESTAPI_DELETETOKENOFACCOUNT = 'http://openapi.xg.qq.com/v2/application/del_app_account_tokens';
  680. const RESTAPI_DELETEALLTOKENSOFACCOUNT = 'http://openapi.xg.qq.com/v2/application/del_app_account_all_tokens';
  681. }
  682. class TagTokenPair
  683. {
  684. public function __construct($tag, $token)
  685. {
  686. $this->tag = strval($tag);
  687. $this->token = strval($token);
  688. }
  689. public function __destruct()
  690. {
  691. }
  692. public $tag;
  693. public $token;
  694. }
  695. class Message
  696. {
  697. public function __construct()
  698. {
  699. $this->m_acceptTimes = array();
  700. $this->m_multiPkg = 0;
  701. $this->m_raw = "";
  702. $this->m_style = new Style(0);
  703. $this->m_action = new ClickAction();
  704. }
  705. public function __destruct()
  706. {
  707. }
  708. public function setTitle($title)
  709. {
  710. $this->m_title = $title;
  711. }
  712. public function setContent($content)
  713. {
  714. $this->m_content = $content;
  715. }
  716. public function setExpireTime($expireTime)
  717. {
  718. $this->m_expireTime = $expireTime;
  719. }
  720. public function getExpireTime()
  721. {
  722. return $this->m_expireTime;
  723. }
  724. public function setSendTime($sendTime)
  725. {
  726. $this->m_sendTime = $sendTime;
  727. }
  728. public function getSendTime()
  729. {
  730. return $this->m_sendTime;
  731. }
  732. public function addAcceptTime($acceptTime)
  733. {
  734. $this->m_acceptTimes[] = $acceptTime;
  735. }
  736. public function acceptTimeToJson()
  737. {
  738. $ret = array();
  739. foreach ($this->m_acceptTimes as $acceptTime) {
  740. $ret[] = $acceptTime->toArray();
  741. }
  742. return $ret;
  743. }
  744. /**
  745. * 消息类型
  746. * @param int $type 1:通知 2:透传消息
  747. */
  748. public function setType($type)
  749. {
  750. $this->m_type = $type;
  751. }
  752. public function getType()
  753. {
  754. return $this->m_type;
  755. }
  756. public function setMultiPkg($multiPkg)
  757. {
  758. $this->m_multiPkg = $multiPkg;
  759. }
  760. public function getMultiPkg()
  761. {
  762. return $this->m_multiPkg;
  763. }
  764. public function setStyle($style)
  765. {
  766. $this->m_style = $style;
  767. }
  768. public function setAction($action)
  769. {
  770. $this->m_action = $action;
  771. }
  772. public function setCustom($custom)
  773. {
  774. $this->m_custom = $custom;
  775. }
  776. public function setRaw($raw)
  777. {
  778. $this->m_raw = $raw;
  779. }
  780. public function getLoopInterval()
  781. {
  782. return $this->m_loopInterval;
  783. }
  784. public function setLoopInterval($loopInterval)
  785. {
  786. $this->m_loopInterval = $loopInterval;
  787. }
  788. public function getLoopTimes()
  789. {
  790. return $this->m_loopTimes;
  791. }
  792. public function setLoopTimes($loopTimes)
  793. {
  794. $this->m_loopTimes = $loopTimes;
  795. }
  796. public function toJson()
  797. {
  798. if (!empty($this->m_raw)) return $this->m_raw;
  799. $ret = array();
  800. if ($this->m_type == self::TYPE_NOTIFICATION) {
  801. $ret['title'] = $this->m_title;
  802. $ret['content'] = $this->m_content;
  803. $ret['accept_time'] = $this->acceptTimeToJson();
  804. $ret['builder_id'] = $this->m_style->getBuilderId();
  805. $ret['ring'] = $this->m_style->getRing();
  806. $ret['vibrate'] = $this->m_style->getVibrate();
  807. $ret['clearable'] = $this->m_style->getClearable();
  808. $ret['n_id'] = $this->m_style->getNId();
  809. if (!is_null($this->m_style->getRingRaw())) {
  810. $ret['ring_raw'] = $this->m_style->getRingRaw();
  811. }
  812. $ret['lights'] = $this->m_style->getLights();
  813. $ret['icon_type'] = $this->m_style->getIconType();
  814. if (!is_null($this->m_style->getIconRes())) {
  815. $ret['icon_res'] = $this->m_style->getIconRes();
  816. }
  817. $ret['style_id'] = $this->m_style->getStyleId();
  818. if (!is_null($this->m_style->getSmallIcon())) {
  819. $ret['small_icon'] = $this->m_style->getSmallIcon();
  820. }
  821. $ret['action'] = $this->m_action->toJson();
  822. } else if ($this->m_type == self::TYPE_MESSAGE) {
  823. $ret['title'] = $this->m_title;
  824. $ret['content'] = $this->m_content;
  825. $ret['accept_time'] = $this->acceptTimeToJson();
  826. }
  827. $ret['custom_content'] = $this->m_custom;
  828. return json_encode($ret);
  829. }
  830. public function isValid()
  831. {
  832. if (is_string($this->m_raw) && !empty($this->raw)) return true;
  833. if (!isset($this->m_title))
  834. $this->m_title = "";
  835. else if (!is_string($this->m_title) || empty($this->m_title))
  836. return false;
  837. if (!isset($this->m_content))
  838. $this->m_content = "";
  839. else if (!is_string($this->m_content) || empty($this->m_content))
  840. return false;
  841. if (!is_int($this->m_type) || $this->m_type < self::TYPE_NOTIFICATION || $this->m_type > self::TYPE_MESSAGE) return false;
  842. if (!is_int($this->m_multiPkg) || $this->m_multiPkg < 0 || $this->m_multiPkg > 1) return false;
  843. if ($this->m_type == self::TYPE_NOTIFICATION) {
  844. if (!($this->m_style instanceof Style) || !($this->m_action instanceof ClickAction))
  845. return false;
  846. if (!$this->m_style->isValid() || !$this->m_action->isValid())
  847. return false;
  848. }
  849. if (isset($this->m_expireTime)) {
  850. if (!is_int($this->m_expireTime) || $this->m_expireTime > 3 * 24 * 60 * 60)
  851. return false;
  852. } else {
  853. $this->m_expireTime = 0;
  854. }
  855. if (isset($this->m_sendTime)) {
  856. if (strtotime($this->m_sendTime) === false) return false;
  857. } else {
  858. $this->m_sendTime = "2013-12-19 17:49:00";
  859. }
  860. foreach ($this->m_acceptTimes as $value) {
  861. if (!($value instanceof TimeInterval) || !$value->isValid())
  862. return false;
  863. }
  864. if (isset($this->m_custom)) {
  865. if (!is_array($this->m_custom))
  866. return false;
  867. } else {
  868. $this->m_custom = array();
  869. }
  870. if (isset($this->m_loopInterval)) {
  871. if (!(is_int($this->m_loopInterval) && $this->m_loopInterval > 0)) {
  872. return false;
  873. }
  874. }
  875. if (isset($this->m_loopTimes)) {
  876. if (!(is_int($this->m_loopTimes) && $this->m_loopTimes > 0)) {
  877. return false;
  878. }
  879. }
  880. if (isset($this->m_loopInterval) && isset($this->m_loopTimes)) {
  881. if (($this->m_loopTimes - 1) * $this->m_loopInterval + 1 > self::MAX_LOOP_TASK_DAYS) {
  882. return false;
  883. }
  884. }
  885. return true;
  886. }
  887. private $m_title;
  888. private $m_content;
  889. private $m_expireTime;
  890. private $m_sendTime;
  891. private $m_acceptTimes;
  892. private $m_type;
  893. private $m_multiPkg;
  894. private $m_style;
  895. private $m_action;
  896. private $m_custom;
  897. private $m_raw;
  898. private $m_loopInterval;
  899. private $m_loopTimes;
  900. const TYPE_NOTIFICATION = 1;
  901. const TYPE_MESSAGE = 2;
  902. const MAX_LOOP_TASK_DAYS = 15;
  903. }
  904. class MessageIOS
  905. {
  906. public function __construct()
  907. {
  908. $this->m_acceptTimes = array();
  909. $this->m_type = self::TYPE_APNS_NOTIFICATION;
  910. }
  911. public function __destruct()
  912. {
  913. }
  914. public function setExpireTime($expireTime)
  915. {
  916. $this->m_expireTime = $expireTime;
  917. }
  918. public function getExpireTime()
  919. {
  920. return $this->m_expireTime;
  921. }
  922. public function setSendTime($sendTime)
  923. {
  924. $this->m_sendTime = $sendTime;
  925. }
  926. public function getSendTime()
  927. {
  928. return $this->m_sendTime;
  929. }
  930. public function addAcceptTime($acceptTime)
  931. {
  932. $this->m_acceptTimes[] = $acceptTime;
  933. }
  934. public function acceptTimeToJson()
  935. {
  936. $ret = array();
  937. foreach ($this->m_acceptTimes as $acceptTime) {
  938. $ret[] = $acceptTime->toArray();
  939. }
  940. return $ret;
  941. }
  942. public function setCustom($custom)
  943. {
  944. $this->m_custom = $custom;
  945. }
  946. public function setRaw($raw)
  947. {
  948. $this->m_raw = $raw;
  949. }
  950. public function setAlert($alert)
  951. {
  952. $this->m_alert = $alert;
  953. }
  954. public function setBadge($badge)
  955. {
  956. $this->m_badge = $badge;
  957. }
  958. public function setSound($sound)
  959. {
  960. $this->m_sound = $sound;
  961. }
  962. /**
  963. * 消息类型
  964. * @param int $type 1:通知 2:静默通知
  965. */
  966. public function setType($type)
  967. {
  968. $this->m_type = $type;
  969. }
  970. public function getType()
  971. {
  972. return $this->m_type;
  973. }
  974. public function getCategory()
  975. {
  976. return $this->m_category;
  977. }
  978. public function setCategory($category)
  979. {
  980. $this->m_category = $category;
  981. }
  982. public function getLoopInterval()
  983. {
  984. return $this->m_loopInterval;
  985. }
  986. public function setLoopInterval($loopInterval)
  987. {
  988. $this->m_loopInterval = $loopInterval;
  989. }
  990. public function getLoopTimes()
  991. {
  992. return $this->m_loopTimes;
  993. }
  994. public function setLoopTimes($loopTimes)
  995. {
  996. $this->m_loopTimes = $loopTimes;
  997. }
  998. public function toJson()
  999. {
  1000. if (!empty($this->m_raw)) return $this->m_raw;
  1001. $ret = $this->m_custom;
  1002. $ret['accept_time'] = $this->acceptTimeToJson();
  1003. $aps = array();
  1004. if ($this->m_type == self::TYPE_APNS_NOTIFICATION) {
  1005. $aps['alert'] = $this->m_alert;
  1006. if (isset($this->m_badge)) $aps['badge'] = $this->m_badge;
  1007. if (isset($this->m_sound)) $aps['sound'] = $this->m_sound;
  1008. if (isset($this->m_category)) $aps['category'] = $this->m_category;
  1009. } else if ($this->m_type == self::TYPE_REMOTE_NOTIFICATION) {
  1010. $aps['content-available'] = 1;
  1011. }
  1012. $ret['aps'] = $aps;
  1013. return json_encode($ret);
  1014. }
  1015. public function isValid()
  1016. {
  1017. if (isset($this->m_expireTime)) {
  1018. if (!is_int($this->m_expireTime) || $this->m_expireTime > 3 * 24 * 60 * 60)
  1019. return false;
  1020. } else {
  1021. $this->m_expireTime = 0;
  1022. }
  1023. if (isset($this->m_sendTime)) {
  1024. if (strtotime($this->m_sendTime) === false) return false;
  1025. } else {
  1026. $this->m_sendTime = "2014-03-13 12:00:00";
  1027. }
  1028. if (!empty($this->m_raw)) {
  1029. if (is_string($this->m_raw))
  1030. return true;
  1031. else
  1032. return false;
  1033. }
  1034. if (!is_int($this->m_type) || $this->m_type < self::TYPE_APNS_NOTIFICATION || $this->m_type > self::TYPE_REMOTE_NOTIFICATION) {
  1035. return false;
  1036. }
  1037. foreach ($this->m_acceptTimes as $value) {
  1038. if (!($value instanceof TimeInterval) || !$value->isValid())
  1039. return false;
  1040. }
  1041. if (isset($this->m_custom)) {
  1042. if (!is_array($this->m_custom))
  1043. return false;
  1044. } else {
  1045. $this->m_custom = array();
  1046. }
  1047. if ($this->m_type == self::TYPE_APNS_NOTIFICATION) {
  1048. if (!isset($this->m_alert)) return false;
  1049. if (!is_string($this->m_alert) && !is_array($this->m_alert))
  1050. return false;
  1051. }
  1052. if (isset($this->m_badge)) {
  1053. if (!is_int($this->m_badge))
  1054. return false;
  1055. }
  1056. if (isset($this->m_sound)) {
  1057. if (!is_string($this->m_sound))
  1058. return false;
  1059. }
  1060. if (isset($this->m_loopInterval)) {
  1061. if (!(is_int($this->m_loopInterval) && $this->m_loopInterval > 0)) {
  1062. return false;
  1063. }
  1064. }
  1065. if (isset($this->m_loopTimes)) {
  1066. if (!(is_int($this->m_loopTimes) && $this->m_loopTimes > 0)) {
  1067. return false;
  1068. }
  1069. }
  1070. if (isset($this->m_loopInterval) && isset($this->m_loopTimes)) {
  1071. if (($this->m_loopTimes - 1) * $this->m_loopInterval + 1 > self::MAX_LOOP_TASK_DAYS) {
  1072. return false;
  1073. }
  1074. }
  1075. return true;
  1076. }
  1077. private $m_expireTime;
  1078. private $m_sendTime;
  1079. private $m_acceptTimes;
  1080. private $m_custom;
  1081. private $m_raw;
  1082. private $m_type;
  1083. private $m_alert;
  1084. private $m_badge;
  1085. private $m_sound;
  1086. private $m_category;
  1087. private $m_loopInterval;
  1088. private $m_loopTimes;
  1089. const TYPE_APNS_NOTIFICATION = 11;
  1090. const TYPE_REMOTE_NOTIFICATION = 12;
  1091. const MAX_LOOP_TASK_DAYS = 15;
  1092. }
  1093. class ClickAction
  1094. {
  1095. /**
  1096. * 动作类型
  1097. * @param int $actionType 1打开activity或app本身,2打开url,3打开Intent
  1098. */
  1099. public function __construct()
  1100. {
  1101. $this->m_atyAttrIntentFlag = 0;
  1102. $this->m_atyAttrPendingIntentFlag = 0;
  1103. $this->m_confirmOnPackageDownloadUrl = 1;
  1104. }
  1105. public function setActionType($actionType)
  1106. {
  1107. $this->m_actionType = $actionType;
  1108. }
  1109. public function setUrl($url)
  1110. {
  1111. $this->m_url = $url;
  1112. }
  1113. public function setComfirmOnUrl($comfirmOnUrl)
  1114. {
  1115. $this->m_confirmOnUrl = $comfirmOnUrl;
  1116. }
  1117. public function setActivity($activity)
  1118. {
  1119. $this->m_activity = $activity;
  1120. }
  1121. public function setIntent($intent)
  1122. {
  1123. $this->m_intent = $intent;
  1124. }
  1125. public function setAtyAttrIntentFlag($atyAttrIntentFlag)
  1126. {
  1127. $this->m_atyAttrIntentFlag = $atyAttrIntentFlag;
  1128. }
  1129. public function setAtyAttrPendingIntentFlag($atyAttrPendingIntentFlag)
  1130. {
  1131. $this->m_atyAttrPendingIntentFlag = $atyAttrPendingIntentFlag;
  1132. }
  1133. public function setPackageDownloadUrl($packageDownloadUrl)
  1134. {
  1135. $this->m_packageDownloadUrl = $packageDownloadUrl;
  1136. }
  1137. public function setConfirmOnPackageDownloadUrl($confirmOnPackageDownloadUrl)
  1138. {
  1139. $this->m_confirmOnPackageDownloadUrl = $confirmOnPackageDownloadUrl;
  1140. }
  1141. public function setPackageName($packageName)
  1142. {
  1143. $this->m_packageName = $packageName;
  1144. }
  1145. public function toJson()
  1146. {
  1147. $ret = array();
  1148. $ret['action_type'] = $this->m_actionType;
  1149. $ret['browser'] = array('url' => $this->m_url, 'confirm' => $this->m_confirmOnUrl);
  1150. $ret['activity'] = $this->m_activity;
  1151. $ret['intent'] = $this->m_intent;
  1152. $aty_attr = array();
  1153. if (isset($this->m_atyAttrIntentFlag)) {
  1154. $aty_attr['if'] = $this->m_atyAttrIntentFlag;
  1155. }
  1156. if (isset($this->m_atyAttrPendingIntentFlag)) {
  1157. $aty_attr['pf'] = $this->m_atyAttrPendingIntentFlag;
  1158. }
  1159. $ret['aty_attr'] = $aty_attr;
  1160. return $ret;
  1161. }
  1162. public function isValid()
  1163. {
  1164. if (!isset($this->m_actionType)) $this->m_actionType = self::TYPE_ACTIVITY;
  1165. if (!is_int($this->m_actionType)) return false;
  1166. if ($this->m_actionType < self::TYPE_ACTIVITY || $this->m_actionType > self::TYPE_INTENT)
  1167. return false;
  1168. if ($this->m_actionType == self::TYPE_ACTIVITY) {
  1169. if (!isset($this->m_activity)) {
  1170. $this->m_activity = "";
  1171. return true;
  1172. }
  1173. if (isset($this->m_atyAttrIntentFlag)) {
  1174. if (!is_int($this->m_atyAttrIntentFlag)) {
  1175. return false;
  1176. }
  1177. }
  1178. if (isset($this->m_atyAttrPendingIntentFlag)) {
  1179. if (!is_int($this->m_atyAttrPendingIntentFlag)) {
  1180. return false;
  1181. }
  1182. }
  1183. if (is_string($this->m_activity) && !empty($this->m_activity))
  1184. return true;
  1185. return false;
  1186. }
  1187. if ($this->m_actionType == self::TYPE_URL) {
  1188. if (is_string($this->m_url) && !empty($this->m_url) &&
  1189. is_int($this->m_confirmOnUrl) &&
  1190. $this->m_confirmOnUrl >= 0 && $this->m_confirmOnUrl <= 1
  1191. )
  1192. return true;
  1193. return false;
  1194. }
  1195. if ($this->m_actionType == self::TYPE_INTENT) {
  1196. if (is_string($this->m_intent) && !empty($this->m_intent))
  1197. return true;
  1198. return false;
  1199. }
  1200. }
  1201. private $m_actionType;
  1202. private $m_url;
  1203. private $m_confirmOnUrl;
  1204. private $m_activity;
  1205. private $m_intent;
  1206. private $m_atyAttrIntentFlag;
  1207. private $m_atyAttrPendingIntentFlag;
  1208. private $m_packageDownloadUrl;
  1209. private $m_confirmOnPackageDownloadUrl;
  1210. private $m_packageName;
  1211. const TYPE_ACTIVITY = 1;
  1212. const TYPE_URL = 2;
  1213. const TYPE_INTENT = 3;
  1214. }
  1215. class Style
  1216. {
  1217. public function __construct($builderId, $ring = 0, $vibrate = 0, $clearable = 1, $nId = 0, $lights = 1, $iconType = 0, $styleId = 1)
  1218. {
  1219. $this->m_builderId = $builderId;
  1220. $this->m_ring = $ring;
  1221. $this->m_vibrate = $vibrate;
  1222. $this->m_clearable = $clearable;
  1223. $this->m_nId = $nId;
  1224. $this->m_lights = $lights;
  1225. $this->m_iconType = $iconType;
  1226. $this->m_styleId = $styleId;
  1227. }
  1228. public function __destruct()
  1229. {
  1230. }
  1231. public function getBuilderId()
  1232. {
  1233. return $this->m_builderId;
  1234. }
  1235. public function getRing()
  1236. {
  1237. return $this->m_ring;
  1238. }
  1239. public function getVibrate()
  1240. {
  1241. return $this->m_vibrate;
  1242. }
  1243. public function getClearable()
  1244. {
  1245. return $this->m_clearable;
  1246. }
  1247. public function getNId()
  1248. {
  1249. return $this->m_nId;
  1250. }
  1251. public function getLights()
  1252. {
  1253. return $this->m_lights;
  1254. }
  1255. public function getIconType()
  1256. {
  1257. return $this->m_iconType;
  1258. }
  1259. public function getStyleId()
  1260. {
  1261. return $this->m_styleId;
  1262. }
  1263. public function setRingRaw($ringRaw)
  1264. {
  1265. return $this->m_ringRaw = $ringRaw;
  1266. }
  1267. public function getRingRaw()
  1268. {
  1269. return $this->m_ringRaw;
  1270. }
  1271. public function setIconRes($iconRes)
  1272. {
  1273. return $this->m_iconRes = $iconRes;
  1274. }
  1275. public function getIconRes()
  1276. {
  1277. return $this->m_iconRes;
  1278. }
  1279. public function setSmallIcon($smallIcon)
  1280. {
  1281. return $this->m_smallIcon = $smallIcon;
  1282. }
  1283. public function getSmallIcon()
  1284. {
  1285. return $this->m_smallIcon;
  1286. }
  1287. public function isValid()
  1288. {
  1289. if (!is_int($this->m_builderId) || !is_int($this->m_ring) ||
  1290. !is_int($this->m_vibrate) || !is_int($this->m_clearable) ||
  1291. !is_int($this->m_lights) || !is_int($this->m_iconType) ||
  1292. !is_int($this->m_styleId)
  1293. )
  1294. return false;
  1295. if ($this->m_ring < 0 || $this->m_ring > 1) return false;
  1296. if ($this->m_vibrate < 0 || $this->m_vibrate > 1) return false;
  1297. if ($this->m_clearable < 0 || $this->m_clearable > 1) return false;
  1298. if ($this->m_lights < 0 || $this->m_lights > 1) return false;
  1299. if ($this->m_iconType < 0 || $this->m_iconType > 1) return false;
  1300. if ($this->m_styleId < 0 || $this->m_styleId > 1) return false;
  1301. return true;
  1302. }
  1303. private $m_builderId;
  1304. private $m_ring;
  1305. private $m_vibrate;
  1306. private $m_clearable;
  1307. private $m_nId;
  1308. private $m_ringRaw;
  1309. private $m_lights;
  1310. private $m_iconType;
  1311. private $m_iconRes;
  1312. private $m_styleId;
  1313. private $m_smallIcon;
  1314. }
  1315. class TimeInterval
  1316. {
  1317. public function __construct($startHour, $startMin, $endHour, $endMin)
  1318. {
  1319. $this->m_startHour = $startHour;
  1320. $this->m_startMin = $startMin;
  1321. $this->m_endHour = $endHour;
  1322. $this->m_endMin = $endMin;
  1323. }
  1324. public function __destruct()
  1325. {
  1326. }
  1327. public function toArray()
  1328. {
  1329. return array(
  1330. 'start' => array('hour' => strval($this->m_startHour), 'min' => strval($this->m_startMin)),
  1331. 'end' => array('hour' => strval($this->m_endHour), 'min' => strval($this->m_endMin))
  1332. );
  1333. }
  1334. public function isValid()
  1335. {
  1336. if (!is_int($this->m_startHour) || !is_int($this->m_startMin) ||
  1337. !is_int($this->m_endHour) || !is_int($this->m_endMin)
  1338. )
  1339. return false;
  1340. if ($this->m_startHour >= 0 && $this->m_startHour <= 23 &&
  1341. $this->m_startMin >= 0 && $this->m_startMin <= 59 &&
  1342. $this->m_endHour >= 0 && $this->m_endHour <= 23 &&
  1343. $this->m_endMin >= 0 && $this->m_endMin <= 59
  1344. )
  1345. return true;
  1346. else
  1347. return false;
  1348. }
  1349. private $m_startHour;
  1350. private $m_startMin;
  1351. private $m_endHour;
  1352. private $m_endMin;
  1353. }
  1354. class ParamsBase
  1355. {
  1356. /**
  1357. * @var array 当前传入的参数列表
  1358. */
  1359. public $_params = array();
  1360. /**
  1361. * 构造函数
  1362. */
  1363. public function __construct($params)
  1364. {
  1365. if (!is_array($params)) {
  1366. return array();
  1367. }
  1368. foreach ($params as $key => $value) {
  1369. //如果是非法的key值,则不使用这个key
  1370. $this->_params[$key] = $value;
  1371. }
  1372. }
  1373. public function set($k, $v)
  1374. {
  1375. if (!isset($k) || !isset($v)) {
  1376. return;
  1377. }
  1378. $this->_params[$k] = $v;
  1379. }
  1380. /**
  1381. * 根据实例化传入的参数生成签名
  1382. */
  1383. public function generateSign($method, $url, $secret_key)
  1384. {
  1385. //将参数进行升序排序
  1386. $param_str = '';
  1387. $method = strtoupper($method);
  1388. $url_arr = parse_url($url);
  1389. if (isset($url_arr['host']) && isset($url_arr['path'])) {
  1390. $url = $url_arr['host'] . $url_arr['path'];
  1391. }
  1392. if (!empty($this->_params)) {
  1393. ksort($this->_params);
  1394. foreach ($this->_params as $key => $value) {
  1395. $param_str .= $key . '=' . $value;
  1396. }
  1397. }
  1398. //print $method.$url.$param_str.$secret_key."\n";
  1399. return md5($method . $url . $param_str . $secret_key);
  1400. }
  1401. }
  1402. class RequestBase
  1403. {
  1404. //get请求方式
  1405. const METHOD_GET = 'get';
  1406. //post请求方式
  1407. const METHOD_POST = 'post';
  1408. /**
  1409. * 发起一个get或post请求
  1410. * @param $url 请求的url
  1411. * @param int $method 请求方式
  1412. * @param array $params 请求参数
  1413. * @param array $extra_conf curl配置, 高级需求可以用, 如
  1414. * $extra_conf = array(
  1415. * CURLOPT_HEADER => true,
  1416. * CURLOPT_RETURNTRANSFER = false
  1417. * )
  1418. * @return bool|mixed 成功返回数据,失败返回false
  1419. * @throws Exception
  1420. */
  1421. public static function exec($url, $params = array(), $method = self::METHOD_GET, $extra_conf = array())
  1422. {
  1423. $params = is_array($params) ? http_build_query($params) : $params;
  1424. //如果是get请求,直接将参数附在url后面
  1425. if ($method == self::METHOD_GET) {
  1426. $url .= (strpos($url, '?') === false ? '?' : '&') . $params;
  1427. }
  1428. //默认配置
  1429. $curl_conf = array(
  1430. CURLOPT_URL => $url, //请求url
  1431. CURLOPT_HEADER => false, //不输出头信息
  1432. CURLOPT_RETURNTRANSFER => true, //不输出返回数据
  1433. CURLOPT_CONNECTTIMEOUT => 3 // 连接超时时间
  1434. );
  1435. //配置post请求额外需要的配置项
  1436. if ($method == self::METHOD_POST) {
  1437. //使用post方式
  1438. $curl_conf[CURLOPT_POST] = true;
  1439. //post参数
  1440. $curl_conf[CURLOPT_POSTFIELDS] = $params;
  1441. }
  1442. //添加额外的配置
  1443. foreach ($extra_conf as $k => $v) {
  1444. $curl_conf[$k] = $v;
  1445. }
  1446. $data = false;
  1447. try {
  1448. //初始化一个curl句柄
  1449. $curl_handle = curl_init();
  1450. //设置curl的配置项
  1451. curl_setopt_array($curl_handle, $curl_conf);
  1452. //发起请求
  1453. $data = curl_exec($curl_handle);
  1454. if ($data === false) {
  1455. throw new Exception('CURL ERROR: ' . curl_error($curl_handle));
  1456. }
  1457. } catch (Exception $e) {
  1458. echo $e->getMessage();
  1459. }
  1460. curl_close($curl_handle);
  1461. return $data;
  1462. }
  1463. }
  1464. ?>