ManageService.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. namespace App\Services\Notice;
  3. use App\Exceptions\AlertException;
  4. use App\Exceptions\ApiException;
  5. use App\Models\Common\NoticeManageModel;
  6. use App\Models\Deed\InvitationCardModel;
  7. use App\Models\Deed\NoticeReadModel;
  8. use App\Services\App\ProfileService;
  9. use App\Services\Service;
  10. class ManageService extends Service
  11. {
  12. /**
  13. * 获取某人某项配置
  14. * @param int $uid
  15. * @param string $key
  16. * @return int
  17. * @throws AlertException
  18. * @throws ApiException
  19. */
  20. public function getNoticeByKey2Int(int $uid, string $key): int
  21. {
  22. $profile = NoticeManageModel::where([
  23. ['uid', $uid],
  24. ['key', $key],
  25. ])->first();
  26. if (collect($profile)->isEmpty()) {
  27. $this->getNoticeByUser($uid);
  28. $profile = NoticeManageModel::where([
  29. ['uid', $uid],
  30. ['key', $key],
  31. ])->first();
  32. if (collect($profile)->isEmpty()) {
  33. throw new AlertException("错误的配置", 400);
  34. }
  35. return $profile->isopen;
  36. }
  37. return $profile->isopen;
  38. }
  39. /**
  40. * 获取某人的通知配置
  41. * @param int $uid
  42. * @return array
  43. * @throws \App\Exceptions\ApiException
  44. */
  45. public function getNoticeByUser(int $uid)
  46. {
  47. $pfs = new ProfileService();
  48. $manages = json_decode($pfs->get('notice_manage'), true) ?? [];
  49. foreach ($manages as $key => &$manage) {
  50. $profile = NoticeManageModel::firstOrCreate([
  51. 'uid' => $uid,
  52. 'key' => $manage['key'],
  53. ], [
  54. 'created_at' => time(),
  55. 'updated_at' => time(),
  56. 'group' => $manage['group'],
  57. 'isopen' => $manage['isopen'] ?? 0,
  58. ]);
  59. if ('app' == $manage['group']) {
  60. unset($manages[$key]);
  61. } else {
  62. $manage["isopen"] = $profile->isopen;
  63. }
  64. }
  65. return $manages;
  66. }
  67. /**
  68. * 获取某人某项配置
  69. * @param int $uid
  70. * @param string $key
  71. * @return bool
  72. * @throws AlertException
  73. * @throws ApiException
  74. */
  75. public function getNoticeByKey(int $uid, string $key): bool
  76. {
  77. $profile = NoticeManageModel::where([
  78. ['uid', $uid],
  79. ['key', $key],
  80. ])->first();
  81. if (collect($profile)->isEmpty()) {
  82. $this->getNoticeByUser($uid);
  83. $profile = NoticeManageModel::where([
  84. ['uid', $uid],
  85. ['key', $key],
  86. ])->first();
  87. if (collect($profile)->isEmpty()) {
  88. throw new AlertException("错误的配置", 400);
  89. }
  90. if ($profile->isopen == 1) {
  91. return true;
  92. }
  93. return false;
  94. }
  95. if ($profile->isopen == 1) {
  96. return true;
  97. }
  98. return false;
  99. }
  100. /**
  101. * 更新某人配置
  102. * @param int $uid
  103. * @param string $key
  104. * @param null $open 0|1 无论当前值为什么,都修改为该值
  105. * @return bool
  106. * @throws AlertException
  107. * @throws ApiException
  108. */
  109. public function updateNoticeByKey(int $uid, string $key, $open = null)
  110. {
  111. $profiles = $this->getNoticeByUser($uid);
  112. $nm = new NoticeManageModel();
  113. $profile = $nm->where([
  114. ['uid', $uid],
  115. ['key', $key],
  116. ])->first();
  117. if (collect($profile)->isEmpty()) {
  118. throw new AlertException("错误的配置", 400);
  119. }
  120. $profile->isopen = abs($profile->isopen - 1);
  121. if (!is_null($open)) {
  122. $profile->isopen = $open;
  123. }
  124. $profile->save();
  125. return true;
  126. }
  127. /**
  128. * 更新某人配置
  129. * @param int $uid
  130. * @param string $group
  131. * @param int $open
  132. * @return bool
  133. * @throws AlertException
  134. * @throws \App\Exceptions\ApiException
  135. */
  136. public function updateNoticeByGroup(int $uid, string $group, int $open)
  137. {
  138. $profiles = $this->getNoticeByUser($uid);
  139. $nm = new NoticeManageModel();
  140. if (
  141. !$profile = $nm->where([
  142. ['uid', $uid],
  143. ['group', $group],
  144. ])->exists()
  145. ) {
  146. throw new AlertException("错误的配置", 400);
  147. }
  148. $nm->where([
  149. ['uid', $uid],
  150. ['group', $group],
  151. ])->update(['isopen' => $open]);
  152. return true;
  153. }
  154. public function inviteNoticeLog($invite_id, $notice_type)
  155. {
  156. $invate = InvitationCardModel::find($invite_id);
  157. $invate->notice_type = $notice_type;
  158. $invate->save();
  159. }
  160. public function noticeIncrRead($to_uid)
  161. {
  162. if (NoticeReadModel::where('uid', $to_uid)->first()) {
  163. NoticeReadModel::where('uid', $to_uid)->increment('unread_cnt');
  164. } else {
  165. NoticeReadModel::create([
  166. 'uid' => $to_uid,
  167. 'unread_cnt' => 1,
  168. ]);
  169. }
  170. }
  171. }