VPush.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 $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' => [],
  75. ];
  76. // 请求服务接口
  77. $result = $this->getData(config('push.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. $data = [
  94. 'regIds' => $this->_regId,
  95. 'taskId' => $this->_taskId,
  96. 'requestId' => Utils::randCode(),
  97. ];
  98. return $this->getData(config('push.vivo.pushToList'), $data);
  99. }
  100. /**
  101. * @return mixed
  102. * @throws GuzzleException
  103. */
  104. public function sendAll()
  105. {
  106. // 校验参数
  107. $this->checkParam();
  108. $data = [
  109. 'requestId' => Utils::randCode(),
  110. 'title' => $this->_title,
  111. 'content' => $this->_content,
  112. 'notifyType' => $this->_notifyType,
  113. 'timeToLive' => $this->_timeToLive,
  114. 'skipType' => $this->_skipType,
  115. 'skipContent' => $this->_skipContent,
  116. 'networkType' => $this->_networkType,
  117. 'clientCustomMap' => [],
  118. ];
  119. return $this->getData(config('push.vivo.sendAll'), $data);
  120. }
  121. public function getStatistics()
  122. {
  123. }
  124. /**
  125. * 校验参数
  126. * @return $this
  127. * @throws GuzzleException
  128. */
  129. private function checkParam()
  130. {
  131. // 授权token校验
  132. if ($this->_accessToken) {
  133. $this->getAccessToken();
  134. }
  135. if (!$this->_appId) {
  136. throw new InvalidArgumentException('VIVO推送必须要设置appId');
  137. }
  138. if (!$this->_appKey) {
  139. throw new InvalidArgumentException('VIVO推送必须要设置appKey');
  140. }
  141. if (!$this->_appSecret) {
  142. throw new InvalidArgumentException('VIVO推送必须要设置appSecret');
  143. }
  144. if (!$this->_accessToken) {
  145. throw new InvalidArgumentException('VIVO推送必须要设置AccessToken');
  146. }
  147. if (!$this->_title) {
  148. throw new InvalidArgumentException('VIVO推送必须要设置title');
  149. }
  150. if (!$this->_content) {
  151. throw new InvalidArgumentException('VIVO推送必须要设置content');
  152. }
  153. if (!$this->_regId) {
  154. throw new InvalidArgumentException('VIVO推送必须要设置regId');
  155. }
  156. if (!$this->_timeToLive) {
  157. throw new InvalidArgumentException('VIVO推送必须要设置timeToLive');
  158. }
  159. return $this;
  160. }
  161. /**
  162. * 授权
  163. * @return $this
  164. * @throws GuzzleException
  165. */
  166. private function getAccessToken()
  167. {
  168. $sendData = [
  169. 'appId' => $this->_appId,
  170. 'appKey' => $this->_appKey,
  171. 'timestamp' => time() . '000',
  172. ];
  173. $sign = md5($sendData['appId'] . $sendData['appKey'] . $sendData['timestamp'] . $this->_appSecret);
  174. $sendData['sign'] = $sign;
  175. $url = config('push.vivo.auth');
  176. $data = $this->getData($url, $sendData);
  177. $this->_accessToken = getProp($data, 'authToken');
  178. return $this;
  179. }
  180. /**
  181. * 请求数据
  182. * @param $url
  183. * @param $sendData
  184. * @param string $method
  185. * @return mixed
  186. * @throws GuzzleException
  187. */
  188. private function getData($url, $sendData, $method = 'POST')
  189. {
  190. // 组装请求数据
  191. $option = ['json' => $sendData];
  192. if ($this->_accessToken) {
  193. $option['headers']['auth_token'] = $this->_accessToken;
  194. }
  195. // 请求
  196. $client = new Client(['timeout' => 10.0]);
  197. if ($method === 'POST') {
  198. $response = $client->request('POST', $url, $option);
  199. } else {
  200. $response = $client->request('GET', $url);
  201. }
  202. $body = $response->getBody();
  203. $result = json_decode($body, true);
  204. myLog('getDataByInfo', ['url' => $url, 'option' => $option, 'res' => $result]);
  205. return $result;
  206. }
  207. }