PushService.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. <?php
  2. namespace App\Modules\Push\Services;
  3. use Exception;
  4. use App\Cache\PushCache;
  5. use App\Consts\PushConst;
  6. use App\Libs\Push\OPPOPush\OPPOPushCommon;
  7. use App\Libs\Push\XMPush\MiPushCommon;
  8. use App\Libs\Push\HuaWei\HwPushCommon;
  9. use App\Modules\Push\Models\QappPushApp;
  10. use App\Modules\Push\Models\QappPushTask;
  11. use App\Modules\Push\Models\QappPushTaskLogs;
  12. use App\Modules\Push\Models\QappPushTaskUsers;
  13. use App\Modules\Push\Models\QappPushUser;
  14. use App\Modules\User\Models\QappPackage;
  15. use GuzzleHttp\Exception\GuzzleException;
  16. class PushService
  17. {
  18. /**
  19. * 设置用户reg_id
  20. * @param $uid
  21. * @param string $regId
  22. * @param string $provider
  23. * @param string $package
  24. * @return bool
  25. */
  26. public static function setUserRegId($uid, string $regId, string $provider, string $package): bool
  27. {
  28. // push服务商(转小写)
  29. $provider = strtolower($provider);
  30. if (empty($uid) || empty($regId) || empty($package) || empty($provider) || $package !== 'com.app.kyy.xnyd') {
  31. return false;
  32. }
  33. myLog('push')->info('setUserRegId', compact('uid', 'regId', 'provider', 'package'));
  34. // 获取缓存
  35. $userCacheRegId = PushCache::getUserPushRegId($uid);
  36. if ($regId === $userCacheRegId) {
  37. return true;
  38. }
  39. // 获取包信息
  40. $packageInfo = QappPackage::getPackageByPackage($package);
  41. $packageId = getProp($packageInfo, 'id');
  42. $channelId = getProp($packageInfo, 'channel_id');
  43. $company = getProp($packageInfo, 'company');
  44. myLog('push')->info('setUserRegId', compact('packageId', 'channelId', 'company'));
  45. if (empty($packageId)) {
  46. return false;
  47. }
  48. // 获取app信息
  49. $pushApp = QappPushApp::getPushAppByPackageIdAndProvider($packageId, $provider);
  50. $pushAppId = getProp($pushApp, 'app_id');
  51. myLog('push')->info('setUserRegId', compact('pushAppId'));
  52. if (!$pushAppId) {
  53. return false;
  54. }
  55. // 设置
  56. $pushUser = QappPushUser::getPushUserByUid($uid);
  57. $pushUserRegId = (string)getProp($pushUser, 'reg_id');
  58. myLog('push')->info('setUserRegId', compact('pushUserRegId'));
  59. if ($pushUserRegId === $regId) {
  60. PushCache::setUserPushRegId($uid, $regId);
  61. return true;
  62. }
  63. // 初始化用户Push信息
  64. if (!$pushUser) {
  65. // 初始化push_users
  66. QappPushUser::initPushUser($uid, $regId, $pushAppId, $provider, $channelId);
  67. // 针对华为用户需要主动订阅topic,方便后续全量推送
  68. if ($provider === PushConst::PROVIDER_HW) {
  69. $client = new HwPushCommon($pushAppId, getProp($pushApp, 'app_secret'));
  70. $client->subscribeTopic(PushConst::TOPIC_ALL, [$regId]);
  71. }
  72. }
  73. // 更新用户数据库中reg_id
  74. $result = QappPushUser::setUserRegId($uid, $regId);
  75. if ($result) {
  76. // 更新缓存
  77. PushCache::setUserPushRegId($uid, $regId);
  78. }
  79. return $result;
  80. }
  81. /**
  82. * 推送消息
  83. * @param $taskId
  84. * @return bool
  85. * @throws GuzzleException
  86. */
  87. public static function pushMessageByTaskId($taskId): bool
  88. {
  89. if (empty($taskId)) {
  90. return false;
  91. }
  92. // 获取任务数据,判断任务状态及发送时间
  93. $pushTask = QappPushTask::getPushTaskById($taskId);
  94. if ((int)getProp($pushTask, 'status') !== PushConst::STATUS_TODO ||
  95. (int)getProp($pushTask, 'select_user_status') !== PushConst::SELECT_USER_OK ||
  96. getProp($pushTask, 'push_time') > date('Y-m-d H:i:s')) {
  97. return false;
  98. }
  99. // 获取全部子任务,判断子任务是否有效
  100. $subTasks = QappPushTaskLogs::getPushTaskLogsByTaskId($taskId);
  101. if (!$subTasks) {
  102. // 更新主任务失败状态及原因
  103. QappPushTask::updateMainTaskStatus($taskId, PushConst::STATUS_FAIL, '无有效子任务');
  104. return false;
  105. }
  106. // 获取推送应用信息
  107. $pushAppIds = array_column($subTasks, 'app_id');
  108. $pushApps = QappPushApp::getPushAppByAppIds($pushAppIds);
  109. if (!$pushApps) {
  110. // 更新主任务失败状态及原因
  111. QappPushTask::updateMainTaskStatus($taskId, PushConst::STATUS_FAIL, '无有效推送APP');
  112. return false;
  113. }
  114. // 更新主任务状态为开始状态
  115. QappPushTask::updateMainTaskStatus($taskId, PushConst::STATUS_DOING, '无有效推送APP');
  116. // 全量发送走标签,否则走批量
  117. if (getProp($pushTask, 'push_filter') === PushConst::FILTER_ALL_USERS) {
  118. $result = self::pushMessageToAll($pushTask, $subTasks, $pushApps);
  119. } else {
  120. $result = self::pushMessageToUsers($pushTask, $subTasks, $pushApps);
  121. }
  122. // 更新主任务最终状态(成功/失败)
  123. $status = $result ? PushConst::STATUS_SUCCESS : PushConst::STATUS_FAIL;
  124. QappPushTask::updateMainTaskStatus($taskId, $status, $result);
  125. return $result;
  126. }
  127. /**
  128. * 全部发送
  129. * @param $pushTask
  130. * @param $subTasks
  131. * @param $pushApps
  132. * @return bool
  133. * @throws GuzzleException
  134. */
  135. private static function pushMessageToAll($pushTask, $subTasks, $pushApps): bool
  136. {
  137. if (empty($pushTask) || empty($subTasks) || empty($pushApps)) {
  138. return false;
  139. }
  140. // 推送结果、推送内容
  141. $pushResult = true;
  142. $title = getProp($pushTask, 'title');
  143. $content = getProp($pushTask, 'content');
  144. $url = getProp($pushTask, 'url');
  145. // 循环群发
  146. foreach ($subTasks as $subTask) {
  147. $appId = getProp($subTask, 'app_id');
  148. $subTaskId = getProp($subTask, 'id');
  149. $pushApp = collect($pushApps)->firstWhere('app_id', $appId);
  150. if (empty($pushApp)) {
  151. continue;
  152. }
  153. // push app相关
  154. $provider = getProp($pushApp, 'provider');
  155. $package = getProp($pushApp, 'package');
  156. $appId = getProp($pushApp, 'app_id');
  157. $appSecret = getProp($pushApp, 'app_secret');
  158. $appKey = getProp($pushApp, 'app_key');
  159. $masterSecret = getProp($pushApp, 'master_secret');
  160. $topic = PushConst::TOPIC_ALL;
  161. // 更新开始状态
  162. QappPushTaskLogs::updateSubTaskStatus($subTaskId, PushConst::STATUS_DOING);
  163. $result = [];
  164. try {
  165. // 针对渠道做不同处理
  166. switch ($provider) {
  167. // 华为
  168. case PushConst::PROVIDER_HW:
  169. // 开发状态还是生产状态
  170. $target = env('APP_ENV') === 'production' ? 2 : 1;
  171. // 设置相关配置
  172. $client = new HwPushCommon($appId, $appSecret);
  173. $client->setFastAppTarget($target);
  174. $client->setTopic($topic);
  175. $client->setBigTag('Task_' . getProp($pushTask, 'id'));
  176. // 推送
  177. $result = $client->sendPushMessage($title, $content, $url);
  178. break;
  179. // 小米
  180. case PushConst::PROVIDER_MI:
  181. $client = new MiPushCommon($package, $appSecret);
  182. $result = $client->sendMessageToAll($title, $content, $url);
  183. break;
  184. // OPPO
  185. case PushConst::PROVIDER_OPPO:
  186. $client = new OPPOPushCommon($appKey, $masterSecret);
  187. $messageId = $client->getMessageId($title, $content, $url);
  188. $result = $client->broadCastAll($messageId);
  189. break;
  190. }
  191. } catch (Exception $e) {
  192. // 最终结果
  193. $pushResult = 0 && $pushResult;
  194. // 更新子任务失败状态
  195. QappPushTaskLogs::updateSubTaskStatus($subTaskId, PushConst::STATUS_FAIL, $result);
  196. continue;
  197. }
  198. // 更新成功状态
  199. QappPushTaskLogs::updateSubTaskStatus($subTaskId, PushConst::STATUS_SUCCESS, $result);
  200. }
  201. return $pushResult;
  202. }
  203. /**
  204. * @param $pushTask
  205. * @param $subTasks
  206. * @param $pushApps
  207. * @return bool
  208. * @throws GuzzleException
  209. */
  210. private static function pushMessageToUsers($pushTask, $subTasks, $pushApps)
  211. {
  212. // 获取需要推送的人
  213. $taskId = getProp($pushTask, 'id');
  214. $taskUsers = QappPushTaskUsers::getTaskUsers($taskId);
  215. if (!$taskUsers) {
  216. // 更新主任务失败状态及原因
  217. QappPushTask::updateMainTaskStatus($taskId, PushConst::STATUS_FAIL, '未设置推送用户');
  218. return false;
  219. }
  220. // 推送结果、推送内容
  221. $pushResult = true;
  222. $title = getProp($pushTask, 'title');
  223. $content = getProp($pushTask, 'content');
  224. $url = getProp($pushTask, 'url');
  225. // 推送
  226. foreach ($subTasks as $subTask) {
  227. $subTaskId = getProp($subTask, 'id');
  228. $appId = getProp($subTask, 'app_id');
  229. $pushApp = collect($pushApps)->firstWhere('app_id', $appId);
  230. $uids = collect($taskUsers)->where('app_id', $appId)->pluck('uid');
  231. $users = QappPushUser::getPushUserByUids($uids);
  232. $regIds = array_column($users, 'reg_id');
  233. // push app相关
  234. $provider = getProp($pushApp, 'provider');
  235. $package = getProp($pushApp, 'package');
  236. $appId = getProp($pushApp, 'app_id');
  237. $appSecret = getProp($pushApp, 'app_secret');
  238. $appKey = getProp($pushApp, 'app_key');
  239. $masterSecret = getProp($pushApp, 'master_secret');
  240. // 更新开始状态
  241. QappPushTaskLogs::updateSubTaskStatus($subTaskId, PushConst::STATUS_DOING);
  242. // 循环批量
  243. $regIdArr = array_chunk($regIds, 1000);
  244. try {
  245. switch ($provider) {
  246. // 华为
  247. case PushConst::PROVIDER_HW:
  248. // 初始化huawei推送
  249. $client = new HwPushCommon($appId, $appSecret);
  250. // 循环推送
  251. $i = 0;
  252. foreach ($regIdArr as $regIdList) {
  253. $client->setToken($regIdList);
  254. $result = $client->sendPushMessage($title, $content, $url);
  255. // 一般的应用默认消息流控是针对单个应用3000QPS(每秒不能超过3000个token数)
  256. $i++;
  257. if ($i % 3 === 0) {
  258. sleep(1);
  259. }
  260. }
  261. break;
  262. // 小米
  263. case PushConst::PROVIDER_MI:
  264. // 初始化小米推送
  265. $client = new MiPushCommon($package, $appSecret);
  266. // 循环推送
  267. $i = 0;
  268. foreach ($regIdArr as $regIdList) {
  269. $client->setRegArr($regIdList);
  270. $result = $client->sendMessage($title, $content, $url);
  271. // 一般的应用默认消息流控是针对单个应用3000QPS(每秒不能超过3000个token数)
  272. $i++;
  273. if ($i % 3 === 0) {
  274. sleep(1);
  275. }
  276. }
  277. break;
  278. // OPPO
  279. case PushConst::PROVIDER_OPPO:
  280. // 初始化oppo推送
  281. $client = new OPPOPushCommon($appKey, $masterSecret);
  282. $messageId = $client->getMessageId($title, $content, $url);
  283. // 循环推送
  284. $i = 0;
  285. foreach ($regIdArr as $regIdList) {
  286. $client->setRegArr($regIdList);
  287. $result = $client->broadCastRegIds($messageId);
  288. // 一般的应用默认消息流控是针对单个应用3000QPS(每秒不能超过3000个token数)
  289. $i++;
  290. if ($i % 3 === 0) {
  291. sleep(1);
  292. }
  293. }
  294. break;
  295. }
  296. } catch (Exception $e) {
  297. // 最终结果
  298. $pushResult = 0 && $pushResult;
  299. // 更新子任务失败状态
  300. QappPushTaskLogs::updateSubTaskStatus($subTaskId, PushConst::STATUS_FAIL, $result);
  301. continue;
  302. }
  303. // 更新成功状态
  304. QappPushTaskLogs::updateSubTaskStatus($subTaskId, PushConst::STATUS_SUCCESS, $result);
  305. }
  306. return $pushResult;
  307. }
  308. }