IndexController.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. //换密码了修改一下密码
  76. if(!Hash::check($data['password'],$user->password)){
  77. $res['password'] = Hash::make($data['password']);
  78. DB::table('users')->where('email',$data['email'])->update($res);
  79. }
  80. $this->denglu($data);
  81. }
  82. }
  83. /*热门话题*/
  84. $hotTags = Taggable::globalHotTags();
  85. /*推荐内容*/
  86. $recommendItems= Cache::remember('recommend_items',Setting()->get('website_cache_time',1),function() {
  87. return Recommendation::where('status','>',0)->orderBy('sort','asc')->orderBy('updated_at','desc')->take(11)->get();
  88. });
  89. /*热门专家*/
  90. $hotExperts = Cache::remember('index_hot_experts',Setting()->get('website_cache_time',1),function(){
  91. return Authentication::hottest(4);
  92. });
  93. $hotUsers = Cache::remember('index_hot_users',30,function() {
  94. return UserData::hottest(20);
  95. });
  96. /*热门问题*/
  97. $newestQuestions = Cache::remember('index_newest_questions',Setting()->get('website_cache_time',1),function() {
  98. return Question::newest(0,5);
  99. });
  100. /*悬赏问题*/
  101. $rewardQuestions = Cache::remember('index_reward_questions',Setting()->get('website_cache_time',1),function() {
  102. return Question::reward(0,5);
  103. });
  104. /*热门文章*/
  105. // $hotArticles = Cache::remember('index_hot_articles',Setting()->get('website_cache_time',1),function() {
  106. // return Article::hottest(0,5);
  107. // });
  108. $hotArticles = DB::table('articles')
  109. ->where('status','>',0)
  110. ->orderBy('views','desc')
  111. ->paginate(5)
  112. ->all();
  113. /*最新文章*/
  114. // $newestArticles = Cache::remember('index_newest_articles',Setting()->get('website_cache_time',1),function() {
  115. // return Article::newest(0,5);
  116. // });
  117. $newestArticles = DB::table('articles')
  118. ->where('status','>',0)
  119. ->orderBy('updated_at','desc')
  120. ->paginate(5)
  121. ->all();
  122. /*最新公告*/
  123. $newestNotices = Cache::remember('newest_notices',Setting()->get('website_cache_time',1),function() {
  124. return Notice::where('status','>','0')->orderBy('updated_at','DESC')->take(6)->get();
  125. });
  126. /*财富榜*/
  127. $topCoinUsers = Cache::remember('index_top_coin_users',Setting()->get('website_cache_time',1),function() {
  128. return UserData::top('coins',8);
  129. });
  130. /*友情链接*/
  131. $friendshipLinks = Cache::remember('friendship_links',Setting()->get('website_cache_time',1),function() {
  132. return FriendshipLink::where('status','=',1)->orderBy('sort','asc')->orderBy('created_at','asc')->take(50)->get();
  133. });
  134. $signDate = Carbon::today();
  135. return view('theme::home.index')->with(compact('hotUsers','recommendItems','hotExperts','newestQuestions','rewardQuestions','hotArticles','newestArticles','newestNotices','hotTags','topCoinUsers','friendshipLinks','signDate'));
  136. }
  137. public function zhuce($data){
  138. $data['status'] = 1;
  139. $data['password'] = Hash::make($data['password']);
  140. $data['site_notifications'] = implode(',', array_keys(config('tipask.notification_types')));
  141. $data['email_notifications'] = 'adopt_answer,invite_answer';
  142. $data['qrcode'] = '/static/images/default_avatar.jpg';
  143. $user = $this->user->create($data);
  144. if ($user) {
  145. $userData = [
  146. 'user_id' => $user->id,
  147. 'coins' => 0,
  148. 'credits' => 0,
  149. 'registered_at' => Carbon::now(),
  150. 'last_visit' => Carbon::now(),
  151. 'last_login_ip' => $data['visit_ip'],
  152. ];
  153. if ($user->mobile) {
  154. $userData['mobile_status'] = 1;
  155. }
  156. $this->userData->create($userData);
  157. }
  158. $user->attachRole(2); //默认注册为普通用户角色
  159. $this->auth->login($user);
  160. $_SESSION['login_time'] = time();
  161. $message = '注册成功!';
  162. if($this->credit($user->id,'register',Setting()->get('coins_register'),Setting()->get('credits_register'))){
  163. $message .= get_credit_message(Setting()->get('credits_register'),Setting()->get('coins_register'));
  164. }
  165. if(Setting()->get('register_type')=='email'){
  166. /*发送邮箱验证邮件*/
  167. $emailToken = EmailToken::create([
  168. 'email' => $user->email,
  169. 'token' => EmailToken::createToken(),
  170. 'action'=> 'register'
  171. ]);
  172. if($emailToken){
  173. $subject = '欢迎注册'.Setting()->get('website_name').',请激活您注册的邮箱!';
  174. $content = "「".$data['name']."」您好,请激活您在 ".Setting()->get('website_name')." 的注册邮箱!<br /> 请在1小时内点击该链接激活注册账号 → ".route('auth.email.verifyToken',['action'=>$emailToken->action,'token'=>$emailToken->token])."<br />如非本人操作,请忽略此邮件!";
  175. $this->sendEmail($emailToken->email,$subject,$content);
  176. }
  177. }
  178. /*记录注册ip*/
  179. $this->counter('register_number_'.md5($data['visit_ip']) , 1,86400 );
  180. }
  181. public function denglu($data){
  182. $credentials = [
  183. 'password' => $data['password']
  184. ];
  185. $credentials['email'] = $data['email'];
  186. /*根据邮箱地址和密码进行认证*/
  187. if ($this->auth->attempt($credentials, true))
  188. {
  189. $id = DB::table('users')->where('email','=',$data['email'])->first()->id;
  190. if($this->credit($id,'login',Setting()->get('coins_login'),Setting()->get('credits_login'))){
  191. $message = '登陆成功! '.get_credit_message(Setting()->get('credits_login'),Setting()->get('coins_login'));
  192. return $this->success(route('website.index'),$message,true);
  193. }
  194. $_SESSION['login_time'] = time();
  195. /*认证成功后跳转到首页*/
  196. return $this->success(route('auth.doing.index'),'登陆成功!',true);
  197. }
  198. }
  199. /*问答模块*/
  200. public function ask($categorySlug='all',$filter='newest')
  201. {
  202. $question = new Question();
  203. if(!method_exists($question,$filter)){
  204. abort(404);
  205. }
  206. $parentId=$currentCategoryId = 0;
  207. $currentCategory = null;
  208. if( $categorySlug != 'all' ){
  209. $category = Category::where("slug","=",$categorySlug)->first();
  210. if(!$category){
  211. abort(404);
  212. }
  213. $currentCategoryId = $category->id;
  214. $parentId = $category->parent_id;
  215. $currentCategory = $category;
  216. if($category->hasChild()){
  217. $parentId = $category->id;
  218. }
  219. }
  220. $questions = call_user_func([$question,$filter] , $currentCategoryId );
  221. /*热门话题*/
  222. $hotTags = Taggable::globalHotTags('questions');
  223. $categories = load_categories('questions');
  224. $parentCategories = Category::getParentCategories($parentId);
  225. $hotUsers = Cache::remember('ask_hot_users',Setting()->get('website_cache_time',1),function() {
  226. return UserData::activities(8);
  227. });
  228. return view('theme::home.ask')->with(compact('currentCategory','questions','hotUsers','hotTags','filter','categories','currentCategoryId','parentId','parentCategories','categorySlug'));
  229. }
  230. public function blog($categorySlug='all', $filter='newest')
  231. {
  232. $article = new Article();
  233. if(!method_exists($article,$filter)){
  234. abort(404);
  235. }
  236. $parentId=$currentCategoryId = 0;
  237. $currentCategory = null;
  238. if( $categorySlug != 'all' ){
  239. $category = Category::where("slug","=",$categorySlug)->first();
  240. if(!$category){
  241. abort(404);
  242. }
  243. $currentCategoryId = $category->id;
  244. $parentId = $category->parent_id;
  245. $currentCategory = $category;
  246. if($category->hasChild()){
  247. $parentId = $category->id;
  248. }
  249. }
  250. $articles = call_user_func([$article,$filter],$currentCategoryId);
  251. /*热门文章*/
  252. $hotArticles = Cache::remember('hot_articles',Setting()->get('website_cache_time',1),function() {
  253. return Article::recommended(0,8);
  254. });
  255. $hotUsers = UserData::activeInArticles();
  256. /*热门话题*/
  257. $hotTags = Taggable::globalHotTags('articles');
  258. $tabData = get_category_tab_data("articles",7);
  259. $parentCategories = Category::getParentCategories($parentId);
  260. return view('theme::home.blog')->with(compact('tabData','currentCategory','articles','hotUsers','hotTags','filter','currentCategoryId','parentId','parentCategories','categorySlug','hotArticles'));
  261. }
  262. public function topic( $categorySlug='all')
  263. {
  264. $parentId=$currentCategoryId = 0;
  265. $query = Tag::query();
  266. if( $categorySlug != 'all' ){
  267. $category = Category::where("slug","=",$categorySlug)->first();
  268. if(!$category){
  269. abort(404);
  270. }
  271. $currentCategoryId = $category->id;
  272. $parentId = $category->parent_id;
  273. if($category->hasChild()){
  274. $parentId = $category->id;
  275. }
  276. $query->whereIn('category_id',$category->getSubIds());
  277. }
  278. $categories = load_categories('tags');
  279. $parentCategories = Category::getParentCategories($parentId);
  280. $topics = $query->orderBy('followers','DESC')->paginate(20);
  281. return view('theme::home.topic')->with(compact('topics','categories','currentCategoryId','categorySlug','currentCategoryId','parentId','parentCategories'));
  282. }
  283. public function user()
  284. {
  285. $hotUsers = Cache::remember('index_user_hot_users',30,function() {
  286. return UserData::hottest(50);
  287. });
  288. $newUsers = Cache::remember('index_new_users',10,function() {
  289. return User::where("status",">",0)->orderBy("created_at","desc")->take(50)->get();
  290. });
  291. return view('theme::home.user')->with(compact('hotUsers','newUsers'));
  292. }
  293. public function experts(Request $request,$categorySlug='all',$provinceId='all'){
  294. $categories = load_categories('experts');
  295. $hotProvinces = Cache::remember('hot_expert_cities',Setting()->get('website_cache_time',1),function() {
  296. return Authentication::select('province', DB::raw('COUNT(user_id) as total'))->groupBy('province')->orderBy('total','desc')->get();
  297. });
  298. $query = Authentication::leftJoin('user_data', 'user_data.user_id', '=', 'authentications.user_id')->where('user_data.authentication_status','=',1);
  299. $categoryId = 0;
  300. if( $categorySlug != 'all' ){
  301. $category = Category::where("slug","=",$categorySlug)->first();
  302. if($category){
  303. $categoryId = $category->id;
  304. $query->where("authentications.category_id","=",$categoryId);
  305. }
  306. }
  307. if($provinceId != 'all'){
  308. $query->where("authentications.province","=",$provinceId);
  309. }
  310. $word = $request->input('word','');
  311. if($word){
  312. $query->where("authentications.real_name",'like',"$word%");
  313. }
  314. $experts = $query->orderBy('user_data.answers','DESC')
  315. ->orderBy('user_data.articles','DESC')
  316. ->orderBy('authentications.updated_at','DESC')
  317. ->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')
  318. ->paginate(16);
  319. return view('theme::home.expert')->with(compact('experts','categories','hotProvinces','categorySlug','categoryId','provinceId','word'));
  320. }
  321. public function shop($categorySlug='all')
  322. {
  323. $parentId=$currentCategoryId = 0;
  324. $query = Goods::query();
  325. if( $categorySlug != 'all' ){
  326. $category = Category::where("slug","=",$categorySlug)->first();
  327. if(!$category){
  328. abort(404);
  329. }
  330. $currentCategoryId = $category->id;
  331. $parentId = $category->parent_id;
  332. if($category->hasChild()){
  333. $parentId = $category->id;
  334. }
  335. $query->whereIn('category_id',$category->getSubIds());
  336. }
  337. $categories = load_categories('goods');
  338. $parentCategories = Category::getParentCategories($parentId);
  339. $goods = $query->where('status','>',0)->where('remnants','>',0)->orderBy('coins','asc')->paginate(16);
  340. $exchanges = Exchange::newest();
  341. return view('theme::home.shop')->with(compact('goods','exchanges','categories','currentCategoryId','categorySlug','currentCategoryId','parentId','parentCategories'));
  342. }
  343. }