<?php
/**
 * @Statement: [ An Ordinary Person ]
 * @Author: 闻子 < QQ：270988107 >
 * @Copyright: 闻子 2016-2019 All rights reserved.
 *
 *    .--,       .--,
 *   ( (  \.---./  ) )
 *    '.__/o   o\__.'
 *       {=  ^  =}
 *        >  -  <
 *       /       \
 *      //       \\
 *     //|   .   |\\
 *     "'\       /'"_.-~^`'-.
 *        \  _  /--'         `
 *      ___)( )(___
 *     (((__) (__)))    高山仰止,景行行止.虽不能至,心向往之.
 *
 */

namespace app\api\controller;

use think\Db;

use Env;

use app\common\logic\Scode as LogicScode;

use app\common\controller\Base as BaseController;

class Scode extends BaseController
{
	/**
	 * 定义逻辑
	 * @Author: 闻子 < QQ：270988107 >
	 */
	private static $ScodeLogic = null;

    /**
     * 实例化逻辑
     * @Author: 闻子 < QQ：270988107 >
     */
    public function __construct()
    {
        parent::__construct();
        self::$ScodeLogic = get_sington_object('LogicScode', LogicScode::class);
    }

    /**
     * session检测
     * @Author: 闻子 < QQ：270988107 >
     */
    public function getSession()
    {
        $AdminExistence = session('admin.id');
        if (!$AdminExistence) {
            return $this->error('登录超时，请重新登录','index/member/login');
        }
    }

    /**
     * 管理员检测
     * @Author: 闻子 < QQ：270988107 >
     */
    public function getManager()
    {
        $userinfo = Db::name('user')->where('id',session('admin.id'))->find();
        if ($userinfo['manager'] != 1) {
            $this->redirect('/member/index');
        } elseif (!$userinfo) {
            return $this->error('登录超时，请重新登录','index/member/login');
        }
    }

    /**
     * 源码接口
     * @Author: 闻子 < QQ：270988107 >
     */
    public function getScode()
    {
        $this->getManager();
        $count = self::$ScodeLogic->getScodeCount();
        $page = $this->request->param('page',1,'intval');
        $limit = $this->request->param('limit',10,'intval');
        $all = Db::name('scode')->alias('a')->join('menu w','a.mid = w.id')->field('a.id,a.title,a.user,a.url,a.price,a.type,a.examine,a.essence,a.browser,a.create_time,w.name')->limit(($page-1)*$limit,$limit)->order('a.id desc')->select();
        $data['code'] = 0;
        $data['msg'] = '';
        $data['count'] = $count;
        $data['data'] = $all;
        return json($data);
    }

    /**
     * 浏览量
     * @Author: 闻子 < QQ：270988107 >
     */
    public function code()
    {
        $id = input('id');
        if (empty($id)) {
            return json(['html'=>1]);
        }
        $info = Db::name('scode')->where('id',$id)->find();
        if ($info) {
            $data = Db::name('scode')->where('id',$id)->setInc('browser');
            if ($data) {
                $c = Db::name('scode')->where('id',$id)->find();
                return json(['html'=>$c['browser']]);
            }
        }else{
            return json(['html'=>1]);
        }
    }

    /**
     * 源码编辑
     * @Author: 闻子 < QQ：270988107 >
     */
    public function getedit()
    {
        $this->getManager();
        $data = self::$ScodeLogic->getedit(input('post.'));
        return $data;
    }

    /**
     * 源码编辑
     * @Author: 闻子 < QQ：270988107 >
     */
    public function edit()
    {
        $this->getSession();
        $data = self::$ScodeLogic->getScodeInfo(['id'=>input('id')]);
        $this->request->isPost() && $this->jump(self::$ScodeLogic->formdata(input('post.')));
        return view('',compact('data'));
    }

    /**
     * 源码删除
     * @Author: 闻子 < QQ：270988107 >
     */
    public function del($id = 0)
    {
        $this->getManager();
        if (empty($id)) {
            return json(['code' => 0, 'msg' => '请求参数异常', 'data' => '']);
        }
        $data = self::$ScodeLogic->getScodeDel($id);
        return json($data);
    }

    /**
     * 源码批量删除
     * @Author: 闻子 < QQ：270988107 >
     */
    public function alldel()
    {
        $this->getManager();
        $data = self::$ScodeLogic->getScodealldel(input('ids'));
        return $data;
    }

    /**
     * 上传TXT
     * @Author: 闻子 < QQ：270988107 >
     */
    public function uptxt()
    {
    	$this->getSession();
        $file = request()->file('file');
        $info = $file->move(Env::get('root_path') . 'public/static/txt');
        if ($info) {
            $res['code'] = 1;
            $res['msg'] = '上传成功';
            $res['name'] = $info->getFilename();
            $NamePath = str_replace('\\', "/", $info->getSaveName());
            $res['filepath'] = '/public/static/txt/' . $NamePath;
        } else {
            $res['code'] = 0;
            $res['msg'] = '上传失败' . $file->getError();
        }
        return $res;

    }

    /**
     * 上传封面
     * @Author: 闻子 < QQ：270988107 >
     */
    public function upthumb()
    {
    	$this->getSession();
        $file = request()->file('file');
        $info = $file->move(Env::get('root_path') . 'public/static/thumb');
        if ($info) {
            $res['code'] = 1;
            $res['msg'] = '上传成功';
            $res['name'] = $info->getFilename();
            $NamePath = str_replace('\\', "/", $info->getSaveName());
            $res['filepath'] = '/public/static/thumb/' . $NamePath;
            $configinfo = Db::name('config')->where('id',1)->find();
            $image = \think\Image::open('./public/static/thumb/' . $NamePath);
            $image->text($configinfo['sy'],'./public/static/simkai.ttf',20,'#f088a9',\think\Image::WATER_WEST,0,30)->save('./public/static/thumb/' . $NamePath);
            $image->text($configinfo['sy'],'./public/static/simkai.ttf',20,'#f088a9',\think\Image::WATER_CENTER,0,30)->save('./public/static/thumb/' . $NamePath);
            $image->text($configinfo['sy'],'./public/static/simkai.ttf',20,'#f088a9',\think\Image::WATER_EAST,0,30)->save('./public/static/thumb/' . $NamePath);
        } else {
            $res['code'] = 0;
            $res['msg'] = '上传失败' . $file->getError();
        }
        return $res;

    }

    /**
     * 富本图片上传
     * @Author: 闻子 < QQ：270988107 >
     */
	public function lay_img_upload()
	{
        $this->getSession();
		$file = request()->file('file');
		if(empty($file)){
			$result["code"] = "1";
			$result["msg"] = "请选择图片";
			$result['data']["src"] = '';
		}else{
			$info = $file->move(Env::get('root_path') . '/public/static/images/' );
			if($info){
				$name_path =str_replace('\\',"/",$info->getSaveName());
				$result["code"] = '0';
				$result["msg"] = "上传成功";
				$result['data']["src"] = "/public/static/images/".$name_path;
                $configinfo = Db::name('config')->where('id',1)->find();
                $image = \think\Image::open('./public/static/images/' . $name_path);
                $image->text($configinfo['sy'],'./public/static/simkai.ttf',20,'#f088a9',\think\Image::WATER_WEST,0,30)->save('./public/static/images/' . $name_path);
                $image->text($configinfo['sy'],'./public/static/simkai.ttf',20,'#f088a9',\think\Image::WATER_CENTER,0,30)->save('./public/static/images/' . $name_path);
                $image->text($configinfo['sy'],'./public/static/simkai.ttf',20,'#f088a9',\think\Image::WATER_EAST,0,30)->save('./public/static/images/' . $name_path);
			}else{
				$result["code"] = "2";
				$result["msg"] = "上传出错";
				$result['data']["src"] ='';
		    }
		}
		return json_encode($result);
	}
}