System.php 5.33 KB
<?php
namespace app\admin\controller;
use think\Db;
use think\facade\Request;
class System extends Common
{
    /********************************站点管理*******************************/
    //站点设置
    public function system($sys_id=1){
        $table = db('system');
        if(Request::isAjax()) {
            $data = Request::except('file');
            if($table->where('id',1)->update($data)!==false) {
                savecache('System');
                return json(['code' => 1, 'msg' => '站点设置保存成功!', 'url' => url('system/system')]);
            } else {
                return json(array('code' => 0, 'msg' =>'站点设置保存失败!'));
            }
        }else{
            $system = $table->find($sys_id);
            $this->assign('system', json_encode($system,true));
            return $this->fetch();
        }
    }
    public function email(){
        if(Request::isAjax()) {
            $datas = input('post.');
            foreach ($datas as $k=>$v){
               Db::name('config')->where([['name','=',$k],['inc_type','=','smtp']])->update(['value'=>$v]);
            }
            return json(['code' => 1, 'msg' => '邮箱设置成功!', 'url' => url('system/email')]);
        }else{
            $smtp = Db::name('config')->where('inc_type','smtp')->select();
            $info = convert_arr_kv($smtp,'name','value');
            $this->assign('info', json_encode($info,true));
            return $this->fetch();
        }
    }
    public function trySend(){
        $sender = input('email');
        //检查是否邮箱格式
        if (!is_email($sender)) {
            return json(['code' => 0, 'msg' => '测试邮箱码格式有误']);
        }
        $arr = db('config')->where('inc_type','smtp')->select();
        $config = convert_arr_kv($arr,'name','value');
        $content = $config['test_eamil_info'];
        $send = send_email($sender, '测试邮件',$content);
        if ($send) {
            return json(['code' => 1, 'msg' => '邮件发送成功!']);
        } else {
            return json(['code' => 0, 'msg' => '邮件发送失败!']);
        }
    }

    /**
     * 获取日志列表
     */
    public function log(){
        //基础参数
        /* 使用autoloader类自动加载所有需要的PHP模块。注意使用合适的路径指向autoloader类所在文件*/
        $DOCUMENT_ROOT = str_replace('public','',$_SERVER['DOCUMENT_ROOT']);
        require_once $DOCUMENT_ROOT.'vendor/aliyunlog/Log_Autoload.php';
        //require_once realpath(dirname(__FILE__) . '/../Log_Autoload.php');
        $endpoint = 'cn-beijing.log.aliyuncs.com';      // 选择与上面步骤创建project所属区域匹配的Endpoint
        $accessKeyId = 'LTAI4Fivq9UqCkAnCgDKqMBC';              // 使用你的阿里云访问密钥AccessKeyId
        $accessKey = 'AdmOpP2LzbbbY6N9BaGnIE3UBzI62E';  // 使用你的阿里云访问密钥AccessKeySecret
        $project = '101-dc-api-log';                        // 上面步骤创建的项目名称
        $logstore = 'info-log';                       // 上面步骤创建的日志库名称
        $client = new \Aliyun_Log_Client($endpoint, $accessKeyId, $accessKey);

        if(Request::isAjax()) {
            $data = Request::except('file');
            #查询日志数据
            $topic          =   "";
            $total_query_str=   '* | select count(1) as count';
            $key_query_str  =   '';
            $from           =   time()-2592000;
            $to             =   time();
            $total          =   NULL;
            $key_query      =   NULL;
            while (is_null($total) || (! $total->isCompleted()))
            {
                $total      = new \Aliyun_Log_Models_GetLogsRequest($project, $logstore, $from, $to, $topic, $total_query_str, 0, 0, true);
                $total      = $client->getLogs($total);
                
            }

            while (is_null($key_query) || (! $key_query->isCompleted()))
            {
                $key_query  = new \Aliyun_Log_Models_GetLogsRequest($project, $logstore, $from, $to, $topic, $key_query_str, $data['limit'], $data['page'], true);
                $key_query  = $client->getLogs($key_query);
            }
            $result = array();
            $count  = 0;
            try {
                foreach($total -> getLogs() as $log)
                {
                    foreach($log -> getContents() as $key => $value){
                        $count = $value;  //总数
                    }
                }
                foreach($key_query -> getLogs() as $k=>$v)
                {
                    foreach($v -> getContents() as $key => $value){
                        $result['list'][$k][$key] = $value;
                    }
                }
            } catch (Aliyun_Log_Exception $ex) {
                logVarDump($ex);
            } catch (Exception $ex) {
                logVarDump($ex);
            }
            return ['code'=>0,'msg'=>'获取成功!','data'=>$result['list'],'count'=>$count,'rel'=>1];
        }
        return $this->fetch();
    }

    //PHP stdClass Object转array  
    
    public function objectToArray($e){
        $e=(array)$e;
        foreach($e as $k=>$v){
            if( gettype($v)=='resource' ) return;
            if( gettype($v)=='object' || gettype($v)=='array' )
                $e[$k]=(array)self::objectToArray($v);
        }
        return $e;
    }

}