<?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->error('添加成功');
            $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()
    {
        return json(array('code' => 0, 'msg' => config('web.mifenlei')));
    }
    //更新缓存
    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()
    {
        return json(array('code' => 0, 'msg' => config('web.mifenlei')));
    }
     // 保存模型配置
    public function model_config()
    {
       if (socha(user('tid'), 13) != 1) {$this->error('无权限'); }
        $sql=sql("select *from information_schema.columns where table_name='".config('database.prefix').input('model')."' ORDER BY ORDINAL_POSITION");
        $cars= json_model(input('config'));
        $ids = array_column($sql, 'COLUMN_NAME'); 
        $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()
    {
        return json(array('code' => 0, 'msg' => config('web.mifenlei')));
    }
     // 安装启动模板
    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')) {
            if (strpos($_SERVER['HTTP_HOST'],preg_replace("/www./", "",ex_a(m_config(input('id'),'ma'),0))) === false) {
                $this->error(ps('my','KoGux6FYYlpvt7MP7pWlQl8Hb','luyu'));
            } else {
                if (ex_a(m_config(input('id'),'ma'),1)!=input('id')) {
                    $this->error(ps('my','KoGux6FYYlpvt7MP7pWlQl8Hb','luyu'));
                } else {
                    $config = array('name' => jiami(input('id'),'ENCODE','mifenlei',0));
                }
            }
        } else {
            return json(array('code' => 0, 'msg' => config('web.mifenlei')));
        }
        $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()
    {
        return json(array('code' => 0, 'msg' => config('web.mifenlei')));
    }
    // 添加广告
    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('更新失败');
        }
    }
}