PushService.php 13 KB

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