BeanRepository.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. <?php
  2. namespace App\Repositories;
  3. use App\Models\Post;
  4. use App\Repositories\Circle\CircleRepository;
  5. use App\Traits\PostTrait;
  6. use App\Traits\UserTrait;
  7. use Illuminate\Support\Carbon;
  8. use Illuminate\Support\Facades\Auth;
  9. use Illuminate\Support\Facades\Log;
  10. use Tymon\JWTAuth\Facades\JWTAuth;
  11. use Illuminate\Support\Facades\Redis;
  12. class BeanRepository
  13. {
  14. use PostTrait;
  15. use UserTrait;
  16. public function __construct(CircleRepository $circleRepository)
  17. {
  18. $this->circleRepository = $circleRepository;
  19. }
  20. public function beanDetail($request)
  21. {
  22. try {
  23. $token = JWTAuth::decode(JWTAuth::getToken());
  24. $uid = $token['user']->uid;
  25. $sign = generateSign(['type' => $request['type'], 'uid' => $uid], config('customer.app_secret'));
  26. $url = config("customer.app_service_url") . '/user/v2/beanDetail';
  27. $array = [
  28. 'json' => ['sign' => $sign, 'type' => $request['type'], 'uid' => $uid], 'query' => [], 'http_errors' => false, 'headers' => ['Authorization' => "Bearer " . JWTAuth::getToken()]
  29. ];
  30. return http($url, $array, 'get');
  31. } catch (\Exception $e) {
  32. Log::debug("beanDetail:" . $e->getMessage());
  33. return [];
  34. }
  35. }
  36. //获取优秀居民信息
  37. function excellentResidents($request)
  38. {
  39. $get_excellent = Redis::get('yesterday_excellent_residents');
  40. $excellent_residents = json_decode($get_excellent, true);
  41. if ($excellent_residents) {
  42. //获取评论内容
  43. $post_ids = array_column($excellent_residents, 'related_content_id');
  44. $comment_content = Post::select('id', 'title', 'content')->whereIn('id', $post_ids)->get();
  45. foreach ($comment_content->toArray() as $value) {
  46. foreach ($excellent_residents as $k => $v) {
  47. if (isset($v['related_content_id']) && $v['related_content_id'] == $value['id']) {
  48. if (!empty($value['title'])) {
  49. $excellent_residents[$k]['post_title'] = $value['title'];
  50. }
  51. if (empty($value['title']) && !empty($value['content'])) {
  52. $content = strip_tags($value['content']);
  53. $excellent_residents[$k]['post_title'] = mb_substr($content, 0, 20);
  54. }
  55. }
  56. }
  57. }
  58. $content_author_id = array_column($excellent_residents, 'content_author_id');
  59. $uids = implode(',', array_unique($content_author_id));
  60. $author_data = $this->getFollowMembersStatus($uids);
  61. if ($author_data) {
  62. foreach ($excellent_residents as $k => $v) {
  63. if (!isset($author_data[$v['content_author_id']])) continue;
  64. $excellent_residents[$k]['follow_status'] = $author_data[$v['content_author_id']]['follow_status'];
  65. $excellent_residents[$k]['username'] = $author_data[$v['content_author_id']]['username'];
  66. $excellent_residents[$k]['avatar'] = $author_data[$v['content_author_id']]['avatar'];
  67. }
  68. }
  69. }
  70. return $excellent_residents;
  71. }
  72. public function rankingList($request)
  73. {
  74. if ($request['type'] == 0) {//排行榜U米达人
  75. $yesterday = Carbon::yesterday()->toDateString();
  76. $all_beans = Redis::ZREVRANGEBYSCORE('user_rainbow_bean' . $yesterday, 100000000, 0, array('WITHSCORES' => true, 'limit' => array(0, 10)));
  77. $new_arr = [];
  78. if ($all_beans) {
  79. $i = 0;
  80. foreach ($all_beans as $key => $val) {
  81. $new_arr[$i]['uid'] = $key;
  82. $new_arr[$i]['count'] = intval($val);
  83. $i++;
  84. }
  85. $uids = implode(',', array_column($new_arr, 'uid'));
  86. $user_data = $this->getFollowMembersStatus($uids);
  87. if ($user_data) {
  88. foreach ($new_arr as $k => $v) {
  89. if (!isset($user_data[$v['uid']])) continue;
  90. // $new_arr[$k]['follow_status'] = $user_data[$v['uid']]['follow_status'];
  91. $new_arr[$k]['username'] = $user_data[$v['uid']]['username'];
  92. $new_arr[$k]['avatar'] = $user_data[$v['uid']]['avatar'];
  93. }
  94. }
  95. }
  96. return $new_arr;
  97. } elseif ($request['type'] == 1) {//排行榜人脉达人
  98. $registered_mosts = Redis::get('yesterday_registered_most');
  99. if ($registered_mosts) {
  100. $registered_most = json_decode($registered_mosts, true);
  101. } else {
  102. $registered_most = [];
  103. }
  104. if (count($registered_most) > 0) {
  105. $superior_uid = array_column($registered_most, 'superior_uid');
  106. $uids = implode(',', array_unique($superior_uid));
  107. $user_data = $this->getFollowMembersStatus($uids);
  108. if ($user_data) {
  109. foreach ($registered_most as $k => $v) {
  110. if (!isset($user_data[$v['superior_uid']])) continue;
  111. $registered_most[$k]['count'] = intval($v['count']);
  112. // $registered_most[$k]['follow_status'] = $user_data[$v['superior_uid']]['follow_status'];
  113. $registered_most[$k]['username'] = $user_data[$v['superior_uid']]['username'];
  114. $registered_most[$k]['avatar'] = $user_data[$v['superior_uid']]['avatar'];
  115. }
  116. }
  117. }
  118. return $registered_most;
  119. } else {//排行榜最佳作者
  120. $all_best_authors = Redis::get('yesterday_best_author');
  121. if ($all_best_authors) {
  122. $all_best_author = json_decode($all_best_authors, true);
  123. } else {
  124. $all_best_author = [];
  125. }
  126. if (count($all_best_author) > 0) {
  127. $content_author_id = array_column($all_best_author, 'content_author_id');
  128. $uids = implode(',', array_unique($content_author_id));
  129. $user_data = $this->getFollowMembersStatus($uids);
  130. if ($user_data) {
  131. foreach ($all_best_author as $k => $v) {
  132. if (!isset($user_data[$v['content_author_id']])) continue;
  133. $all_best_author[$k]['count'] = intval($v['count']);
  134. // $all_best_author[$k]['follow_status'] = $user_data[$v['content_author_id']]['follow_status'];
  135. $all_best_author[$k]['username'] = $user_data[$v['content_author_id']]['username'];
  136. $all_best_author[$k]['avatar'] = $user_data[$v['content_author_id']]['avatar'];
  137. }
  138. }
  139. }
  140. return $all_best_author;
  141. }
  142. }
  143. function getFollowMembersStatus($uids)
  144. {
  145. try {
  146. $sign = generateSign(['uids' => $uids], config('customer.app_secret'));
  147. $url = config("customer.app_service_url") . '/user/v2/member/getMemberIds';
  148. $array = [
  149. 'json' => ['sign' => $sign, 'uids' => $uids], 'query' => [], 'http_errors' => false, 'headers' => ['Authorization' => "Bearer " . JWTAuth::getToken()]
  150. ];
  151. return http($url, $array, 'get');
  152. } catch (\Exception $e) {
  153. Log::debug($e->getMessage());
  154. return [];
  155. }
  156. }
  157. //后院首页
  158. public function starHome($request)
  159. {
  160. $today = Carbon::now()->toDateString();
  161. $token = JWTAuth::decode(JWTAuth::getToken());
  162. $uid = $token['user']->uid;
  163. $bean = Redis::ZSCORE('user_rainbow_bean' . $today, $uid);
  164. $userInfo = $this->userInfo($uid);
  165. $star_home = [];
  166. $user_bean = Redis::get('my_bean');
  167. Log::debug($user_bean);
  168. $user_bean = json_decode($user_bean, true);
  169. $yesterday_quantity_issued = Redis::get('yesterday_quantity_issued');
  170. Log::debug($user_bean);
  171. $my_bean = $this->get_user_bean($uid);
  172. $star_home['mybean']['user_all_bean'] = intval($my_bean['user_all_bean']) ?? 0;//用户累计总U米
  173. $star_home['mybean']['user_surplus_bean'] = intval($my_bean['user_surplus_bean']) ?? 0;//用户剩余U米
  174. $star_home['mybean']['today_add_bean'] = intval($bean) ?? 0;//今日发放U米
  175. $star_home['mybean']['user_count'] = intval($user_bean['user_count']) ?? 0;//已入驻老板
  176. $star_home['mybean']['yesterday_add_user'] = intval($user_bean['yesterday_add_user']) ?? 0;//昨日新增老板
  177. $star_home['mybean']['yesterday_quantity_issued'] = intval($yesterday_quantity_issued) ?? 0;//昨日发放总U米
  178. //$star_home['excellent_residents'] = $this->excellentResidents($request) ?? [];
  179. $star_home['daily_news'] = $this->getNews($request);
  180. $star_home['tips'] = $this->getPlatformContent($id = 1);
  181. $backyard = $this->backyard($uid);
  182. $star_home['backyard']['backyard_imgs'] = [
  183. "http://oss.uptoyo.com/h5/planet/cover_01.png",
  184. "http://oss.uptoyo.com/h5/planet/cover_02.png",
  185. "http://oss.uptoyo.com/h5/planet/cover_03.png",
  186. "http://oss.uptoyo.com/h5/planet/cover_04.png",
  187. "http://oss.uptoyo.com/h5/planet/cover_05.png",
  188. "http://oss.uptoyo.com/h5/planet/cover_06.png",
  189. "http://oss.uptoyo.com/h5/planet/cover_07.png",
  190. "http://oss.uptoyo.com/h5/planet/cover_08.png",
  191. "http://oss.uptoyo.com/h5/planet/cover_09.png",
  192. "http://oss.uptoyo.com/h5/planet/cover_10.png"
  193. ];
  194. if ($backyard) {
  195. $star_home['backyard']['backyard_open'] = $backyard['backyard_open'];
  196. $star_home['backyard']['backyard_img'] = $backyard['backyard_img'];
  197. } else {
  198. $star_home['backyard']['backyard_open'] = 0;
  199. $star_home['backyard']['backyard_img'] = '';
  200. }
  201. $star_home['user']['username'] = $userInfo['username'];
  202. $star_home['user']['avatar'] = $userInfo['avatar'];
  203. //battle信息
  204. // $activityInfo = $this->getActivity();
  205. // $star_home['battle']['start'] = $activityInfo['start'];
  206. // $star_home['battle']['end'] = $activityInfo['end'];
  207. // $star_home['battle']['news'] = Redis::get('battle_latest_info_'.$activityInfo['id'])??'战场四平八稳,劳驾老板亲自去战场督战';
  208. $circleList = $this->circleRepository->circleLists(['is_recommend' => 1]);
  209. $circles = [];
  210. foreach ($circleList as $circle) {
  211. $temp = [];
  212. $temp['circle_id'] = $circle['id'];
  213. $temp['image'] = $circle['image'];
  214. $temp['join_limit'] = $circle['join_limit'];
  215. $temp['circle_name'] = mb_substr($circle['name'], 0, 6);
  216. $circles[] = $temp;
  217. }
  218. $star_home['circles'] = $circles;
  219. //聊天室
  220. $tmpChatroom = Redis::get('user_service_chatroom');
  221. $chatrooms = $tmpChatroom ? json_decode($tmpChatroom, true) : [];
  222. $current = Carbon::now()->format('H:i:s');
  223. $chatRoomsData1 = [];
  224. $chatRoomsData0 = [];
  225. foreach ($chatrooms as &$chatroom) {
  226. if ($chatroom['start'] < $chatroom['end']) {
  227. if ($current < $chatroom['start'] || $current > $chatroom['end']) {
  228. $chatroom['is_open'] = 0;
  229. $chatRoomsData0[] = $chatroom;
  230. } else {
  231. $chatroom['is_open'] = 1;
  232. $chatRoomsData1[] = $chatroom;
  233. }
  234. } elseif ($chatroom['start'] > $chatroom['end']) {
  235. if ($current > $chatroom['end'] && $current < $chatroom['start']) {
  236. $chatroom['is_open'] = 0;
  237. $chatRoomsData0[] = $chatroom;
  238. } else {
  239. $chatroom['is_open'] = 1;
  240. $chatRoomsData1[] = $chatroom;
  241. }
  242. } else {
  243. $chatroom['is_open'] = 1;
  244. $chatRoomsData1[] = $chatroom;
  245. }
  246. }
  247. $star_home['chatroom'] = array_merge($chatRoomsData1, $chatRoomsData0);
  248. $star_home['is_show'] = intval(config('customer.exchange_shop_is_show'));
  249. return $star_home;
  250. }
  251. function getActivity()
  252. {
  253. $activity = config('customer.battle_activity_info');
  254. $activityInfo = ['id' => 1];
  255. if ($activity) {
  256. try {
  257. $activityInfo = \GuzzleHttp\json_decode($activity, true);
  258. } catch (\Exception $e) {
  259. Log::debug('解析活动信息配置失败:' . $e->getMessage());
  260. return $activityInfo;
  261. }
  262. }
  263. return $activityInfo;
  264. }
  265. //获取某用户豆数
  266. function get_user_bean($uid)
  267. {
  268. try {
  269. $url = config("customer.app_service_url") . '/user/v2/user_bean';
  270. $array = [
  271. 'json' => ['uid' => $uid], 'query' => [], 'http_errors' => false, 'headers' => ['Authorization' => "Bearer " . JWTAuth::getToken()]
  272. ];
  273. return http($url, $array, 'get');
  274. } catch (\Exception $e) {
  275. return [];
  276. }
  277. }
  278. //获取用户后院权限等信息
  279. function backyard($uid)
  280. {
  281. try {
  282. $url = config("customer.app_service_url") . '/user/v2/backyard';
  283. $array = [
  284. 'json' => ['uid' => $uid], 'query' => [], 'http_errors' => false, 'headers' => ['Authorization' => "Bearer " . JWTAuth::getToken()]
  285. ];
  286. return http($url, $array, 'get');
  287. } catch (\Exception $e) {
  288. return [];
  289. }
  290. }
  291. //获取每日新闻
  292. function getNews($request)
  293. {
  294. try {
  295. $url = config("customer.app_service_url") . '/config/v2/starNews/lists';
  296. $array = [
  297. 'json' => [], 'query' => [], 'http_errors' => false
  298. ];
  299. return http($url, $array, 'get');
  300. } catch (\Exception $e) {
  301. Log::debug($e->getMessage());
  302. return [];
  303. }
  304. }
  305. //获取平台内容
  306. function getPlatformContent($id)
  307. {
  308. try {
  309. $url = config("customer.app_service_url") . '/config/v2/platformContent/lists';
  310. $array = [
  311. 'json' => ['id' => $id], 'query' => [], 'http_errors' => false, 'headers' => ['Authorization' => "Bearer " . JWTAuth::getToken()]
  312. ];
  313. return http($url, $array, 'get');
  314. } catch (\Exception $e) {
  315. Log::debug($e->getMessage());
  316. return [];
  317. }
  318. }
  319. public function starDetail($request)
  320. {
  321. $star_detail = [];
  322. $user_bean = Redis::get('my_bean');
  323. $user_bean = json_decode($user_bean, true);
  324. $yesterday_quantity_issued = Redis::get('yesterday_quantity_issued');
  325. $exchange = Redis::ZREVRANGEBYSCORE('post_purchase_message', '+inf', '-inf');
  326. foreach ($exchange as $k => $v) {
  327. $exchange[$k] = json_decode($v, true);
  328. }
  329. Log::debug($user_bean);
  330. Log::debug($yesterday_quantity_issued);
  331. Log::debug($exchange);
  332. if (isset($request['post_id']) && $request['post_id']) {
  333. $post = $this->getPostInfo($request['post_id'], 1);
  334. if ($post) {
  335. $user = $this->userInfo($post['uid']);
  336. $topic = $this->getTopic($post['topic_ids']);
  337. $post['created_time'] = Carbon::parse($post['created_at'])->diffForHumans();
  338. $post['topic'] = array_column($topic, 'name');
  339. $post['username'] = $user['username'];
  340. $post['avatar'] = $user['avatar'];
  341. $star_detail['post'] = $post;
  342. } else {
  343. Log::debug($request['post_id']);
  344. return jsonError('该帖子已被删除');
  345. }
  346. }
  347. $user_info = $this->getUserInfo($invite_code = $request['invite_code']);
  348. Log::debug($request['invite_code']);
  349. Log::debug($user_info);
  350. if ($user_info) {
  351. $star_detail['invite']['username'] = $user_info['username'] ?? '';
  352. $star_detail['invite']['avatar'] = $user_info['avatar'] ?? '';
  353. $star_detail['invite']['bean'] = config("customer.share_post_bean") ?? 0;
  354. } else {
  355. Log::debug($request['invite_code']);
  356. //return jsonError('邀请码有误');
  357. }
  358. $star_detail['mybean']['user_count'] = intval($user_bean['user_count']) ?? 0;//已入驻老板
  359. $star_detail['mybean']['yesterday_add_user'] = intval($user_bean['yesterday_add_user']) ?? 0;//昨日新增老板
  360. $star_detail['mybean']['yesterday_quantity_issued'] = intval($yesterday_quantity_issued) ?? 0;//昨日发放总U米
  361. $star_detail['daily_news'] = $this->getNews($request);
  362. $star_detail['exchange'] = $exchange;
  363. $circleList = $this->circleRepository->circleLists(['is_recommend' => 1]);
  364. $circles = [];
  365. foreach ($circleList as $circle) {
  366. $temp = [];
  367. $temp['circle_id'] = $circle['id'];
  368. $temp['image'] = $circle['image'];
  369. $temp['join_limit'] = $circle['join_limit'];
  370. $temp['circle_name'] = mb_substr($circle['name'], 0, 6);
  371. $circles[] = $temp;
  372. }
  373. $star_detail['circles'] = $circles;
  374. return $star_detail;
  375. }
  376. //根据邀请码获取用户信息
  377. function getUserInfo($invite_code)
  378. {
  379. try {
  380. $sign = generateSign(['invite_code' => $invite_code], config('customer.app_secret'));
  381. $url = config("customer.app_service_url") . '/user/v2/member/getInviteCodeMember';
  382. $array = [
  383. 'json' => ['sign' => $sign, 'invite_code' => $invite_code], 'query' => [], 'http_errors' => false, 'headers' => ['Authorization' => "Bearer " . JWTAuth::getToken()]
  384. ];
  385. return http($url, $array, 'get');
  386. } catch (\Exception $e) {
  387. Log::debug($e->getMessage());
  388. return [];
  389. }
  390. }
  391. //不登录单独返回每日新闻、小贴士
  392. public function lists($request)
  393. {
  394. $star_lists = [];
  395. $user_bean = Redis::get('my_bean');
  396. $user_bean = json_decode($user_bean, true);
  397. $yesterday_quantity_issued = Redis::get('yesterday_quantity_issued');
  398. Log::debug($user_bean);
  399. Log::debug($yesterday_quantity_issued);
  400. $star_lists['mybean']['user_count'] = intval($user_bean['user_count']) ?? 0;//已入驻老板
  401. $star_lists['mybean']['yesterday_add_user'] = intval($user_bean['yesterday_add_user']) ?? 0;//昨日新增老板
  402. $star_lists['mybean']['yesterday_quantity_issued'] = intval($yesterday_quantity_issued) ?? 0;//昨日发放总U米
  403. $star_lists['daily_news'] = $this->getNews($request);
  404. $star_lists['tips'] = $this->getPlatformContent($id = 1);
  405. //聊天室
  406. $tmpChatroom = Redis::get('user_service_chatroom');
  407. $chatrooms = $tmpChatroom ? json_decode($tmpChatroom, true) : [];
  408. $current = Carbon::now()->format('H:i:s');
  409. $chatRoomsData1 = [];
  410. $chatRoomsData0 = [];
  411. foreach ($chatrooms as &$chatroom) {
  412. if ($chatroom['start'] < $chatroom['end']) {
  413. if ($current < $chatroom['start'] || $current > $chatroom['end']) {
  414. $chatroom['is_open'] = 0;
  415. $chatRoomsData0[] = $chatroom;
  416. } else {
  417. $chatroom['is_open'] = 1;
  418. $chatRoomsData1[] = $chatroom;
  419. }
  420. } elseif ($chatroom['start'] > $chatroom['end']) {
  421. if ($current > $chatroom['end'] && $current < $chatroom['start']) {
  422. $chatroom['is_open'] = 0;
  423. $chatRoomsData0[] = $chatroom;
  424. } else {
  425. $chatroom['is_open'] = 1;
  426. $chatRoomsData1[] = $chatroom;
  427. }
  428. } else {
  429. $chatroom['is_open'] = 1;
  430. $chatRoomsData1[] = $chatroom;
  431. }
  432. }
  433. $star_lists['chatroom'] = array_merge($chatRoomsData1, $chatRoomsData0);
  434. return $star_lists;
  435. }
  436. }