NoticeService.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. <?php
  2. namespace App\Services\User;
  3. use App\Exceptions\AlertException;
  4. use App\Http\Controllers\Core\User;
  5. use App\Http\Controllers\Wechat\Kfaccount;
  6. use App\Jobs\NoticeJob;
  7. use App\Models\Common\TokenModel;
  8. use App\Models\Deed\EachLikeModel;
  9. use App\Models\Deed\InvitationCardModel;
  10. use App\Models\NoticeLogModel;
  11. use App\Models\NoticeModel;
  12. use App\Models\PraiseModel;
  13. use App\Models\User\SuperLikeModel;
  14. use App\Models\User\UserModel;
  15. use App\Services\Log\NoticeLogService;
  16. use App\Services\Service;
  17. use Illuminate\Support\Facades\Config;
  18. use Illuminate\Support\Facades\Redis;
  19. use PocketBE\MsyPush\Jobs\RegistEventJob;
  20. /**
  21. * Class NoticeService
  22. * @package App\Services\User
  23. */
  24. class NoticeService extends Service
  25. {
  26. /**
  27. * 心动通知延迟
  28. */
  29. public function thumbMeDelay()
  30. {
  31. $json = Redis::lpop("{fpdx:thumbme:notice:dely}");
  32. while (!empty($json)) {
  33. try {
  34. $data = json_decode($json, true);
  35. $this->thumbMe($data['uid'], $data['partner_id'], true);
  36. $json = Redis::lpop("{fpdx:thumbme:notice:dely}");
  37. } catch (\Exception $exception) {
  38. }
  39. }
  40. }
  41. /**
  42. * 喜欢我的卡片时通知
  43. * @param int $uid 触发者
  44. * @param int $partner_id 通知者卡片
  45. * @param bool $is_callback 是否作为回调函数
  46. * @return bool
  47. */
  48. public function thumbMe(int $uid, int $partner_id, bool $is_callback = true)
  49. {
  50. if (time() < mktime(8, 0, 0) || time() > mktime(23, 0, 0)) {
  51. // 延迟发送
  52. Redis::rpush("{fpdx:thumbme:notice:dely}", [json_encode(['uid' => $uid, 'partner_id' => $partner_id])]);
  53. return true;
  54. }
  55. /** @var UserModel $to_user */
  56. $to_user = UserModel::where('partner_id', $partner_id)->firstOrFail();
  57. $from_user = UserModel::find($uid);
  58. $to_uid = $to_user->uid;
  59. // 三天内的点赞次数
  60. $praise_count = PraiseModel::where([
  61. ['partner_id', $to_user->partner_id],
  62. ['created_at', '>', strtotime("-3 day")],
  63. ])->count();
  64. $invite_count = InvitationCardModel::where([
  65. ['invite_uid', $to_user->uid],
  66. ['created_at', '>', $to_user->login_at],
  67. ])->count();
  68. $description = "";
  69. if ($from_user->partner_id) {
  70. $from_user_praise = PraiseModel::where([
  71. ['partner_id', $from_user->partner_id],
  72. ])->count();
  73. if ($from_user_praise > 10) {
  74. switch ($from_user->sex) {
  75. case "1":
  76. $gender = "他";
  77. $appellation = "小姐姐";
  78. break;
  79. case "2":
  80. $gender = "她";
  81. $appellation = "小哥哥";
  82. break;
  83. default:
  84. $gender = "ta";
  85. $appellation = "人";
  86. break;
  87. }
  88. $description = "{$gender}被{$from_user_praise}个{$appellation}心动过!";
  89. }
  90. }
  91. // 通知队列
  92. $payload = [
  93. 'to_user' => $to_user->getAuth(),
  94. 'user' => $to_user->toArray(),
  95. 'from_user' => $from_user->toArray(),
  96. 'meta' => [
  97. 'praise_count' => $praise_count ? $praise_count - 1 : 0,
  98. 'invite_count' => $invite_count ? $invite_count - 1 : 0,
  99. 'date' => date('Y-m-d'),
  100. 'datetime' => date('Y-m-d H:i'),
  101. 'description' => $description,
  102. ],
  103. ];
  104. if ($is_callback) {
  105. dispatch(new RegistEventJob(100012, $payload))->onQueue("{push}");
  106. NoticeJob::dispatch($to_uid, array(new self(), __FUNCTION__), array($uid, $partner_id));
  107. return true;
  108. }
  109. dispatch(new RegistEventJob(100005, $payload))->onQueue("{push}");
  110. return true;
  111. }
  112. /**
  113. * 获取全局通知的数量
  114. * @param int $uid
  115. * @return array
  116. */
  117. public function noticeCnt(int $uid): array
  118. {
  119. $user = UserModel::findOrFail($uid);
  120. // 心动我的通知
  121. $like_me = PraiseModel::where([['read', 0], ['partner_id', $user->partner_id], ['type', 1]])->count();
  122. // 系统消息
  123. if (in_array(Config::get("platform"), ['ios', 'android'])) {
  124. // APP通知
  125. if (Config::get("version") < "2.17.6") {
  126. $shield = [20, 13, 14];
  127. } else {
  128. $shield = [20];
  129. }
  130. } else {
  131. // 小程序通知
  132. $shield = [5];
  133. }
  134. $system = NoticeModel::where([['uid', $uid], ['is_read', 0]])->whereIn('type', $shield)->count();
  135. // 我发出的邀请
  136. $send_invite = InvitationCardModel::where([['send_read', 0], ['uid', $uid]])->count();
  137. // 邀请我的
  138. $invite_me = InvitationCardModel::where([['read', 0], ['state', 0], ['invite_uid', $uid]])->count();
  139. // 邀请我的用户信息
  140. $invite_user = null;
  141. try {
  142. if ($invite_me > 0) {
  143. $invitation = InvitationCardModel::where([
  144. ['read', 0],
  145. ['state', 0],
  146. ['invite_uid', $uid],
  147. ])->OrderBy('id', 'desc')->first();
  148. $invite_user = UserModel::findOrFail($invitation->uid, ['uid', 'headimgurl', 'nickname']);
  149. }
  150. } catch (\Exception $exception) {
  151. $invite_user = null;
  152. }
  153. return array(
  154. 'like_me' => $like_me,
  155. 'system' => $system,
  156. 'send_invite' => $send_invite,
  157. 'invite_me' => $invite_me,
  158. 'invite_user' => $invite_user ?? null,
  159. );
  160. }
  161. /**
  162. * 会员加入通知
  163. * @param int $to_uid
  164. * @return bool
  165. */
  166. public function vip(int $to_uid)
  167. {
  168. $title = '会员加入通知';
  169. $uuid = uuid();
  170. $noticeLog = new NoticeLogService();
  171. try {
  172. $user = \App\Models\User\UserModel::findOrFail($to_uid);
  173. $public_id = config('wechat.fpdx.public_id');
  174. $openid = \DB::table('kddx_user_openid')->where('public_id', $public_id)->where(
  175. 'uid',
  176. $to_uid
  177. )->value('openid');
  178. // 客服消息
  179. // 限流
  180. if (
  181. time() > mktime(18, 0, 0) && NoticeLogModel::where([
  182. ['uid', $to_uid],
  183. ['title', $title],
  184. ['created_at', '>', mktime(18, 0, 0)],
  185. ['notice_type', '公众号客服消息'],
  186. ])->count() >= 3
  187. ) {
  188. $noticeLog->record($to_uid, $title, "发送频繁", $uuid);
  189. return false;
  190. } elseif (
  191. time() <= mktime(18, 0, 0) && NoticeLogModel::where([
  192. ['uid', $to_uid],
  193. ['title', $title],
  194. ['created_at', '>', mktime(18, 0, 0) - 86400],
  195. ['notice_type', '公众号客服消息'],
  196. ])->count() >= 3
  197. ) {
  198. $noticeLog->record($to_uid, $title, "发送频繁", $uuid);
  199. return false;
  200. }
  201. $kfcontent = "欢迎{$user->nickname}(ID:{$user->uid})成为我们的会员。【<a href='https://m.fenpeiduixiang.com/fpdx-attend/index.html#/vip/index'>点此查看</a>】会员特权";
  202. $result = Kfaccount::text([
  203. 'touser' => $openid,
  204. 'content' => $kfcontent,
  205. 'kf_account' => 'notice@admin',
  206. ]);
  207. if ($result['code'] == 0) {
  208. $noticeLog->record($to_uid, $title, "公众号客服消息", $uuid, 1, $kfcontent);
  209. return true;
  210. }
  211. // 模板消息
  212. // 限流
  213. if (
  214. time() > mktime(18, 0, 0) && NoticeLogModel::where([
  215. ['uid', $to_uid],
  216. ['created_at', '>', mktime(18, 0, 0)],
  217. ['notice_type', '公众号模板消息'],
  218. ])->count() >= 1
  219. ) {
  220. $noticeLog->record($to_uid, $title, "发送频繁", $uuid);
  221. return false;
  222. } elseif (
  223. time() <= mktime(18, 0, 0) && NoticeLogModel::where([
  224. ['uid', $to_uid],
  225. ['created_at', '>', mktime(18, 0, 0) - 86400],
  226. ['notice_type', '公众号模板消息'],
  227. ])->count() >= 1
  228. ) {
  229. $noticeLog->record($to_uid, $title, "发送频繁", $uuid);
  230. return false;
  231. }
  232. $template_id = "alWufNQA9zhVec2RynEUqEUIl2l7TJZjvI6K0YZLzJM";
  233. $data = [
  234. 'first' => [
  235. 'value' => "欢迎{$user->nickname}成为我们的会员\n",
  236. ],
  237. 'keyword1' => [
  238. 'value' => $user->uid,
  239. ],
  240. 'keyword2' => [
  241. 'value' => date('Y-m-d'),
  242. ],
  243. 'remark' => [
  244. 'value' => "\n点击查看,会员特权",
  245. 'color' => "#ff7e98",
  246. ],
  247. ];
  248. $access_token = TokenModel::getToken($public_id);
  249. $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={$access_token}";
  250. $response = \Curl::to($url)->withData([
  251. 'touser' => $openid,
  252. 'template_id' => $template_id,
  253. "url" => "https://m.fenpeiduixiang.com/fpdx-attend/index.html#/vip/index",
  254. "data" => $data,
  255. ])->asJson()->post();
  256. if (isset($response->errcode) && !$response->errcode) {
  257. $noticeLog->record($to_uid, $title, "公众号模板消息", $uuid, 1, $data);
  258. return true;
  259. }
  260. } catch (\Exception $e) {
  261. app('sentry')->captureException($e);
  262. }
  263. $noticeLog->record($to_uid, $title, "发送失败", $uuid);
  264. return false;
  265. }
  266. public function bevip(int $to_uid)
  267. {
  268. $nm = new NoticeModel();
  269. $notice = $nm->fill([
  270. 'uid' => $to_uid,
  271. 'title' => '成功激活VIP普通会员特权',
  272. 'content' => "亲爱的VIP用户,恭喜你,接下来你将享受会员相关特权:1.解锁\"心动我的\"专属进度加成。2.专属活动匹配成功率加成。3.每日专属小fa红包。4.及时的心动消息通知。",
  273. 'type' => 5,
  274. 'type_id' => 0,
  275. 'create_at' => time(),
  276. 'update_at' => time(),
  277. 'tab_content' => '了解详情',
  278. 'tab_url' => '/pages/vip/vip?type=normal',
  279. ]);
  280. $notice->save();
  281. $openidData = User::getOpenidByUid($to_uid);
  282. $openid = $openidData['openid'];
  283. $data = [
  284. 'touser' => $openid,
  285. 'content' => "恭喜你!成功激活VIP普通会员资格。从现在开始:你将拥有以下专属特权!\n\n
  286. 1.解锁“心动我的”卡片时,可获取VIP专属进度加成。\n\n
  287. 2.免费提升在72小时恋爱活动中匹配\"恋爱对象\"以及组CP开黑中匹配\"玩伴\"时的10-20%匹配成功率。\n\n
  288. 3.天天领额外小fa红包,每天向分配对象公众号回复关键词:\"红包\"即可获得会员专属随机小fa红包1个!\n\n
  289. 点击【<a href='weixin://bizmsgmenu?msgmenucontent=红包&msgmenuid=234234'>这里</a>】领取一个\n\n
  290. 4.\"超即时\"心动通知,从现在开始你可以通过分配对象公众号以最及时的方式获取到你关心的通知。让你不错过每一次心动。",
  291. 'kf_account' => 'notice@admin',
  292. ];
  293. Kfaccount::text($data);
  294. $data = [
  295. 'touser' => $openid,
  296. 'content' => "你的会员号是:{$to_uid}。升级「超级会员」,你将拥有更多特权!<a href='https://m.fenpeiduixiang.com/fpdx-attend/index.html#/vip/index'>点击查看</a>",
  297. 'kf_account' => 'notice@admin',
  298. ];
  299. Kfaccount::text($data);
  300. }
  301. /**
  302. * 退出会员系统通知
  303. * @param int $to_uid
  304. */
  305. public function outvip(int $to_uid)
  306. {
  307. $nm = new NoticeModel();
  308. $notice = $nm->fill([
  309. 'uid' => $to_uid,
  310. 'title' => '会籍取消通知',
  311. 'content' => "很抱歉,因为你已取关了官方公众号「分配对象」,我们不得不取消你的会籍。从现在,你将不能享受会员相关特权:1.专属成功率。2.每日专属小fa红包。3.每日精选日报。4.及时的心动消息通知。重新关注即可立即恢复会籍。",
  312. 'type' => 5,
  313. 'type_id' => 0,
  314. 'create_at' => time(),
  315. 'update_at' => time(),
  316. 'tab_content' => '去激活',
  317. 'tab_url' => '/pages/vip/vip?type=normal',
  318. ]);
  319. $notice->save();
  320. }
  321. /**
  322. * 阅读超级心动消息
  323. * @param int $uid
  324. * @param int $like_uid
  325. * @return bool
  326. */
  327. public function readSuperlike(int $uid, int $like_uid)
  328. {
  329. $user = UserModel::findOrFail($uid);
  330. SuperLikeModel::where([
  331. ['uid', $like_uid],
  332. ['partner_id', $user->partner_id],
  333. ['read_at', 0],
  334. ])->update(['read_at' => time()]);
  335. return true;
  336. }
  337. /**
  338. * 阅读相互心动消息
  339. * @param int $uid
  340. * @param int $id
  341. * @return bool
  342. * @throws AlertException
  343. */
  344. public function readEachLikeList(int $uid, int $id)
  345. {
  346. $eachlike = EachLikeModel::findOrFail($id);
  347. if ($uid != $eachlike->tuid) {
  348. throw new AlertException("无权限", 101);
  349. }
  350. if ($eachlike->read_at == 0) {
  351. $eachlike->read_at = time();
  352. }
  353. $eachlike->save();
  354. return true;
  355. }
  356. /**
  357. * 阅读相互心动消息
  358. * @param int $uid
  359. * @param int $id
  360. * @return bool
  361. * @throws AlertException
  362. */
  363. public function readSystemList(int $uid, int $id)
  364. {
  365. $notice = NoticeModel::findOrFail($id);
  366. if ($uid != $notice->uid) {
  367. throw new AlertException("无权限", 101);
  368. }
  369. if ($notice->read_at == 0) {
  370. $notice->read_at = time();
  371. $notice->save();
  372. }
  373. return true;
  374. }
  375. /**
  376. * 超级会员-过期通知
  377. * @param int $to_uid
  378. */
  379. public function supvipExpire(int $to_uid)
  380. {
  381. $user = UserModel::find($to_uid);
  382. $payload = [
  383. 'to_user' => $user->getAuth(),
  384. 'user' => $user->toArray(),
  385. ];
  386. dispatch(new RegistEventJob(10701, $payload))->onQueue("{push}");
  387. // 系统消息
  388. NoticeModel::create([
  389. 'uid' => $to_uid,
  390. 'title' => '超级会员特权即将失效',
  391. 'content' => "你的超级会员特权即将失效,1天后到期。立即续订超级会员,系统将额外赠送最多31天会员时长(过期重新开通不享受赠送)",
  392. 'type' => 5,
  393. 'type_id' => 0,
  394. 'create_at' => time(),
  395. 'update_at' => time(),
  396. 'tab_content' => '查看详情',
  397. 'tab_url' => '/pages/vip/vip?type=super',
  398. ]);
  399. }
  400. /**
  401. * 超级会员-过期系统通知
  402. * @param int $to_uid
  403. */
  404. private function supvipExpireOfsys(int $to_uid)
  405. {
  406. $nm = new NoticeModel();
  407. $notice = $nm->fill([
  408. 'uid' => $to_uid,
  409. 'title' => '超级会员特权即将失效',
  410. 'content' => "你的超级会员特权即将失效,1天后到期。立即续订超级会员,系统将额外赠送最多31天会员时长(过期重新开通不享受赠送)",
  411. 'type' => 5,
  412. 'type_id' => 0,
  413. 'create_at' => time(),
  414. 'update_at' => time(),
  415. 'tab_content' => '查看详情',
  416. 'tab_url' => '/pages/vip/vip?type=super',
  417. ]);
  418. $notice->save();
  419. }
  420. }