BeanRepository.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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_count'] = $circle['join_count'];
  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. //没有推荐的则不显示
  227. if ($chatroom['is_recommend'] == 0) {
  228. continue;
  229. }
  230. if ($chatroom['start'] < $chatroom['end']) {
  231. if ($current < $chatroom['start'] || $current > $chatroom['end']) {
  232. $chatroom['is_open'] = 0;
  233. $chatRoomsData0[] = $chatroom;
  234. } else {
  235. $chatroom['is_open'] = 1;
  236. $chatRoomsData1[] = $chatroom;
  237. }
  238. } elseif ($chatroom['start'] > $chatroom['end']) {
  239. if ($current > $chatroom['end'] && $current < $chatroom['start']) {
  240. $chatroom['is_open'] = 0;
  241. $chatRoomsData0[] = $chatroom;
  242. } else {
  243. $chatroom['is_open'] = 1;
  244. $chatRoomsData1[] = $chatroom;
  245. }
  246. } else {
  247. $chatroom['is_open'] = 1;
  248. $chatRoomsData1[] = $chatroom;
  249. }
  250. }
  251. $star_home['chatroom'] = array_merge($chatRoomsData1, $chatRoomsData0);
  252. $star_home['is_show'] = intval(config('customer.exchange_shop_is_show'));
  253. return $star_home;
  254. }
  255. function getActivity()
  256. {
  257. $activity = config('customer.battle_activity_info');
  258. $activityInfo = ['id' => 1];
  259. if ($activity) {
  260. try {
  261. $activityInfo = \GuzzleHttp\json_decode($activity, true);
  262. } catch (\Exception $e) {
  263. Log::debug('解析活动信息配置失败:' . $e->getMessage());
  264. return $activityInfo;
  265. }
  266. }
  267. return $activityInfo;
  268. }
  269. //获取某用户豆数
  270. function get_user_bean($uid)
  271. {
  272. try {
  273. $url = config("customer.app_service_url") . '/user/v2/user_bean';
  274. $array = [
  275. 'json' => ['uid' => $uid], 'query' => [], 'http_errors' => false, 'headers' => ['Authorization' => "Bearer " . JWTAuth::getToken()]
  276. ];
  277. return http($url, $array, 'get');
  278. } catch (\Exception $e) {
  279. return [];
  280. }
  281. }
  282. //获取用户后院权限等信息
  283. function backyard($uid)
  284. {
  285. try {
  286. $url = config("customer.app_service_url") . '/user/v2/backyard';
  287. $array = [
  288. 'json' => ['uid' => $uid], 'query' => [], 'http_errors' => false, 'headers' => ['Authorization' => "Bearer " . JWTAuth::getToken()]
  289. ];
  290. return http($url, $array, 'get');
  291. } catch (\Exception $e) {
  292. return [];
  293. }
  294. }
  295. //获取每日新闻
  296. function getNews($request)
  297. {
  298. try {
  299. $url = config("customer.app_service_url") . '/config/v2/starNews/lists';
  300. $array = [
  301. 'json' => [], 'query' => [], 'http_errors' => false
  302. ];
  303. return http($url, $array, 'get');
  304. } catch (\Exception $e) {
  305. Log::debug($e->getMessage());
  306. return [];
  307. }
  308. }
  309. //获取平台内容
  310. function getPlatformContent($id)
  311. {
  312. try {
  313. $url = config("customer.app_service_url") . '/config/v2/platformContent/lists';
  314. $array = [
  315. 'json' => ['id' => $id], 'query' => [], 'http_errors' => false, 'headers' => ['Authorization' => "Bearer " . JWTAuth::getToken()]
  316. ];
  317. return http($url, $array, 'get');
  318. } catch (\Exception $e) {
  319. Log::debug($e->getMessage());
  320. return [];
  321. }
  322. }
  323. public function starDetail($request)
  324. {
  325. $star_detail = [];
  326. $user_bean = Redis::get('my_bean');
  327. $user_bean = json_decode($user_bean, true);
  328. $yesterday_quantity_issued = Redis::get('yesterday_quantity_issued');
  329. $exchange = Redis::ZREVRANGEBYSCORE('post_purchase_message', '+inf', '-inf');
  330. foreach ($exchange as $k => $v) {
  331. $exchange[$k] = json_decode($v, true);
  332. }
  333. Log::debug($user_bean);
  334. Log::debug($yesterday_quantity_issued);
  335. Log::debug($exchange);
  336. if (isset($request['post_id']) && $request['post_id']) {
  337. $post = $this->getPostInfo($request['post_id'], 1);
  338. if ($post) {
  339. $user = $this->userInfo($post['uid']);
  340. $topic = $this->getTopic($post['topic_ids']);
  341. $post['created_time'] = Carbon::parse($post['created_at'])->diffForHumans();
  342. $post['topic'] = array_column($topic, 'name');
  343. $post['username'] = $user['username'];
  344. $post['avatar'] = $user['avatar'];
  345. $star_detail['post'] = $post;
  346. } else {
  347. Log::debug($request['post_id']);
  348. return jsonError('该帖子已被删除');
  349. }
  350. }
  351. $user_info = $this->getUserInfo($invite_code = $request['invite_code']);
  352. Log::debug($request['invite_code']);
  353. Log::debug($user_info);
  354. if ($user_info) {
  355. $star_detail['invite']['username'] = $user_info['username'] ?? '';
  356. $star_detail['invite']['avatar'] = $user_info['avatar'] ?? '';
  357. $star_detail['invite']['bean'] = config("customer.share_post_bean") ?? 0;
  358. } else {
  359. Log::debug($request['invite_code']);
  360. //return jsonError('邀请码有误');
  361. }
  362. $star_detail['mybean']['user_count'] = intval($user_bean['user_count']) ?? 0;//已入驻老板
  363. $star_detail['mybean']['yesterday_add_user'] = intval($user_bean['yesterday_add_user']) ?? 0;//昨日新增老板
  364. $star_detail['mybean']['yesterday_quantity_issued'] = intval($yesterday_quantity_issued) ?? 0;//昨日发放总U米
  365. $star_detail['daily_news'] = $this->getNews($request);
  366. $star_detail['exchange'] = $exchange;
  367. $circleList = $this->circleRepository->circleLists(['is_recommend' => 1]);
  368. $circles = [];
  369. foreach ($circleList as $circle) {
  370. $temp = [];
  371. $temp['circle_id'] = $circle['id'];
  372. $temp['image'] = $circle['image'];
  373. $temp['join_count'] = $circle['join_count'];
  374. $temp['circle_name'] = mb_substr($circle['name'], 0, 6);
  375. $circles[] = $temp;
  376. }
  377. $star_detail['circles'] = $circles;
  378. return $star_detail;
  379. }
  380. //根据邀请码获取用户信息
  381. function getUserInfo($invite_code)
  382. {
  383. try {
  384. $sign = generateSign(['invite_code' => $invite_code], config('customer.app_secret'));
  385. $url = config("customer.app_service_url") . '/user/v2/member/getInviteCodeMember';
  386. $array = [
  387. 'json' => ['sign' => $sign, 'invite_code' => $invite_code], 'query' => [], 'http_errors' => false, 'headers' => ['Authorization' => "Bearer " . JWTAuth::getToken()]
  388. ];
  389. return http($url, $array, 'get');
  390. } catch (\Exception $e) {
  391. Log::debug($e->getMessage());
  392. return [];
  393. }
  394. }
  395. //不登录单独返回每日新闻、小贴士
  396. public function lists($request)
  397. {
  398. $star_lists = [];
  399. $user_bean = Redis::get('my_bean');
  400. $user_bean = json_decode($user_bean, true);
  401. $yesterday_quantity_issued = Redis::get('yesterday_quantity_issued');
  402. Log::debug($user_bean);
  403. Log::debug($yesterday_quantity_issued);
  404. $star_lists['mybean']['user_count'] = intval($user_bean['user_count']) ?? 0;//已入驻老板
  405. $star_lists['mybean']['yesterday_add_user'] = intval($user_bean['yesterday_add_user']) ?? 0;//昨日新增老板
  406. $star_lists['mybean']['yesterday_quantity_issued'] = intval($yesterday_quantity_issued) ?? 0;//昨日发放总U米
  407. $star_lists['daily_news'] = $this->getNews($request);
  408. $star_lists['tips'] = $this->getPlatformContent($id = 1);
  409. //聊天室
  410. $tmpChatroom = Redis::get('user_service_chatroom');
  411. $chatrooms = $tmpChatroom ? json_decode($tmpChatroom, true) : [];
  412. $current = Carbon::now()->format('H:i:s');
  413. $chatRoomsData1 = [];
  414. $chatRoomsData0 = [];
  415. foreach ($chatrooms as &$chatroom) {
  416. //没有推荐的则不显示
  417. if ($chatroom['is_recommend'] == 0) {
  418. continue;
  419. }
  420. if ($chatroom['start'] < $chatroom['end']) {
  421. if ($current < $chatroom['start'] || $current > $chatroom['end']) {
  422. $chatroom['is_open'] = 0;
  423. $chatRoomsData0[] = $chatroom;
  424. } else {
  425. $chatroom['is_open'] = 1;
  426. $chatRoomsData1[] = $chatroom;
  427. }
  428. } elseif ($chatroom['start'] > $chatroom['end']) {
  429. if ($current > $chatroom['end'] && $current < $chatroom['start']) {
  430. $chatroom['is_open'] = 0;
  431. $chatRoomsData0[] = $chatroom;
  432. } else {
  433. $chatroom['is_open'] = 1;
  434. $chatRoomsData1[] = $chatroom;
  435. }
  436. } else {
  437. $chatroom['is_open'] = 1;
  438. $chatRoomsData1[] = $chatroom;
  439. }
  440. }
  441. $star_lists['chatroom'] = array_merge($chatRoomsData1, $chatRoomsData0);
  442. return $star_lists;
  443. }
  444. }