IndexController.php 16 KB

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