<?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) 2020 http://myucms.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: 梦雨 <50361804@qq.com>
// +----------------------------------------------------------------------
class Api extends Common
{
    //批量删除
    public function delss()
    {
        //权限
        if (socha(user('tid'), 1) != 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'), 2) != 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'), 3) != 1) {
            $this->error('权限不足');
        }
        $_data = $this->request->post();
        $_data['time'] = time();
        if (!Db::name(ps(input('model')))->strict(false)->data($_data)->insert()) {
            $this->error('添加失败');
        } else {
            $this->success('添加成功');
        }
    }
    //通用更新内容
    public function edit()
    {
        //权限
        if (socha(user('tid'), 4) != 1) {
            $this->error('权限不足');
        }
        $id = input('id');
        $data = $this->request->post();
        $data['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'), 15) != 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'), 5) != 1) {
            $this->error('权限不足');
        }
        $id = input('id');
        $data = $this->request->post();
        $data['times'] = time();
        $key = ps('www.115cms.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'), 6) != 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'), 14) != 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()
    {
        array_map('unlink', glob(TEMP_PATH . '/*.php'));
        rmdir(TEMP_PATH);
        return json(array('code' => 1, 'msg' => '更新缓存成功'));
    }
}