<?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>
// +----------------------------------------------------------------------

// 应用公共文件

/*微信小程序用
 * code 成功
 * 20210927
 * wj
 */
function backjson($code, $data)
{
    $jsonData = [
        'code' => $code,
        'resultData' => $data,
    ];
    return json_encode($jsonData, 320);
}
/*app用
 * 20210927
 * wj
 */
function backjson2($code, $msg = "", $data = [])
{
    $jsonData = [
        'code' => $code,
        'msg' => $msg,
        'data' => $data,
    ];
    return json_encode($jsonData, 320);
}
/* 内部使用
 * 20210927
 * wj
 */
function backarr($status, $msg = "", $data = [])
{
    $backData = [
        'status' => $status,
        'msg' => $msg,
        'data' => $data,
    ];
    return $backData;
}
/***
 * 获取单号
 * wj
 * 20210911
 */
function createOrderNo()
{
    $ordrNo = date("YmdHis") . rand(0, 1000);
    return $ordrNo;
}
/***
 * 获取随机字符串
 * wj
 * 20210911
 */
function getRandomStrings($str = "")
{
    $str .= time();
    return md5($str);
}
/***
 * 判断是否为日期
 * wj
 * 20211013
 */
function isDate($date, $foramt)
{
    $date1 = $date;
    $time = strtotime($date1);
    if (!$time) {
        return false;
    }
    $date2 = date($foramt, $time);
    return $date1 == $date2;
}

//curl请求
function requestCurl($url, $method = "get", $data = '', $header = [], $second = 10)
{
    $method = strtoupper($method);
    $method = in_array($method, ['GET', 'POST']) ? $method : 'GET';

    $ch = curl_init();
    //设置超时
    curl_setopt($ch, CURLOPT_TIMEOUT, $second);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

    //要求结果为字符串且输出到屏幕上
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //严格校验2
    //设置header
    curl_setopt($ch, CURLOPT_HEADER, false);
    if (!empty($header)) {
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    }
    if ('POST' == $method) {
        //$data = json_encode($data, JSON_UNESCAPED_UNICODE);
        curl_setopt($ch, CURLOPT_POST, true);
        if (is_array($data)) {
            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data, ''));
        }
        if (is_string($data)) {
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
        }
    }
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //禁止 cURL 验证对等证书
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //是否检测服务器的域名与证书上的是否一致
    //运行curl
    $data = curl_exec($ch);
    if (curl_errno($ch)) { //出错则显示错误信息
        $error = curl_error($ch);
        curl_close($ch);
        return $error;
    } else {
        curl_close($ch);
        return $data;
    }
}
function arry_string($data)
{
    if (!is_array($data)) {
        return false;
    }
    $usedata = [];
    foreach ($data as $key => $value) {
        if (!is_string($value) && !is_numeric($value)) {
            return false;
        }
        $usedata[] = $key . '=' . $value;
    }
    $str = implode('&', $usedata);
    return $str;
}

//根据身份证获取性别
function getGenderBySfzid($sfzid)
{
    $gender = false;
    switch (mb_strlen($sfzid)) {
        case 15:
            $genderNum = substr($sfzid, 14, 1);
            break;
        case 18:
            $genderNum = substr($sfzid, 16, 1);
            break;
    }
    if (($genderNum % 2) === 0) {
        $gender = "女";
    } else {
        $gender = "男";
    }
    return $gender;
}
function getGenderByAge($sfzid)
{
    $birthday = substr($sfzid, 6, 8);
    $bmd = substr($birthday, 4, 4);
    $byear = substr($birthday, 0, 4);
    $strtoday = date('Ymd');
    $md = substr($strtoday, 4, 4);
    $year = substr($strtoday, 0, 4);
    $age = intval($year) - intval($byear);
    if ($md < $bmd) {
        $age = $age - 1;
    }
    return $age;
}
//获取身份证结束时间
function getSfzend($sfzenddate)
{
    if (is_numeric($sfzenddate)) {
        if (isDate($sfzenddate, "Ymd")) {
            $strenddate = $sfzenddate;
            $strendmonth = substr($strenddate, 4, 2);
            $strendyear = substr($strenddate, 0, 4);
            $strendday = substr($strenddate, 6, 2);
            $sfzenddate = $strendyear . '-' . $strendmonth . '-' . $strendday;
        } else {
            $sfzenddate = '2050-12-31';
        }
    } else {
        $sfzenddate = false;
    }
    return $sfzenddate;
}
//为cp后台请求验证token
/*function checkToken()
{
$l_w = new app\index\logic\webmanger();
$token = request()->server('HTTP_TOKEN');
$param = ['token' => $token];
$result = $l_w->queryinfobytoken($param);
if (1 != $result['status']) {
$str = backjson2(0, '登录失效', $result['data']);
exit($str);
}
}*/
//判断是否是手机号
function isMoblid($tel)
{
    if (preg_match("/^1[3-9]{1}\d{9}$/", $tel)) {
        return true;
    } else {
        return false;
    }
}
//获取httpsurl
function getselfurl($name = null)
{
    $env = config('env');
    $sellurl = config('selfurl');
    $urllist = isset($sellurl[$env]) ? $sellurl[$env] : $sellurl['pro'];
    $defaultname = "default";
    if (empty($name)) {
        $name = $defaultname;
    }
    return isset($urllist[$name]) ? $urllist[$name] : $urllist[$defaultname];
}
/**
 * 随机验证码
 *
 * @param  integer $type
 * @param  integer $len
 * @return void
 * @author wj
 * @date 2022-07-25
 */
function randomStr($type = 1, $len = 6)
{
    $number = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
    $strArray = [];
    switch ($type) {
        case 1:
            $strArray = $number;
            break;
    }
    $len = $len <= 0 ? 6 : $len;
    $len = $len > 10 ? 6 : $len;
    $str = '';
    for ($i = 0; $i < $len; $i++) {
        $num = $strArray[mt_rand(0, count($strArray) - 1)];
        $str .= $num;
    }
    return $str;
}
