VPush.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php
  2. namespace App\Libs\Push\VPush;
  3. use App\Libs\Utils;
  4. use Exception;
  5. use GuzzleHttp\Client;
  6. use GuzzleHttp\Exception\GuzzleException;
  7. /**
  8. * Vivo推送
  9. * 服务端接口文档地址:https://dev.vivo.com.cn/documentCenter/doc/362
  10. * Class VPush
  11. * @package App\Libs\Push\VPush
  12. */
  13. class VPush
  14. {
  15. // App基本配置
  16. private $_appId;
  17. private $_appKey;
  18. private $_appSecret;
  19. // 授权token
  20. private $_accessToken = '';
  21. // 推送内容及推送用户
  22. private $_title = '';
  23. private $_content = '';
  24. private $_skipContent = '';
  25. private $_regId = [];
  26. // 消息体的id
  27. private $_taskId = '';
  28. // 推送设置
  29. private $_notifyType = 4;
  30. private $_timeToLive = 86400;
  31. private $_skipType = 4;
  32. private $_networkType = -1;
  33. public function __construct($appId, $appKey, $appSecret)
  34. {
  35. $this->_appId = $appId;
  36. $this->_appKey = $appKey;
  37. $this->_appSecret = $appSecret;
  38. }
  39. /**
  40. * 设置推送消息
  41. * @param $title
  42. * @param $content
  43. * @param $url
  44. * @param array $regId
  45. */
  46. public function setPushData($title, $content, $url, $regId = [])
  47. {
  48. // 赋值
  49. $this->_title = $title;
  50. $this->_content = $content;
  51. $this->_skipContent = $url;
  52. $this->_regId = $regId;
  53. }
  54. /**
  55. * 保存群推消息公共体接口
  56. * @return mixed
  57. * @throws GuzzleException
  58. * @throws Exception
  59. */
  60. public function saveListPayload()
  61. {
  62. // 校验参数
  63. $this->checkParam();
  64. $data = [
  65. 'requestId' => Utils::randCode(),
  66. 'title' => $this->_title,
  67. 'content' => $this->_content,
  68. 'notifyType' => $this->_notifyType,
  69. 'timeToLive' => $this->_timeToLive,
  70. 'skipType' => $this->_skipType,
  71. 'skipContent' => $this->_skipContent,
  72. 'networkType' => $this->_networkType,
  73. 'clientCustomMap' => (object)[],
  74. ];
  75. // 请求服务接口
  76. $result = $this->getData(config('push.server.vivo.saveListPayload'), $data);
  77. $this->_taskId = getProp($result, 'taskId');
  78. return $result;
  79. }
  80. /**
  81. * 批量推送用户接口
  82. * @return mixed
  83. * @throws GuzzleException
  84. */
  85. public function sendMessage()
  86. {
  87. // 参数判断
  88. $this->checkParam();
  89. if (!$this->_taskId) {
  90. throw new Exception('VIVO推送必须要设置taskId');
  91. }
  92. if (!$this->_regId) {
  93. throw new Exception('VIVO推送必须要设置regId');
  94. }
  95. $data = [
  96. 'regIds' => $this->_regId,
  97. 'taskId' => $this->_taskId,
  98. 'requestId' => Utils::randCode(),
  99. ];
  100. return $this->getData(config('push.server.vivo.pushToList'), $data);
  101. }
  102. /**
  103. * 全量发送(默认是每个app每日可发送一条。)
  104. * @return mixed
  105. * @throws GuzzleException
  106. */
  107. public function sendAll()
  108. {
  109. // 校验参数
  110. $this->checkParam();
  111. $data = [
  112. 'requestId' => Utils::randCode(),
  113. 'title' => $this->_title,
  114. 'content' => $this->_content,
  115. 'notifyType' => $this->_notifyType,
  116. 'timeToLive' => $this->_timeToLive,
  117. 'skipType' => $this->_skipType,
  118. 'skipContent' => $this->_skipContent,
  119. 'networkType' => $this->_networkType,
  120. 'clientCustomMap' => (object)[],
  121. ];
  122. return $this->getData(config('push.server.vivo.sendAll'), $data);
  123. }
  124. public function addTagForUsers($tag, $regIds)
  125. {
  126. if (empty($tag) || empty($regIds)) {
  127. return false;
  128. }
  129. // token授权
  130. $this->getAccessToken();
  131. }
  132. /**
  133. * 获取消息推送的统计值接口
  134. * @param $taskIds
  135. * @return array|mixed
  136. * @throws GuzzleException
  137. */
  138. public function getStatistics($taskIds)
  139. {
  140. if (empty($taskIds)) {
  141. return [];
  142. }
  143. // token授权
  144. $this->getAccessToken();
  145. // 拼接链接
  146. $url = config('push.server.vivo.getStatistics');
  147. $url .= '?taskIds=' . implode(',', $taskIds);
  148. return $this->getData($url, [], 'GET');
  149. }
  150. /**
  151. * 校验参数
  152. * @return $this
  153. * @throws GuzzleException
  154. */
  155. private function checkParam()
  156. {
  157. // 授权token校验
  158. if (!$this->_accessToken) {
  159. $this->getAccessToken();
  160. }
  161. if (!$this->_appId) {
  162. throw new Exception('VIVO推送必须要设置appId');
  163. }
  164. if (!$this->_appKey) {
  165. throw new Exception('VIVO推送必须要设置appKey');
  166. }
  167. if (!$this->_appSecret) {
  168. throw new Exception('VIVO推送必须要设置appSecret');
  169. }
  170. if (!$this->_accessToken) {
  171. throw new Exception('VIVO推送必须要设置AccessToken');
  172. }
  173. if (!$this->_title) {
  174. throw new Exception('VIVO推送必须要设置title');
  175. }
  176. if (!$this->_content) {
  177. throw new Exception('VIVO推送必须要设置content');
  178. }
  179. if (!$this->_timeToLive) {
  180. throw new Exception('VIVO推送必须要设置timeToLive');
  181. }
  182. return $this;
  183. }
  184. /**
  185. * 授权
  186. * @return $this
  187. * @throws GuzzleException
  188. */
  189. private function getAccessToken()
  190. {
  191. $sendData = [
  192. 'appId' => $this->_appId,
  193. 'appKey' => $this->_appKey,
  194. 'timestamp' => time() . '000',
  195. ];
  196. $sign = md5($sendData['appId'] . $sendData['appKey'] . $sendData['timestamp'] . $this->_appSecret);
  197. $sendData['sign'] = $sign;
  198. $url = config('push.server.vivo.auth');
  199. $data = $this->getData($url, $sendData);
  200. $this->_accessToken = getProp($data, 'authToken');
  201. return $this;
  202. }
  203. /**
  204. * 请求数据
  205. * @param $url
  206. * @param $sendData
  207. * @param string $method
  208. * @return mixed
  209. * @throws GuzzleException
  210. */
  211. private function getData($url, $sendData, $method = 'POST')
  212. {
  213. // 组装请求数据
  214. $authToken = $this->_accessToken;
  215. $option = ['json' => $sendData];
  216. // 请求
  217. $client = new Client(['timeout' => 5.0, 'headers' => ['authToken' => $authToken]]);
  218. if ($method === 'POST') {
  219. $response = $client->request('POST', $url, $option);
  220. } else {
  221. $response = $client->request('GET', $url);
  222. }
  223. $body = $response->getBody()->__toString();
  224. $result = json_decode($body, true);
  225. myLog('push')->info('[VIVO] getDataByInfo', compact('url', 'option', 'authToken', 'result'));
  226. return $result;
  227. }
  228. }