VPush.php 7.1 KB

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