<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2021 http://mifenlei.com.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 作者@梦雨 <50361804@qq.com>
// +----------------------------------------------------------------------
class User extends Controller
{
    public function _initialize()
    {
        $this->assign('hover', 'home');
    }
    // 登录
    public function login()
    {
        $html='template/'.muban().'/user/login.html';
        if (file_exists($html)) {
            return view($html);
        }else {
            return view('mifenlei/index/view/login.html');
        }
    }
    // 注册
    public function reg()
    {
        $html='template/'.muban().'/user/reg.html';
        if (file_exists($html)) {
            return view($html);
        }else {
            return view('mifenlei/index/view/reg.html');
        }
    }
    //找回密码
    public function forget()
    {
        $html='template/'.muban().'/user/forget.html';
        if (file_exists($html)) {
            return view($html);
        }else {
            return view('mifenlei/index/view/forget.html');
        }
    }
    // 会员主页
    public function home()
    {
        if (!user('userid')) {
            $this->success('请登录',url('index/user/login'));
        }
        $t = Db::name('qiandao')->where(array('uid'=>session('userid')))->where("stime > ".Date_k(0)." and stime < ".Date_k(1)."")->find();
        $this->assign('t', $t);
        // 更新vip
        if (user('vip_times')<time()) {
            Db::name('member')->strict(false)->where(array('userid'=>user('userid')))->update(array('vip'=>0,'vip_times'=>0));
        }
        $html='template/'.muban().'/user/home.html';
        if (file_exists($html)) {
            return view($html);
        }else {
            return view('mifenlei/index/view/home.html');
        }
    }
    // 基本设置
    public function set()
    {
        if (!user('userid')) {
            $this->success('请登录',url('index/user/login'));
        }
        $html='template/'.muban().'/user/set.html';
        if (file_exists($html)) {
            return view($html);
        }else {
            return view('mifenlei/index/view/set.html');
        }
    }
    // 投稿
    public function tougao()
    {
        if (!user('userid')) {
            $this->success('请登录',url('index/user/login'));
        }
        $html='template/'.muban().'/user/tougao.html';
        if (file_exists($html)) {
            return view($html);
        }else {
            return view('mifenlei/index/view/tougao.html');
        }
    }
    // 信息管理
    public function guanli()
    {
        $c=Db::name(input('m'))->where(array('id'=>input('id')))->find();
        $this->assign('c',$c);
        if (user('userid')) {
            // 已登录
            if (user('userid')!=$c['uid'] && input('add')!=1) {
                return $this->error('无权限');
            } 
        } else {
            // 未登录
            if (!$c['mima']) {
                if (!input('add')) {
                    exit('无权限');
                } 
            } else {
                if (!input('add')) {
                    if (cookie('mima')!=$c['mima']) {
                        exit('请输入密码');
                    } 
                } 
            }
        }
        $html='template/'.muban().'/user/guanli.html';
        if (file_exists($html)) {
            return view($html);
        }else {
            return view('mifenlei/index/view/guanli.html');
        }
    }
    // 收藏
    public function shoucang()
    {
        if (!user('userid')) {
            $this->success('请登录',url('index/user/login'));
        }
        $html='template/'.muban().'/user/shoucang.html';
        if (file_exists($html)) {
            return view($html);
        }else {
            return view('mifenlei/index/view/shoucang.html');
        }
    }
    // 充值中心
    public function chongzhi()
    {
        if (!user('userid')) {
            $this->success('请登录',url('index/user/login'));
        }
        $t = Db::name('qiandao')->where(array('uid'=>session('userid')))->where("stime > ".Date_k(0)." and stime < ".Date_k(1)."")->find();
        $this->assign('t', $t);
        $html='template/'.muban().'/user/chongzhi.html';
        if (file_exists($html)) {
            return view($html);
        }else {
            return view('mifenlei/index/view/chongzhi.html');
        }
    }
    // 积分记录
    public function point()
    {
        if (!user('userid')) {
            $this->success('请登录',url('index/user/login'));
        }
        $html='template/'.muban().'/user/point.html';
        if (file_exists($html)) {
            return view($html);
        }else {
            return view('mifenlei/index/view/point.html');
        }
    }
    // 我的消息
    public function xiaoxi()
    {
        if (!user('userid')) {
            $this->success('请登录',url('index/user/login'));
        }
        $html='template/'.muban().'/user/xiaoxi.html';
        if (file_exists($html)) {
            return view($html);
        }else {
            return view('mifenlei/index/view/xiaoxi.html');
        }
    }
    // vip购买
    public function vip()
    {
        if (!user('userid')) {
            $this->success('请登录',url('index/user/login'));
        }
        $html='template/'.muban().'/user/vip.html';
        if (file_exists($html)) {
            return view($html);
        }else {
            return view('mifenlei/index/view/vip.html');
        }
    }
    // 商家中心
    public function shangjia()
    {
        if (!user('userid')) {
            $this->success('请登录',url('index/user/login'));
        }
        $c=Db::name('shangjia')->where(array('id'=>fun_sc_p('shangjia', array('uid'=>user('userid')), 'id')))->find();
        $this->assign('c',$c);
        if ($c['open']!=1) {
            if (input('or')) {
                $this->error('请先申请商家');
            }
        }
        $html='template/'.muban().'/user/shangjia.html';
        if (file_exists($html)) {
            return view($html);
        }else {
            return view('mifenlei/index/view/shangjia.html');
        }
    }
    // 提交商家资料
    public function add()
    {
        if (!user('userid')) {
            $this->success('请登录',url('index/user/login'));
        }
        $c=Db::name('shangjia')->where(array('id'=>input('id')))->find();
        $this->assign('c',$c);
        if (!input('id')) {
            if(fun_st('shangjia', array('uid'=>user('userid')))>0){
                $this->error('一个账户无法添加多个商家');
            }
        }
        $html='template/'.muban().'/user/add.html';
        if (file_exists($html)) {
            return view($html);
        }else {
            return view('mifenlei/index/view/add.html');
        }
    }
}