VPush.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. /**
  125. * 标签推
  126. * @param $tag
  127. * @return mixed
  128. * @throws GuzzleException
  129. */
  130. public function tagPush($tag)
  131. {
  132. // 校验参数
  133. $this->checkParam();
  134. $data = [
  135. 'requestId' => Utils::randCode(),
  136. 'title' => $this->_title,
  137. 'content' => $this->_content,
  138. 'notifyType' => $this->_notifyType,
  139. 'timeToLive' => $this->_timeToLive,
  140. 'skipType' => $this->_skipType,
  141. 'skipContent' => $this->_skipContent,
  142. 'networkType' => $this->_networkType,
  143. 'clientCustomMap' => (object)[],
  144. 'tagExpression' => [
  145. 'andTags' => [$tag],
  146. 'notTags' => [],
  147. 'orTags' => []
  148. ]
  149. ];
  150. return $this->getData(config('push.server.vivo.tagPush'), $data, 'POST');
  151. }
  152. /**
  153. * 新增标签
  154. * @param $tag
  155. * @return false|mixed
  156. * @throws GuzzleException
  157. */
  158. public function addTag($tag)
  159. {
  160. if (empty($tag)) {
  161. return false;
  162. }
  163. // token授权
  164. $this->getAccessToken();
  165. // 拼接链接
  166. $url = config('push.server.vivo.addTag');
  167. return $this->getData($url, [
  168. 'name' => $tag
  169. ], 'POST');
  170. }
  171. /**
  172. * 给用户绑定tag
  173. * @param $tag
  174. * @param $regIds
  175. * @return false|mixed
  176. * @throws GuzzleException
  177. */
  178. public function addTagForUsers($tag, $regIds)
  179. {
  180. if (empty($tag) || empty($regIds)) {
  181. return false;
  182. }
  183. // token授权
  184. $this->getAccessToken();
  185. // 拼接链接
  186. $url = config('push.server.vivo.addMembers');
  187. return $this->getData($url, [
  188. 'name' => $tag,
  189. 'type' => 1,
  190. 'ids' => $regIds
  191. ], 'POST');
  192. }
  193. /**
  194. * 获取消息推送的统计值接口
  195. * @param $taskIds
  196. * @return array|mixed
  197. * @throws GuzzleException
  198. */
  199. public function getStatistics($taskIds)
  200. {
  201. if (empty($taskIds)) {
  202. return [];
  203. }
  204. // token授权
  205. $this->getAccessToken();
  206. // 拼接链接
  207. $url = config('push.server.vivo.getStatistics');
  208. $url .= '?taskIds=' . implode(',', $taskIds);
  209. return $this->getData($url, [], 'GET');
  210. }
  211. /**
  212. * 校验参数
  213. * @return $this
  214. * @throws GuzzleException
  215. */
  216. private function checkParam()
  217. {
  218. // 授权token校验
  219. if (!$this->_accessToken) {
  220. $this->getAccessToken();
  221. }
  222. if (!$this->_appId) {
  223. throw new Exception('VIVO推送必须要设置appId');
  224. }
  225. if (!$this->_appKey) {
  226. throw new Exception('VIVO推送必须要设置appKey');
  227. }
  228. if (!$this->_appSecret) {
  229. throw new Exception('VIVO推送必须要设置appSecret');
  230. }
  231. if (!$this->_accessToken) {
  232. throw new Exception('VIVO推送必须要设置AccessToken');
  233. }
  234. if (!$this->_title) {
  235. throw new Exception('VIVO推送必须要设置title');
  236. }
  237. if (!$this->_content) {
  238. throw new Exception('VIVO推送必须要设置content');
  239. }
  240. if (!$this->_timeToLive) {
  241. throw new Exception('VIVO推送必须要设置timeToLive');
  242. }
  243. return $this;
  244. }
  245. /**
  246. * 授权
  247. * @return $this
  248. * @throws GuzzleException
  249. */
  250. private function getAccessToken()
  251. {
  252. $sendData = [
  253. 'appId' => $this->_appId,
  254. 'appKey' => $this->_appKey,
  255. 'timestamp' => time() . '000',
  256. ];
  257. $sign = md5($sendData['appId'] . $sendData['appKey'] . $sendData['timestamp'] . $this->_appSecret);
  258. $sendData['sign'] = $sign;
  259. $url = config('push.server.vivo.auth');
  260. $data = $this->getData($url, $sendData);
  261. $this->_accessToken = getProp($data, 'authToken');
  262. return $this;
  263. }
  264. /**
  265. * 请求数据
  266. * @param $url
  267. * @param $sendData
  268. * @param string $method
  269. * @return mixed
  270. * @throws GuzzleException
  271. */
  272. private function getData($url, $sendData, $method = 'POST')
  273. {
  274. // 组装请求数据
  275. $authToken = $this->_accessToken;
  276. $option = ['json' => $sendData];
  277. // 请求
  278. $client = new Client(['timeout' => 5.0, 'headers' => ['authToken' => $authToken]]);
  279. if ($method === 'POST') {
  280. $response = $client->request('POST', $url, $option);
  281. } else {
  282. $response = $client->request('GET', $url);
  283. }
  284. $body = $response->getBody()->__toString();
  285. $result = json_decode($body, true);
  286. myLog('push')->info('[VIVO] getDataByInfo', compact('url', 'option', 'authToken', 'result'));
  287. return $result;
  288. }
  289. }