CommentRepository.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. /**
  3. * 评论仓库.
  4. *
  5. * @author jim <jim458734573@gmail.com>
  6. * @date 2020-01-28 20:17:45
  7. */
  8. declare(strict_types=1);
  9. namespace App\Repositories;
  10. use App\Model\CommentModel;
  11. use App\Model\TopicModel;
  12. use App\Model\VideoModel;
  13. use App\Repositories\Service\CommonService;
  14. use Carbon\Carbon;
  15. use Hyperf\Database\Model\Builder;
  16. use Hyperf\HttpServer\Contract\RequestInterface;
  17. use Hyperf\Utils\ApplicationContext;
  18. use KZ2\Repositories\Model\Repository;
  19. class CommentRepository extends Repository
  20. {
  21. public function model()
  22. {
  23. return CommentModel::class;
  24. }
  25. /**
  26. * 创建一个评论.
  27. * @param $setarr
  28. * @return \Hyperf\DbConnection\Model\Model
  29. */
  30. public function createComment($setarr)
  31. {
  32. $commonService = make(CommonService::class);
  33. $setarr['parent_user_id'] = '';
  34. $setarr['floor'] = 1;
  35. $setarr['quote'] = '';
  36. $setarr['replaied_at'] = null;
  37. // 这里可以随机伪造支持,反对数据.
  38. $setarr['total_support'] = 0;
  39. $setarr['total_against'] = 0;
  40. $setarr['total_quote'] = 0;
  41. if (isset($setarr['ip'])) {
  42. // 命令行本地执行,取不到request
  43. $clientIp = '113.238.142.208';
  44. $arrUa = [
  45. 'cli run cmd',
  46. ];
  47. } else {
  48. $request = request();
  49. // $serverParams = $request->getServerParams();
  50. $clientIp = $commonService->getIp();
  51. $arrUa = $request->getHeader('user-agent');
  52. }
  53. // 取第一个.
  54. $ua = current($arrUa);
  55. $setarr['ip'] = $clientIp;
  56. $setarr['ua'] = $ua;
  57. $parentObj = $relationObj = null;
  58. if (!empty($setarr['parent_id'])) {
  59. // 处理父级引用回复.
  60. $parentObj = CommentModel::where('id', $setarr['parent_id'])->first();
  61. if (!empty($parentObj)) {
  62. $parentFloor = intval($parentObj->floor);
  63. $setarr['parent_user_id'] = $parentObj->user_id;
  64. $setarr['floor'] = $parentFloor + 1;
  65. if (empty($parentObj->quote)) {
  66. $setarr['quote'] = str_pad('', $parentFloor, '>', STR_PAD_RIGHT) . $parentObj->content;
  67. } else {
  68. $setarr['quote'] = $parentObj->quote . "\n" . str_pad('', $parentFloor, '>',
  69. STR_PAD_RIGHT) . $parentObj->content;
  70. }
  71. // 引用了就代表是回复.
  72. $setarr['replaied_at'] = Carbon::now()->toDateTimeString();
  73. }
  74. }
  75. // 处理关联模块.
  76. if (!empty($setarr['module'])) {
  77. switch ($setarr['module']) {
  78. case 'topic':
  79. if (!empty($setarr['svalue'])) {
  80. $id = intval($setarr['svalue']);
  81. $relationObj = TopicModel::where('id', $id)->first();
  82. if (!empty($relationObj->user_id)) {
  83. $setarr['module_user_uid'] = $relationObj->user_id;
  84. }
  85. }
  86. break;
  87. case 'video':
  88. if (!empty($setarr['svalue'])) {
  89. $id = intval($setarr['svalue']);
  90. $relationObj = VideoModel::where('id', $id)->first();
  91. if (!empty($relationObj->uid)) {
  92. $setarr['module_user_uid'] = $relationObj->uid;
  93. }
  94. if (empty($setarr['subject'])) {
  95. $setarr['subject'] = mb_substr($relationObj->title, 0, 60);
  96. }
  97. }
  98. break;
  99. default:
  100. break;
  101. }
  102. }
  103. $item = CommentModel::create($setarr);
  104. if ($item) {
  105. if (!empty($parentObj)) {
  106. // 父级累计引用次数累加+1
  107. $parentObj->total_quote = intval($parentObj->total_quote) + 1;
  108. $parentObj->save();
  109. }
  110. if (!empty($relationObj)) {
  111. // 累计追加话题统计.
  112. if ($setarr['module'] == 'topic') {
  113. $relationObj->total_comment = intval($relationObj->total_comment) + 1;
  114. } elseif ($setarr['module'] == 'video') {
  115. $relationObj->comment = intval($relationObj->comment) + 1;
  116. }
  117. $relationObj->save();
  118. }
  119. }
  120. return $item;
  121. }
  122. /**
  123. * 递增支持.
  124. * @return int
  125. */
  126. public function incSupport()
  127. {
  128. return (new CommentModel)->increment('total_support', 1);
  129. }
  130. /**
  131. * 统计某人今天评论数
  132. * @param $userId
  133. * @param string $module
  134. * @return int
  135. */
  136. public function countToday($userId, $module = '')
  137. {
  138. if (empty($userId)) {
  139. return 0;
  140. }
  141. $startToday = Carbon::today()->startOfDay();
  142. $endToday = Carbon::today()->endOfDay();
  143. $model = CommentModel::query()->where(['user_id' => $userId]);
  144. if (!empty($module)) {
  145. $model = $model->where(['module' => $module]);
  146. }
  147. $model = $model->whereBetween('created_at', [$startToday, $endToday]);
  148. return $model->count();
  149. }
  150. /**
  151. * 检查权限.
  152. * @param string $userCode
  153. * @param string $module
  154. * @return array
  155. */
  156. public function checkPermission($userCode, $module = '')
  157. {
  158. $result = [
  159. 'amount' => 0,
  160. 'posted' => 0,
  161. 'check_result' => false,
  162. ];
  163. $userRepo = make(UserRepository::class);
  164. $userInfo = $userRepo->getUserInfoFromCache($userCode);
  165. $vipLevel = 1;
  166. $dayLimit = 0;
  167. if ($userInfo['is_vip'] == 'y') {
  168. $vipLevel = isset($userInfo['vip_level']) ? intval($userInfo['vip_level']) : 1;
  169. $dayLimit = $this->getDayLimitByVipLevel($vipLevel); // vip每天送的
  170. }
  171. $todayTaskVideoNum = make(TaskAwarkLogRepository::class)->coutTodayOnce($userInfo['code'], 2);
  172. $maxComment = intval($userInfo['max_comment']); // 永久的奖励,非vip里的
  173. $amount = $maxComment + $dayLimit + $todayTaskVideoNum;
  174. $postedNum = $this->countToday($userInfo['code'], $module);
  175. $result['amount'] = $amount;
  176. $result['posted'] = $postedNum;
  177. if ($postedNum >= $amount) {
  178. $result['check_result'] = false;
  179. } else {
  180. $result['check_result'] = true;
  181. }
  182. return $result;
  183. }
  184. /**
  185. * 根据vip等级获取每天评论限制.
  186. * @param int $vipLevel
  187. * @return int
  188. */
  189. public function getDayLimitByVipLevel($vipLevel = 1)
  190. {
  191. $vipLists = make(VipRepository::class)->getCanUseVipList();
  192. $limit = 0;
  193. if (isset($vipLists[$vipLevel])) {
  194. $arrFeatures = $vipLists[$vipLevel]['jdata']['feature'] ?? [];
  195. if (!empty($arrFeatures)) {
  196. foreach ($arrFeatures as $v) {
  197. if ($v['type'] == 'comment') {
  198. if (isset($v['count'])) {
  199. $limit = $v['count'];
  200. }
  201. break;
  202. }
  203. }
  204. }
  205. }
  206. return $limit;
  207. }
  208. /**
  209. * 获取所有数据
  210. * @param array $inputData
  211. * @return object
  212. */
  213. public function getLists(array $inputData): object
  214. {
  215. $digest = $inputData['digest'] ?? '';
  216. $top = $inputData['top'] ?? '';
  217. $page = $inputData['page'] ?? 1;
  218. $page_size = $inputData['page_size'] ?? 30;
  219. $list = CommentModel::query()->where(function ($q) use ($inputData, $digest, $top) {
  220. if ($inputData['user_uid']) {
  221. $q->where('user_id', $inputData['user_uid']);
  222. }
  223. if ($inputData['module']) {
  224. $q->where('module', $inputData['module']);
  225. }
  226. if ($digest !== '') {
  227. $q->where('is_digest', $inputData['digest']);
  228. }
  229. if ($top !== '') {
  230. $q->where('is_top', $inputData['top']);
  231. }
  232. if ($inputData['like_content']) {
  233. $q->where('content', 'like', '%' . $inputData['like_content'] . '%');
  234. }
  235. if ($inputData['stime'] && $inputData['otime']) {
  236. $q->whereBetween(
  237. 'updated_at',
  238. [
  239. Carbon::parse($inputData['stime'])->toDateString(),
  240. Carbon::parse($inputData['otime'])->toDateString()
  241. ]
  242. );
  243. }
  244. })
  245. ->orderByDesc('id')
  246. ->paginate($page_size, ['*'], 'page', $page);
  247. return $list;
  248. }
  249. /**
  250. * 获取分组
  251. */
  252. public function getModule():object
  253. {
  254. return CommentModel::query()
  255. ->groupBy(['module'])
  256. ->pluck('module');
  257. }
  258. }