IndexController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Jobs\AttachIpAddressJob;
  4. use App\Models\Posts\WxPost;
  5. use App\Models\Posts\WxPost as Model;
  6. use App\Models\Circle\WxCircle;
  7. use App\Models\Shop\WxShopGoods;
  8. use App\Models\User\WxUser;
  9. use App\Wen\Utils\FieldUtils;
  10. use App\Wen\Utils\PostUtils;
  11. use App\Wen\Utils\Settings;
  12. use App\Http\Controllers\Api\Repositories\PostsRepositores;
  13. use App\Models\WxSlideshow;
  14. use App\Wen\Utils\ShopUtils;
  15. use Illuminate\Http\Request;
  16. use Illuminate\Support\Facades\Cache;
  17. use Illuminate\Support\Facades\DB;
  18. class IndexController extends BaseController
  19. {
  20. public function test(Request $request)
  21. {
  22. }
  23. /**
  24. * 圈子轮播图
  25. */
  26. public function circles_banner()
  27. {
  28. if(Settings::get('app_circles_banner', '', true)){
  29. $banner = WxSlideshow::wherein('id', explode(',', Settings::get('app_circles_banner', '', true)))->orderBy('sort', 'desc')->get();
  30. return $this->success($banner);
  31. }
  32. return $this->fail(200003, []);
  33. }
  34. public function getPosts(Request $request)
  35. {
  36. $uid = $request->uid;
  37. $type = (int)($request->type);
  38. $plateId = $request->plate_id;
  39. $channel = $request->channel;
  40. global $__MINI_GLOBAL_SCENE__;
  41. if(_empty_($channel, true)){
  42. $channel = 1;
  43. }else{
  44. $channel = _abs($channel);
  45. }
  46. $city = _empty_default_($request->city, '');
  47. if(_empty_($city) || $city == '全国' || $city == '重新定位' || $city == '定位中'){
  48. $city = '';
  49. }
  50. $limit = $request->input('limit', 10);
  51. // 我关注的笔记列表
  52. if(_empty_($plateId)){
  53. if($__MINI_GLOBAL_SCENE__ == 165){
  54. $is_waterfall = false;
  55. $tab_ = 1;
  56. }else{
  57. $index_tab = json_decode(Settings::get('app_index_tab', '', true), true);
  58. if(_empty_($index_tab)){
  59. return $this->fail(200008, [], '管理员暂未配置首页Tab数据');
  60. }
  61. if(!isset($index_tab[$type])){
  62. return $this->fail(200008, [], '管理员暂未配置该tab');
  63. }
  64. $tab_ = $index_tab[$type]['type'];
  65. $is_waterfall = _array_key($index_tab[$type], 'waterfall', '0') == '1';
  66. $is_force = _array_key($index_tab[$type], 'force', '0') == '1';
  67. $target_id = _array_key($index_tab[$type], 'target', '');
  68. global $__MINI_GLOBAL_DEVICE__,$__MINI_GLOBAL_MODE__;
  69. if($channel === 0){
  70. $tab_ = 0;
  71. $is_waterfall = false;
  72. }else if($channel === 2){
  73. $tab_ = 7;
  74. $is_waterfall = true;
  75. }else if($channel === 1){
  76. if($is_force){
  77. }else{
  78. $diy_list_style = mini_current_user(2, 'diy_scene_1_post_list_style');
  79. if($diy_list_style == 3){
  80. $is_waterfall = true;
  81. }else if($diy_list_style == 2){
  82. $is_waterfall = false;
  83. }
  84. }
  85. }
  86. }
  87. if ($tab_ == 0) {
  88. $data = PostsRepositores::followPosts($uid, $limit, $is_waterfall);
  89. }
  90. // 推荐的笔记列表
  91. else if ($tab_ == 1) {
  92. $data = PostsRepositores::list($uid, $limit, $is_waterfall, $city);
  93. }
  94. // 热榜的笔记列表
  95. else if ($tab_ == 2 || $tab_ == 9) {
  96. $data = PostsRepositores::hotPosts($uid, $limit, $is_waterfall, $tab_ == 2);
  97. }
  98. else if ($tab_ == 3) {
  99. $shop_classifys = Settings::get('app_index_shop_classify', []);
  100. if(_empty_($shop_classifys)){
  101. $data = WxShopGoods::where('state', 1)->orderBy('sort', 'desc')->orderBy('id', 'desc')->simplePaginate($limit);
  102. }else{
  103. $ban_ids = ShopUtils::get_ban_classes($request->header('device', 'mp'));
  104. if($ban_ids){
  105. $data = WxShopGoods::where('state', 1)->whereIn('classify_id', $shop_classifys)->whereNotIn('classify_id', $ban_ids)->orderBy('sort', 'desc')->orderBy('id', 'desc')->simplePaginate($limit);
  106. }else{
  107. $data = WxShopGoods::where('state', 1)->whereIn('classify_id', $shop_classifys)->orderBy('sort', 'desc')->orderBy('id', 'desc')->simplePaginate($limit);
  108. }
  109. }
  110. }
  111. // 板块
  112. else if ($tab_ == 4) {
  113. $plate_ids = explode(',', _array_key($index_tab[$type], 'plate_ids', ''));
  114. $circleIds = WxCircle::whereIn('plate_id', $plate_ids)->pluck('id');
  115. $query = (new Model())
  116. ->where('is_examine', 1)
  117. ->where('posts_state', 0);
  118. // 控制视频隐藏
  119. if($__MINI_GLOBAL_DEVICE__ != 'mp'){
  120. global $__MINI_GLOBAL_FRONT_VERSION__;
  121. // todo: 临时
  122. if($__MINI_GLOBAL_FRONT_VERSION__ >= '1.2.2.24'){
  123. }else {
  124. $query = $query->where('is_wechat_sph', 0);
  125. }
  126. }
  127. if($__MINI_GLOBAL_MODE__ == 'examine'){
  128. $query = $query->where('post_type', '<>', 'video');
  129. }
  130. $data = $query->whereIn('circle_id', $circleIds)
  131. ->orderBy('id', 'desc')
  132. ->simplePaginate($limit, FieldUtils::postInfoColums());
  133. if($is_waterfall){
  134. PostUtils::reset_loop();
  135. $data->map(function ($v, $k) use ($uid){
  136. return PostUtils::WaterfallProcess($k, $v,1);
  137. });
  138. }else {
  139. $data = PostsRepositores::postsParame($data, $uid, 1);
  140. }
  141. }
  142. // 视频
  143. else if ($tab_ == 5) {
  144. $query = (new Model())
  145. ->where('is_examine', 1)
  146. ->where('posts_state', 0);
  147. // 控制视频隐藏
  148. if($__MINI_GLOBAL_DEVICE__ != 'mp'){
  149. global $__MINI_GLOBAL_FRONT_VERSION__;
  150. // todo: 临时
  151. if($__MINI_GLOBAL_FRONT_VERSION__ >= '1.2.2.24'){
  152. }else {
  153. $query = $query->where('is_wechat_sph', 0);
  154. }
  155. }
  156. if($__MINI_GLOBAL_MODE__ == 'examine'){
  157. $query = $query->where('post_type', '<>', 'video');
  158. }
  159. $data = $query->has('video')
  160. ->orderBy('id', 'desc')
  161. ->simplePaginate($limit, FieldUtils::postInfoColums());
  162. if($is_waterfall){
  163. PostUtils::reset_loop();
  164. $data->map(function ($v, $k) use ($uid){
  165. return PostUtils::WaterfallProcess($k, $v,1);
  166. });
  167. }else {
  168. $data = PostsRepositores::postsParame($data, $uid, 1);
  169. }
  170. }
  171. // 长图文
  172. else if ($tab_ == 6) {
  173. $query = (new Model())
  174. ->where('is_examine', 1)
  175. ->where('posts_state', 0);
  176. // 控制视频隐藏
  177. if($__MINI_GLOBAL_DEVICE__ != 'mp'){
  178. global $__MINI_GLOBAL_FRONT_VERSION__;
  179. // todo: 临时
  180. if($__MINI_GLOBAL_FRONT_VERSION__ >= '1.2.2.24'){
  181. }else {
  182. $query = $query->where('is_wechat_sph', 0);
  183. }
  184. }
  185. if($__MINI_GLOBAL_MODE__ == 'examine'){
  186. $query = $query->where('post_type', '<>', 'video');
  187. }
  188. $data = $query->where('post_type', 'single')
  189. ->orderBy('id', 'desc')
  190. ->simplePaginate($limit, FieldUtils::postInfoColums());
  191. if($is_waterfall){
  192. PostUtils::reset_loop();
  193. $data->map(function ($v, $k) use ($uid){
  194. return PostUtils::WaterfallProcess($k, $v,1);
  195. });
  196. }else {
  197. $data = PostsRepositores::postsParame($data, $uid, 1);
  198. }
  199. }
  200. else if ($tab_ == 7) {
  201. if($uid > 0){
  202. $user = WxUser::find($uid);
  203. global $__MINI_GLOBAL_IP__;
  204. $longitude = $user->longitude;
  205. $latitude = $user->latitude;
  206. if(_empty_($longitude) && _empty_($latitude)){
  207. $res = _ip_address($__MINI_GLOBAL_IP__);
  208. if(_empty_($res)){
  209. return $this->fail(503003);
  210. }
  211. $longitude = $res['longitude'];
  212. $latitude = $res['latitude'];
  213. WxUser::where('id', $uid)->update([
  214. 'ip' => $__MINI_GLOBAL_IP__
  215. ]);
  216. AttachIpAddressJob::dispatch(2, $uid)->delay(2);
  217. }
  218. $the_base_id = Cache::remember('posts:new:1000', 3600 * 5, function (){
  219. $base_id = WxPost::where('is_examine', 1)->where('posts_state', 0)->orderBy('id', 'desc')->skip(500)->value('id');
  220. return $base_id > 0 ? $base_id : 0;
  221. });
  222. $query = WxPost::select(DB::raw('*, ( 6378137 * acos( cos( radians('.$latitude.') ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians('.$longitude.') ) + sin( radians('.$latitude.') ) * sin( radians( latitude ) ) ) ) AS distance'))
  223. ->where('id', '>', $the_base_id)
  224. ->where('is_examine', 1)
  225. ->where('posts_state', 0);
  226. // 控制视频隐藏
  227. if($__MINI_GLOBAL_DEVICE__ != 'mp'){
  228. global $__MINI_GLOBAL_FRONT_VERSION__;
  229. // todo: 临时
  230. if($__MINI_GLOBAL_FRONT_VERSION__ >= '1.2.2.24'){
  231. }else {
  232. $query = $query->where('is_wechat_sph', 0);
  233. }
  234. }
  235. if($__MINI_GLOBAL_MODE__ == 'examine'){
  236. $query = $query->where('post_type', '<>', 'video');
  237. }
  238. $data = $query->where('user_id', '<>', $uid)
  239. ->having('distance', '<', 100000)
  240. ->inRandomOrder()
  241. ->simplePaginate($limit, FieldUtils::postInfoColums());
  242. if($is_waterfall){
  243. PostUtils::reset_loop();
  244. $data->map(function ($v, $k) use ($uid){
  245. return PostUtils::WaterfallProcess($k, $v,1);
  246. });
  247. }else {
  248. $data = PostsRepositores::postsParame($data, $uid, 1);
  249. }
  250. }else{
  251. return $this->fail(200003, [], '没有获取到您的经纬度信息');
  252. }
  253. }else if ($tab_ == 8) {
  254. // 圈子
  255. $circleIds = explode(',', $target_id);
  256. if(_empty_($circleIds)){
  257. return null;
  258. }
  259. $query = (new Model())
  260. ->where('is_examine', 1)
  261. ->where('posts_state', 0);
  262. // 控制视频隐藏
  263. if($__MINI_GLOBAL_DEVICE__ != 'mp'){
  264. global $__MINI_GLOBAL_FRONT_VERSION__;
  265. // todo: 临时
  266. if($__MINI_GLOBAL_FRONT_VERSION__ >= '1.2.2.24'){
  267. }else {
  268. $query = $query->where('is_wechat_sph', 0);
  269. }
  270. }
  271. if($__MINI_GLOBAL_MODE__ == 'examine'){
  272. $query = $query->where('post_type', '<>', 'video');
  273. }
  274. $data = $query->whereIn('circle_id', $circleIds)
  275. ->orderBy('id', 'desc')
  276. ->simplePaginate($limit, FieldUtils::postInfoColums());
  277. if($is_waterfall){
  278. PostUtils::reset_loop();
  279. $data->map(function ($v, $k) use ($uid){
  280. return PostUtils::WaterfallProcess($k, $v,1);
  281. });
  282. }else {
  283. $data = PostsRepositores::postsParame($data, $uid, 1);
  284. }
  285. }
  286. return $this->success($data);
  287. }else{
  288. $data = PostsRepositores::getListByPlate($uid, $plateId, $limit);
  289. return $this->success($data);
  290. }
  291. }
  292. }