VPush.php 9.4 KB

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