/**
* 接口返回
* @param number $id 参数id
*/
if (!function_exists('jsonReturn')) {
function jsonReturn($code = 200, $msg = '', $data = null)
{
$arr = [
'code' => $code,
'msg' => $msg,
];
if ($data) $arr['data'] = $data;
return json_encode($arr);
}
}
/**
* 生成订单号接口
* @return string
*/
if (!function_exists('orderCreate')) {
function orderCreate()
{
$code = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J');
$osn = $code[intval(date('Y')) - 2011] . strtoupper(dechex(date('m')))
. date('d') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));
return $osn;
}
}
/**
*字符串截取
*@param $sourcesstr string 原字符串
*@param $cutlength int 截取个数
*@return string
*/
if(!function_exists('cut_str)){ function cut_str($sourcestr, $cutlength)
{
$returnstr = '';
$i = 0;
$n = 0;
$str_length = strlen($sourcestr);//字符串的字节数
while (($n < $cutlength) and ($i <= $str_length)) {
$temp_str = substr($sourcestr, $i, 1);
$ascnum = Ord($temp_str);//得到字符串中第$i位字符的ascii码
if ($ascnum >= 224) //如果ASCII位高与224,
{
//根据UTF-8编码规范,将3个连续的字符计为单个字符
$returnstr = $returnstr . substr($sourcestr, $i, 3);
$i = $i + 3; //实际Byte计为3
$n++; //字串长度计1
} elseif ($ascnum >= 192) //如果ASCII位高与192,
{
//根据UTF-8编码规范,将2个连续的字符计为单个字符
$returnstr = $returnstr . substr($sourcestr, $i, 2);
$i = $i + 2; //实际Byte计为2
$n++; //字串长度计1
} elseif ($ascnum >= 65 && $ascnum <= 90) //如果是大写字母,
{
$returnstr = $returnstr . substr($sourcestr, $i, 1);
$i = $i + 1; //实际的Byte数仍计1个
$n++; //但考虑整体美观,大写字母计成一个高位字符
} else //其他情况下,包括小写字母和半角标点符号,
{
$returnstr = $returnstr . substr($sourcestr, $i, 1);
$i = $i + 1; //实际的Byte数计1个
$n = $n + 0.5; //小写字母和半角标点等与半个高位字符宽...
}
}
if ($str_length > $i) {
$returnstr = $returnstr . "...";//超过长度时在尾处加上省略号
}
return $returnstr;
}
}
/**
* 无线分类树
* @param $list
* @param int $pid
* @param int $level
* @param string $html
* @return array
*/
if(!function_exists()){
function tree(&$list, $pid = 0, $level = 0, $html = '—|')
{
static $tree = array();
foreach ($list as $v) {
if ($v['pid'] == $pid) {
$v['level'] = $level;
$v['html'] = str_repeat($html, $level);
$tree[] = $v;
tree($list, $v['category_id'], $level + 1, $html);
}
}
return $tree;
}
}
/**
* [CheckMobile 手机号码格式校验]
* @param [int] $mobile [手机号码]
* @return [boolean] [正确true,失败false]
* @author Fesion
* @version 0.0.1
* @datetime 2019-08-28
*/
if (!function_exists('CheckMobile')) {
function CheckMobile($mobile)
{
return (preg_match('/^1((3|5|8|7){1}\d{1})\d{8}$/', $mobile) == 1) ? true : false;
}
}
/**
* [EncryptPassword 密码加密]
* @param [string] $pwd [需要加密的密码]
* @param [string] $salt [配合密码加密的随机数]
* @return [string] [加密后的密码]
*/
if (!function_exists('EncryptPassword')) {
function EncryptPassword($pwd, $salt = '', $encrypt = 'md5')
{
return $encrypt(trim($pwd) . $salt);
}
}
/**
* 获取全球唯一标识
* @return string
*/
if (!function_exists('uuid')) {
function uuid()
{
return sprintf(
'%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
mt_rand(0, 0xffff),
mt_rand(0, 0xffff),
mt_rand(0, 0xffff),
mt_rand(0, 0x0fff) | 0x4000,
mt_rand(0, 0x3fff) | 0x8000,
mt_rand(0, 0xffff),
mt_rand(0, 0xffff),
mt_rand(0, 0xffff)
);
}
}
/**
*
* 获取客户端真实ip
* @return int
*/
if (!function_exists('getIp')) {
function getIp()
{
$realip = '';
$unknown = 'unknown';
if (isset($_SERVER)) {
if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && !empty($_SERVER['HTTP_X_FORWARDED_FOR'])
&& strcasecmp($_SERVER['HTTP_X_FORWARDED_FOR'], $unknown)) {
$arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
foreach ($arr as $ip) {
$ip = trim($ip);
if ($ip != 'unknown') {
$realip = $ip;
break;
}
}
} else if (isset($_SERVER['HTTP_CLIENT_IP']) && !empty($_SERVER['HTTP_CLIENT_IP'])
&& strcasecmp($_SERVER['HTTP_CLIENT_IP'], $unknown)) {
$realip = $_SERVER['HTTP_CLIENT_IP'];
} else if (isset($_SERVER['REMOTE_ADDR']) && !empty($_SERVER['REMOTE_ADDR'])
&& strcasecmp($_SERVER['REMOTE_ADDR'], $unknown)) {
$realip = $_SERVER['REMOTE_ADDR'];
} else {
$realip = $unknown;
}
} else {
if (getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'),
$unknown)) {
$realip = getenv("HTTP_X_FORWARDED_FOR");
} else if (getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'),
$unknown)) {
$realip = getenv("HTTP_CLIENT_IP");
} else if (getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), $unknown)) {
$realip = getenv("REMOTE_ADDR");
} else {
$realip = $unknown;
}
}
$realip = preg_match("/[\d\.]{7,15}/", $realip, $matches) ? $matches[0] : $unknown;
return $realip;
}
}
/**
* 请求
*/
if (!function_exists('https_request')) {
function https_request($url, $curlPost = null, $async = 'GET')
{
$headers = array("Content-Type:application/json;charset='utf-8'",
"Accept: application/json", "Cache-Control: no-cache", "Pragma: no-cache");
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
if ($async == 'POST') {
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
curl_setopt($ch, CURLOPT_POST, 1);
}
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
}
/**
* 随机生成8位字符串 数组 前三位字母
* @param $num
* @return array
*/
if(!function_exists('createCodeNumber')){
function createCodeNumber($num = 1)
{
$str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$letter = substr(str_shuffle($str), mt_rand(0, strlen($str) - 4), 3);
$numGenerate = array();
$max = 99999;
for ($i = 0; $i < $num; $i++) {
$str = $letter . str_pad(mt_rand(0, $max), 5, 0, STR_PAD_LEFT);
while (in_array($str, $numGenerate)) {
$str = $letter . str_pad(mt_rand(0, $max), 5, 0, STR_PAD_LEFT);
}
$numGenerate[$i] = $str;
}
return $numGenerate;
}
}