IndexController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. $data['qrcode'] = '/static/images/default_avatar.jpg';
  117. $user = $this->user->create($data);
  118. if ($user) {
  119. $userData = [
  120. 'user_id' => $user->id,
  121. 'coins' => 0,
  122. 'credits' => 0,
  123. 'registered_at' => Carbon::now(),
  124. 'last_visit' => Carbon::now(),
  125. 'last_login_ip' => $data['visit_ip'],
  126. ];
  127. if ($user->mobile) {
  128. $userData['mobile_status'] = 1;
  129. }
  130. $this->userData->create($userData);
  131. }
  132. $user->attachRole(2); //默认注册为普通用户角色
  133. $this->auth->login($user);
  134. $message = '注册成功!';
  135. if($this->credit($user->id,'register',Setting()->get('coins_register'),Setting()->get('credits_register'))){
  136. $message .= get_credit_message(Setting()->get('credits_register'),Setting()->get('coins_register'));
  137. }
  138. if(Setting()->get('register_type')=='email'){
  139. /*发送邮箱验证邮件*/
  140. $emailToken = EmailToken::create([
  141. 'email' => $user->email,
  142. 'token' => EmailToken::createToken(),
  143. 'action'=> 'register'
  144. ]);
  145. if($emailToken){
  146. $subject = '欢迎注册'.Setting()->get('website_name').',请激活您注册的邮箱!';
  147. $content = "「".$data['name']."」您好,请激活您在 ".Setting()->get('website_name')." 的注册邮箱!<br /> 请在1小时内点击该链接激活注册账号 → ".route('auth.email.verifyToken',['action'=>$emailToken->action,'token'=>$emailToken->token])."<br />如非本人操作,请忽略此邮件!";
  148. $this->sendEmail($emailToken->email,$subject,$content);
  149. }
  150. }
  151. /*记录注册ip*/
  152. $this->counter('register_number_'.md5($data['visit_ip']) , 1,86400 );
  153. }
  154. public function denglu($data){
  155. $credentials = [
  156. 'password' => $data['password']
  157. ];
  158. $credentials['email'] = $data['email'];
  159. /*根据邮箱地址和密码进行认证*/
  160. if ($this->auth->attempt($credentials, true))
  161. {
  162. $id = DB::table('users')->where('email','=',$data['email'])->first()->id;
  163. if($this->credit($id,'login',Setting()->get('coins_login'),Setting()->get('credits_login'))){
  164. $message = '登陆成功! '.get_credit_message(Setting()->get('credits_login'),Setting()->get('coins_login'));
  165. return $this->success(route('website.index'),$message,true);
  166. }
  167. /*认证成功后跳转到首页*/
  168. return $this->success(route('auth.doing.index'),'登陆成功!',true);
  169. }
  170. }
  171. /*问答模块*/
  172. public function ask($categorySlug='all',$filter='newest')
  173. {
  174. $question = new Question();
  175. if(!method_exists($question,$filter)){
  176. abort(404);
  177. }
  178. $parentId=$currentCategoryId = 0;
  179. $currentCategory = null;
  180. if( $categorySlug != 'all' ){
  181. $category = Category::where("slug","=",$categorySlug)->first();
  182. if(!$category){
  183. abort(404);
  184. }
  185. $currentCategoryId = $category->id;
  186. $parentId = $category->parent_id;
  187. $currentCategory = $category;
  188. if($category->hasChild()){
  189. $parentId = $category->id;
  190. }
  191. }
  192. $questions = call_user_func([$question,$filter] , $currentCategoryId );
  193. /*热门话题*/
  194. $hotTags = Taggable::globalHotTags('questions');
  195. $categories = load_categories('questions');
  196. $parentCategories = Category::getParentCategories($parentId);
  197. $hotUsers = Cache::remember('ask_hot_users',Setting()->get('website_cache_time',1),function() {
  198. return UserData::activities(8);
  199. });
  200. return view('theme::home.ask')->with(compact('currentCategory','questions','hotUsers','hotTags','filter','categories','currentCategoryId','parentId','parentCategories','categorySlug'));
  201. }
  202. public function blog($categorySlug='all', $filter='newest')
  203. {
  204. $article = new Article();
  205. if(!method_exists($article,$filter)){
  206. abort(404);
  207. }
  208. $parentId=$currentCategoryId = 0;
  209. $currentCategory = null;
  210. if( $categorySlug != 'all' ){
  211. $category = Category::where("slug","=",$categorySlug)->first();
  212. if(!$category){
  213. abort(404);
  214. }
  215. $currentCategoryId = $category->id;
  216. $parentId = $category->parent_id;
  217. $currentCategory = $category;
  218. if($category->hasChild()){
  219. $parentId = $category->id;
  220. }
  221. }
  222. $articles = call_user_func([$article,$filter],$currentCategoryId);
  223. /*热门文章*/
  224. $hotArticles = Cache::remember('hot_articles',Setting()->get('website_cache_time',1),function() {
  225. return Article::recommended(0,8);
  226. });
  227. $hotUsers = UserData::activeInArticles();
  228. /*热门话题*/
  229. $hotTags = Taggable::globalHotTags('articles');
  230. $tabData = get_category_tab_data("articles",7);
  231. $parentCategories = Category::getParentCategories($parentId);
  232. return view('theme::home.blog')->with(compact('tabData','currentCategory','articles','hotUsers','hotTags','filter','currentCategoryId','parentId','parentCategories','categorySlug','hotArticles'));
  233. }
  234. public function topic( $categorySlug='all')
  235. {
  236. $parentId=$currentCategoryId = 0;
  237. $query = Tag::query();
  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. if($category->hasChild()){
  246. $parentId = $category->id;
  247. }
  248. $query->whereIn('category_id',$category->getSubIds());
  249. }
  250. $categories = load_categories('tags');
  251. $parentCategories = Category::getParentCategories($parentId);
  252. $topics = $query->orderBy('followers','DESC')->paginate(20);
  253. return view('theme::home.topic')->with(compact('topics','categories','currentCategoryId','categorySlug','currentCategoryId','parentId','parentCategories'));
  254. }
  255. public function user()
  256. {
  257. $hotUsers = Cache::remember('index_user_hot_users',30,function() {
  258. return UserData::hottest(50);
  259. });
  260. $newUsers = Cache::remember('index_new_users',10,function() {
  261. return User::where("status",">",0)->orderBy("created_at","desc")->take(50)->get();
  262. });
  263. return view('theme::home.user')->with(compact('hotUsers','newUsers'));
  264. }
  265. public function experts(Request $request,$categorySlug='all',$provinceId='all'){
  266. $categories = load_categories('experts');
  267. $hotProvinces = Cache::remember('hot_expert_cities',Setting()->get('website_cache_time',1),function() {
  268. return Authentication::select('province', DB::raw('COUNT(user_id) as total'))->groupBy('province')->orderBy('total','desc')->get();
  269. });
  270. $query = Authentication::leftJoin('user_data', 'user_data.user_id', '=', 'authentications.user_id')->where('user_data.authentication_status','=',1);
  271. $categoryId = 0;
  272. if( $categorySlug != 'all' ){
  273. $category = Category::where("slug","=",$categorySlug)->first();
  274. if($category){
  275. $categoryId = $category->id;
  276. $query->where("authentications.category_id","=",$categoryId);
  277. }
  278. }
  279. if($provinceId != 'all'){
  280. $query->where("authentications.province","=",$provinceId);
  281. }
  282. $word = $request->input('word','');
  283. if($word){
  284. $query->where("authentications.real_name",'like',"$word%");
  285. }
  286. $experts = $query->orderBy('user_data.answers','DESC')
  287. ->orderBy('user_data.articles','DESC')
  288. ->orderBy('authentications.updated_at','DESC')
  289. ->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')
  290. ->paginate(16);
  291. return view('theme::home.expert')->with(compact('experts','categories','hotProvinces','categorySlug','categoryId','provinceId','word'));
  292. }
  293. public function shop($categorySlug='all')
  294. {
  295. $parentId=$currentCategoryId = 0;
  296. $query = Goods::query();
  297. if( $categorySlug != 'all' ){
  298. $category = Category::where("slug","=",$categorySlug)->first();
  299. if(!$category){
  300. abort(404);
  301. }
  302. $currentCategoryId = $category->id;
  303. $parentId = $category->parent_id;
  304. if($category->hasChild()){
  305. $parentId = $category->id;
  306. }
  307. $query->whereIn('category_id',$category->getSubIds());
  308. }
  309. $categories = load_categories('goods');
  310. $parentCategories = Category::getParentCategories($parentId);
  311. $goods = $query->where('status','>',0)->where('remnants','>',0)->orderBy('coins','asc')->paginate(16);
  312. $exchanges = Exchange::newest();
  313. return view('theme::home.shop')->with(compact('goods','exchanges','categories','currentCategoryId','categorySlug','currentCategoryId','parentId','parentCategories'));
  314. }
  315. }