<?php
namespace app\admin\controller;

use think\Controller;
use think\Db;
use Think\Config;
use think\File;
/*
后台通用提交
*/
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2021 http://mifenlei.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 梦雨 <50361804@qq.com>
// +----------------------------------------------------------------------
class Api extends Common
{
    //批量删除
    public function delss()
    {
        if (socha(user('tid'), 3) != 1) {$this->error('无权限'); }
        $id = input('id');
        $params = input('post.');
        $ids = implode(',', $params['ids']);
        $result = Db::name(input('model'))->delete($ids);
        if ($result) {
            return json(array('code' => 1, 'msg' => '批量删除成功'));
        } else {
            return json(array('code' => 0, 'msg' => '批量删除失败'));
        }
    }
    //单个删除
    public function catedelete()
    {
        if (socha(user('tid'), 4) != 1) {$this->error('无权限'); }
        $id = input('id');
        if (Db::name(ps(input('model')))->delete(array(get('i', 'id') => $id))) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
    //通用添加内容
    public function add()
    {
        if (socha(user('tid'), 5) != 1) {$this->error('无权限'); }
        $_data = $this->request->post();
        $_data['pics'] = json_encode($_data['pics']);
        $_data['time'] = time();
        $_data['times'] = time();
        if (!Db::name(ps(input('model')))->strict(false)->data($_data)->insert()) {
            $this->error('添加失败');
        } else {
            $this->success('添加成功');
        }
    }
    //通用更新内容
    public function edit()
    {
        if (socha(user('tid'), 6) != 1) {$this->error('无权限'); }
        $id = input('id');
        $data = $this->request->post();
        $data['pics'] = json_encode($data['pics']);
        $data['times'] = dingyi(input('times'),time());
        if (Db::name(ps(input('model')))->strict(false)->where(get('i', 'id'), $id)->update($data)) {
            $this->success('更新成功');
        } else {
            $this->error('更新失败');
        }
    }
    //添加member用户
    public function add_member()
    {
        if (socha(user('tid'), 7) != 1) {$this->error('无权限'); }
        $data = $this->request->post();
        $data['reg_time'] = time();
        $data['userip'] = $_SERVER['REMOTE_ADDR'];
        $key = ps('www.115cms.com');
        if (empty(input('password'))) {
            $data['password'] = jiami('123456', 'ENCODE', $key, 0);
        } else {
            $data['password'] = jiami(input('password'), 'ENCODE', $key, 0);
        }
        if (empty($_POST['tid'])) {
            $data['tid'] = 0;
        } else {
            $tid = $_POST['tid'];
            $data['tid'] = implode(',', $tid);
        }
        if (Db::name(ps(input('model')))->strict(false)->data($data)->insert()) {
            $this->success('添加成功');
        } else {
            $this->error('添加失败');
        }
    }
    //更新内容member用户
    public function edit_member()
    {
        if (socha(user('tid'), 8) != 1) {$this->error('无权限'); }
        $id = input('id');
        $data = $this->request->post();
        $data['times'] = time();
        $key = ps('www.mifenlei.com');
        if (!empty(input('password'))) {
            $data['password'] = jiami(input('password'), 'ENCODE', $key, 0);
        } else {
            $data['password'] = fun_sc_p(ps(input('model')), array(get('i', 'id') => $id), 'password');
        }
        if (empty($_POST['tid'])) {
            $data['tid'] = 0;
        } else {
            $tid = $_POST['tid'];
            $data['tid'] = implode(',', $tid);
        }
        if (Db::name(ps(input('model')))->strict(false)->where(get('i', 'id'), $id)->update($data)) {
            $this->success('更新成功');
        } else {
            $this->error('更新失败');
        }
    }
    //一键更新-首页-公用
    public function uindex()
    {
        if (socha(user('tid'), 9) != 1) {$this->error('无权限'); }
        $id = input('id');
        $or = input('or');
        $data = $this->request->post();
        $s = Db::name(ps(input('model')))->where(get('i', 'id'), $id)->find();
        if ($s[$or] == 1) {
            Db::name(ps(input('model')))->where(get('i', 'id'), $id)->update([$or => 0]);
            return json(array('code' => 1, 'msg' => '更新成功', 'or' => 1));
        } else {
            Db::name(ps(input('model')))->where(get('i', 'id'), $id)->update([$or => 1]);
            return json(array('code' => 1, 'msg' => '更新成功', 'or' => 2));
        }
    }
    // 删除附件
    public function filedelete()
    {
        if (socha(user('tid'), 10) != 1) {$this->error('无权限'); }
        $id = input('id');
        $m = input('m');
        $time =fun_sc_p('file', array('id'=>$id), 'create_time');
        $savename =fun_sc_p('file', array('id'=>$id), 'savename');
        $thumb=fun_sc_p('file', array('id'=>$id), 'sha1');
        $ext=fun_sc_p('file', array('id'=>$id), 'ext');
        $data = $this->request->get();
        if (Db::name('file')->where('id', $id)->data($data)->delete($data)) {
            unlink(ROOT_PATH . '/uploads/'.date('Ymd',$time).'/'.$savename);
            unlink(ROOT_PATH . '/uploads/thumb/'.$thumb.'thumb.'.$ext);
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
    //更新缓存
    function update()
    {
        if (socha(user('tid'), 11) != 1) {$this->error('无权限'); }
        $res = deleteun(ROOT_PATH . 'runtime/cache/');
        array_map('unlink', glob(TEMP_PATH . '/*.php'));
        rmdir(TEMP_PATH);
        return json(array('code' => 1, 'msg' => '更新缓存成功'));
    }
    // 获取数据 创建字段
    public function model_config_api()
    {
        $this->error(config('web.sq')); 
    }
     // 保存模型配置
    public function model_config()
    {
       if (socha(user('tid'), 13) != 1) {$this->error('无权限'); }
        $data = $this->request->param();
        if (Db::name('model')->strict(false)->where(get('i', 'id'), input('id'))->update($data)) {
            $this->success('更新成功');
        } else {
            $this->error('更新失败');
        }
    }
     // 删除字段
    public function modelzd()
    {
        $this->error(config('web.sq')); 
    }
     // 安装启动模板
    public function addmuban()
    {
        if (socha(user('tid'), 15) != 1) {$this->error('无权限'); }
        $data = json_decode(fun_sc_p('set', array('id'=>1), 'config'),true);
        $data["muban"]=input('id');
        Db::name('set')->where(array('id'=>1))->setField('config', json_encode($data));
        $path = 'mifenlei/extra/template.php';
        $file = include $path;
        if (input('id')) {
            $config = array('name' => jiami(input('id'),'ENCODE','mifenlei',0));
        } else {
            $config = array('wap' => jiami(input('idwap'),'ENCODE','mifenlei',0));
        }
        $config = preg_replace("/[?><?]/", '', $config);
        $res = array_merge($file, $config);
        $str = '<?php return [';
        foreach ($res as $key => $value) {
            $str .= '\'' . $key . '\'' . '=>' . '\'' . $value . '\'' . ',';
        }
        $str .= ']; ';
        if (file_put_contents($path, $str)) {
            array_map('unlink', glob(TEMP_PATH . '/*.php'));
            rmdir(TEMP_PATH);
            return json(array('code' => 1, 'msg' => '启动成功'));
        } else {
            return json(array('code' => 0, 'msg' => '启动失败'));
        }  

    }
    // 设置模板
    public function muban()
    {
        if (socha(user('tid'), 16) != 1) {$this->error('无权限'); }
        if (request()->isPost()) {
            $config = $this->request->post();
            $url = 'template/' . input('id') . '/set.txt';
            $str = json_encode($config);
            if (file_put_contents($url, $str)) {
                return json(array('code' => 1, 'msg' => '保存配置成功'));
            } else {
                return json(array('code' => 0, 'msg' => '请检查template/' . input('id') . '是否有写入权限'));
            }
        }
    }
    // 删除模板
    public function unmuban()
    {
        if (socha(user('tid'), 17) != 1) {$this->error('无权限'); }
        $info = input('id');
        if ($info=='guojicms') {
            return json(array('code' => 0, 'msg' => '默认模板禁止删除'));
        } 
        $res = deleteun(ROOT_PATH . 'template/' . DS . $info);
        if ($res) {
            return json(array('code' => 1, 'msg' => '删除成功'));
        } else {
            return json(array('code' => 0, 'msg' => '删除失败'));
        }
    }
    // 删除模型
    public function modelun()
    {
        if (socha(user('tid'), 18) != 1) {$this->error('无权限'); }
        if (Db::name('model')->delete(array('id' => input('id')))) {
            // 删除表
            $sql =sql("DROP TABLE IF EXISTS `my_".input('name')."`;");
            // 删除该模型下的评论
            Db::name('comment')->where(array('m' => input('name')))->delete();
            // 删除该模型下的收藏
            Db::name('shoucang')->where(array('m' => input('name')))->delete();
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
    // 添加广告
    public function add_ad()
    {
        if (socha(user('tid'), 19) != 1) {$this->error('无权限'); }
        $data['name'] = '广告';
        $data['alias'] = input('id');
        $data['time'] = time();
        $data['times'] = time()+31536000;
        if (Db::name('ad')->strict(false)->data($data)->insert()) {
            return json(array('code'=>1,'msg'=>'添加成功')); 
        }else{
            return json(array('code'=>0,'msg'=>'添加失败')); 
        }
    }
    // 更新广告
    public function upad()
    {
        if (socha(user('tid'), 20) != 1) {$this->error('无权限'); }
        $id = input('id');
        $data = $this->request->post();
        $data['times'] = strtotime(input('times'));
        if (Db::name('ad')->strict(false)->where('id', $id)->update($data)) {
            $this->success('更新成功');
        } else {
            $this->error('更新失败');
        }
    }
}