BeanRepository.php 20 KB

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