<?php
/**
 *  Guojiz网址导航系统
 *	www.guojiz.com
 *  作者：梦雨
 *  @ QQ50361804
 */
namespace app\index\controller;
use think\Controller;
use think\Db;
use app\index\model\Member as MemberModel;
class Login extends Controller
{
    public function _initialize()
    {
        if (!session('userid') || !session('username')) {
        $show['show'] = 1;
        $tpto = Db::name('category')->where($show)->order('sort desc')->limit(120)->select();
        $this->assign('tpto', $tpto);
        $GZdh = Db::name('links')->where($show)->order('px desc')->select();
        $this->assign('GZdh', $GZdh);
        $keyss = Db::name('member')->where('userid = 1')->value('key');
        $this->assign('keyss', $keyss);
        $newscate = Db::name('newscate')->where($show)->order('id desc')->limit(120)->select();
        $this->assign('newscate', $newscate);
        $guojizgg = Db::name('banner')->where($show)->order('id desc')->select();
        $this->assign('guojizgg', $guojizgg);
        $html = Db::name('html')->where('open = 1')->select();
        $this->assign('html', $html);
        $htmls = Db::name('html')->order('id desc')->select();
        $this->assign('htmls', $htmls);
        $news = Db::name('news')->where($show)->order('id desc')->select();
        $this->assign('news', $news);
        $newscate = Db::name('newscate')->where($show)->order('id desc')->select();
        $this->assign('newscate', $newscate);
        $wang = Db::name('wang')->where('open = 1')->order('id desc')->select();
        $this->assign('wang', $wang);
        $wangcate = Db::name('wangcate')->where($show)->order('id desc')->select();
        $this->assign('wangcate', $wangcate);
        $guojizcai = Db::name('html')->where('open = 1')->orderRaw('rand()')->select();
        $this->assign('guojizcai', $guojizcai);
        $dan = Db::name('dan')->where($show)->order('id desc')->select();
        $this->assign('dan', $dan);
        //分类展示网址
        $artbycatelist = Db::name('category')->where($show)->order('sort desc')->select();
        foreach ($artbycatelist as $k => $v) {
            $artbycatelist[$k]['artlists'] = get_articles_by_cid($v['id'], 5);
        }
        $this->assign('artbycatelist', $artbycatelist);

        //最近30天文章排行
        $maptop30['open'] = 1;
        $maptop30['time'] = array('egt', strtotime("-1 month"));
        $news_top30 = Db::name('news')->where($show)->order('view desc')->select();
        $this->assign('news_top30', $news_top30);  
          
          
        } else {
        $show['show'] = 1;
        $tpto = Db::name('category')->where($show)->order('sort desc')->limit(120)->select();
        $this->assign('tpto', $tpto);
        $GZdh = Db::name('links')->where($show)->order('px desc')->select();
        $this->assign('GZdh', $GZdh);
        $keyss = Db::name('member')->where('userid = 1')->value('key');
        $this->assign('keyss', $keyss);
        $newscate = Db::name('newscate')->where($show)->order('id desc')->limit(120)->select();
        $this->assign('newscate', $newscate);
        $guojizgg = Db::name('banner')->where($show)->order('id desc')->select();
        $this->assign('guojizgg', $guojizgg);
        $html = Db::name('html')->where('open = 1')->select();
        $this->assign('html', $html);
        $htmls = Db::name('html')->order('id desc')->select();
        $this->assign('htmls', $htmls);
        $news = Db::name('news')->where($show)->order('id desc')->select();
        $this->assign('news', $news);
        $newscate = Db::name('newscate')->where($show)->order('id desc')->select();
        $this->assign('newscate', $newscate);
        $wang = Db::name('wang')->where('open = 1')->order('id desc')->select();
        $this->assign('wang', $wang);
        $wangcate = Db::name('wangcate')->where($show)->order('id desc')->select();
        $this->assign('wangcate', $wangcate);
        $guojizcai = Db::name('html')->where('open = 1')->orderRaw('rand()')->select();
        $this->assign('guojizcai', $guojizcai);
        $dan = Db::name('dan')->where($show)->order('id desc')->select();
        $this->assign('dan', $dan);
        //分类展示网址
        $artbycatelist = Db::name('category')->where($show)->order('sort desc')->select();
        foreach ($artbycatelist as $k => $v) {
            $artbycatelist[$k]['artlists'] = get_articles_by_cid($v['id'], 5);
        }
        $this->assign('artbycatelist', $artbycatelist);
            $uid = session('userid');
            $member = Db::name('member')->where("userid = {$uid}")->find();
            $this->assign('member', $member);
        //最近30天文章排行
        $maptop30['open'] = 1;
        $maptop30['time'] = array('egt', strtotime("-1 month"));
        $news_top30 = Db::name('news')->where($show)->order('view desc')->select();
        $this->assign('news_top30', $news_top30); 
        
      }
    }
    public function index()
    {
        $member = new MemberModel();
        if(session('userid')!=''){
        	echo"<script>history.go(-1);</script>";
           // $this->error('你已登录',url('/'));
        }
		if (request()->isPost()) {
            $data = input('post.');
			$status['status'] = 1;
            $user = $member->where($status)->where('usermail', $data['usermail'])->find();
 
          
            if ($user) {
                if ($user['password'] == substr(md5($data['password']), 8, 16)) {
					session('userhead', $user['userhead']);
                    session('username', $user['username']);
                    session('userid', $user['userid']);
					session('point', $user['point']);
					$member->where('userid',session('userid'))->setInc('point',config('point.GJJF-DL'));
                    return json(array('code' => 200, 'msg' => '登录成功'));
                } else {
                    return json(array('code' => 0, 'msg' => '密码错误'));
                }
            } else {
                return json(array('code' => 0, 'msg' => '账号错误'));
            }
          
 
          
          
        }
        return view();
    }
    public function reg()
    {
        $member = new MemberModel();
        if(session('userid')!=''){
        	echo"<script>history.go(-1);</script>";
           // $this->error('你已登录',url('/'));
        }
        if (request()->isPost()) {
            $data = input('post.');
			$webreg = 1;
            $password = input('password');
            $passwords = input('passwords');
            $mail = $member->where('usermail', $data['usermail'])->find();
            if (!$mail) {
                $user = $member->where('username', $data['username'])->find();
                if (!$user) {
                    if ($password != $passwords) {
                        return json(array('code' => 0, 'msg' => '两次密码不一样'));
                    }
					if ($webreg != config('web.WEB_REG')) {
                        return json(array('code' => 0, 'msg' => '已关闭会员注册'));
                    }
                    $data['regtime'] = time();
                    $data['grades'] = 0;
                    $data['status'] = config('web.WEB_REG');
                    $data['point'] = config('point.GJJF-ZC');
                    $data['userhead'] = '/public/img/touxiang.jpg';
                    $data['userip'] = $_SERVER['REMOTE_ADDR'];
                    $data['password'] = substr(md5($password), 8, 16);
                  
                  
                if (isset($_REQUEST['authcode'])) {
                   // session_start();
                    if (strtolower($_REQUEST['authcode']) == $_SESSION['authcode']) {
                    if ($member->add($data)) {
                        return json(array('code' => 200, 'msg' => '注册成功'));
                    } else {
                        return json(array('code' => 0, 'msg' => '注册失败'));
                    }
                  } else {
                        $this->error('验证码错误');
                    }
                                     
                }
                      
                } else {
                    return json(array('code' => 0, 'msg' => '该昵称已存在'));
                }
            } else {
                return json(array('code' => 0, 'msg' => '该邮箱已存在'));
            }
        }
        return view();
    }
 
 
  
    public function logout()
    {
        session("userid", NULL);
        session("username", NULL);
		session("usermail", NULL);
        session("kouling", NULL);
        return json(array('code' => 200, 'msg' => '退出成功'));
        return NULL;
    }
}