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


/**
 * 获取对象
 * @param string $object_name 对象名
 * @return object
 */
function get_sington_object($object_name = '', $class = null)
{
    $request = \think\facade\Request::instance();
    $request->__isset($object_name) ?: $request->bind($object_name, new $class());
    return $request->__get($object_name);
}

function getcurl($url,$params=false,$ispost=0)
{
    $httpInfo = array();
    $ch = curl_init();
    curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
    curl_setopt( $ch, CURLOPT_USERAGENT , 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22' );
    curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 30 );
    curl_setopt( $ch, CURLOPT_TIMEOUT , 30);
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
    if( $ispost ){
        curl_setopt( $ch , CURLOPT_POST , true );
        curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
        curl_setopt( $ch , CURLOPT_URL , $url );
    }else{
        if($params){
            curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
        }else{
            curl_setopt( $ch , CURLOPT_URL , $url);
        }
    }
    $response = curl_exec( $ch );
    if ($response === FALSE) {
        return false;
    }
    $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
    $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
    curl_close( $ch );
    return $response;
}

function assortmentInfo($id)
{
    $model = model('assortment');
    $where['pid'] = $id;
    $list = $model->where($where)->select();
    return $list;
}

function subtext($text, $length)
{
    if(mb_strlen($text, "utf8") > $length)
    return mb_substr($text,0,$length,'utf8').'…';
    return $text;
}

function htmltotxt($string)
{    
    $string = htmlspecialchars_decode($string);
    $string = str_replace ( '"', '＂', $string );    
    $string = str_replace ( "'", '＇', $string );
    $string = htmlspecialchars(strip_tags ( $string ));     
    return $string;
}

function time_ago($posttime)
{
     //当前时间的时间戳
    $nowtimes = strtotime(date('Y-m-d H:i:s'),time());
    //之前时间参数的时间戳
    $posttimes = strtotime($posttime);
    //相差时间戳
    $counttime = $nowtimes - $posttimes;
    //进行时间转换
    if($counttime<=10){
       return '刚刚';
    }else if($counttime>10 && $counttime<=30){
       return '刚才';
    }else if($counttime>30 && $counttime<=60){
        return '刚一会';
    }else if($counttime>60 && $counttime<=120){
       return '1分钟前';
    }else if($counttime>120 && $counttime<=180){
       return '2分钟前';
    }else if($counttime>180 && $counttime<3600){
       return intval(($counttime/60)).'分钟前';
    }else if($counttime>=3600 && $counttime<3600*24){
       return intval(($counttime/3600)).'小时前';
    }else if($counttime>=3600*24 && $counttime<3600*24*2){
       return '昨天';
    }else if($counttime>=3600*24*2 && $counttime<3600*24*3){
       return '前天';
    }else if($counttime>=3600*24*3 && $counttime<=3600*24*20){
       return intval(($counttime/(3600*24))).'天前';
    }else{
       return $posttime;
    }
}

function userTime($pid)
{
    switch($pid){
        case '1':
        $lattice = date('Y-m-d H:i:s',strtotime('+1week'));
        break;
        case '2':
        $lattice = date('Y-m-d H:i:s',strtotime('+1month'));
        break;
        case '3':
        $lattice = date('Y-m-d H:i:s',strtotime('+1year'));
        break;
        case '4':
        $lattice = date('Y-m-d H:i:s',strtotime('+30year'));
        break;
    }
    return $lattice;
}

function userXfTime($pid,$time)
{
    switch($pid){
        case '1':
        $lattice = date('Y-m-d H:i:s',strtotime('+1week', strtotime($time)));
        break;
        case '2':
        $lattice = date('Y-m-d H:i:s',strtotime('+1month', strtotime($time)));
        break;
        case '3':
        $lattice = date('Y-m-d H:i:s',strtotime('+1year', strtotime($time)));
        break;
        case '4':
        $lattice = date('Y-m-d H:i:s',strtotime('+10year', strtotime($time)));
        break;
    }
    return $lattice;
}

function userYmcw($pid)
{
    $data = model('user');
    $datac = model('upgrade');
    $user = $data->where('id',$pid)->find();
    $upgrade = $datac->where('id',$user['auth_id'])->find();
    if ($user['auth_id'] == 0) {
        $upgrade['title'] = "普通会员";
    }
    return $upgrade['title'];
}

function ScodeInfo($id)
{
    $data = model('scode');
    $menu = model('menu');
    $menuinfo = $menu->order('sort asc')->find();
    if (empty($id)) {
        $ScodeInfo = $data->where('mid',$menuinfo['id'])->where('examine',1)->select();
    }else{
        $ScodeInfo = $data->where('mid',$id)->where('examine',1)->select();
    }
    return $ScodeInfo;
}

function getSign($row) {
    $t = $row + 1;
    if ($t > date('d')) {
        $td = "<td>
                <div class='on'>" . $t . "</div>
                </td>";
    } else {
        if (strlen($t) == 1) {
            $day = "0" . $t;
        } else {
            $day = $t;
        }
        $t2 = strtotime(date("Y-m-" . $day . ""));
        $where['addtime'] = $t2;
        $where['status'] = 0;
        $where['uid'] = session('admin.id');
        $info = model("sign")->field("id")->where($where)->find();
        if ($info) {
            $td = "<td>
                    <div class='yes'><i class='fa fa-check-circle-o'></i></div>
                    </td>";
        } else {
            if ($t == date('d')) {
                $td = "<td onclick='signDay($(this))'><div class='on yc' style='padding: 0px 0;'><a href='javascript:;'>签到</a></div></td>";
            } else {
                $td = "<td>
                        <div class='yq on'>" . $t . "</div>
                        </td>";
            }
        }
    }
    return $td;
}

function taginfo($data)
{
    $arr = explode(",",$data);
    return $arr;
}

function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true){
    if(function_exists("mb_substr")){
        if($suffix){
            mb_substr($str, $start, $length, $charset)."...";
        }else{
            return mb_substr($str, $start, $length, $charset);
        }
    }elseif(function_exists('iconv_substr')) {
        if($suffix){
            return iconv_substr($str,$start,$length,$charset)."...";
        }else{
            return iconv_substr($str,$start,$length,$charset);
        }
    }
    $re['utf-8']   = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
    $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
    $re['gbk']    = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
    $re['big5']   = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
    preg_match_all($re[$charset], $str, $match);
    $slice = join("",array_slice($match[0], $start, $length));
    if($suffix){
        return $slice."...";
    }else{
        return $slice;
    }
}