VPush.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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' => (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(true);
  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.server.vivo.pushToList'), $data);
  99. }
  100. /**
  101. * 全量发送(默认是每个app每日可发送一条。)
  102. * @return mixed
  103. * @throws GuzzleException
  104. */
  105. public function sendAll()
  106. {
  107. // 校验参数
  108. $this->checkSendAllParam();
  109. $data = [
  110. 'requestId' => Utils::randCode(),
  111. 'title' => $this->_title,
  112. 'content' => $this->_content,
  113. 'notifyType' => $this->_notifyType,
  114. 'timeToLive' => $this->_timeToLive,
  115. 'skipType' => $this->_skipType,
  116. 'skipContent' => $this->_skipContent,
  117. 'networkType' => $this->_networkType,
  118. 'clientCustomMap' => (object)[],
  119. ];
  120. return $this->getData(config('push.server.vivo.sendAll'), $data);
  121. }
  122. public function addTagForUsers($tag, $regIds)
  123. {
  124. if (empty($tag) || empty($regIds)) {
  125. return false;
  126. }
  127. // token授权
  128. $this->getAccessToken();
  129. }
  130. /**
  131. * 获取消息推送的统计值接口
  132. * @param $taskIds
  133. * @return array|mixed
  134. * @throws GuzzleException
  135. */
  136. public function getStatistics($taskIds)
  137. {
  138. if (empty($taskIds)) {
  139. return [];
  140. }
  141. // token授权
  142. $this->getAccessToken();
  143. // 拼接链接
  144. $url = config('push.server.vivo.getStatistics');
  145. $url .= '?taskIds=' . implode(',', $taskIds);
  146. return $this->getData($url, [], 'GET');
  147. }
  148. /**
  149. * 校验参数
  150. * @return $this
  151. * @throws GuzzleException
  152. */
  153. private function checkParam()
  154. {
  155. // 授权token校验
  156. if (!$this->_accessToken) {
  157. $this->getAccessToken();
  158. }
  159. if (!$this->_appId) {
  160. throw new InvalidArgumentException('VIVO推送必须要设置appId');
  161. }
  162. if (!$this->_appKey) {
  163. throw new InvalidArgumentException('VIVO推送必须要设置appKey');
  164. }
  165. if (!$this->_appSecret) {
  166. throw new InvalidArgumentException('VIVO推送必须要设置appSecret');
  167. }
  168. if (!$this->_accessToken) {
  169. throw new InvalidArgumentException('VIVO推送必须要设置AccessToken');
  170. }
  171. if (!$this->_title) {
  172. throw new InvalidArgumentException('VIVO推送必须要设置title');
  173. }
  174. if (!$this->_content) {
  175. throw new InvalidArgumentException('VIVO推送必须要设置content');
  176. }
  177. if (!$this->_timeToLive) {
  178. throw new InvalidArgumentException('VIVO推送必须要设置timeToLive');
  179. }
  180. if (!$this->_regId) {
  181. throw new InvalidArgumentException('VIVO推送必须要设置regId');
  182. }
  183. return $this;
  184. }
  185. /**
  186. * 校验参数
  187. * @param false $sendAll
  188. * @return $this
  189. * @throws GuzzleException
  190. */
  191. private function checkSendAllParam()
  192. {
  193. // 授权token校验
  194. if (!$this->_accessToken) {
  195. $this->getAccessToken();
  196. }
  197. if (!$this->_appId) {
  198. throw new InvalidArgumentException('VIVO推送必须要设置appId');
  199. }
  200. if (!$this->_appKey) {
  201. throw new InvalidArgumentException('VIVO推送必须要设置appKey');
  202. }
  203. if (!$this->_appSecret) {
  204. throw new InvalidArgumentException('VIVO推送必须要设置appSecret');
  205. }
  206. if (!$this->_accessToken) {
  207. throw new InvalidArgumentException('VIVO推送必须要设置AccessToken');
  208. }
  209. if (!$this->_title) {
  210. throw new InvalidArgumentException('VIVO推送必须要设置title');
  211. }
  212. if (!$this->_content) {
  213. throw new InvalidArgumentException('VIVO推送必须要设置content');
  214. }
  215. if (!$this->_timeToLive) {
  216. throw new InvalidArgumentException('VIVO推送必须要设置timeToLive');
  217. }
  218. return $this;
  219. }
  220. /**
  221. * 授权
  222. * @return $this
  223. * @throws GuzzleException
  224. */
  225. private function getAccessToken()
  226. {
  227. $sendData = [
  228. 'appId' => $this->_appId,
  229. 'appKey' => $this->_appKey,
  230. 'timestamp' => time() . '000',
  231. ];
  232. $sign = md5($sendData['appId'] . $sendData['appKey'] . $sendData['timestamp'] . $this->_appSecret);
  233. $sendData['sign'] = $sign;
  234. $url = config('push.server.vivo.auth');
  235. $data = $this->getData($url, $sendData);
  236. $this->_accessToken = getProp($data, 'authToken');
  237. return $this;
  238. }
  239. /**
  240. * 请求数据
  241. * @param $url
  242. * @param $sendData
  243. * @param string $method
  244. * @return mixed
  245. * @throws GuzzleException
  246. */
  247. private function getData($url, $sendData, $method = 'POST')
  248. {
  249. // 组装请求数据
  250. $authToken = $this->_accessToken;
  251. $option = ['json' => $sendData];
  252. // 请求
  253. $client = new Client(['timeout' => 5.0, 'headers' => ['authToken' => $authToken]]);
  254. if ($method === 'POST') {
  255. $response = $client->request('POST', $url, $option);
  256. } else {
  257. $response = $client->request('GET', $url);
  258. }
  259. $body = $response->getBody()->__toString();
  260. $result = json_decode($body, true);
  261. myLog('push')->info('[VIVO] getDataByInfo', compact('url', 'option', 'authToken', 'result'));
  262. return $result;
  263. }
  264. }