| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392 |
- <?php
- namespace App\Http\Controllers;
- use App\Http\Controllers\Account\UserController;
- use App\Jobs\SendEmail;
- use App\Models\Article;
- use App\Models\Authentication;
- use App\Models\Category;
- use App\Models\EmailToken;
- use App\Models\Exchange;
- use App\Models\FriendshipLink;
- use App\Models\Goods;
- use App\Models\Notice;
- use App\Models\Question;
- use App\Models\Recommendation;
- use App\Models\Tag;
- use App\Models\Taggable;
- use App\Models\User;
- use App\Models\UserData;
- use App\Services\CaptchaService;
- use App\Services\MailService;
- use App\Services\SmsService;
- use Carbon\Carbon;
- use Illuminate\Contracts\Auth\Guard;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Cache;
- use Illuminate\Support\Facades\DB;
- use App\Repositories\UserRepository;
- use Illuminate\Support\Facades\Hash;
- class IndexController extends Controller
- {
- /**
- * Display a listing of the resource.
- *
- * @return Response
- */
- protected $auth;
- protected $userRepository;
- protected $captchaService;
- public function __construct(Guard $auth,UserRepository $userRepository,CaptchaService $captchaService,User $user, UserData $userData){
- $this->auth = $auth;
- $this->userRepository = $userRepository;
- $this->captchaService = $captchaService;
- $this->user = $user;
- $this->userData = $userData;
- }
- // public function __construct(User $user, UserData $userData)
- // {
- // $this->user = $user;
- // $this->userData = $userData;
- // }
- public function index()
- {
- session_start();
- if(isset($_SESSION['login_time'])){
- $time = time()- $_SESSION['login_time'];
- if($time > 7200){
- setcookie("token",0,time()-7200,"/",".nxjiewei.com");
- unset($_SESSION['login_time']);
- $this->auth->logout();
- return redirect()->to(route('website.index'));
- }
- }
- if(isset($_COOKIE['token'])){// && Auth()->guest()==true 加上切换账号问答系统不更换账号
- $_COOKIE['token'] = base64_decode($_COOKIE['token']);
- $data['email'] = explode(',',$_COOKIE['token'])[0];
- $data['_token'] = csrf_token();
- $data['password_confirmation'] = explode(',',$_COOKIE['token'])[1];
- $data['password'] = explode(',',$_COOKIE['token'])[1];
- $data['visit_ip'] = explode(',',$_COOKIE['token'])[2];
- $admins = DB::select('select * from admins where staff_num = "'.$data['email'].'"');
- //访问不同域名数据库
- if(sizeof($admins) >0){
- $data['name'] = $admins[0]->name;
- }else{
- $data['name'] = '未命名';
- }
- $user = DB::table('users')->where('email','=',$data['email'])->first();
- if($user == null){
- $this->zhuce($data);
- }else{
- //换密码了修改一下密码
- if(!Hash::check($data['password'],$user->password)){
- $res['password'] = Hash::make($data['password']);
- DB::table('users')->where('email',$data['email'])->update($res);
- }
- $this->denglu($data);
- }
- }
- /*热门话题*/
- $hotTags = Taggable::globalHotTags();
- /*推荐内容*/
- $recommendItems= Cache::remember('recommend_items',Setting()->get('website_cache_time',1),function() {
- return Recommendation::where('status','>',0)->orderBy('sort','asc')->orderBy('updated_at','desc')->take(11)->get();
- });
- /*热门专家*/
- $hotExperts = Cache::remember('index_hot_experts',Setting()->get('website_cache_time',1),function(){
- return Authentication::hottest(4);
- });
- $hotUsers = Cache::remember('index_hot_users',30,function() {
- return UserData::hottest(20);
- });
- /*热门问题*/
- $newestQuestions = Cache::remember('index_newest_questions',Setting()->get('website_cache_time',1),function() {
- return Question::newest(0,5);
- });
- /*悬赏问题*/
- $rewardQuestions = Cache::remember('index_reward_questions',Setting()->get('website_cache_time',1),function() {
- return Question::reward(0,5);
- });
- /*热门文章*/
- // $hotArticles = Cache::remember('index_hot_articles',Setting()->get('website_cache_time',1),function() {
- // return Article::hottest(0,5);
- // });
- $hotArticles = DB::table('articles')
- ->where('status','>',0)
- ->orderBy('views','desc')
- ->paginate(5)
- ->all();
- /*最新文章*/
- // $newestArticles = Cache::remember('index_newest_articles',Setting()->get('website_cache_time',1),function() {
- // return Article::newest(0,5);
- // });
- $newestArticles = DB::table('articles')
- ->where('status','>',0)
- ->orderBy('updated_at','desc')
- ->paginate(5)
- ->all();
- /*最新公告*/
- $newestNotices = Cache::remember('newest_notices',Setting()->get('website_cache_time',1),function() {
- return Notice::where('status','>','0')->orderBy('updated_at','DESC')->take(6)->get();
- });
- /*财富榜*/
- $topCoinUsers = Cache::remember('index_top_coin_users',Setting()->get('website_cache_time',1),function() {
- return UserData::top('coins',8);
- });
- /*友情链接*/
- $friendshipLinks = Cache::remember('friendship_links',Setting()->get('website_cache_time',1),function() {
- return FriendshipLink::where('status','=',1)->orderBy('sort','asc')->orderBy('created_at','asc')->take(50)->get();
- });
- $signDate = Carbon::today();
- return view('theme::home.index')->with(compact('hotUsers','recommendItems','hotExperts','newestQuestions','rewardQuestions','hotArticles','newestArticles','newestNotices','hotTags','topCoinUsers','friendshipLinks','signDate'));
- }
- public function zhuce($data){
- $data['status'] = 1;
- $data['password'] = Hash::make($data['password']);
- $data['site_notifications'] = implode(',', array_keys(config('tipask.notification_types')));
- $data['email_notifications'] = 'adopt_answer,invite_answer';
- $data['qrcode'] = '/static/images/default_avatar.jpg';
- $user = $this->user->create($data);
- if ($user) {
- $userData = [
- 'user_id' => $user->id,
- 'coins' => 0,
- 'credits' => 0,
- 'registered_at' => Carbon::now(),
- 'last_visit' => Carbon::now(),
- 'last_login_ip' => $data['visit_ip'],
- ];
- if ($user->mobile) {
- $userData['mobile_status'] = 1;
- }
- $this->userData->create($userData);
- }
- $user->attachRole(2); //默认注册为普通用户角色
- $this->auth->login($user);
- $_SESSION['login_time'] = time();
- $message = '注册成功!';
- if($this->credit($user->id,'register',Setting()->get('coins_register'),Setting()->get('credits_register'))){
- $message .= get_credit_message(Setting()->get('credits_register'),Setting()->get('coins_register'));
- }
- if(Setting()->get('register_type')=='email'){
- /*发送邮箱验证邮件*/
- $emailToken = EmailToken::create([
- 'email' => $user->email,
- 'token' => EmailToken::createToken(),
- 'action'=> 'register'
- ]);
- if($emailToken){
- $subject = '欢迎注册'.Setting()->get('website_name').',请激活您注册的邮箱!';
- $content = "「".$data['name']."」您好,请激活您在 ".Setting()->get('website_name')." 的注册邮箱!<br /> 请在1小时内点击该链接激活注册账号 → ".route('auth.email.verifyToken',['action'=>$emailToken->action,'token'=>$emailToken->token])."<br />如非本人操作,请忽略此邮件!";
- $this->sendEmail($emailToken->email,$subject,$content);
- }
- }
- /*记录注册ip*/
- $this->counter('register_number_'.md5($data['visit_ip']) , 1,86400 );
- }
- public function denglu($data){
- $credentials = [
- 'password' => $data['password']
- ];
- $credentials['email'] = $data['email'];
- /*根据邮箱地址和密码进行认证*/
- if ($this->auth->attempt($credentials, true))
- {
- $id = DB::table('users')->where('email','=',$data['email'])->first()->id;
- if($this->credit($id,'login',Setting()->get('coins_login'),Setting()->get('credits_login'))){
- $message = '登陆成功! '.get_credit_message(Setting()->get('credits_login'),Setting()->get('coins_login'));
- return $this->success(route('website.index'),$message,true);
- }
- $_SESSION['login_time'] = time();
- /*认证成功后跳转到首页*/
- return $this->success(route('auth.doing.index'),'登陆成功!',true);
- }
- }
- /*问答模块*/
- public function ask($categorySlug='all',$filter='newest')
- {
- $question = new Question();
- if(!method_exists($question,$filter)){
- abort(404);
- }
- $parentId=$currentCategoryId = 0;
- $currentCategory = null;
- if( $categorySlug != 'all' ){
- $category = Category::where("slug","=",$categorySlug)->first();
- if(!$category){
- abort(404);
- }
- $currentCategoryId = $category->id;
- $parentId = $category->parent_id;
- $currentCategory = $category;
- if($category->hasChild()){
- $parentId = $category->id;
- }
- }
- $questions = call_user_func([$question,$filter] , $currentCategoryId );
- /*热门话题*/
- $hotTags = Taggable::globalHotTags('questions');
- $categories = load_categories('questions');
- $parentCategories = Category::getParentCategories($parentId);
- $hotUsers = Cache::remember('ask_hot_users',Setting()->get('website_cache_time',1),function() {
- return UserData::activities(8);
- });
- return view('theme::home.ask')->with(compact('currentCategory','questions','hotUsers','hotTags','filter','categories','currentCategoryId','parentId','parentCategories','categorySlug'));
- }
- public function blog($categorySlug='all', $filter='newest')
- {
- $article = new Article();
- if(!method_exists($article,$filter)){
- abort(404);
- }
- $parentId=$currentCategoryId = 0;
- $currentCategory = null;
- if( $categorySlug != 'all' ){
- $category = Category::where("slug","=",$categorySlug)->first();
- if(!$category){
- abort(404);
- }
- $currentCategoryId = $category->id;
- $parentId = $category->parent_id;
- $currentCategory = $category;
- if($category->hasChild()){
- $parentId = $category->id;
- }
- }
- $articles = call_user_func([$article,$filter],$currentCategoryId);
- /*热门文章*/
- $hotArticles = Cache::remember('hot_articles',Setting()->get('website_cache_time',1),function() {
- return Article::recommended(0,8);
- });
- $hotUsers = UserData::activeInArticles();
- /*热门话题*/
- $hotTags = Taggable::globalHotTags('articles');
- $tabData = get_category_tab_data("articles",7);
- $parentCategories = Category::getParentCategories($parentId);
- return view('theme::home.blog')->with(compact('tabData','currentCategory','articles','hotUsers','hotTags','filter','currentCategoryId','parentId','parentCategories','categorySlug','hotArticles'));
- }
- public function topic( $categorySlug='all')
- {
- $parentId=$currentCategoryId = 0;
- $query = Tag::query();
- if( $categorySlug != 'all' ){
- $category = Category::where("slug","=",$categorySlug)->first();
- if(!$category){
- abort(404);
- }
- $currentCategoryId = $category->id;
- $parentId = $category->parent_id;
- if($category->hasChild()){
- $parentId = $category->id;
- }
- $query->whereIn('category_id',$category->getSubIds());
- }
- $categories = load_categories('tags');
- $parentCategories = Category::getParentCategories($parentId);
- $topics = $query->orderBy('followers','DESC')->paginate(20);
- return view('theme::home.topic')->with(compact('topics','categories','currentCategoryId','categorySlug','currentCategoryId','parentId','parentCategories'));
- }
- public function user()
- {
- $hotUsers = Cache::remember('index_user_hot_users',30,function() {
- return UserData::hottest(50);
- });
- $newUsers = Cache::remember('index_new_users',10,function() {
- return User::where("status",">",0)->orderBy("created_at","desc")->take(50)->get();
- });
- return view('theme::home.user')->with(compact('hotUsers','newUsers'));
- }
- public function experts(Request $request,$categorySlug='all',$provinceId='all'){
- $categories = load_categories('experts');
- $hotProvinces = Cache::remember('hot_expert_cities',Setting()->get('website_cache_time',1),function() {
- return Authentication::select('province', DB::raw('COUNT(user_id) as total'))->groupBy('province')->orderBy('total','desc')->get();
- });
- $query = Authentication::leftJoin('user_data', 'user_data.user_id', '=', 'authentications.user_id')->where('user_data.authentication_status','=',1);
- $categoryId = 0;
- if( $categorySlug != 'all' ){
- $category = Category::where("slug","=",$categorySlug)->first();
- if($category){
- $categoryId = $category->id;
- $query->where("authentications.category_id","=",$categoryId);
- }
- }
- if($provinceId != 'all'){
- $query->where("authentications.province","=",$provinceId);
- }
- $word = $request->input('word','');
- if($word){
- $query->where("authentications.real_name",'like',"$word%");
- }
- $experts = $query->orderBy('user_data.answers','DESC')
- ->orderBy('user_data.articles','DESC')
- ->orderBy('authentications.updated_at','DESC')
- ->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')
- ->paginate(16);
- return view('theme::home.expert')->with(compact('experts','categories','hotProvinces','categorySlug','categoryId','provinceId','word'));
- }
- public function shop($categorySlug='all')
- {
- $parentId=$currentCategoryId = 0;
- $query = Goods::query();
- if( $categorySlug != 'all' ){
- $category = Category::where("slug","=",$categorySlug)->first();
- if(!$category){
- abort(404);
- }
- $currentCategoryId = $category->id;
- $parentId = $category->parent_id;
- if($category->hasChild()){
- $parentId = $category->id;
- }
- $query->whereIn('category_id',$category->getSubIds());
- }
- $categories = load_categories('goods');
- $parentCategories = Category::getParentCategories($parentId);
- $goods = $query->where('status','>',0)->where('remnants','>',0)->orderBy('coins','asc')->paginate(16);
- $exchanges = Exchange::newest();
- return view('theme::home.shop')->with(compact('goods','exchanges','categories','currentCategoryId','categorySlug','currentCategoryId','parentId','parentCategories'));
- }
- }
|