1. 程式人生 > 實用技巧 >專案可能需要用到的公共方法

專案可能需要用到的公共方法

/**
* 介面返回
* @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;
}
}

/**
* [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;
  }
}