IndexController.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Controllers\Account\UserController;
  4. use App\Jobs\SendEmail;
  5. use App\Models\Article;
  6. use App\Models\Authentication;
  7. use App\Models\Category;
  8. use App\Models\EmailToken;
  9. use App\Models\Exchange;
  10. use App\Models\FriendshipLink;
  11. use App\Models\Goods;
  12. use App\Models\Notice;
  13. use App\Models\Question;
  14. use App\Models\Recommendation;
  15. use App\Models\Tag;
  16. use App\Models\Taggable;
  17. use App\Models\User;
  18. use App\Models\UserData;
  19. use App\Services\CaptchaService;
  20. use App\Services\MailService;
  21. use App\Services\SmsService;
  22. use Carbon\Carbon;
  23. use Illuminate\Contracts\Auth\Guard;
  24. use Illuminate\Http\Request;
  25. use Illuminate\Support\Facades\Cache;
  26. use Illuminate\Support\Facades\DB;
  27. use App\Repositories\UserRepository;
  28. use Illuminate\Support\Facades\Hash;
  29. class IndexController extends Controller
  30. {
  31. /**
  32. * Display a listing of the resource.
  33. *
  34. * @return Response
  35. */
  36. protected $auth;
  37. protected $userRepository;
  38. protected $captchaService;
  39. public function __construct(Guard $auth,UserRepository $userRepository,CaptchaService $captchaService,User $user, UserData $userData){
  40. $this->auth = $auth;
  41. $this->userRepository = $userRepository;
  42. $this->captchaService = $captchaService;
  43. $this->user = $user;
  44. $this->userData = $userData;
  45. }
  46. // public function __construct(User $user, UserData $userData)
  47. // {
  48. // $this->user = $user;
  49. // $this->userData = $userData;
  50. // }
  51. public function index()
  52. {
  53. session_start();
  54. if(isset($_SESSION['login_time'])){
  55. $time = time()- $_SESSION['login_time'];
  56. if($time > 7200){
  57. setcookie("token",0,time()-7200,"/",".nxjiewei.com");
  58. unset($_SESSION['login_time']);
  59. $this->auth->logout();
  60. return redirect()->to(route('website.index'));
  61. }
  62. }
  63. if(isset($_COOKIE['token']) && Auth()->guest()==true){
  64. $_COOKIE['token'] = base64_decode($_COOKIE['token']);
  65. $data['email'] = explode(',',$_COOKIE['token'])[0];
  66. $data['_token'] = csrf_token();
  67. $data['password_confirmation'] = explode(',',$_COOKIE['token'])[1];
  68. $data['password'] = explode(',',$_COOKIE['token'])[1];
  69. $data['visit_ip'] = explode(',',$_COOKIE['token'])[2];
  70. $data['name'] = DB::select('select * from admins where staff_num = "'.$data['email'].'"')[0]->name;
  71. $user = DB::table('users')->where('email','=',$data['email'])->first();
  72. if($user == null){
  73. $this->zhuce($data);
  74. }else{
  75. $this->denglu($data);
  76. }
  77. }
  78. /*热门话题*/
  79. $hotTags = Taggable::globalHotTags();
  80. /*推荐内容*/
  81. $recommendItems= Cache::remember('recommend_items',Setting()->get('website_cache_time',1),function() {
  82. return Recommendation::where('status','>',0)->orderBy('sort','asc')->orderBy('updated_at','desc')->take(11)->get();
  83. });
  84. /*热门专家*/
  85. $hotExperts = Cache::remember('index_hot_experts',Setting()->get('website_cache_time',1),function(){
  86. return Authentication::hottest(8);
  87. });
  88. $hotUsers = Cache::remember('index_hot_users',30,function() {
  89. return UserData::hottest(20);
  90. });
  91. /*热门问题*/
  92. $newestQuestions = Cache::remember('index_newest_questions',Setting()->get('website_cache_time',1),function() {
  93. return Question::newest(0,12);
  94. });
  95. /*悬赏问题*/
  96. $rewardQuestions = Cache::remember('index_reward_questions',Setting()->get('website_cache_time',1),function() {
  97. return Question::reward(0,12);
  98. });
  99. /*热门文章*/
  100. $hotArticles = Cache::remember('index_hot_articles',Setting()->get('website_cache_time',1),function() {
  101. return Article::hottest(0,12);
  102. });
  103. /*最新文章*/
  104. $newestArticles = Cache::remember('index_newest_articles',Setting()->get('website_cache_time',1),function() {
  105. return Article::newest(0,12);
  106. });
  107. /*最新公告*/
  108. $newestNotices = Cache::remember('newest_notices',Setting()->get('website_cache_time',1),function() {
  109. return Notice::where('status','>','0')->orderBy('updated_at','DESC')->take(6)->get();
  110. });
  111. /*财富榜*/
  112. $topCoinUsers = Cache::remember('index_top_coin_users',Setting()->get('website_cache_time',1),function() {
  113. return UserData::top('coins',8);
  114. });
  115. /*友情链接*/
  116. $friendshipLinks = Cache::remember('friendship_links',Setting()->get('website_cache_time',1),function() {
  117. return FriendshipLink::where('status','=',1)->orderBy('sort','asc')->orderBy('created_at','asc')->take(50)->get();
  118. });
  119. $signDate = Carbon::today();
  120. return view('theme::home.index')->with(compact('hotUsers','recommendItems','hotExperts','newestQuestions','rewardQuestions','hotArticles','newestArticles','newestNotices','hotTags','topCoinUsers','friendshipLinks','signDate'));
  121. }
  122. public function zhuce($data){
  123. $data['status'] = 1;
  124. $data['password'] = Hash::make($data['password']);
  125. $data['site_notifications'] = implode(',', array_keys(config('tipask.notification_types')));
  126. $data['email_notifications'] = 'adopt_answer,invite_answer';
  127. $data['qrcode'] = '/static/images/default_avatar.jpg';
  128. $user = $this->user->create($data);
  129. if ($user) {
  130. $userData = [
  131. 'user_id' => $user->id,
  132. 'coins' => 0,
  133. 'credits' => 0,
  134. 'registered_at' => Carbon::now(),
  135. 'last_visit' => Carbon::now(),
  136. 'last_login_ip' => $data['visit_ip'],
  137. ];
  138. if ($user->mobile) {
  139. $userData['mobile_status'] = 1;
  140. }
  141. $this->userData->create($userData);
  142. }
  143. $user->attachRole(2); //默认注册为普通用户角色
  144. $this->auth->login($user);
  145. $_SESSION['login_time'] = time();
  146. $message = '注册成功!';
  147. if($this->credit($user->id,'register',Setting()->get('coins_register'),Setting()->get('credits_register'))){
  148. $message .= get_credit_message(Setting()->get('credits_register'),Setting()->get('coins_register'));
  149. }
  150. if(Setting()->get('register_type')=='email'){
  151. /*发送邮箱验证邮件*/
  152. $emailToken = EmailToken::create([
  153. 'email' => $user->email,
  154. 'token' => EmailToken::createToken(),
  155. 'action'=> 'register'
  156. ]);
  157. if($emailToken){
  158. $subject = '欢迎注册'.Setting()->get('website_name').',请激活您注册的邮箱!';
  159. $content = "「".$data['name']."」您好,请激活您在 ".Setting()->get('website_name')." 的注册邮箱!<br /> 请在1小时内点击该链接激活注册账号 → ".route('auth.email.verifyToken',['action'=>$emailToken->action,'token'=>$emailToken->token])."<br />如非本人操作,请忽略此邮件!";
  160. $this->sendEmail($emailToken->email,$subject,$content);
  161. }
  162. }
  163. /*记录注册ip*/
  164. $this->counter('register_number_'.md5($data['visit_ip']) , 1,86400 );
  165. }
  166. public function denglu($data){
  167. $credentials = [
  168. 'password' => $data['password']
  169. ];
  170. $credentials['email'] = $data['email'];
  171. /*根据邮箱地址和密码进行认证*/
  172. if ($this->auth->attempt($credentials, true))
  173. {
  174. $id = DB::table('users')->where('email','=',$data['email'])->first()->id;
  175. if($this->credit($id,'login',Setting()->get('coins_login'),Setting()->get('credits_login'))){
  176. $message = '登陆成功! '.get_credit_message(Setting()->get('credits_login'),Setting()->get('coins_login'));
  177. return $this->success(route('website.index'),$message,true);
  178. }
  179. $_SESSION['login_time'] = time();
  180. /*认证成功后跳转到首页*/
  181. return $this->success(route('auth.doing.index'),'登陆成功!',true);
  182. }
  183. }
  184. /*问答模块*/
  185. public function ask($categorySlug='all',$filter='newest')
  186. {
  187. $question = new Question();
  188. if(!method_exists($question,$filter)){
  189. abort(404);
  190. }
  191. $parentId=$currentCategoryId = 0;
  192. $currentCategory = null;
  193. if( $categorySlug != 'all' ){
  194. $category = Category::where("slug","=",$categorySlug)->first();
  195. if(!$category){
  196. abort(404);
  197. }
  198. $currentCategoryId = $category->id;
  199. $parentId = $category->parent_id;
  200. $currentCategory = $category;
  201. if($category->hasChild()){
  202. $parentId = $category->id;
  203. }
  204. }
  205. $questions = call_user_func([$question,$filter] , $currentCategoryId );
  206. /*热门话题*/
  207. $hotTags = Taggable::globalHotTags('questions');
  208. $categories = load_categories('questions');
  209. $parentCategories = Category::getParentCategories($parentId);
  210. $hotUsers = Cache::remember('ask_hot_users',Setting()->get('website_cache_time',1),function() {
  211. return UserData::activities(8);
  212. });
  213. return view('theme::home.ask')->with(compact('currentCategory','questions','hotUsers','hotTags','filter','categories','currentCategoryId','parentId','parentCategories','categorySlug'));
  214. }
  215. public function blog($categorySlug='all', $filter='newest')
  216. {
  217. $article = new Article();
  218. if(!method_exists($article,$filter)){
  219. abort(404);
  220. }
  221. $parentId=$currentCategoryId = 0;
  222. $currentCategory = null;
  223. if( $categorySlug != 'all' ){
  224. $category = Category::where("slug","=",$categorySlug)->first();
  225. if(!$category){
  226. abort(404);
  227. }
  228. $currentCategoryId = $category->id;
  229. $parentId = $category->parent_id;
  230. $currentCategory = $category;
  231. if($category->hasChild()){
  232. $parentId = $category->id;
  233. }
  234. }
  235. $articles = call_user_func([$article,$filter],$currentCategoryId);
  236. /*热门文章*/
  237. $hotArticles = Cache::remember('hot_articles',Setting()->get('website_cache_time',1),function() {
  238. return Article::recommended(0,8);
  239. });
  240. $hotUsers = UserData::activeInArticles();
  241. /*热门话题*/
  242. $hotTags = Taggable::globalHotTags('articles');
  243. $tabData = get_category_tab_data("articles",7);
  244. $parentCategories = Category::getParentCategories($parentId);
  245. return view('theme::home.blog')->with(compact('tabData','currentCategory','articles','hotUsers','hotTags','filter','currentCategoryId','parentId','parentCategories','categorySlug','hotArticles'));
  246. }
  247. public function topic( $categorySlug='all')
  248. {
  249. $parentId=$currentCategoryId = 0;
  250. $query = Tag::query();
  251. if( $categorySlug != 'all' ){
  252. $category = Category::where("slug","=",$categorySlug)->first();
  253. if(!$category){
  254. abort(404);
  255. }
  256. $currentCategoryId = $category->id;
  257. $parentId = $category->parent_id;
  258. if($category->hasChild()){
  259. $parentId = $category->id;
  260. }
  261. $query->whereIn('category_id',$category->getSubIds());
  262. }
  263. $categories = load_categories('tags');
  264. $parentCategories = Category::getParentCategories($parentId);
  265. $topics = $query->orderBy('followers','DESC')->paginate(20);
  266. return view('theme::home.topic')->with(compact('topics','categories','currentCategoryId','categorySlug','currentCategoryId','parentId','parentCategories'));
  267. }
  268. public function user()
  269. {
  270. $hotUsers = Cache::remember('index_user_hot_users',30,function() {
  271. return UserData::hottest(50);
  272. });
  273. $newUsers = Cache::remember('index_new_users',10,function() {
  274. return User::where("status",">",0)->orderBy("created_at","desc")->take(50)->get();
  275. });
  276. return view('theme::home.user')->with(compact('hotUsers','newUsers'));
  277. }
  278. public function experts(Request $request,$categorySlug='all',$provinceId='all'){
  279. $categories = load_categories('experts');
  280. $hotProvinces = Cache::remember('hot_expert_cities',Setting()->get('website_cache_time',1),function() {
  281. return Authentication::select('province', DB::raw('COUNT(user_id) as total'))->groupBy('province')->orderBy('total','desc')->get();
  282. });
  283. $query = Authentication::leftJoin('user_data', 'user_data.user_id', '=', 'authentications.user_id')->where('user_data.authentication_status','=',1);
  284. $categoryId = 0;
  285. if( $categorySlug != 'all' ){
  286. $category = Category::where("slug","=",$categorySlug)->first();
  287. if($category){
  288. $categoryId = $category->id;
  289. $query->where("authentications.category_id","=",$categoryId);
  290. }
  291. }
  292. if($provinceId != 'all'){
  293. $query->where("authentications.province","=",$provinceId);
  294. }
  295. $word = $request->input('word','');
  296. if($word){
  297. $query->where("authentications.real_name",'like',"$word%");
  298. }
  299. $experts = $query->orderBy('user_data.answers','DESC')
  300. ->orderBy('user_data.articles','DESC')
  301. ->orderBy('authentications.updated_at','DESC')
  302. ->select('authentications.user_id','authentications.real_name','authentications.description','authentications.title','user_data.coins','user_data.credits','user_data.followers','user_data.supports','user_data.answers','user_data.articles','user_data.authentication_status')
  303. ->paginate(16);
  304. return view('theme::home.expert')->with(compact('experts','categories','hotProvinces','categorySlug','categoryId','provinceId','word'));
  305. }
  306. public function shop($categorySlug='all')
  307. {
  308. $parentId=$currentCategoryId = 0;
  309. $query = Goods::query();
  310. if( $categorySlug != 'all' ){
  311. $category = Category::where("slug","=",$categorySlug)->first();
  312. if(!$category){
  313. abort(404);
  314. }
  315. $currentCategoryId = $category->id;
  316. $parentId = $category->parent_id;
  317. if($category->hasChild()){
  318. $parentId = $category->id;
  319. }
  320. $query->whereIn('category_id',$category->getSubIds());
  321. }
  322. $categories = load_categories('goods');
  323. $parentCategories = Category::getParentCategories($parentId);
  324. $goods = $query->where('status','>',0)->where('remnants','>',0)->orderBy('coins','asc')->paginate(16);
  325. $exchanges = Exchange::newest();
  326. return view('theme::home.shop')->with(compact('goods','exchanges','categories','currentCategoryId','categorySlug','currentCategoryId','parentId','parentCategories'));
  327. }
  328. }