<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件


function page_result($code = 0, $msg = '', $data = [])
{
    $res      = ['code' => $code, 'msg' => $msg, 'data' => $data];
    $response = \think\Response::create($res, 'json');
    throw new \think\exception\HttpResponseException($response);
}


/**
 * 时间
 */
function tranTime($utime)
{
    $time = time() - $utime;
    if ($time < 60) {
        $str = '刚刚';
    } elseif ($time < 60 * 60) {
        $min = floor($time / 60);
        $str = $min . '分钟前';
    } elseif ($time < 60 * 60 * 24) {
        $h   = floor($time / (60 * 60));
        $str = $h . '小时前';
    } elseif ($time < 60 * 60 * 24 * 3) {
        $d = floor($time / (60 * 60 * 24));
        if ($d == 1) {
            $str = '昨天';
        } else {
            $str = '前天';
        }
    } else {
        $str = date("m-d", $utime);
    }
    return $str;
}


/**
 * api 接口正确输出
 * @param string $data 返回数据
 * @param string $message 提示信息
 */
function json_success($data = '', $message = 'success')
{
    header('Content-Type:application/json; charset=utf-8');
    $result['status']  = 1;
    $result['message'] = $message;
    $result['data']    = empty($data) ? [] : $data;

    exit(json_encode($result));
}

/**
 * api 接口错误输出
 * @param int $status 状态码： -1参数错误（开发提示） -2用户提示（用户输入错误、商品不存在等） -9token过期
 * @param string $message 提示信息
 */
function json_error($message = 'error', $status = -1)
{
    header('Content-Type:application/json; charset=utf-8');
    $result['status']  = $status;
    $result['message'] = $message;

    exit(json_encode($result));
}

/**
 * 获取客户端IP地址
 * @param int $type 返回类型 0 返回IP地址 1 返回IPV4地址数字
 * @return mixed
 */
function get_client_ip($type = 0)
{
    $type = $type ? 1 : 0;
    static $ip = null;
    if ($ip !== null) {
        return $ip[$type];
    }

    if (isset($_SERVER['HTTP_X_REAL_IP'])) {
        //nginx 代理模式下，获取客户端真实IP
        $ip = $_SERVER['HTTP_X_REAL_IP'];
    } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
        //客户端的ip
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    } elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        //浏览当前页面的用户计算机的网关
        $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
        $pos = array_search('unknown', $arr);
        if (false !== $pos) {
            unset($arr[$pos]);
        }
        $ip = trim($arr[0]);
    } else {
        //浏览当前页面的用户计算机的ip地址
        $ip = $_SERVER['REMOTE_ADDR'];
    }

    //IP地址合法验证
    $long = sprintf("%u", ip2long($ip));
    $ip   = $long ? [$ip, $long] : ['0.0.0.0', 0];

    return $ip[$type];
}

/**
 * 字符转码
 * @param $data
 * @return array|string
 */
function gbk2utf8($data)
{
    if (is_array($data)) {
        return array_map('gbk2utf8', $data);
    }

    return iconv('gbk', 'utf-8//IGNORE', $data); //IGNORE 会忽略掉不能转化的字符，而默认效果是从第一个非法字符截断。
}


/**
 *  根据身份证号码获取性别
 *  author:xiaochuan
 * @param string $idcard 身份证号码
 * @return int $sex 性别 1男 2女 0未知
 */
function get_sex($idcard)
{
    if (empty($idcard)) {
        return null;
    }
    $sexint = (int)substr($idcard, 16, 1);
    return $sexint % 2 === 0 ? 2 : 1;
}

/**
 *  根据身份证号码获取生日
 *  author:xiaochuan
 * @param string $idcard 身份证号码
 * @return $birthday
 */
function get_birthday($idcard)
{
    if (empty($idcard)) {
        return null;
    }
    $bir   = substr($idcard, 6, 8);
    $year  = (int)substr($bir, 0, 4);
    $month = (int)substr($bir, 4, 2);
    $day   = (int)substr($bir, 6, 2);
    return $year . "-" . $month . "-" . $day;
}

/**
 *  根据身份证号码计算年龄
 *  author:xiaochuan
 * @param string $idcard 身份证号码
 * @return int $age
 */
function get_age($idcard)
{
    if (empty($idcard)) {
        return null;
    }
    #  获得出生年月日的时间戳
    $date = strtotime(substr($idcard, 6, 8));
    #  获得今日的时间戳
    $today = strtotime('today');
    #  得到两个日期相差的大体年数
    $diff = floor(($today - $date) / 86400 / 365);
    #  strtotime加上这个年数后得到那日的时间戳后与今日的时间戳相比
    $age = strtotime(substr($idcard, 6, 8) . ' +' . $diff . 'years') > $today ? ($diff + 1) : $diff;
    return $age;
}

/**
 *  根据身份证号码获取出身地址
 *  author:xiaochuan
 * @param string $idcard 身份证号码
 * @return string $address
 */
// function get_address($idcard, $type=1){
//     if(empty($idcard)) return null;
//     $address = include('./address.php');
//     switch ($type) {
//         case 1:
//                 # 截取前六位数(获取基体到县区的地址)
//                 $key = substr($idcard,0,6);
//                 if(!empty($address[$key])) return $address[$key];
//                 # 截取前两位数(没有基体到县区的地址就获取省份)
//                 $key = substr($idcard,0,2);
//                 if(!empty($address[$key])) return $address[$key];
//                 # 都没有
//                 return '未知地址';
//             break;
//         case 2:
//                 # 截取前两位数(只获取省份)
//                 $key = substr($idcard,0,2);
//                 if(!empty($address[$key])) return $address[$key];
//             break;
//         default:
//                 return null;
//             break;
//     }
// }

/**
 *  判断字符串是否是身份证号
 *  author:xiaochuan
 * @param string $idcard 身份证号码
 */
function isIdCard($idcard)
{
    #  转化为大写，如出现x
    $idcard = strtoupper($idcard);
    #  加权因子
    $wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
    $ai = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];
    #  按顺序循环处理前17位
    $sigma = 0;
    #  提取前17位的其中一位，并将变量类型转为实数
    for ($i = 0; $i < 17; $i++) {
        $b = (int)$idcard{$i};
        #  提取相应的加权因子
        $w = $wi[$i];
        #  把从身份证号码中提取的一位数字和加权因子相乘，并累加
        $sigma += $b * $w;
    }
    #  计算序号
    $sidcard = $sigma % 11;
    #  按照序号从校验码串中提取相应的字符。
    $check_idcard = $ai[$sidcard];
    if ($idcard{17} == $check_idcard) {
        return true;
    } else {
        return false;
    }
}

/**
 *  根据身份证号，返回对应的生肖
 *  author:xiaochuan
 * @param string $idcard 身份证号码
 */
function get_zodiac($idcard)
{ //
    if (empty($idcard)) {
        return null;
    }
    $start = 1901;
    $end   = (int)substr($idcard, 6, 4);
    $x     = ($start - $end) % 12;
    $val   = '';
    if ($x == 1 || $x == -11) {
        $val = '鼠';
    }
    if ($x == 0) {
        $val = '牛';
    }
    if ($x == 11 || $x == -1) {
        $val = '虎';
    }
    if ($x == 10 || $x == -2) {
        $val = '兔';
    }
    if ($x == 9 || $x == -3) {
        $val = '龙';
    }
    if ($x == 8 || $x == -4) {
        $val = '蛇';
    }
    if ($x == 7 || $x == -5) {
        $val = '马';
    }
    if ($x == 6 || $x == -6) {
        $val = '羊';
    }
    if ($x == 5 || $x == -7) {
        $val = '猴';
    }
    if ($x == 4 || $x == -8) {
        $val = '鸡';
    }
    if ($x == 3 || $x == -9) {
        $val = '狗';
    }
    if ($x == 2 || $x == -10) {
        $val = '猪';
    }
    return $val;
}

/**
 *  根据身份证号，返回对应的星座
 *  author:xiaochuan
 * @param string $idcard 身份证号码
 */
function get_starsign($idcard)
{
    if (empty($idcard)) {
        return null;
    }
    $b   = substr($idcard, 10, 4);
    $m   = (int)substr($b, 0, 2);
    $d   = (int)substr($b, 2);
    $val = '';
    if (($m == 1 && $d <= 21) || ($m == 2 && $d <= 19)) {
        $val = "水瓶座";
    } elseif (($m == 2 && $d > 20) || ($m == 3 && $d <= 20)) {
        $val = "双鱼座";
    } elseif (($m == 3 && $d > 20) || ($m == 4 && $d <= 20)) {
        $val = "白羊座";
    } elseif (($m == 4 && $d > 20) || ($m == 5 && $d <= 21)) {
        $val = "金牛座";
    } elseif (($m == 5 && $d > 21) || ($m == 6 && $d <= 21)) {
        $val = "双子座";
    } elseif (($m == 6 && $d > 21) || ($m == 7 && $d <= 22)) {
        $val = "巨蟹座";
    } elseif (($m == 7 && $d > 22) || ($m == 8 && $d <= 23)) {
        $val = "狮子座";
    } elseif (($m == 8 && $d > 23) || ($m == 9 && $d <= 23)) {
        $val = "处女座";
    } elseif (($m == 9 && $d > 23) || ($m == 10 && $d <= 23)) {
        $val = "天秤座";
    } elseif (($m == 10 && $d > 23) || ($m == 11 && $d <= 22)) {
        $val = "天蝎座";
    } elseif (($m == 11 && $d > 22) || ($m == 12 && $d <= 21)) {
        $val = "射手座";
    } elseif (($m == 12 && $d > 21) || ($m == 1 && $d <= 20)) {
        $val = "魔羯座";
    }
    return $val;
}
