<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
/*
通用提交
*/
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2020 http://mifenlei.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 梦雨 <50361804@qq.com>
// +----------------------------------------------------------------------
class Api extends Controller
{
    // 发送验证码
    public function yanzhengma()
    {
        //手机号码注册
        if (!preg_match("/^1[34578]{1}\\d{9}\$/", input('phone'))) {
            $this->error('手机号码格式错误');
        }
        $phone = Db::name('member')->where('phone', input('phone'))->find();
        if (input('id')==2) {
            // 找回
            if (!$phone) {
                $this->error('手机号码不存在');
            }
        } elseif(input('id')==3) {
            if ($phone) {
                $this->error('手机号码已存在');
            }

        } elseif(get('id',1)==1) {
            // 注册
            if ($phone) {
                $this->error('当前手机号码被注册');
            }
        }
        $code = mt_rand(100000, 999999);
        //查询当前时间该验证码是否过期
        //输出当天最新验证码过期时间
        $times = Db::name('yanzhengma')->where(array('phone' => input('phone'), 'fl' => get('id',1)))->where('time > ' . Date_k(0) . ' and time < ' . Date_k(1) . '')->order('id desc')->value('times');
        if (fun_tj('yanzhengma', 'time', Date_k(0), 'time', Date_k(1), array('open' => get('id',1))) > set('dxb_ip')) {
            $this->error('您当前发送次数过多');
        }
        if ($times >= time()) {
            $this->error('验证码还未过期');
        } else {
            //发送短信
            if (input('id')==2) {
                // 找回
                $content = preg_replace("/\{M}/is", $code, set('dxb_zh'));
            } elseif(input('id')==3) {
                // 更换
                $content = preg_replace("/\{M}/is", $code, set('dxb_gh'));
            } elseif(input('id')==4) {
                // 发布信息
                $content = preg_replace("/\{M}/is", $code, set('dxb_ph',1));
            } elseif(get('id',1)==1) {
                // 注册
                $content = preg_replace("/\{M}/is", $code, set('dxb_title'));
            }
            duanxin(input('phone'), $content);
            // 添加验证码记录
            yanzhengma($code, input('phone'), $content, get('id',1), time()+300);
            if (set('open1',4)==1) {
                // 邮件发送
                send_mail(set('mailUsername',1), '【' . set('title') . '】验证码发送', '有新验证码进行了发送操作');
            }
            $this->success('发送成功');
        }
    }
    //提交注册
    public function reg()
    {
        // 手机注册
        if (request()->isPost()) {
            if (input('reg_e')!=1) {
                $this->error('请同意并且阅读注册须知');
            }
            $password = input('password');
            $key = 'www.mifenlei.com';
            $map['phone'] = input('phone');
            $phone = Db::name('member')->where($map)->find();
            //查询当前手机号码对应的验证码
            //查询过期时间
            $times = Db::name('yanzhengma')->where(array('phone' => input('phone'), 'fl' => 1))->where('time > ' . Date_k(0) . ' and time < ' . Date_k(1) . '')->order('id desc')->value('times');
            if ($times >= time()) {
                $ma = Db::name('yanzhengma')->where(array('phone' => input('phone'), 'fl' => 1))->where('time > ' . Date_k(0) . ' and time < ' . Date_k(1) . '')->order('id desc')->value('ma');
            }
            if (strlen(input('code'))!=6) {
                $this->error('请输入正确的验证码');
            }
            if ($ma != input('code')) { 
                $this->error('验证码错误');
            }
            if (!$phone) {
                if (!preg_match("/^1[34578]{1}\\d{9}\$/", input('phone'))) {
                    $this->error('手机号码格式错误');
                }
                $data['regtime'] = time();
                $data['last_login_time'] = time();
                $data['open'] = 1;
                $data['phone_open'] = 1;
                $data['phone'] = input('phone');
                $data['username'] = '用户'.generate_password(8);
                $data['userhead'] = '/public/img/touxiang.jpg';
                $data['userip'] = $_SERVER['REMOTE_ADDR'];
                $data['last_login_ip'] = $_SERVER['REMOTE_ADDR'];
                $data['password'] = jiami($password, 'ENCODE', $key, 0);
                $data['key'] = generate_password(32);
                if (Db::name('member')->strict(false)->insert($data)) {
                    $getid = Db::name('member')->where($map)->find();
                    session('phone', $getid['phone']);
                    session('userid', $getid['userid']);
                    //注册送积分
                    point(set('jifen1', 2), session('userid'), '注册');
                    //登录送积分
                    point(set('jifen2', 2), session('userid'), '登录');
                    if (set('open2',4)==1) {
                        // 邮件发送
                        send_mail(set('mailUsername',1), '【' . set('title') . '】用户注册通知', '您网站有新的用户进行了注册');
                    }
                    $this->success('注册成功');
                } else {
                    $this->error('注册失败');
                }
            } else {
                $this->error('该手机号码已存在');
            }
        }
    }
    //提交登录
    public function login()
    {
        if (session('userid') != '') {
            $this->error('您已登录');
        }
        $member = Db::name('member');
        if (request()->isPost()) {
            if (!preg_match("/^1[34578]{1}\\d{9}\$/", input('phone'))) {
                $this->error('手机号码格式错误');
            }
            $data = input('post.');
            $open['open'] = 1;
            $map['phone'] = $data['phone'];
            $key = 'www.mifenlei.com';
            $user = Db::name('member')->where($open)->where($map)->find();
            if ($user) {
                if (jiami($user['password'], 'DECODE', $key, 0) == $data['password']) {
                    session('phone', $user['phone']);
                    session('userid', $user['userid']);
                    if ($data['mima']==1) {
                        cookie('mima',user('key'), 2592000);
                    }else if ($data['mima']==2) {
                        cookie('mima',user('key'), 2592000*36);
                    }else {
                        cookie('mima', null);
                    } 
                    $member->where($map)->setField(array('last_login_ip' => $_SERVER['REMOTE_ADDR'], 'last_login_time' => time()));
                    //登录送积分
                    point(set('jifen2', 2), session('userid'), '登录');
                    $this->success('登录成功');
                } else {
                    $this->error('密码错误');
                }
            } else {
                $this->error('账号不存在 或者异常');
            }
        }
    }
    //找回密码手机找回
    public function retrieve()
    {
        $member = Db::name('member');
        $data = input('post.');
        $password = input('password');
        $key = 'www.mifenlei.com';
        //不能随便更改会导致用户无法登录
        $times = Db::name('yanzhengma')->where(array('phone' => input('phone'), 'fl' => 2))->where('time > ' . Date_k(0) . ' and time < ' . Date_k(1) . '')->order('id desc')->value('times');
        if ($times >= time()) {
            $ma = Db::name('yanzhengma')->where(array('phone' => input('phone'), 'fl' => 2))->where('time > ' . Date_k(0) . ' and time < ' . Date_k(1) . '')->order('id desc')->value('ma');
        }
        if (strlen(input('code'))!=6) {
            $this->error('请输入正确的验证码');
        }
        $map['phone'] = $data['phone'];
        $phone = $member->where($map)->find();
        if (!phone) {
            return json(array('code' => 0, 'msg' => '手机号码不存在'));
        } else {
            if ($ma != input('code')) {
                $this->error('验证码错误');
            }
            $datam['password'] = jiami($password, 'ENCODE', $key, 0);
            if ($member->where(array('phone' => input('phone')))->update($datam)) {
                Db::name('yanzhengma')->where(array('phone' => input('phone')))->setField(array('open' => 0));
                $this->success('密码重置成功',url('index/user/login'));
            } else {
                $this->error('密码重置失败');
            }
        } 

    }
    // 退出登录
    public function logout()
    {
        session("userid", NULL);
        session("phone", NULL);
        cookie('mima', null);
        $this->success('退出成功','/');
        return NULL;
    }
    // 举报信息
    public function jubao()
    {
        //发送邮件
        if (send_mail(set('mailUsername',1), '【' . set('title') . '】举报信息', input('content').'<br>举报人ip:'.$_SERVER['REMOTE_ADDR'].'<br>举报人电话'.user('phone'))) {
            $this->success('举报成功，等待核实');
        } else {
            $this->error('异常');
        }
    }
    //管理信息验证
    public function guanlixinxi()
    {
        $t=Db::name(input('m'))->where(array('id'=>input('id'),'open'=>1))->find();
        if (!$t['mima']) {
            $this->error('未设置密码');
        }
        if ($t['mima']==input('content')) {
            cookie('mima',input('content'));
            $this->success('密码正确',url('index/user/guanli',array('m'=>$t['m'],'id'=>$t['id'])));
        } else {
            $this->error('密码错误');
        }
    }
    // 更新信息
    public function edit()
    {
        $t = Db::name(input('model'))->where(array('id' => input('id')))->find();
        if (user('userid')) {
            // 已登录
            if (user('userid')!=$t['uid']) {
                $this->error('无权限');
            } 
        } else {
            // 未登录
            if (!$t['mima']) {
                if (!input('add')) {
                   $this->error('无权限');
                } 
            } else {
                if (!input('add')) {
                    if (cookie('mima')!=$t['mima']) {
                        exit('请输入密码');
                    } 
                } 
            }
        }
        if (input('leixing')==3) {
            $id = input('id');
            if (cookie('mima')!= $t['mima']||user('userid')!=$t['uid']) {
                $this->error('参数不全');
            }
            if (Db::name(input('model'))->delete(array('id' => $id))) {
                $this->success('删除成功');
            } else {
                $this->error('删除失败');
            }
        }
        $cate_diqu = Db::name('cate_diqu')->where(array('id' => input('sid')))->find();
        if (!$cate_diqu['id']) {
            $this->error('请选择正确的地区');
        }
        $da = $this->request->post();
        // 刷新信息
        if (input('leixing')==1) {
            if (user('vip_times')>time()) {
                // 会员可无限制刷新
                $data['times'] = time();
            } else {
                // 非会员刷新限制开始
                if ($t['times']>time()-3600) {
                    $this->error('刷新过于频繁');
                } else {
                    $data['times'] = time();
                }
            }
            $data['times'] = time();
        }
        // 当前主题是自动过期更新后都是正常状态
        if ($t['open']==4) {
            $data['open'] = 1;
        }
        // 审核拒绝后更新恢复待审状态
        if ($t['open']==3) {
            $data['open'] = 2;
        }
        $res = $this->request->post();
        $data['title'] = xss(input('title'));
        $data['weixin'] = xss(input('weixin'));
        $data['mail'] = xss(input('mail'));
        $data['mima'] = xss(input('mima'));
        $data['tid'] = xss(input('tid'));
        $data['sid'] = xss(input('sid'));
        $data['time_daoqi'] = xss(input('time_daoqi'));
        $data['content'] = xss(input('content'));
        $data['pic'] = xss(input('pic'));
        $data['pics'] = json_encode($res['pics']);
        if (fun_model(input('model'), 'config')) {
            $carq= json_model(fun_model(input('model'), 'config'));
            foreach($carq as $x=>$v){
                $data[''.json_model_v($v,'zhiduan').''] = xss($da[json_model_v($v,'zhiduan')]);
            }
        }
        if (Db::name(ps(input('model')))->strict(false)->where(get('i', 'id'), $da['id'])->update($data)) {
            if (set('open3',4)==1) {
                // 邮件发送
                send_mail(set('mailUsername',1), '【' . set('title') . '】信息修改通知', '有新信息进行了修改，请前去网站后台查看');
            }
            $this->success('操作成功');
        } else {
            $this->error('操作失败');
        }
    }
    // 发布信息
    public function add()
    {
        $cate_diqu = Db::name('cate_diqu')->where(array('id' => input('sid')))->find();
        $m = Db::name('model')->where(array('name' => input('model')))->find();
        if (!$cate_diqu['id']) {
            $this->error('请选择正确的地区');
        }
        $da = $this->request->post();
        if (!user('userid')) {
            // 游客未登录是否需要审核
            if ($m['fu_open2']==1) {
                $data['open'] = 2;
            } else {
                $data['open'] = 1;
            }
            // 未登录今天发布是否超出限制
            if (fun_tj(input('model'), 'time', Date_k(0), 'time',  Date_k(1), array('ip'=>$_SERVER['REMOTE_ADDR']))>$m['fu_y']) {
                $this->error('发布信息'.$m['title'].'今天已超出限制');
            }
            $times = Db::name('yanzhengma')->where(array('phone' => input('phone'), 'fl' => 4))->where('time > ' . Date_k(0) . ' and time < ' . Date_k(1) . '')->order('id desc')->value('times');
            if ($times >= time()) {
                $ma = Db::name('yanzhengma')->where(array('phone' => input('phone'), 'fl' => 4))->where('time > ' . Date_k(0) . ' and time < ' . Date_k(1) . '')->order('id desc')->value('ma');
            }
            if (strlen(input('code'))!=6) {
                $this->error('请输入正确的验证码');
            }
            if ($ma != input('code')) { 
                $this->error('验证码错误');
            }
            $data['uid'] = 0;
        } else {
            if (user('vip')==1) {
                // 是VIP
                // 已登录是否需要审核
                if ($m['fu_open1']==1) {
                    $data['open'] = 2;
                } else {
                    $data['open'] = 1;
                }
                // 已登录今天发布是否超出限制
                if (fun_tj(input('model'), 'time', Date_k(0), 'time',  Date_k(1), array('uid'=>user('userid')))>=$m['fu_v']) {
                    $this->error('发布信息'.$m['title'].'今天已超出限制');
                }
            } else {
                // 普通用户
                // 已登录是否需要审核
                if ($m['fu_open']==1) {
                    $data['open'] = 2;
                } else {
                    $data['open'] = 1;
                }
                // 已登录今天发布是否超出限制
                if (fun_tj(input('model'), 'time', Date_k(0), 'time',  Date_k(1), array('uid'=>user('userid')))>=$m['fu_p']) {
                    $this->error('发布信息'.$m['title'].'今天已超出限制');
                }
            }
            $data['uid'] = user('userid');
        }
        $res = $this->request->post();
        $data['ip'] = $_SERVER['REMOTE_ADDR'];
        $data['weixin'] = xss(input('weixin'));
        $data['mail'] = xss(input('mail'));
        $data['title'] = xss(input('title'));
        $data['mima'] = xss(input('mima'));
        $data['time'] = time();
        $data['times'] = time();
        $data['tid'] = xss(input('tid'));
        $data['sid'] = xss(input('sid'));
        $data['time_daoqi'] = xss(input('time_daoqi'));
        $data['username'] = xss(input('username'));
        $data['phone'] = xss(input('phone'));
        $data['content'] = xss(input('content'));
        $data['pic'] = xss(input('pic'));
        $data['pics'] = json_encode($res['pics']);
        if (fun_model(input('model'), 'config')) {
            $carq= json_model(fun_model(input('model'), 'config'));
            foreach($carq as $x=>$v){
                $data[''.json_model_v($v,'zhiduan').''] = xss($da[json_model_v($v,'zhiduan')]);
            }
        }
        if (Db::name(ps(input('model')))->strict(false)->data($data)->insert()) {
            if (set('open4',4)==1) {
                // 邮件发送
                send_mail(set('mailUsername',1), '【' . set('title') . '】发布信息通知', '有新信息已发布请登录网站后台查看');
            }
            $this->success('操作成功');
        } else {
            $this->error('操作失败');
        }
    }
    // 置顶信息
    public function settop()
    {
        if (!session('userid')) {
            $this->error('未登录');
        }
        $c = Db::name(input('m'))->where(array('id'=>input('id')))->find();
        if (user('vip_times')>time()) {
            // 是vip
            // 查询vip置顶条数
            $sp = Db::name(input('m'))->where(array('uid' => session('userid'),'vip'=>1))->where('time > ' . Date_k(-30) . ' and time < ' . Date_k(1) . '')->count();
            if ($sp>$c['fu_s']) {
                $this->error('当前置顶用完');  
            }  

            $data['vip'] = 1;   
        } else {
            if (input('ids')=='vip') {
                $this->error('当前置顶用完');
            }
            if (!set('s'.input('id'),2)) {
                $this->error('参数不存在');
            }
            //查询积分余额
            if(user('point')<set('s'.input('id'),2)){
                $this->error(set('point',2).'不足请先充值');
            }
            //扣除积分
            point(-set('s'.input('id'),2), session('userid'), '置顶信息 - '.$c['title']);
        }
        // 更置顶数据
        $data['settop'] = 1;
        $data['settop_time'] = time()+86400*input('id');
        if (Db::name(input('m'))->strict(false)->where(array('id'=>input('idc')))->update($data)) {
            if (set('open5',4)==1) {
                // 邮件发送
                send_mail(set('mailUsername',1), '【' . set('title') . '】购买置顶服务', '有新的用户进行了信息置顶');
            }
            $this->success('购买成功');
        } else {
            $this->error('购买失败');
        }
         
    }
    // 签到
    public function qiandao()
    {
        $this->error(config('web.sq')); 
    }
    // 收藏
    public function shoucang()
    {
        if (!session('userid')) {
            $this->error('未登录');
        }
        if (fun_st('shoucang',array('tid'=>input('id'),'m'=>get('m','html')))>0) {
            Db::name('shoucang')->where(array('tid'=>input('id'),'m'=>get('m','html')))->delete();
            $this->error('取消收藏成功');
        } else {
            Db::name('shoucang')->insert(array('tid'=>input('id'),'time'=>time(),'uid'=>user('userid'),'m'=>get('m','html')));
            $this->success('收藏成功');
        }
    }
    //通用删除
    public function deletes()
    {
        if (!session('userid')) {
            $this->error('未登录');
        }
        $id = input('id');
        if (fun_sc_p(input('m'), array('id' => $id), 'uid') != session('userid')) {
            $this->error('参数不全');
        }
        if (Db::name(input('m'))->delete(array('id' => $id, 'uid' => session('userid')))) {
            $this->success('操作成功');
        } else {
            $this->error('操作失败');
        }
    }
    // 修改资料
    public function user_set()
    {
        if (!session('userid')) {
            $this->error('未登录');
        }
        if (mb_strlen(input('username'), 'UTF-8') > 15) {
            $this->error('用户名不能大于15个字符');
        }
        $member = Db::name('member');
        $uid = session('userid');
        $data['userhead'] = xss(input('userhead'));
        $data['sex'] = xss(input('sex'));
        $data['userhome'] = xss(input('userhome'));
        $data['description'] = xss(input('description'));
        $data['username'] = xss(input('username'));
        if ($member->strict(false)->where(array('userid' => $uid))->update($data)) {
            return json(array('code' => 1, 'msg' => '修改成功'));
        } else {
            return json(array('code' => 0, 'msg' => '修改失败'));
        }
    }
    // 实名认证
    public function user_set_sm()
    {
        if (!session('userid')) {
            $this->error('未登录');
        }
        $member = Db::name('member');
        $uid = session('userid');
        if (!is_idcard(input('sm_hao'))) {
            $this->error('身份证号码验证失败');
        }
        // 0是第一次提交1是通过2是待审3是需要重新提交审核失败
        if (user('sm_open')==0) {
            $data['sm_open'] = 2;
        } else if(user('sm_open')==1){
            $this->error('当前实名已认证');
        } else if(user('sm_open')==2){
            $this->error('正在审核中,无需重新提交');
        } else if(user('sm_open')==3){
            $data['sm_open'] = 2;
        }
        $data['sm_hao'] = xss(input('sm_hao'));
        $data['sm_name'] = xss(input('sm_name'));
        $data['sm_z'] = xss(input('sm_z'));
        $data['sm_f'] = xss(input('sm_f'));
        $data['sm_time'] = time();
        if ($member->strict(false)->where(array('userid' => $uid))->update($data)) {
            if (set('open6',4)==1) {
                // 邮件发送
                send_mail(set('mailUsername',1), '【' . set('title') . '】实名认证', '有新的用户进行了实名认证,需要您去审核');
            }
            return json(array('code' => 1, 'msg' => '提交成功'));
        } else {
            return json(array('code' => 0, 'msg' => '提交失败'));
        }
    }
    //账号更换邮箱及手机
    public function user_set_phone()
    {

        if (!session('userid')) {
            $this->error('请登录');
        } 

        $member = Db::name('member');
        if (request()->isPost()) {
            $mail = $member->where(array('userid' => user('userid')))->find();
            //查询当前手机号码对应的验证码
            //查询过期时间
            $times = Db::name('yanzhengma')->where(array('phone' => input('phone'), 'fl' => 3))->where('time > ' . Date_k(0) . ' and time < ' . Date_k(1) . '')->order('id desc')->value('times');
            if ($times >= time()) {
                $ma = Db::name('yanzhengma')->where(array('phone' => input('phone'), 'fl' => 3))->where('time > ' . Date_k(0) . ' and time < ' . Date_k(1) . '')->order('id desc')->value('ma');
            }
            if (strlen(input('yanzhengma'))!=6) {
                $this->error('请输入正确的验证码');
            }
            if ($ma != input('code')) {
                $this->error('验证码错误');
            }
            $_data['phone'] = get('phone');
            if ($member->where(array('userid' => user('userid')))->update($_data)) {
                return json(array('code' => 1, 'msg' => '修改成功'));
            } else {
                return json(array('code' => 0, 'msg' => '修改失败'));
            }
        }
    }
    // 修改密码
    public function user_set_password()
    {
        if (!session('userid')) {
            $this->error('请登录');
        } 
        $member = Db::name('member');
        $t = $member->where(array('userid' => user('userid')))->find();
        $member = Db::name('member');
        if (request()->isPost()) {
            $t = $member->where(array('userid' => user('userid')))->find();
            if (input('password') != jiami($t['password'], 'DECODE', 'www.mifenlei.com', 0)) {
                return json(array('code' => 0, 'msg' => '原密码不正确'));
            }
            // 加密
            $_data['password'] = jiami(input('passwords'), 'ENCODE', 'www.mifenlei.com', 0);
            if ($member->where(array('userid' => user('userid')))->update($_data)) {
                return json(array('code' => 1, 'msg' => '修改成功'));
            } else {
                return json(array('code' => 0, 'msg' => '修改失败'));
            }
        }
    }
    // 开通vip
    public function vip()
    {
        $this->error(config('web.sq')); 
    }
    // 商家开通vip
    public function svip()
    {
        $this->error(config('web.sq')); 
    }
    // 发送消息
    public function xiaoxi()
    {
        if (!session('userid')) {
            $this->error('请登录');
        } 
        if(fun_tj('xiaoxi', 'time', Date_k(0), 'time',  Date_k(1), array('sid'=>user('userid')))>50){
            $this->error('今天发送过多');
        }
        if(user('userid')==input('uid')){
            $this->error('不能给自己');
        }
        if(fun_u(input('uid'),'userid')<1){
            $this->error('禁止发送消息');
        }
        $_data['uid'] = input('uid');
        $_data['sid'] = user('userid');
        $_data['config'] = xss(input('config'));
        $_data['time'] = time();
        if (Db::name('xiaoxi')->insert($_data)) {
            return json(array('code' => 1, 'msg' => '发送成功'));
        } else {
            return json(array('code' => 0, 'msg' => '发送失败'));
        }
    }
    // 评论管理
    public function pinglun()
    {
        if (!session('userid')) {
            $this->error('请登录');
        } 
        if (set('pl_open',5)!=1) {
            $t='评论成功';
            $_data['open'] = 1;
        } else {
            $t='评论成功,等待审核';
            $_data['open'] = 0;
        }
        $_data['uid'] = user('userid');
        $_data['content'] = xss(input('content'));
        $_data['m'] = xss(input('m'));
        $_data['tid'] = xss(input('id'));
        $_data['time'] = time();
        $_data['ip'] = $_SERVER['REMOTE_ADDR'];
        if (Db::name('comment')->insert($_data)) {
            if (set('open8',4)==1) {
                // 邮件发送
                send_mail(set('mailUsername',1), '【' . set('title') . '】评论', '网站有新的评论');
            }
            return json(array('code' => 1, 'msg' => $t));
        } else {
            return json(array('code' => 0, 'msg' => '评论失败'));
        }
    }
    // 创建商家
    public function add_s()
    {
        $this->error(config('web.sq')); 
    }
    // 修改商家
    public function edit_s()
    {
        $this->error(config('web.sq')); 
    }
    // 商家配置
    public function shangjia()
    {
        $this->error(config('web.sq')); 
    }
    // 自定义定义cookie
    public function cookie()
    {
        cookie(input('name'),input('key'), dingyi(input('time'),86400*365));
        $this->success(dingyi(input('title'),'操作成功'));
    }
    
}