<?php
/**
 * Base.php
 * 文件描述
 * Created by: Owen
 * Created on: 2019/7/12 11:18
 * Description:
 *
 */
Class Init{
    /**@var $db DB*/
    private $db;
    /**@var $redis muredis*/
    private $redis;


    /**
     *  gatewayWorker 原生链接数据库
     *  $this->db = Db::instance('common');
     *  $this->redis = Store::instance('redis');
     *
     *  redis 两种连接方式
     *  ini_set('default_socket_timeout', -1); //redis不超时
        global $redis;
        $redis = new \Redis();
        $redis->connect('127.0.0.1', 6379);
        $user_cp_redis = $redis->hGetAll('SLOTCONFIG');
        var_dump($user_cp_redis);
        $user_cp_redis = $this->redis->hGetAll('SLOTCONFIG');
        var_dump($user_cp_redis);
     */

    /**
     *  原生 PHP 链接数据库
     *
        $this->redis = oo::commonOprRedis('common');
        $this->db = oo::commonOprDb('common');
        if($this->db->connect() && !$this->db->ping()){
        $this->db->close();
    }
     */

    /**
     * 确定SID
     * Init constructor.
     * @param $sid
     */
    public $client_id='';
    public function __construct($sid,$client_id=''){
//        define('SGSID', $sid);
//        define('DEVICEID', SGSID);//平台
        $this->client_id = $client_id;
    }


    /**
     *  $method 获得方法名
     *  $arg 获得方法的参数集合
     */
    public function __call($method,$arg){
        return false;
    }

    function __get($property){
        global $dbs;
        global $rediss;
        if($property == "redis"){
            if(!isset($this->redis)){
                $this->redis = $rediss['common'];
            }
            return $this->$property;
        }else if($property == "db"){
            if(!isset($this->db)){
                $this->db = $dbs;
            }
            return $this->$property;
        }
    }

    public function encryp($str){
        $data = $this->client_id?$_SESSION[$this->client_id]:[];
        if(isset($data['encryp'])&&!$data['encryp']){
            $out = $str;
        }else{
            $str = base64_encode($str);
            $key = floor(strlen($str)/2)-1;
            $out = substr($str,0,$key).substr(md5(time()),0,16).substr($str,$key,strlen($str));
            $out = substr($out,0,1).substr(md5(time()),0,1).substr($out,1,strlen($out));
        }
        return $out;
    }
}
