IndexController.php 14 KB

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